[MOBY-guts] biomoby commit

Gary Schlitz gss at pub.open-bio.org
Thu Apr 1 00:33:35 UTC 2004


gss
Wed Mar 31 19:33:35 EST 2004
Update of /home/repository/moby/moby-live/S-MOBY/ref-impl/example-providers/acmepubs.com/src/com/acmepubs/servlets
In directory pub.open-bio.org:/tmp/cvs-serv2062/src/com/acmepubs/servlets

Modified Files:
	CitationSearchServlet.java 
Log Message:
Changes to vocabulary, incomplete changes to servlet code to handle
new description graph.

moby-live/S-MOBY/ref-impl/example-providers/acmepubs.com/src/com/acmepubs/servlets CitationSearchServlet.java,1.5,1.6
===================================================================
RCS file: /home/repository/moby/moby-live/S-MOBY/ref-impl/example-providers/acmepubs.com/src/com/acmepubs/servlets/CitationSearchServlet.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- /home/repository/moby/moby-live/S-MOBY/ref-impl/example-providers/acmepubs.com/src/com/acmepubs/servlets/CitationSearchServlet.java	2004/03/30 22:24:28	1.5
+++ /home/repository/moby/moby-live/S-MOBY/ref-impl/example-providers/acmepubs.com/src/com/acmepubs/servlets/CitationSearchServlet.java	2004/04/01 00:33:35	1.6
@@ -7,6 +7,7 @@
 import javax.servlet.http.*;
 
 import org.smoby.tools.common.graph.MOBYGraph;
+import org.smoby.tools.common.vocabulary.MOBY;
 import org.smoby.tools.server.servlet.AbstractMobyServlet;
 
 import com.acmepubs.db.CitationDB;
@@ -36,42 +37,124 @@
     /**
      * Handle a request for a citation search by looking up
      * a gene symbol and returning a list of publication
-     * abstracts.
+     * abstracts. In this simplistic first cut, only look
+     * at the first "hasMapping" property, find the "geneID"
+     * property value as a lookup value, and for each matching
+     * publication date past the first one, create a copy of
+     * the entire subgraph and fill in its "pubDate" property.
      */
     protected void handleRequest(MOBYGraph graph)
     {
-        Model model = graph.getModel();
-        
         // Retrieve the subject of the moby:mapsTo statement
         //
-        Resource subject = graph.getMapsToStmt().getSubject();
+        Resource mobyGraphSubject = (Resource) graph.getOperatesOnStmt().getObject();
         
-        // Retrieve the subject of the acmepubs:geneID statement
+        // Get the subject of the first hasMapping statement (the only one
+        // considered in this first cut)
         //
-        StmtIterator it = model.listStatements(
-        	subject, AcmePubs.geneID, (String) null);
+        Resource start = graph.getModel()
+			.listStatements(mobyGraphSubject, MOBY.hasMapping, (RDFNode) null)
+			.nextStatement().getSubject();
         
-        if (it.hasNext())
-        {
-        	Statement s = it.nextStatement();
-        	try
-			{
-        		String geneSymbol = s.getString();
-        		List pubDates = CitationDB.getCitations(geneSymbol);
-	        	if (pubDates != null)
-	        	{
-	        		StmtIterator it2 = model.listStatements(
-	        			(Resource) graph.getMapsToStmt().getObject(),
-						AcmePubs.pubDate,
-						(RDFNode) null);
-	        		
-	        		if (it2.hasNext())
-	        		{
-	        			Statement s2 = it2.nextStatement();
-	        		}
-	        	}
-			} catch (Throwable t) {}
-        }
+        // Retrieve the subgraph that is "reachable" from the subject of the
+        // first hasMapping statement.
+        //
+        Model subgraph = ModelFactory.createDefaultModel();
+    	subgraph.setNsPrefixes(graph.getModel().getNsPrefixMap());
+        addReachableStatements(start, graph.getModel(), subgraph);
+    	
+    	// Find the "geneID" statement in the subgraph; if there isn't one,
+    	// then we can't handle it (really should throw an exception...)
+    	//
+    	StmtIterator it = subgraph.listStatements(null, AcmePubs.geneID, (RDFNode) null);
+    	if (! it.hasNext()) return;
+    	
+    	// Get the object of the geneID statement; if it isn't a String, then
+    	// again we can't handle it.
+    	//
+    	String geneID = null;
+    	try {
+    		geneID = it.nextStatement().getString();
+    	} catch (Throwable t) {
+    		return;
+    	}
+    	
+    	// Look up the gene symbol; if not found, then there's nothing to do
+    	//
+    	List pubDates = CitationDB.getCitations(geneID);
+    	if (pubDates == null) return;
+    	
+    	// For each publication date found, replace the object of the pubDate
+    	// statement with the publication date in the subgraph (for the first
+    	// pub date) or in a copy (for subsequent pub dates)
+    	//
+    	int i = 0;
+    	for (Iterator iter = pubDates.iterator(); iter.hasNext(); i++)
+    	{
+    		String pubDate = iter.next().toString();
+    		
+    		// In the first match, we use the subgraph pulled from the original
+    		// graph; in additional matches, we use a copy (whose statements have
+    		// to be added to the original model)
+    		//
+    		if (i > 0)
+    		{
+    			subgraph = copyModel(subgraph);
+    			graph.getModel().add(subgraph);
+    		}
+    		
+        	// Find the "pubDate" statement in the subgraph; if there isn't one,
+        	// then we can't handle it (really should throw an exception...)
+        	//
+        	it = subgraph.listStatements(null, AcmePubs.pubDate, (RDFNode) null);
+        	if (! it.hasNext()) return;
+        	
+        	// Replace the object of the pubDate statement with a literal value,
+        	// namely the publication date
+        	//
+        	Statement stmt = it.nextStatement();
+        	stmt.changeObject(pubDate);
+    	}
+    }
+    
+    protected Model copyModel(Model model)
+	{
+    	Model copy = ModelFactory.createDefaultModel();
+    	copy.setNsPrefixes(model.getNsPrefixMap());
+    	for (StmtIterator it = model.listStatements(); it.hasNext();)
+    	{
+    		Statement stmt = it.nextStatement();
+    		copy.add(copy.createStatement(
+	    				stmt.getSubject(),
+						stmt.getPredicate(),
+						stmt.getObject()));
+    	}
+    	return copy;
+    }
+    
+    /**
+     * Add to a model the depth-first traversal
+     * of a source graph starting from a given subject
+     * @param subject the resource that is the starting poing
+     * @param source the model containing a set of statements from which to add
+     * @param subgraph the model to which to add reachable statements
+     */
+    protected void addReachableStatements(Resource subject, Model source, Model subgraph)
+	{
+    	StmtIterator it = source.listStatements(subject, null, (RDFNode) null);
+    	while (it.hasNext())
+    	{
+    		Statement stmt = it.nextStatement();
+    		if (! subgraph.contains(stmt))
+    		{
+    			subgraph.add(stmt);
+    			addReachableStatements(stmt.getSubject(), source, subgraph);
+    			addReachableStatements(stmt.getPredicate(), source, subgraph);
+    			if (stmt.getObject() instanceof Resource) {
+    				addReachableStatements((Resource) stmt.getObject(), source, subgraph);
+    			}
+    		}
+    	}
     }
     
     /**




More information about the MOBY-guts mailing list