Search in sources :

Example 1 with HashGraph

use of org.sbpax.impl.HashGraph in project vcell by virtualcell.

the class SBMLAnnotationUtil method readAnnotation.

/**
 * readAnnotation : reads SBML annotations and writes it to corresponding 'identifiables' in vcMetaData. Everything except <VCellRelatedInfo>
 * 				is read here.
 * @param identifiable - vcReaction, vcSpecies, vcCompartment, vcBiomodel
 * @param sBase - corresponding SBML elements
 * @throws XMLStreamException
 * @throws
 * @throws RDFHandlerException
 * @throws RDFParseException
 */
public void readAnnotation(Identifiable identifiable, SBase sBase) throws XMLStreamException {
    readMetaID(identifiable, sBase);
    XMLNode annotationRoot = sBase.getAnnotation().getXMLNode();
    if (annotationRoot != null) {
        long childCount = annotationRoot.getNumChildren();
        for (int i = 0; i < childCount; ++i) {
            XMLNode annotationBranch = annotationRoot.getChild(i);
            String namespace = annotationBranch.getNamespaceURI(annotationBranch.getPrefix());
            if (namespace != null) {
                if (namespace.equals(DefaultNameSpaces.RDF.uri)) {
                    // read in RDF annotation
                    String text = annotationBranch.toXMLString();
                    // TODO this is a hack to be replaced by proper URI management.
                    text = text.replace("about=\"#", "about=\"");
                    Graph rdfNew = new HashGraph();
                    Map<String, String> nsMap = new HashMap<String, String>();
                    try {
                        SesameRioUtil.readRDFFromString(text, rdfNew, nsMap, RDFFormat.RDFXML, nsSBML);
                    } catch (RDFParseException e) {
                        e.printStackTrace();
                    } catch (RDFHandlerException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // System.out.println("SBML NS :\n" + MIRIAMAnnotationViewer.prettyPrintJenaModel(rdfNew, nsSBML));
                    metaData.add(rdfNew);
                // System.out.println("VCML NS :\n" + MIRIAMAnnotationViewer.prettyPrintJenaModel(metaData.getRdfData(), XMLTags.VCML_NS));
                // System.out.println("SBBox Data :\n" + MIRIAMAnnotationViewer.prettyPrintJenaModel(metaData.getSBbox().getData(), XMLTags.VCML_NS));
                // System.out.println(MIRIAMAnnotationViewer.printResourceMappings(metaData));
                } else if (namespace.equals(tripleVCellInfo.getURI()) || namespace.equals(XMLTags.VCML_NS_OLD) || namespace.equals(XMLTags.VCML_NS)) {
                    int numChildren = (int) annotationBranch.getNumChildren();
                    for (int j = 0; j < numChildren; j++) {
                        XMLNode child = annotationBranch.getChild(j);
                        if (child.isElement() && child.getName().equals(XMLTags.FreeTextAnnotationTag)) {
                            XMLNode contentFreeText = child.getChild(0);
                            // read in the string (not XML string, but character string) from the XMLNode;
                            // set free text annotation for identifiable in metadata.
                            String freeText = contentFreeText.getCharacters();
                            metaData.setFreeTextAnnotation(identifiable, freeText);
                        }
                    }
                } else {
                    // other (tool-specific, non-RDF, XML) annotations
                    Element elementXML = null;
                    // Element annotationElement = null;
                    try {
                        XMLNode clonedAnnotRoot = new XMLNode(annotationRoot);
                        clonedAnnotRoot.setNamespaces(annotationRoot.getNamespaces());
                        clonedAnnotRoot.removeChildren();
                        clonedAnnotRoot.addChild(annotationBranch.clone());
                        // (XmlUtil.stringToXML(xmlString, null)).getRootElement();
                        elementXML = (XmlUtil.stringToXML(clonedAnnotRoot.toXMLString(), null)).getRootElement();
                    } catch (Exception e) {
                    // e.printStackTrace(System.out);
                    // don't do anything .... we want to continue reading in the model, we cannot fail import because annotation is not well-formed.
                    // try wrap in namespace element
                    // System.out.println(sBase.toSBML()+"\n\n"+annotationRoot.toXMLString());
                    // XMLNamespaces xmlNamespaces = sBase.getNamespaces();
                    // if(xmlNamespaces != null && !xmlNamespaces.isEmpty()){
                    // String xmlnsStr = "";
                    // for (int j = 0; j < xmlNamespaces.getNumNamespaces(); j++) {
                    // xmlnsStr+= "\nxmlns"+(xmlNamespaces.getPrefix(j).length()==0?"":":"+xmlNamespaces.getPrefix(j))+"=\""+xmlNamespaces.getURI(j)+"\"";
                    // }
                    // String wrap = "<annotation "+xmlnsStr+">\n"+annotationBranch.toXMLString()+"\n</annotation>";
                    // System.out.println(wrap);
                    // try{
                    // elementXML = (XmlUtil.stringToXML(wrap, null)).getRootElement();
                    // System.out.println("-----PROBLEM FIXED-----");
                    // }catch(Exception e2){
                    // e.printStackTrace();
                    // }
                    // }
                    }
                    // Element elementXML = xmlNodeToElement(annotationBranch);
                    Element[] xmlAnnotations = metaData.getXmlAnnotations(identifiable);
                    Vector<Element> xmlAnnotList = new Vector<Element>();
                    if (xmlAnnotations != null && xmlAnnotations.length > 0) {
                        xmlAnnotList.addAll(Arrays.asList(xmlAnnotations));
                    }
                    if (elementXML != null) {
                        xmlAnnotList.add(elementXML);
                        metaData.setXmlAnnotations(identifiable, xmlAnnotList.toArray(new Element[0]));
                    }
                }
            }
        }
    }
}
Also used : XMLNode(org.sbml.jsbml.xml.XMLNode) HashMap(java.util.HashMap) Element(org.jdom.Element) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) RDFParseException(org.openrdf.rio.RDFParseException) HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) RDFHandlerException(org.openrdf.rio.RDFHandlerException) Vector(java.util.Vector) RDFParseException(org.openrdf.rio.RDFParseException)

