001package org.biopax.paxtools.examples;
002
003import org.biopax.paxtools.io.BioPAXIOHandler;
004import org.biopax.paxtools.io.SimpleIOHandler;
005import org.biopax.paxtools.model.BioPAXFactory;
006import org.biopax.paxtools.model.BioPAXLevel;
007import org.biopax.paxtools.model.Model;
008import org.biopax.paxtools.model.level3.ProteinReference;
009import org.biopax.paxtools.model.level3.UnificationXref;
010
011import java.io.ByteArrayOutputStream;
012import java.io.FileInputStream;
013import java.io.IOException;
014import java.io.OutputStream;
015
016/**
017 * A basic example that shows the basic IO operations.
018 */
019public final class SimpleIOExample {
020
021        public static void main(String[] args) throws IOException {
022                if (args.length != 1) {
023                        System.out.println("Please run again providing one argument, "
024                                        + "a BioPAX OWL file path/name.");
025                        System.exit(-1);
026                }
027
028                // import BioPAX from OWL file (auto-detects level)
029                BioPAXIOHandler biopaxIO = new SimpleIOHandler();
030                Model model = biopaxIO.convertFromOWL(new FileInputStream(args[0]));
031                // write (as BioPAX OWL)
032                output(model);
033                // TODO play with model...
034                /*
035                 * if(model == null || model.getLevel() != BioPAXLevel.L3) { throw new
036                 * IllegalArgumentException(" is not supported!"); }
037                 */
038
039                // Well, let's do something with a (new) BioPAX model
040                
041                BioPAXFactory bioPAXFactory = BioPAXLevel.L3.getDefaultFactory();
042                
043                Model model2 = bioPAXFactory.createModel();
044                
045                // set default name space prefix (base);
046                model2.setXmlBase("http://baderlab.org#");
047                // create and add a new element to the model;
048                // still, rdfid must be set in full (not just "#xref_P62158")
049                UnificationXref uxref = model2.addNew(UnificationXref.class,
050                                "http://baderlab.org#xref_P62158");
051                uxref.setDb("uniprotkb");
052                uxref.setId("P62158");
053                // using absolute (ext.) URI as id
054                ProteinReference prf = model2.addNew(ProteinReference.class,
055                                "urn:miriam:uniprot:P62158");
056                prf.setDisplayName("CALM_HUMAN");
057                prf.addXref(uxref);
058                // (do not need to explicitly add objects to the model)
059                // write
060                output(model2);
061                // compare this output with the previous one (see IDs and references)    
062        }
063
064        
065        public  static void output(Model model) throws IOException {
066                BioPAXIOHandler simpleExporter = new SimpleIOHandler();
067                OutputStream out = new ByteArrayOutputStream();
068                simpleExporter.convertToOWL(model, out);
069                System.out.println(out + "\n");
070        }
071
072}