use of org.sbml.jsbml.xml.XMLNode 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.sbml.jsbml.xml.XMLNode 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.sbml.jsbml.xml.XMLNode in project vcell by virtualcell.
the class SBMLAnnotationUtil method elementToXMLNode.
// Converts from JDOM element (used in VCML) to libSBML XMLNode
private static XMLNode elementToXMLNode(Element element) throws XMLStreamException {
String xmlString = XmlUtil.xmlToString(element);
XMLNode xmlNode = XMLNode.convertStringToXMLNode(xmlString);
if (xmlNode == null) {
System.err.println("failed to parse annotation from " + xmlString);
return null;
}
// xmlnode and return the child which is an 'element'
if (!xmlNode.isElement()) {
int numChildren = xmlNode.getNumChildren();
for (int iChild = 0; iChild < numChildren; ++iChild) {
XMLNode child = xmlNode.getChild(iChild);
if (child.isElement()) {
xmlNode = child;
break;
}
}
}
return xmlNode;
}
use of org.sbml.jsbml.xml.XMLNode in project vcell by virtualcell.
the class SBMLAnnotationUtil method readNotes.
/*
// Something like this is what we read:
<notes>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
my text
</body>
</html>
</notes>
*/
public void readNotes(Identifiable identifiable, SBase sBaseObj) throws XMLStreamException {
XMLNode notesNode = sBaseObj.getNotes();
if (notesNode != null) {
String txt = notesNode.toXMLString();
metaData.setFreeTextAnnotation(identifiable, txt);
}
}
use of org.sbml.jsbml.xml.XMLNode in project vcell by virtualcell.
the class SBMLAnnotationUtil method readVCellSpecificAnnotation.
/**
* readVCellSpecificAnnotation : separate method to handle only the VCellRelatedInfo annotation stored in VCellInfo element of
* SBML <annotation>. This is currently used only for species and reactions, since the information stored in these
* elements in the SBML annotation is required for building the corresponding VCell element.
* @param sBase - the corresponding SBML element
* @return - the <VcellRelatedInfo> element
* @throws XMLStreamException
*/
public Element readVCellSpecificAnnotation(SBase sBase) throws XMLStreamException {
Element vcellSpecificElement = null;
Annotation annotation = sBase.getAnnotation();
XMLNode annotationRoot = annotation.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) && (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()) {
continue;
}
// if this child has a prefix, but no explicit namespace (it is defined in its parent); try getting prefix from parent
String childPrefix = child.getPrefix();
if (childPrefix != null) {
String childNamespaceURI = child.getNamespaceURI(childPrefix);
if (childNamespaceURI == null || childNamespaceURI.length() == 0) {
childNamespaceURI = annotationBranch.getNamespaceURI(childPrefix);
child.addNamespace(childNamespaceURI, childPrefix);
}
}
if (child.getName().equals(XMLTags.VCellRelatedInfoTag)) {
// new style VCellInfo element (with <FreeText> and <SbmlImportRelated> subelements
vcellSpecificElement = xmlNodeToElement(child);
} else {
// check if 'child' is oldStyle VCellInfo element.
vcellSpecificElement = processOldStyleVCellInfo(child);
if (vcellSpecificElement == null) {
System.out.println("Unknown VCellInfo annotation type : '" + child.getName());
}
}
// if - else (child is VCellRelatedInfoTag)
}
// for - numChildren
}
// nameSpace != null & equals VCell_NS
}
// for - childCount
}
// annotationRoot != null
return vcellSpecificElement;
}
Aggregations