001package org.biopax.paxtools.examples;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import org.biopax.paxtools.controller.*;
006import org.biopax.paxtools.io.BioPAXIOHandler;
007import org.biopax.paxtools.io.SimpleIOHandler;
008import org.biopax.paxtools.model.BioPAXElement;
009import org.biopax.paxtools.model.BioPAXLevel;
010import org.biopax.paxtools.model.Model;
011import org.biopax.paxtools.model.level2.*;
012import org.biopax.paxtools.util.ClassFilterSet;
013import org.biopax.paxtools.util.Filter;
014
015import java.io.File;
016import java.io.FileInputStream;
017import java.io.FileNotFoundException;
018import java.io.FilenameFilter;
019import java.util.Set;
020
021
022/**
023 * This example class processes all the Level2 BioPAX OWL
024 * files in the input directory to find all the protein names
025 *
026 * Notes:
027 *
028 * - recent fix: it doesn't traverse into the NEXT-STEP property,
029 * as it may lead beyond the boundaries of the pathway of interest!
030 *
031 * - one may prefer using the Paxtools' jenaIO instead of the simpleIO:
032 *
033 * import org.biopax.paxtools.io.jena.JenaIOHandler;
034 * JenaIOHandler handler = new JenaIOHandler(null, BioPAXLevel.L2);
035 */
036public class ProteinNameLister
037{
038// ------------------------------ FIELDS ------------------------------
039
040        private static Log log = LogFactory.getLog(ProteinNameLister.class);
041
042        private static Fetcher fetcher;
043
044// --------------------------- main() method ---------------------------
045
046        public static void main(String[] args)
047        {
048                if (args.length != 1)
049                {
050                        System.out.println("\nUse Parameter: path (to biopax OWL files)\n");
051                        System.exit(-1);
052                }
053
054                BioPAXIOHandler reader = new SimpleIOHandler();
055                final String pathname = args[0];
056                File testDir = new File(pathname);
057
058                /*
059                                 * Customized Fetcher is to fix the issue with Level2
060                                 * - when NEXT-STEP leads out of the pathway...
061                                 * (do not worry - those pathway steps that are part of
062                                 * the pathway must be in the PATHWAY-COMPONENTS set)
063                                 */
064                Filter<PropertyEditor> nextStepPropertyFilter = new Filter<PropertyEditor>()
065                {
066                        public boolean filter(PropertyEditor editor)
067                        {
068                                return !editor.getProperty().equals("NEXT-STEP");
069                        }
070                };
071                fetcher = new Fetcher(SimpleEditorMap.L2, nextStepPropertyFilter);
072
073                FilenameFilter filter = new FilenameFilter()
074                {
075                        public boolean accept(File dir, String name)
076                        {
077                                return (name.endsWith("owl"));
078                        }
079                };
080
081                for (String s : testDir.list(filter))
082                {
083                        try
084                        {
085                                process(pathname, s, reader);
086                        }
087                        catch (Exception e)
088                        {
089                                log.error("Failed at testing " + s, e);
090                        }
091                }
092        }
093
094        private static void process(String pathname, String name, BioPAXIOHandler reader) throws FileNotFoundException
095        {
096                System.out.println("--------------" + name + "---------");
097                Model model = reader.convertFromOWL(new FileInputStream(pathname + "/" + name));
098                listProteinUnificationXrefsPerPathway(model);
099        }
100
101
102        public static void listProteinUnificationXrefsPerPathway(Model model)
103        {
104                Set<pathway> pathways = model.getObjects(pathway.class);
105                for (pathway aPathway : pathways)
106                {
107                        //printout aPathway's name
108                        System.out.println(aPathway.getNAME());
109
110                        //Use new fetcher to get all dependents in a new level2
111                        Model onePathwayModel = BioPAXLevel.L2.getDefaultFactory().createModel();
112                        fetcher.fetch(aPathway, onePathwayModel);
113                        //get all proteins in the new level2
114                        Set<protein> proteins = onePathwayModel.getObjects(protein.class);
115
116                        //iterate and print names
117                        for (protein aProtein : proteins)
118                        {
119                                System.out.println("\t" + aProtein.getNAME());
120                                //now list xrefs and print if uni
121                                Set<unificationXref> xrefs = new ClassFilterSet<xref,unificationXref>(aProtein.getXREF(),
122                                                                                                 unificationXref.class);
123                                for (unificationXref x : xrefs)
124                                {
125                                        System.out.println("\t\t" + x.getDB() + ":" + x.getID());
126                                }
127                        }
128                }
129
130        }
131
132        /**
133         * Here is a more elegant way of doing the previous method!
134         * @param model BioPAX object Model
135         */
136        public static void listUnificationXrefsPerPathway(Model model)
137        {
138                // This is a visitor for elements in a pathway - direct and indirect
139                Visitor visitor = new Visitor()
140                {
141                        public void visit(BioPAXElement domain, Object range, Model model, PropertyEditor editor)
142                        {
143                                if (range instanceof physicalEntity)
144                                {
145                                        // Do whatever you want with the pe and xref here
146                                        physicalEntity pe = (physicalEntity) range;
147                                        ClassFilterSet<xref,unificationXref> unis = new ClassFilterSet<xref,unificationXref>(pe.getXREF(),
148                                                                                                                   unificationXref.class);
149                                        for (unificationXref uni : unis)
150                                        {
151                                                System.out.println("pe.getNAME() = " + pe.getNAME());
152                                                System.out.println("uni = " + uni.getID());
153                                        }
154                                }
155                        }
156                };
157
158                Traverser traverser = new Traverser(SimpleEditorMap.L2, visitor);
159
160                Set<pathway> pathways = model.getObjects(pathway.class);
161                for (pathway pathway : pathways)
162                {
163                        traverser.traverse(pathway, model);
164                }
165        }
166}
167