Search in sources :

Example 6 with Graph

use of org.openrdf.model.Graph in project vcell by virtualcell.

the class VCMetaDataMiriamManager method queryAllMiriamRefGroups.

private Map<MiriamRefGroup, MIRIAMQualifier> queryAllMiriamRefGroups(Identifiable identifiable) {
    Entry entry = vcMetaData.getRegistry().getEntry(identifiable);
    if (entry.getResource() == null) {
        return null;
    }
    MIRIAMizer miriamizer = new MIRIAMizer();
    Graph rdfData = vcMetaData.getRdfData();
    Resource resource = entry.getResource();
    Map<RefGroup, MIRIAMQualifier> allRefGroups = miriamizer.getAllRefGroups(rdfData, resource);
    Map<MiriamRefGroup, MIRIAMQualifier> wrappedRefGroups = new HashMap<MiriamRefGroup, MIRIAMQualifier>();
    for (RefGroup refGroup : allRefGroups.keySet()) {
        MiriamRefGroup miriamRefGroup = new VCMetaDataMiriamRefGroup(refGroup);
        MIRIAMQualifier miriamQualifier = allRefGroups.get(refGroup);
        wrappedRefGroups.put(miriamRefGroup, miriamQualifier);
    }
    return wrappedRefGroups;
}
Also used : Entry(cbit.vcell.biomodel.meta.registry.Registry.Entry) Graph(org.openrdf.model.Graph) HashMap(java.util.HashMap) MIRIAMizer(org.vcell.sybil.models.miriam.MIRIAMizer) MiriamResource(cbit.vcell.biomodel.meta.MiriamManager.MiriamResource) Resource(org.openrdf.model.Resource) MIRIAMQualifier(org.vcell.sybil.models.miriam.MIRIAMQualifier) RefGroup(org.vcell.sybil.models.miriam.RefGroup)

Example 7 with Graph