Example 2 with HashGraph

use of org.sbpax.impl.HashGraph in project vcell by virtualcell.

the class ModelComparer method extractSubModels.

private void extractSubModels(Graph diff, Set<Graph> subModels) {
    MultiMap<Resource, Resource> bNodesMap = new MultiHashMap<Resource, Resource>();
    MultiMap<Resource, Statement> triples = new MultiHashMap<Resource, Statement>();
    for (Statement statement : diff) {
        Set<Resource> bNodesInTriple = new HashSet<Resource>();
        recordIfBlankNode(statement.getSubject(), statement, bNodesInTriple, triples);
        recordIfBlankNode(statement.getPredicate(), statement, bNodesInTriple, triples);
        recordIfBlankNode(statement.getObject(), statement, bNodesInTriple, triples);
        Set<Resource> bNodesUnion = new HashSet<Resource>();
        bNodesUnion.addAll(bNodesInTriple);
        for (Resource bNode : bNodesInTriple) {
            Set<Resource> bNodes = bNodesMap.get(bNode);
            if (bNodes != null) {
                bNodesUnion.addAll(bNodes);
            }
        }
        for (Resource bNode : bNodesUnion) {
            bNodesMap.put(bNode, bNodesUnion);
        }
    }
    for (Set<Resource> bNodes : bNodesMap.values()) {
        Graph model = new HashGraph();
        for (Resource bNode : bNodes) {
            for (Statement statement : triples.get(bNode)) {
                model.add(statement);
            }
        }
        subModels.add(model);
    }
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) MultiHashMap(org.sbpax.util.MultiHashMap) HashSet(java.util.HashSet)

Example 3 with HashGraph

use of org.sbpax.impl.HashGraph in project vcell by virtualcell.

the class NamespaceAssimilator method smelt.

public Graph smelt(Graph rdf) {
    Graph rdfNew = new HashGraph();
    for (Statement statement : rdf) {
        Resource subjectNew = smelt(rdf, statement.getSubject());
        URI predicateNew = smelt(rdf, statement.getPredicate());
        Value objectNew = statement.getObject() instanceof Resource ? smelt(rdf, (Resource) statement.getObject()) : statement.getObject();
        rdfNew.add(subjectNew, predicateNew, objectNew);
    }
    for (Map.Entry<Resource, Resource> entry : resourceMap.entrySet()) {
        rdfNew.add(entry.getKey(), OWL.SAMEAS, entry.getValue());
    }
    return rdfNew;
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) Value(org.openrdf.model.Value) URI(org.openrdf.model.URI) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with HashGraph

use of org.sbpax.impl.HashGraph in project vcell by virtualcell.

the class TestJPAX method main.

public static void main(String[] args) {
    Graph graph = new HashGraph();
    String ns = DefaultNameSpaces.EX.uri;
    URI r1 = graph.getValueFactory().createURI(ns + "r1");
    URI pe1 = graph.getValueFactory().createURI(ns + "pe1");
    URI pe2 = graph.getValueFactory().createURI(ns + "pe2");
    graph.add(r1, RDF.TYPE, BioPAX3.BiochemicalReaction);
    graph.add(pe1, RDF.TYPE, BioPAX3.Protein);
    graph.add(pe2, RDF.TYPE, BioPAX3.Protein);
    graph.add(r1, BioPAX3.left, pe1);
    graph.add(r1, BioPAX3.right, pe2);
    try {
        Map<String, String> nsMap = DefaultNameSpaces.defaultMap.convertToMap();
        SesameRioUtil.writeRDFToStream(System.out, graph, nsMap, RDFFormat.RDFXML);
    } catch (RDFHandlerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) RDFHandlerException(org.openrdf.rio.RDFHandlerException) URI(org.openrdf.model.URI)

Example 5 with HashGraph

use of org.sbpax.impl.HashGraph in project vcell by virtualcell.

the class VCMetaData method getRdfDataCopy.

public Graph getRdfDataCopy() {
    Graph rdfModelCopy = new HashGraph();
    rdfModelCopy.addAll(getRdfData());
    return rdfModelCopy;
}
Also used : HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph)

Aggregations

Graph (org.openrdf.model.Graph)9 HashGraph (org.sbpax.impl.HashGraph)9 HashMap (java.util.HashMap)5 IOException (java.io.IOException)3 URL (java.net.URL)3 URLConnection (java.net.URLConnection)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3 MalformedURLException (java.net.MalformedURLException)2 Resource (org.openrdf.model.Resource)2 Statement (org.openrdf.model.Statement)2 URI (org.openrdf.model.URI)2 RDFParseException (org.openrdf.rio.RDFParseException)2 PCExceptionResponse (org.vcell.sybil.util.http.pathwaycommons.PCExceptionResponse)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Vector (java.util.Vector)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Element (org.jdom.Element)1 Value (org.openrdf.model.Value)1