001package org.biopax.paxtools.examples;
002
003import org.biopax.paxtools.controller.*;
004import org.biopax.paxtools.io.BioPAXIOHandler;
005import org.biopax.paxtools.io.SimpleIOHandler;
006import org.biopax.paxtools.model.BioPAXElement;
007import org.biopax.paxtools.model.BioPAXFactory;
008import org.biopax.paxtools.model.BioPAXLevel;
009import org.biopax.paxtools.model.Model;
010import org.biopax.paxtools.model.level3.*;
011import org.biopax.paxtools.query.QueryExecuter;
012import org.biopax.paxtools.query.algorithm.Direction;
013import org.biopax.paxtools.util.Filter;
014
015import java.io.FileInputStream;
016import java.io.FileNotFoundException;
017import java.io.InputStream;
018import java.io.OutputStream;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Set;
022
023/**
024 * This class used for a Paxtools Tutorial during Harmony 2010. It contains most of the examples in the user's guide.
025 */
026public class Tutorial
027{
028
029
030 public static void myFirstModel()
031 {
032  BioPAXFactory factory = BioPAXLevel.L3.getDefaultFactory();
033  Model model = factory.createModel();
034  Protein protein1 = model.addNew(Protein.class,
035                                  "http://biopax.org/tutorial/test1");
036  protein1.addName("Tutorial Example Small molecule Transporter 1");
037  protein1.setDisplayName("TEST1");
038
039  BiochemicalReaction rxn1 = model.addNew(BiochemicalReaction.class,
040                                          "http://biopax.org/tutorial/rxn1");
041  rxn1.addLeft(protein1);
042
043 }
044
045
046 public static void IO(InputStream inputStreamFromFile,
047                       OutputStream outputStream)
048 {
049  BioPAXIOHandler handler = new SimpleIOHandler(); // auto-detects Level
050  Model model = handler.convertFromOWL(inputStreamFromFile);
051  handler.convertToOWL(model, outputStream);
052  String id1 = null, id2 = null, id3 = null;
053  handler.convertToOWL(model, outputStream, id1, id2, id3);
054
055 }
056
057 public static void tempted(BioPAXIOHandler handler, InputStream inputStream,
058                            OutputStream outputStream)
059 {
060  //read initial model
061  Model model1 = handler.convertFromOWL(inputStream);
062  //create an empty model
063  Model model2 = model1.getLevel().getDefaultFactory().createModel();
064  //extract reaction
065  model2.add(model1.getByID("The_reaction_id"));
066  //write it out
067  handler.convertToOWL(model2, outputStream);
068 }
069
070
071 /**
072  * A controller that excises/extracts an element and all the elements it is
073  * dependent on from a model and adds them into a new model.
074  */
075 class Excisor implements Visitor
076 {
077  private Traverser traverser;
078
079  private EditorMap editorMap;
080
081  private Model targetModel;
082
083  public Excisor(EditorMap editorMap)
084  {
085   this.editorMap = editorMap;
086   this.traverser = new Traverser(editorMap, this);
087  }
088
089  public Excisor(EditorMap editorMap, boolean filtering)
090  {
091   this.editorMap = editorMap;
092   if (filtering)
093   //We will filter nextStep property, as Reactome pathways leads
094   //outside the current pathway. Step processes are listed in the
095   //pathwayComponent property as well so this does not affect the fetcher.
096   {
097    final Filter<PropertyEditor> nextStepFilter = new Filter<PropertyEditor>()
098    {
099     public boolean filter(PropertyEditor editor)
100     {
101      return !editor.getProperty().equals("nextStep");
102     }
103    };
104    this.traverser = new Traverser(editorMap, this, nextStepFilter);
105   } else this.traverser = new Traverser(editorMap, this);
106  }
107
108  //The visitor will add all elements that are reached into the new model,
109  // and recursively traverse it
110  public void visit(BioPAXElement domain, Object range, Model model,
111                    PropertyEditor editor)
112  {
113   // We are only interested in the BioPAXElements since
114   // primitive fields are always copied by value
115   if (range != null && range instanceof BioPAXElement)
116   {
117    BioPAXElement bpe = (BioPAXElement) range;
118
119    if (!targetModel.contains(bpe))
120    {
121     targetModel.add(bpe);
122     traverser.traverse(bpe, model);
123    }
124   }
125  }
126
127
128  public Model excise(Model sourceModel, String... ids)
129  {
130   // Create a new model that will contain the element(s) of interest
131   this.targetModel = editorMap.getLevel().getDefaultFactory().createModel();
132
133   for (String id : ids)
134   {
135    // Get the BioPAX element
136    BioPAXElement bpe = sourceModel.getByID(id);
137    // Add it to the model
138    targetModel.add(bpe);
139    // Add the elements that bpe is dependent on
140    traverser.traverse(bpe, sourceModel);
141   }
142
143   return targetModel;
144  }
145 }
146
147 public static void merge(EditorMap editorMap, Model srcModel2,
148                          Model srcModel1)
149 {
150  Model targetModel = editorMap.getLevel().getDefaultFactory().createModel();
151  Merger merger = new Merger(editorMap);
152  merger.merge(targetModel, srcModel1, srcModel2);
153
154 }
155
156 public Set access1(Complex complex)
157 {
158  Set<UnificationXref> xrefs = new HashSet<UnificationXref>();
159  recursivelyObtainMembers(complex, xrefs);
160  return xrefs;
161 }
162
163 private void recursivelyObtainMembers(Complex complex,
164                                       Set<UnificationXref> xrefs)
165 {
166  for (PhysicalEntity pe : complex.getComponent())
167  {
168   if (pe instanceof Complex)
169   {
170    recursivelyObtainMembers((Complex) pe, xrefs);
171   } else
172   {
173    Set<Xref> memberxrefs =
174      ((SimplePhysicalEntity) pe).getEntityReference().getXref();
175    for (Xref xref : memberxrefs)
176    {
177     if (xref instanceof UnificationXref)
178     {
179      xrefs.add((UnificationXref) xref);
180     }
181    }
182   }
183
184  }
185 }
186
187 @SuppressWarnings({"unchecked"}) public Set access2(Complex complex)
188 {
189  Model model = BioPAXLevel.L3.getDefaultFactory().createModel();
190
191  //  Set up the Path Accessor
192  PathAccessor pathAccessor = new PathAccessor(
193    "ProteinReference/xref:UnificationXref", BioPAXLevel.L3);
194  // Iterate through all proteins in the model
195  for (Protein currentProtein : model.getObjects(Protein.class))
196  {
197   Set<Xref> unificationXrefs = pathAccessor.getValueFromBean(currentProtein);
198   for (Xref currentRef : unificationXrefs)
199   {
200    System.out.println(
201      "Unification XRef:  " + currentRef.getDb() + ": " + currentRef.getId());
202   }
203  }
204  PathAccessor accessor = new PathAccessor(
205    "Complex/component*/EntityReference/xref:UnificationXref", BioPAXLevel.L3);
206  return new PathAccessor(
207    "Complex/component*/EntityReference/xref:UnificationXref",
208    BioPAXLevel.L3).getValueFromBean(complex);
209 }
210
211 public void graphQuery(Model model, PhysicalEntity entity3,
212                        PhysicalEntity entity2, PhysicalEntity entity1)
213 {
214  Set<BioPAXElement> sourceSet = new HashSet<BioPAXElement>();
215
216  // Add the related source PhysicalEntity (or children) objects to the
217  // source set
218  Collections.addAll(sourceSet, entity1, entity2, entity3);
219
220  int limit = 2;
221
222  // Direction can be upstream, downstream, or bothstream.
223  Direction direction = Direction.BOTHSTREAM;
224
225
226  Set<BioPAXElement> result = QueryExecuter.runNeighborhood(sourceSet, model,
227                                                            limit, direction, null);
228
229  Completer c = new Completer(SimpleEditorMap.get(BioPAXLevel.L3));
230  result = c.complete(result, model);
231 }
232
233
234 public void simpleTraverse() throws FileNotFoundException
235 {
236  //  Load a sample DNA Repair BioPAX File via Simple IO Handler
237  FileInputStream fin = new FileInputStream("test_data/dna_repair.owl");
238  BioPAXIOHandler handler = new SimpleIOHandler();
239  Model model = handler.convertFromOWL(fin);
240
241  // Iterate through all BioPAX Elements and Display RDF ID and Class Name
242  Set<BioPAXElement> elementSet = model.getObjects();
243  for (BioPAXElement currentElement : elementSet)
244  {
245   String rdfId = currentElement.getRDFId();
246   String className = currentElement.getClass().getName();
247   System.out.println("Element:  " + rdfId + ": " + className);
248  }
249  //  Get Proteins Only
250  Set<Protein> proteinSet = model.getObjects(Protein.class);
251  for (Protein currentProtein : proteinSet)
252  {
253   System.out.println("Protein:  " + currentProtein.getName() + ": " +
254                      currentProtein.getDisplayName());
255  }
256
257  //  Iterate through all proteins in the model
258  proteinSet = model.getObjects(Protein.class);
259
260  for (Protein currentProtein : proteinSet)
261  {
262   EntityReference entityReference = currentProtein.getEntityReference();
263   if (entityReference != null)
264   {
265    Set<Xref> xrefSet = entityReference.getXref();
266    for (Xref currentRef : xrefSet)
267    {
268     if (currentRef instanceof UnificationXref)
269     {
270      System.out.println(
271        "Unification XRef:  " + currentRef.getDb() + ": " + currentRef.getId
272          ());
273     }
274    }
275   }
276  }
277 }
278
279 public void highlightWorkaround()
280 {
281  myFirstModel();
282  IO(null, null);
283  Excisor ex = new Excisor(null);
284  access1(null);
285  graphQuery(null, null, null, null);
286 }
287
288
289}