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;
}
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]));
}
}
}
}
}
}
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);
}
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;
}
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);
}
}
Aggregations