001package org.biopax.paxtools.examples;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import org.biopax.paxtools.io.BioPAXIOHandler;
006import org.biopax.paxtools.io.SimpleIOHandler;
007import org.biopax.paxtools.model.Model;
008import org.biopax.paxtools.model.level2.XReferrable;
009import org.biopax.paxtools.model.level2.relationshipXref;
010import org.biopax.paxtools.model.level2.unificationXref;
011
012import java.io.File;
013import java.io.FileInputStream;
014import java.io.FileNotFoundException;
015import java.io.FileOutputStream;
016import java.lang.reflect.InvocationTargetException;
017import java.util.HashSet;
018import java.util.Set;
019
020/**
021 * User: Emek Demir Date: Jan 18, 2007 Time: 4:56:53 PM
022 *
023 * In this example we get all the unification xrefs in the model
024 * and check if they point to the Gene Ontology. If this is the case
025 * we convert them to relationship xrefs.
026 *
027 */
028public class GOUnificationXREFtoRelationshipXREFConverter
029{
030        private static Log log = LogFactory.getLog(
031                GOUnificationXREFtoRelationshipXREFConverter.class);
032   
033        static BioPAXIOHandler reader = new SimpleIOHandler();
034
035    //args - a space seperated list of owl files to be processed
036    public static void main(String[] args)
037                throws IllegalAccessException, InvocationTargetException
038        {
039        // Process all the args
040        for (String arg : args)
041        {
042            log.info(arg);
043            if (arg.toLowerCase().endsWith("owl"))
044            {
045                try
046                {
047                    processXrefs(arg);
048                }
049                catch (FileNotFoundException e)
050                {
051                    e.printStackTrace();
052                }
053            }
054
055        }
056        }
057
058    /**
059     * Main conversion method. Demonstrates how to read and write a BioPAX
060     * model and accessing its objects.
061     * @param arg file name to be processed
062     */
063    private static void processXrefs(String arg) throws
064                FileNotFoundException,
065                IllegalAccessException,
066                InvocationTargetException
067        {
068                //Read in the model
069        FileInputStream in =
070                        new FileInputStream(new File(arg));
071                Model level2 =
072                        reader.convertFromOWL(in);
073
074        //Get all unification xrefs.
075        Set<unificationXref> unis =
076                        level2.getObjects(unificationXref.class);
077                //Create another set for avoiding concurrent modifications
078        Set<unificationXref> gos = new HashSet<unificationXref>();
079
080        //Process all uni. refs
081        for (unificationXref uni : unis)
082                {
083                        log.trace(uni.getDB());
084                        //Assuming DB is represented as "GO"
085            if (uni.getDB().equalsIgnoreCase("GO"))
086                        {
087                                //this it to avoid concurrent modification.
088                                log.info("scheduling " + uni.getRDFId());
089                                gos.add(uni);
090
091                        }
092                }
093        //Now we have a list of xrefs to be converted. Let's do it.
094        for (unificationXref go : gos)
095                {
096                        convert(go, level2);
097                }
098                //And finally write out the file. We are done !
099        reader.convertToOWL(level2, new FileOutputStream(
100                        arg.substring(0, arg.lastIndexOf('.')) +
101                                "-converted.owl"));
102        }
103
104    /**
105     * This method converts the given unification xref to a relationship xref
106     * @param uni xref to be converted
107     * @param level2 model containing the xref
108     */
109    private static void convert(unificationXref uni, Model level2)
110        {
111                //We can not simply convert a class, so we need to remove the
112        //uni and insert a new relationship xref
113
114        //First get all the objects that refers to this uni
115        Set<XReferrable> referrables =
116                        new HashSet<XReferrable>(uni.isXREFof());
117
118        //Create the new relationship xref in the model.
119        relationshipXref relationshipXref =
120                level2.addNew(relationshipXref.class, uni.getRDFId());
121
122        //Copy the fields from uni
123                relationshipXref.setCOMMENT(uni.getCOMMENT());
124                relationshipXref.setDB(uni.getDB());
125                relationshipXref.setDB_VERSION(uni.getDB_VERSION());
126                relationshipXref.setID(uni.getID());
127                relationshipXref.setID_VERSION(uni.getID_VERSION());
128                relationshipXref.setRELATIONSHIP_TYPE(
129                        "http://www.biopax.org/paxtools/convertedGOUnificationXREF");
130
131        //Create a link to the new xref from all the owners.
132        for (XReferrable referrable : referrables)
133                {
134                        referrable.addXREF(relationshipXref);
135                }
136
137        //Remove the references to the old uni
138        for (XReferrable referrable : referrables)
139        {
140            referrable.removeXREF(uni);
141        }
142        //Now remove it from the model.
143        level2.remove(uni);
144
145        //We are done!
146    }
147}
148