use of org.openrdf.model.Graph 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().getFullAnnotation();
    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));
                // TODO: old way of reading the text annotations, as application-specific (vCell) annotations. Now we use Notes, which is more compatible with SBML standard
                // } 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 clonedAnnotationBranch = annotationBranch.clone();
                        String annotationBranchString = clonedAnnotationBranch.toXMLString();
                        annotationBranchString = annotationBranchString.replace("\t", "");
                        annotationBranchString = annotationBranchString.replace("\r", "");
                        annotationBranchString = annotationBranchString.replace("\n", "");
                        annotationBranchString = annotationBranchString.trim();
                        if (annotationBranchString.isEmpty()) {
                            // TODO: why (and where) are they being generated anyway
                            continue;
                        }
                        XMLNode clonedAnnotRoot = new XMLNode(annotationRoot);
                        clonedAnnotRoot.setNamespaces(annotationRoot.getNamespaces());
                        clonedAnnotRoot.removeChildren();
                        clonedAnnotRoot.addChild(annotationBranch.clone());
                        String str = clonedAnnotRoot.toXMLString();
                        // (XmlUtil.stringToXML(xmlString, null)).getRootElement();
                        elementXML = (XmlUtil.stringToXML(str, 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 8 with Graph

use of org.openrdf.model.Graph in project vcell by virtualcell.

the class SBMLAnnotationUtil method writeAnnotation.

public void writeAnnotation(Identifiable identifiable, SBase sBase, Element vcellImportRelatedElement) throws XMLStreamException {
    // Deal with RDF annotation
    XMLNode rootAnnotation = new XMLNode(tripleAnnotation, new XMLAttributes());
    Resource resource = metaData.getRegistry().getEntry(identifiable).getResource();
    Graph rdfChunk = null;
    if (resource != null) {
        rdfChunk = chopper.getChops().get(resource);
    }
    if (identifiable == root && rdfChunk != null) {
        rdfChunk.addAll(chopper.getRemains());
    }
    String strRdfAnnotations = null;
    XMLNode rootRDF = null;
    if (rdfChunk != null && metaData.getBaseURIExtended() != null) {
        Element element = XMLRDFWriter.createElement(rdfChunk, nsSBML);
        // System.out.println(ss1);
        if (element.getAttributes().isEmpty() && element.getContent().isEmpty()) {
            // hence this test, we just ignore such elements
            ;
        } else {
            XMLNamespaces xmlnss = new XMLNamespaces();
            xmlnss.add(DefaultNameSpaces.RDF.uri, DefaultNameSpaces.RDF.prefix);
            strRdfAnnotations = XmlUtil.xmlToString(element);
            // COPASI doesn't understand our metaid namespace, so we just replace it with a #
            String olds = "http://sourceforge.net/projects/vcell/vcml/cbit.vcell.model.Species/metaid_";
            String news = "#metaid_";
            strRdfAnnotations = strRdfAnnotations.replace(olds, news);
            // TODO: for some reason, converting strRdfAnnotations directly to rootRDF node through XMLNode.convertStringToXMLNode isn't working
            // we need to use the trick below to create a temp root annotation and extract the rootRdf node from it
            strRdfAnnotations = "<annotation>" + strRdfAnnotations;
            strRdfAnnotations += "</annotation>";
            XMLNode tempRootAnnotation = XMLNode.convertStringToXMLNode(strRdfAnnotations, xmlnss);
            // it's supposed to be exactly one, the rdf root node
            int numChildren = tempRootAnnotation.getNumChildren();
            rootRDF = tempRootAnnotation.getChild(0);
        }
    }
    if (rootRDF != null && rootRDF.getNumChildren() > 0) {
        // add the rdf root node which may contain multiple rdf annotations
        rootAnnotation.addChild(rootRDF);
    }
    // Deal with the non-RDF; VCell free-text annotations
    // get free text annotation from NonRDFAnnotation (associated with identifiable); create XMLNode
    XMLNode rootVCellInfo = new XMLNode(tripleVCellInfo, new XMLAttributes());
    rootVCellInfo.addNamespace(XMLTags.SBML_VCELL_NS, XMLTags.VCELL_NS_PREFIX);
    // VCell specific info to be exported to SBML as annotation - used for import, not needed for metadata
    if (vcellImportRelatedElement != null) {
        XMLNode xn = elementToXMLNode(vcellImportRelatedElement);
        if (xn != null) {
            xn.removeNamespace(XMLTags.VCELL_NS_PREFIX);
            rootVCellInfo.addChild(xn);
        }
    }
    if (rootVCellInfo.getNumChildren() > 0) {
        // add the vcellinfo root node which may contain the free text annotation (not anymore!  dan aug 2019)
        rootAnnotation.addChild(rootVCellInfo);
    }
    if (rootAnnotation.getNumChildren() > 0) {
        sBase.setAnnotation(rootAnnotation);
    }
    writeMetaID(identifiable, sBase);
}
Also used : XMLAttributes(org.sbml.jsbml.xml.XMLAttributes) HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) XMLNode(org.sbml.jsbml.xml.XMLNode) Element(org.jdom.Element) Resource(org.openrdf.model.Resource) XMLNamespaces(org.sbml.jsbml.xml.XMLNamespaces)

Example 9 with Graph

use of org.openrdf.model.Graph 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()) {
        Resource subject = entry.getKey();
        Resource value = entry.getValue();
        rdfNew.add(subject, OWL.SAMEAS, value);
    }
    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 10 with Graph

use of org.openrdf.model.Graph 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)

Aggregations

Graph (org.openrdf.model.Graph)26 HashGraph (org.sbpax.impl.HashGraph)12 Resource (org.openrdf.model.Resource)10 HashMap (java.util.HashMap)8 Statement (org.openrdf.model.Statement)7 HashSet (java.util.HashSet)6 URI (org.openrdf.model.URI)6 RDFHandlerException (org.openrdf.rio.RDFHandlerException)5 Entry (cbit.vcell.biomodel.meta.registry.Registry.Entry)4 Literal (org.openrdf.model.Literal)4 Value (org.openrdf.model.Value)4 GraphImpl (org.openrdf.model.impl.GraphImpl)4 MiriamResource (cbit.vcell.biomodel.meta.MiriamManager.MiriamResource)3 IOException (java.io.IOException)3 URL (java.net.URL)3 URLConnection (java.net.URLConnection)3 Map (java.util.Map)3 MalformedURLException (java.net.MalformedURLException)2 Date (java.util.Date)2 Set (java.util.Set)2