-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from sbmlteam/issue-296-setannotation
Issue 296 setannotation
- Loading branch information
Showing
15 changed files
with
623 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ foreach(example | |
translateL3Math | ||
unsetAnnotation | ||
unsetNotes | ||
setAnnotation | ||
validateSBML | ||
|
||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Example, reading an SBML file containing an element with provided | ||
* meta id, reading a raw annoation from a file, and adding it to the | ||
* element. | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
#include <sbml/SBMLTypes.h> | ||
|
||
using namespace std; | ||
LIBSBML_CPP_NAMESPACE_USE | ||
|
||
XMLNode* parseAnnotation(const std::string& annotationFile) | ||
{ | ||
// read file into memory | ||
FILE* file = fopen(annotationFile.c_str(), "r"); | ||
fseek(file, 0, SEEK_END); | ||
long length = ftell(file); | ||
|
||
fseek(file, 0, SEEK_SET); | ||
char* data = new char[length + 1]; | ||
|
||
// fill data with zeros | ||
memset(data, 0, length + 1); | ||
|
||
fread(data, 1, length, file); | ||
fclose(file); | ||
data[length] = '\0'; | ||
|
||
// parse the string | ||
XMLNode* xmlnode = XMLNode::convertStringToXMLNode(data); | ||
delete[] data; | ||
return xmlnode; | ||
} | ||
|
||
bool setAnnotation(const std::string& sbmlFile, const std::string& metaId, | ||
const std::string& annotationFile, const std::string& outputFile) | ||
{ | ||
SBMLDocument *document = readSBML(sbmlFile.c_str()); | ||
if (document->getNumErrors(LIBSBML_SEV_ERROR) > 0) | ||
{ | ||
cerr << "Encountered the following SBML errors:" << std::endl; | ||
document->printErrors(); | ||
return false; | ||
} | ||
|
||
SBase* element = document->getElementByMetaId(metaId); | ||
if (!element) | ||
{ | ||
cerr << "No element with meta id " << metaId << " found." << std::endl; | ||
return false; | ||
} | ||
|
||
XMLNode* annotation = parseAnnotation(annotationFile); | ||
if (!annotation) | ||
{ | ||
cerr << "the annotation could not be parsed from file: " << annotationFile << "." << std::endl; | ||
return false; | ||
} | ||
std::string annotationString = annotation->toXMLString(); | ||
element->setAnnotation(annotation); | ||
|
||
// at this point we'd expect the annotation to be set precisely to what it was | ||
// in the file. | ||
|
||
|
||
std::string resultingAnnotation = element->getAnnotationString(); | ||
|
||
if (annotationString != resultingAnnotation) | ||
{ | ||
cerr << "Annotation was not set correctly." << std::endl; | ||
return false; | ||
} | ||
|
||
// write document to output file | ||
writeSBMLToFile(document, outputFile.c_str()); | ||
|
||
delete document; | ||
|
||
return true; | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
std::string sbmlFile; | ||
std::string metaId; | ||
std::string annotationFile; | ||
std::string outputFile; | ||
|
||
if (argc != 5) | ||
{ | ||
cout << endl | ||
<< " usage: setAnnotaion <input-filename> <element-meta-id> <annotation-file> <output-file>" << endl | ||
<< " Adds controlled vocabulary term to a species" << endl | ||
<< endl; | ||
return 1; | ||
|
||
|
||
sbmlFile = "bm190.xml"; | ||
annotationFile = "annotation.xml"; | ||
metaId = "metaid_0000036"; | ||
outputFile = "bm190_out.xml"; | ||
|
||
} | ||
else | ||
{ | ||
sbmlFile = argv[1]; | ||
metaId = argv[2]; | ||
annotationFile = argv[3]; | ||
outputFile = argv[4]; | ||
} | ||
|
||
setAnnotation(sbmlFile, metaId, annotationFile, outputFile); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.