Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/bdgp/apps/dagedit/dataadapter/GORDFAdapter.java


1   package org.bdgp.apps.dagedit.dataadapter;
2   
3   import com.hp.hpl.mesa.rdf.jena.mem.ModelMem;
4   import com.hp.hpl.mesa.rdf.jena.model.*;
5   import com.hp.hpl.mesa.rdf.jena.vocabulary.*;
6   import com.hp.hpl.mesa.rdf.jena.common.*;
7   
8   import java.io.*;
9   import java.net.*;
10  import java.util.*;
11  import org.bdgp.apps.dagedit.datamodel.*;
12  import org.bdgp.apps.dagedit.DAGEditor;
13  import org.bdgp.io.*;
14  import org.bdgp.util.*;
15  
16  public class GORDFAdapter extends AbstractDataAdapter implements DEDataAdapterI {
17      private DEDataAdapterI idAdapter = new DefaultIDAdapter();
18  
19      protected Vector progressListeners;
20      protected ProgressMeter progressMeter = new ProgressMeter();
21  
22      protected static final IOOperation [] supported =
23      {DEDataAdapterI.READ_TERMS, DEDataAdapterI.WRITE_TERMS};
24  
25      protected static final Vector relationshipTypes = new Vector();
26      protected static final Vector categories = new Vector();
27  
28      protected static final TermRelationshipType ISA =
29      new TermRelationshipType("ISA", "is a");
30      protected static final TermRelationshipType PARTOF =
31      new TermRelationshipType("PARTOF", "part of");
32  
33      protected String filePath;
34      protected boolean ignoreMissingRelationships = true;
35  
36      protected static Property ID_PROPERTY;
37      protected static Property NAME_PROPERTY;
38      protected static Property DEF_PROPERTY;
39      protected static Property SYNONYM_PROPERTY;
40      protected static Property REF_PROPERTY;
41      protected static Property REFID_PROPERTY;
42      protected static Property DB_PROPERTY;
43      protected static Property ISA_PROPERTY;
44      protected static Property PARTOF_PROPERTY;
45  
46  
47      static {
48    buildConstants();
49      }
50  
51      protected static void buildConstants() {
52    try {
53        ID_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#accession");
54        NAME_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#name");
55        DEF_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#definition");
56        SYNONYM_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#synonym");
57        REF_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#dbxref");
58        ISA_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#isa");
59        PARTOF_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#part-of");
60        REFID_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#reference");
61        DB_PROPERTY = new PropertyImpl("http://www.geneontology.org/dtds/go.dtd#database_symbol");
62    } catch (Exception e) {
63        System.err.println("Couldn't create constants");
64        e.printStackTrace();
65    }
66      }
67  
68      public GORDFAdapter() {
69    progressListeners = new Vector();
70      }
71  
72      public void setFilePath(String filePath) {
73    this.filePath = filePath;
74      }
75  
76      static {
77    relationshipTypes.add(ISA);
78    relationshipTypes.add(PARTOF);
79      }
80  
81      protected class ProgressMeter implements ProgressListener {
82    public void progressMade(ProgressEvent e) {
83        for(int i=0; i < progressListeners.size(); i++) {
84      ProgressListener pl = (ProgressListener) progressListeners.
85          elementAt(i);
86      pl.progressMade(e);
87        }
88    }
89      }
90  
91      public IOOperation [] getSupportedOperations () {
92    return supported;
93      }
94  
95      public String getName() {
96    return "GO RDF Flat File Adapter";
97      }
98  
99      public String getType() {
100   return "GO RDF Format";
101     }
102 
103     public void init() {
104     }
105 
106     public DataAdapterUI getUI(IOOperation op) {
107   return new GORDFGUI(op);
108     }
109 
110     public DEEditHistory getRoot() throws DataAdapterException {
111   Term term = readFile(filePath);
112   DEEditHistory history = new DEEditHistory(term);
113   return history;
114     }
115 
116     public void addProgressListener(ProgressListener listener) {
117   progressListeners.addElement(listener);
118     }
119 
120     public void removeProgressListener(ProgressListener listener) {
121   progressListeners.removeElement(listener);
122     }
123 
124     protected Term readFile(String fileName) throws DataAdapterException {
125   try {
126       Model model = new ModelMem();
127   
128       ProgressableFileInputStream stream =
129     new ProgressableFileInputStream(filePath);
130       stream.addProgressListener(progressMeter);
131 
132       InputStreamReader reader = new InputStreamReader(
133     new BufferedInputStream(stream));
134 
135       // silence garbage to standard out while reading
136       PrintStream outStream = System.out;
137       System.setOut(new PrintStream(new EmptyStream()));
138       model.read(reader, "");
139       System.setOut(outStream);
140 
141       ResIterator iterator = model.listSubjectsWithProperty(
142     model.getProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "type"),
143     model.getResource("http://www.geneontology.org/dtds/go.dtd#term"));
144 
145       Hashtable termHash = new Hashtable();
146       Hashtable parentHash = new Hashtable();
147       Term root = null;
148 
149       while(iterator.hasNext()) {
150     Resource res = iterator.next();
151     String id = res.getProperty(ID_PROPERTY).getLiteral().
152         getString();
153     String name = res.getProperty(NAME_PROPERTY).getLiteral().
154         getString();
155     Vector parents = new Vector();
156 
157     Term term = new Term(name, id);
158     termHash.put(res, term);
159     parentHash.put(term, parents);
160 
161     if (res.hasProperty(DEF_PROPERTY)) {
162         term.setDefinition(res.getProperty(DEF_PROPERTY).
163                getLiteral().getString());
164     }
165     if (res.hasProperty(SYNONYM_PROPERTY)) {
166         StmtIterator stmts = res.listProperties(SYNONYM_PROPERTY);
167         while(stmts.hasNext()) {
168       Statement stmt = stmts.next();
169       term.addSynonym(new Synonym(stmt.getLiteral().
170                 getString()));
171         }
172     }
173     if (res.hasProperty(REF_PROPERTY)) {
174         StmtIterator stmts = res.listProperties(REF_PROPERTY);
175         while(stmts.hasNext()) {
176       Statement stmt = stmts.next();
177       Resource target = stmt.getResource();
178       Dbxref dbxref =
179           new Dbxref(target.getProperty(DB_PROPERTY).
180                getLiteral().getString(),
181                target.getProperty(REFID_PROPERTY).
182                getLiteral().getString());
183       term.addDbxref(dbxref);
184         }
185     }
186     if (res.hasProperty(ISA_PROPERTY)) {
187         StmtIterator stmts = res.listProperties(ISA_PROPERTY);
188         while(stmts.hasNext()) {
189       Statement stmt = stmts.next();
190       Resource target = stmt.getResource();
191       parents.addElement(new ParentWrapper(target,ISA));
192         }
193     }
194     if (res.hasProperty(PARTOF_PROPERTY)) {
195         StmtIterator stmts = res.listProperties(PARTOF_PROPERTY);
196         while(stmts.hasNext()) {
197       Statement stmt = stmts.next();
198       Resource target = stmt.getResource();
199       parents.addElement(new ParentWrapper(target,PARTOF));
200         }
201     }
202       }
203 
204       Enumeration keys = termHash.keys();
205       while(keys.hasMoreElements()) {
206     Resource res = (Resource) keys.nextElement();
207     Term term = (Term) termHash.get(res);
208     Vector v = (Vector) parentHash.get(term);
209     for(int i=0; i < v.size(); i++) {
210         ParentWrapper pw = (ParentWrapper) v.elementAt(i);
211         Term parent = (Term) termHash.get(pw.id);
212         parent.addChild(term, pw.type);
213     }
214     if (v.size() == 0) {
215         term.setRoot(true);
216         root = term;
217     }
218       }
219 
220       return root;
221   } catch (Exception e) {
222       throw new DataAdapterException(e, "Read failed");
223   }
224     }
225 
226     protected static TermRelationshipType getTypeForString(String type) {
227   if (type.equals("ISA"))
228       return ISA;
229   else if (type.equals("PARTOF"))
230       return PARTOF;
231   else
232       return null;
233     }
234 
235     protected class ParentWrapper {
236   public Resource id;
237   public TermRelationshipType type;
238 
239   public ParentWrapper(Resource id, TermRelationshipType type) {
240       this.id = id;
241       this.type = type;
242   }
243     }
244 
245     protected static String trimData(String data) {
246   return data.substring(7, data.length() - 1);
247     }
248 
249 
250 
251 
252     public String escapeString(String in) {
253   String out = StringUtil.replace(in, "&", "&amp;");
254   out = StringUtil.replace(out, "<", "&lt;");
255   out = StringUtil.replace(out, ">", "&gt;");
256   return out;
257     }
258 
259 
260 
261 
262     public DEEditHistory write(DEEditHistory history) throws DataAdapterException {
263   try {
264       FileOutputStream stream = new FileOutputStream(filePath);
265       PrintStream printer = new PrintStream(stream);
266 
267       printer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
268       printer.println("<!DOCTYPE go:go PUBLIC \"-//Gene Ontology//Custom XML/RDF Version 2.0//EN\" \"http://www.geneontology.org/xml-dtd/08022001/go.dtd\">");
269       printer.println("<go:go xmlns:go=\"http://www.geneontology.org/dtds/go.dtd#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
270       printer.println("  <go:version timestamp=\""+(new Date())+"\" />");
271       printer.println("  <rdf:RDF>");
272 
273       Term root = history.getRoot();
274       Enumeration e = root.getAllDescendants(true);
275       Vector v = VectorUtil.sort(e);
276       for(int i=0; i < v.size(); i++) {
277     Term term = (Term) v.elementAt(i);
278 
279     double percentage = 100 * ((double) i) / ((double) v.size());
280 
281     ProgressEvent event = new ProgressEvent(this,
282               new Double(percentage),
283               "writing...");
284 
285     progressMeter.progressMade(event);
286                    
287 
288     printer.println("    <go:term rdf:about=\"http://www.geneontology.org/go#"+term.getID()+"\" n_associations=\"0\">");
289     printer.println("      <go:accession>"+term.getID()
290         +"</go:accession>");
291     printer.println("      <go:name>"+escapeString(term.getTerm())
292         +"</go:name>");
293     if (term.getDefinition() != null &&
294         term.getDefinition().length() != 0) {
295         printer.println("      <go:definition>"+
296             escapeString(term.getDefinition())
297             +"</go:definition>");
298     }
299     for(int j=0; j < term.getParents().size(); j++) {
300         TermRelationship tr = (TermRelationship) term.
301       getParents().elementAt(j);
302         if (tr.getType().equals(ISA)) {
303       printer.println("      <go:isa rdf:resource=\"http://www.geneontology.org/go#"+tr.getParent().getID()+"\" />");
304         } else if (tr.getType().equals(PARTOF)) {
305       printer.println("      <go:part-of rdf:resource=\"http://www.geneontology.org/go#"+tr.getParent().getID()+"\" />");
306         } else if (!ignoreMissingRelationships) {
307       throw new
308           DataAdapterException("Cannot write term "+
309              term+" ("+term.getID()+") "+
310              "because of unsupported "+
311              "relationship type "+
312              tr.getType()+" to parent "+
313              tr.getParent()+" ("+
314              tr.getParent().getID()+")");
315         }
316     }
317     for(int j=0; j < term.getDbxrefs().size(); j++) {
318         Dbxref ref = (Dbxref) term.getDbxrefs().elementAt(j);
319         printer.println("      <go:dbxref>");
320         printer.println("        <go:database_symbol>"+
321       escapeString(ref.getDatabase())+
322             "</go:database_symbol>");
323         printer.println("        <go:reference>"+
324       escapeString(ref.getID())+"</go:reference>");
325         printer.println("      </go:dbxref>");
326     }
327 
328     printer.println("    </go:term>");
329       }
330 
331       printer.println("  </rdf:RDF>");
332       printer.println("</go:go>");
333 
334       return history;
335   } catch (Exception e) {
336       throw new DataAdapterException(e, "Write failure");
337   }
338     }
339 
340     public String [] getIDs(Term root, Term term, String prefix, int length, int count) throws DataAdapterException {
341   throw new DataAdapterException("Not supported");
342     }
343     public String [] getIDs(Term root, Term term, String prefix, int min, int max, int length, int count) throws DataAdapterException {
344   throw new DataAdapterException("Not supported");
345     }
346 
347     public Vector getHistories() throws DataAdapterException {
348   throw new DataAdapterException("Not supported");
349     }
350 
351     public Vector getRelationshipTypes() throws DataAdapterException {
352   return relationshipTypes;
353     }
354 
355     public Vector getTermCategories() throws DataAdapterException {
356   return categories;
357     }
358     public void delegateToIDAdapter(DEDataAdapterI adapter) {
359   this.idAdapter = idAdapter;
360     }
361     public Properties getStateInformation() {
362   return null;
363     }
364 
365     public void setStateInformation(Properties props) {
366     }
367 }