Commit 04a398bb authored by Claus Jonathan Fritzemeier's avatar Claus Jonathan Fritzemeier
Browse files

merged master with writeSBML branch

parents 4f3015c4 3b8f5e09
Package: sybilSBML
Type: Package
Title: SBML Integration in Package 'Sybil'
Version: 2.0.11
Date: 2016-06-06
Version: 3.0.1
Date: 2016-12-16
Authors@R: c(person(c("C.", "Jonathan"), "Fritzemeier", role = c("cre", "ctb"), email = "clausjonathan.fritzemeier@uni-duesseldorf.de"),
person("Gabriel", "Gelius-Dietrich", role = c("aut", "ctb")),
person("Deya", "Alzoubi", role = "ctb"))
person("Deya", "Alzoubi", role = "ctb"),
person("Ardalan", "Habil", role = "ctb"))
Maintainer: C. Jonathan Fritzemeier <clausjonathan.fritzemeier@uni-duesseldorf.de>
Depends: R (>= 2.14.2), Matrix, sybil (>= 2.0.0)
Imports: methods
Description: 'SBML' (Systems Biology Markup Language) integration in 'sybil'. Many constraint based metabolic models are published in 'SBML' format ('*.xml'). Herewith is the ability to read and check 'SBML' files in 'sybil' provided.
Description: 'SBML' (Systems Biology Markup Language) with FBC (Flux Balance Constraints) integration in 'sybil'. Many constraint based metabolic models are published in 'SBML' format ('*.xml'). Herewith is the ability to read, write, and check 'SBML' files in 'sybil' provided.
License: GPL-3 | file LICENSE
LazyLoad: yes
Collate: generics.R sbmlPtrClass.R sbmlErrorClass.R sybilSBML.R
uglyHack.R readSBMLmod.R zzz.R
Packaged: 2015-06-06 07:11:55 UTC; jonathan
Packaged: 2016-12-16 07:11:55 UTC; jonathan
Author: C. Jonathan Fritzemeier [cre, ctb],
Gabriel Gelius-Dietrich [aut, ctb],
Deya Alzoubi [ctb]
Deya Alzoubi [ctb],
Ardalan Habil [ctb]
NeedsCompilation: yes
Repository: CRAN
......@@ -48,5 +48,9 @@ getSBMLunitDefinitionsList,
getSBMLCompartList,
getSBMLSpeciesList,
getSBMLReactionsList,
readSBMLmod
readSBMLmod,
writeSBML,
getSBMLFbcversion,
getSBMLmodNotes,
getSBMLmodAnnotation
)
......@@ -247,7 +247,13 @@ parseNotesReact <- function(notes) {
gpr <- sub("GENE[_ ]?ASSOCIATION: *", "", fields_str[j])
gene_rule <- sybil:::.parseBoolean(gpr)
#print(gene_rule)
}#Ardalan Habil
else if (grepl("GPR[_ ]?ASSOCIATION", fields_str[j])) {
gpr <- sub("GPR[_ ]?ASSOCIATION: *", "", fields_str[j])
gene_rule <- sybil:::.parseBoolean(gpr)
}
if (charmatch("SUBSYSTEM", fields_str[j], nomatch = -1) != -1) {
subSyst <- sub("SUBSYSTEM: *", "", fields_str[j])
subSyst <- sub("^S_", "", subSyst, perl = TRUE)
......@@ -283,6 +289,14 @@ sbmldoc <- openSBMLfile(filename)
message("OK")
# warning if new Version/Level/
SBMLlevel<- getSBMLlevel(sbmldoc)
SBMLversion<- getSBMLversion(sbmldoc)
FBCversion<-getSBMLFbcversion(sbmldoc)
if(SBMLlevel == 3 && SBMLversion > 1)
warning(paste("No support for Level 3 Version ",SBMLversion))
if (FBCversion > 2)
warning(paste("No support for Fbc Version ",FBCversion))
#------------------------------------------------------------------------------#
# check the model #
......@@ -380,6 +394,7 @@ if (mdesc == filename) {
sybil::mod_desc(mod) <- mdesc
#------------------------------------------------------------------------------#
# units #
#------------------------------------------------------------------------------#
......@@ -398,7 +413,7 @@ if (is.null(compartmentsList)) {
}
missingId(compartmentsList)
sybil::mod_compart(mod) <- compartmentsList[["id"]]
comp_tmp_id <- compartmentsList[["id"]]
#------------------------------------------------------------------------------#
......@@ -430,6 +445,7 @@ missingId(metabolitesList)
metSpIds <- metabolitesList[["id"]]
#nummet <- getSBMLnumSpecies(sbmlmod)
if (isTRUE(bndCond)) {
metSpBnd <- metabolitesList[["boundaryCondition"]]
met_id_pos <- !metSpBnd
......@@ -485,12 +501,20 @@ gpr <- character(numreact)
hasNotes <- FALSE
hasAnnot <- FALSE
#FBC contraints @Ardalan Habil
fbclowbnd<-reactionsList[["fbc_lowbnd"]]
fbcuppbnd<-reactionsList[["fbc_uppbnd"]]
fbcgprRules<-reactionsList[["fbc_gprRules"]]
fbcObjectives<-reactionsList[["fbc_Objectives"]]
for (i in 1 : numreact) {
# the notes/annotations field
notes <- reactionsList[["notes"]][i]
annot <- reactionsList[["annotation"]][i]
# Notes und Annotation can be null ( @Ardalan Habil)
if(!is.null( reactionsList[["notes"]]))
if (nchar(notes) > 0) {
hasNotes <- TRUE
......@@ -504,7 +528,7 @@ for (i in 1 : numreact) {
}
else {
if(!is.null( reactionsList[["annotation"]]))
if (nchar(annot) > 0) {
hasAnnot <- TRUE
pn <- regexpr("Pathway Name: [^<]+", annot, perl = TRUE)
......@@ -514,6 +538,17 @@ for (i in 1 : numreact) {
}
fbcgene_rule <- NA
if ( !is.null(fbcgprRules))
{
fbcgene_rule<- sybil:::.parseBoolean(fbcgprRules[i])
genes[[i]] <- fbcgene_rule$gene # list of genes
rules[i] <- fbcgene_rule$rule # rules
gpr[i] <- fbcgprRules[i]
}
# Check here if reactants and products lists exist, same for the stoichiometry slot
# Entries for S -- the reactants
......@@ -565,6 +600,16 @@ for (i in 1 : numreact) {
# }
# the constraints
#FBC contraints @Ardalan Habil
if ( !is.null(fbclowbnd) && !is.null(fbcuppbnd))
{
lbnd[i] <- checkupplowbnd(fbclowbnd[i])
ubnd[i] <- checkupplowbnd(fbcuppbnd[i])
}
#read from kinetic_law if fbc is empty
else
{
parm <- reactionsList[["kinetic_law"]][[i]]
if (is.null(parm)) {
ubnd[i] <- def_bnd
......@@ -591,9 +636,15 @@ for (i in 1 : numreact) {
# reduced cost? (sbml file)
}
}
}
#FBC Objective @Ardalan Habil
if(!is.null(fbcObjectives))
{
ocof[i]<-as.numeric(fbcObjectives[i])
}
}
}
# ---------------------------------------------------------------------------- #
# search for unused metabolites and unused reactions
......@@ -877,7 +928,7 @@ else {
rules <- rules[SKIP_REACTION]
gpr <- gpr[SKIP_REACTION]
if (isTRUE(hasNotes)) {
if (isTRUE(hasNotes) || !is.null(fbcgprRules) ) {
message("GPR mapping ... ", appendLF = FALSE)
#allGenes <- unique(allGenes)
......@@ -898,13 +949,12 @@ else {
geneInd <- match(genes[[i]], allGenes)
rxnGeneMat[i, geneInd] <- TRUE
# no use because new gprRule format
# for (j in 1 : length(geneInd)) {
# pat <- paste("x(", j, ")", sep = "")
# repl <- paste("x[", geneInd[j], "]", sep = "")
#
# rules[i] <- gsub(pat, repl, rules[i], fixed = TRUE)
# }
for (j in 1 : length(geneInd)) {
pat <- paste("x(", j, ")", sep = "")
repl <- paste("x[", geneInd[j], "]", sep = "")
rules[i] <- gsub(pat, repl, rules[i], fixed = TRUE)
}
}
}
......@@ -970,6 +1020,63 @@ react_name_tmp <- sub( "\\s+$", "", react_name_tmp, perl = TRUE)
sybil::react_name(mod) <- react_name_tmp
#------------------------------------------------------------------------------#
# Reaction Attr @Ardalan #
#------------------------------------------------------------------------------#
# Test for new Slots
if( .hasSlot(mod,"mod_attr") && .hasSlot(mod,"comp_attr") && .hasSlot(mod,"met_attr") && .hasSlot(mod,"react_attr") )
newSybil<-TRUE
else newSybil<-FALSE
numreact<-nummet <- sum(SKIP_REACTION)
reactannotation <- reactionsList[["annotation"]][SKIP_REACTION]
reactnotes <- reactionsList[["notes"]][SKIP_REACTION]
if(newSybil)
{
sybil::react_attr(mod) <-data.frame(row.names=1:numreact)
#Speed optimierung durch notes NULL falls nichts drin steht
if( !is.null(reactannotation) && length(reactannotation)==numreact )sybil::react_attr(mod)[['annotation']]<-reactannotation
if( !is.null(reactnotes) && length(reactnotes)==numreact )sybil::react_attr(mod)[['notes']]<-reactnotes
}
#------------------------------------------------------------------------------#
# Model Attr @Ardalan #
#------------------------------------------------------------------------------#
modanno<-getSBMLmodAnnotation(sbmlmod)
modnotes<-getSBMLmodNotes(sbmlmod)
if(newSybil)
{
sybil::mod_attr(mod) <-data.frame(row.names=1)
if(nchar(modanno)>1)sybil::mod_attr(mod)[['annotation']]<-modanno
if(nchar(modnotes)>1)sybil::mod_attr(mod)[['notes']]<-modnotes
}
#------------------------------------------------------------------------------#
# compartments Attr @Ardalan #
#------------------------------------------------------------------------------#
# Define SKIP_COMPARTMENT FALSE= HAS NO REFERENCE
met_comp_tmp <- metabolitesList[["compartment"]][met_id_pos][SKIP_METABOLITE]
SKIP_COMPARTMENT<- comp_tmp_id %in% unique(met_comp_tmp)
sybil::mod_compart(mod) <- comp_tmp_id[SKIP_COMPARTMENT]
numcom<-length(mod_compart(mod))
comannotation <- compartmentsList[["annotation"]][SKIP_COMPARTMENT]
comnotes <- compartmentsList[["notes"]][SKIP_COMPARTMENT]
if(newSybil)
{
sybil::comp_attr(mod) <-data.frame(row.names=1:numcom)
if( !is.null(comannotation) && length(comannotation)==numcom )sybil::comp_attr(mod)[['annotation']]<-comannotation
if( !is.null(comnotes) && length(comnotes)==numcom )sybil::comp_attr(mod)[['notes']]<-comnotes
}
#------------------------------------------------------------------------------#
# metabolite id's #
#------------------------------------------------------------------------------#
......@@ -985,7 +1092,7 @@ sybil::met_id(mod) <- gsub("-", "_", met_id_tmp, fixed = TRUE)
# metabolite compartments #
#------------------------------------------------------------------------------#
met_comp_tmp <- metabolitesList[["compartment"]][met_id_pos][SKIP_METABOLITE]
#met_comp_tmp <- metabolitesList[["compartment"]][met_id_pos][SKIP_METABOLITE]
sybil::met_comp(mod) <- match(met_comp_tmp, sybil::mod_compart(mod))
......@@ -1002,6 +1109,69 @@ met_name_tmp <- sub( "\\s+$", "", met_name_tmp, perl = TRUE)
sybil::met_name(mod) <- met_name_tmp
#------------------------------------------------------------------------------#
# metabolite attr @Ardalan Habil #
#------------------------------------------------------------------------------#
#ChemicalFormula Charge Notes Annotation MetaID @Ardalan Habil
metformula <- metabolitesList[["chemicalFormula"]][met_id_pos][SKIP_METABOLITE]
metcharge <- metabolitesList[["charge"]][met_id_pos][SKIP_METABOLITE]
metnotes <- metabolitesList[["notes"]][met_id_pos][SKIP_METABOLITE]
metannotation <- metabolitesList[["annotation"]][met_id_pos][SKIP_METABOLITE]
metchargenote<-NULL
metformulanote<-NULL
# check metnotes for Formula and Charge
if( !is.null(metnotes) && length(metnotes==nummet))
{
pn <- regexpr("FORMULA: [^<]+", metnotes, perl = TRUE)
metformulanote <- substr(metnotes, (pn+9), pn + ((attr(pn, "match.length"))-1))
pn <- regexpr("CHARGE: [^<]+", metnotes, perl = TRUE)
metchargenote <- substr(metnotes, (pn+8), pn + ((attr(pn, "match.length"))-1))
metchargenote <- as.integer(metchargenote)
metchargenote[is.na(metchargenote)] <- 0
}
nummet <- sum(SKIP_METABOLITE)
if(newSybil)
{
# save attributes to met_attr slot
sybil::met_attr(mod) <-data.frame(row.names=1:nummet)
if( !is.null(metformula) && length(metformula)==nummet)
{sybil::met_attr(mod)[['chemicalFormula']]<-metformula}
else{
if(length(metformulanote)==nummet)
{ if(max(nchar(metformulanote)) >0)
sybil::met_attr(mod)[['chemicalFormula']]<-metformulanote
}
}
if( !is.null(metcharge) && length(metcharge)==nummet && sum(metcharge)!=0)
{sybil::met_attr(mod)[['charge']]<-metcharge}
else{
if( length(metchargenote)==nummet)
{ if(max(nchar(metchargenote)) >0)
sybil::met_attr(mod)[['charge']]<-metchargenote
}
}
if( !is.null(metnotes) && length(metnotes)==nummet)sybil::met_attr(mod)[['notes']]<-metnotes
if( !is.null(metannotation) && length(metannotation)==nummet)sybil::met_attr(mod)[['annotation']]<-metannotation
# Save boundaryCondition when bndCond=FALSE
if (!isTRUE(bndCond)) {
metBnd <- metabolitesList[["boundaryCondition"]][met_id_pos][SKIP_METABOLITE]
# When all metBnd = False -> metabolite removed by extMetFlag
if( !is.null(metBnd) && length(metBnd)==nummet && !all(metBnd == FALSE) )sybil::met_attr(mod)[['boundaryCondition']]<-metBnd
}
}
#------------------------------------------------------------------------------#
# check reversibilities #
#------------------------------------------------------------------------------#
......
......@@ -124,6 +124,17 @@ getSBMLversion <- function(sbmlf) {
return(version)
}
#------------------------------------------------------------------------------#
getSBMLFbcversion <- function(sbmlf) {
version <- .Call("getSBMLFbcversion", PACKAGE = "sybilSBML",
sbmlPointer(sbmlf)
)
return(version)
}
#------------------------------------------------------------------------------#
......@@ -186,6 +197,28 @@ getSBMLmodName <- function(sbmlm) {
return(modn)
}
#------------------------------------------------------------------------------#
getSBMLmodNotes <- function(sbmlm) {
modnotes <- .Call("getSBMLmodNotes", PACKAGE = "sybilSBML",
sbmlPointer(sbmlm)
)
return(modnotes)
}
#------------------------------------------------------------------------------#
getSBMLmodAnnotation <- function(sbmlm) {
modanno <- .Call("getSBMLmodAnnotation", PACKAGE = "sybilSBML",
sbmlPointer(sbmlm)
)
return(modanno)
}
#------------------------------------------------------------------------------#
......@@ -271,10 +304,272 @@ getSBMLReactionsList <- function(sbmlm) {
}
#------------------------------------------------------------------------------#
#export mod to SBML
deformatSBMLid <- function(idstr) {
idstr <- gsub("-", "_DASH_", idstr, fixed = TRUE)
idstr <- gsub("(", "_LPAREN_", idstr, fixed = TRUE)
idstr <- gsub(")", "_RPAREN_", idstr, fixed = TRUE)
idstr <- gsub("[", "_", idstr, fixed = TRUE)
idstr <- gsub("]", "", idstr, fixed = TRUE)
idstr <- gsub(",", "_COMMA_", idstr, fixed = TRUE)
idstr <- gsub(".", "_PERIOD_", idstr, fixed = TRUE)
idstr <- gsub("'", "_APOS_", idstr, fixed = TRUE)
idstr <- sub("\\(e\\)$", "_e_", idstr)
idstr <- gsub("-", "_", idstr, fixed = TRUE)
return(idstr)
}
deformatGene<-function(idstr) {
idstr<-gsub("\\((\\S+)\\)", "\\1", idstr)
#idstr <- gsub("( ", "(", idstr, fixed = TRUE)
#idstr <- gsub(" (", "(", idstr, fixed = TRUE)
#idstr <- gsub(") ", ")", idstr, fixed = TRUE)
#idstr <- gsub(" )", ")", idstr, fixed = TRUE)
idstr <- gsub(":", "_", idstr, fixed = TRUE)
return(idstr)
}
writeSBML<- function(morg=NULL,level=2,version=4,fbcLevel=0,filename="export.xml",recoverExtMet=FALSE,printNotes=TRUE,printAnnos=TRUE,validation=FALSE ){
if(class(morg)!="modelorg"){
stop("morg has to be of class modelorg\n")
}
###right
if(level==1)
{
# test if Matrix has no double values
if( !all( S(morg) == floor(S(morg))) ) warning("Level 1 does not support double values")
fbcLevel=0
if(version != 2)
{
warning("just Level 1 Version 2 will be supported")
version=2
}
}else if (level==2)
{
fbcLevel=0
if(version >5)
{
warning("Level 2 Version 5 will be supported")
version=5
}
if(version < 1)
{
warning("Level 2 Version 1 will be supported")
version=1
}
}
else if (level==3)
{ if(fbcLevel >2)fbcLevel=2
if(version != 1)
{
print("Level 3 Version 1 will be supported")
version=1
}
}else {
stop(" Support just for Level 1,2 and 3 \n")
}
##All GENES###
allgenes<- unique(unlist(genes(morg)))
allgenes<-allgenes[allgenes != ""]
##EXCHANGE REACTIONS##
ex <- findExchReact(morg)
ex_react<-NULL
# if recoverExtMet== FALSE => null for ex_react
if( (!is.null(ex)) && (recoverExtMet) )
{
if(!(all(diag(S(morg)[met_pos(ex), react_pos(ex)])==-1)))
stop("exchange reactions with Scoeff different than -1\n")
ex_react<-as.integer(react_pos(ex))
}
### Build wrapper for C Function #####
##Met Attribute###
com_meta_id<- NULL
met_meta_id<- NULL
com_notes <- NULL
com_annotation<- NULL
met_notes<- NULL
met_anno<- NULL
met_bnd<- NULL
met_charge<-NULL
met_formula<-NULL
react_notes<-NULL
react_anno<-NULL
mod_notes<-NULL
mod_anno<-NULL
#subsystem
# is subsystem Empty
if(length( colnames(subSys(morg)))==1 && colnames(subSys(morg))[1]=="" ) modhasubSys<-FALSE
else modhasubSys<-TRUE
newsubS<- NULL
if( .hasSlot(morg,"mod_attr") && .hasSlot(morg,"comp_attr") && .hasSlot(morg,"met_attr") && .hasSlot(morg,"react_attr") )
newSybil<-TRUE
else newSybil<-FALSE
### Start newSybil attr
if(newSybil)
{
if(("notes" %in% colnames(mod_attr(morg))) && (printNotes) ) mod_notes<-as.character(mod_attr(morg)[['notes']])
if(("annotation" %in% colnames(mod_attr(morg))) && (printAnnos) ) mod_annotation<-as.character(mod_attr(morg)[['annotation']])
if(("notes" %in% colnames(comp_attr(morg))) && (printNotes) ) com_notes<-as.character(as.list((comp_attr(morg)[['notes']])))
if(("annotation" %in% colnames(comp_attr(morg))) && (printAnnos) ) com_annotation<-as.character(as.list((comp_attr(morg)[['annotation']])))
if("charge" %in% colnames(met_attr(morg))) met_charge<- as.integer(as.list((met_attr(morg)[['charge']])))
if("chemicalFormula" %in% colnames(met_attr(morg))) met_formula<-as.character(as.list((met_attr(morg)[['chemicalFormula']])))
if(("annotation" %in% colnames(met_attr(morg))) && (printAnnos)) met_anno<-as.character(as.list((met_attr(morg)[['annotation']])))
if("boundaryCondition" %in% colnames(met_attr(morg))) met_bnd<-as.logical(as.list((met_attr(morg)[['boundaryCondition']])))
if(("notes" %in% colnames(met_attr(morg))) && (printNotes) )
{ # delete Formular and charge from notes to do
met_notes<-as.character(as.list((met_attr(morg)[['notes']])))
if (!is.null(met_charge) || !is.null(met_formula))
{
for ( i in 1:met_num(morg))
{
if(nchar(met_notes[i])> 8)
{
# Have Assocation in Notes?yes: replace no: append
if (regexpr("html:p", met_notes[i], fixed = TRUE) == -1)tag <- "p"
else tag <- "html:p"
if (!is.null(met_formula))
{
haveform<-grepl("FORMULA: [^<]+",met_notes[i])
#Have Gene if not ->no need to write in FBC2
if(haveform)
{
if(fbcLevel==0)met_notes[i]<-sub("FORMULA: [^<]+",paste("FORMULA: ",met_formula[i], sep = ""), met_notes[i], perl = TRUE)
else met_notes[i]<-sub(paste("<",tag,">","FORMULA: [^<]+","</",tag,">",sep = ""),"",met_notes[i], perl = TRUE)
}
else if(fbcLevel==0) met_notes[i]<-gsub("</notes>",paste("<",tag,">","FORMULA: ",met_formula[i],"</",tag,">","\n</notes>",sep = ""),met_notes[i])
}
if (!is.null(met_charge))
{
havecharge<-grepl("CHARGE: [^<]+",met_notes[i])
#Have Subsystem
if(havecharge)
{
if(fbcLevel !=0 || (level==2 && version==1 ))
met_notes[i]<-sub(paste("<",tag,">","CHARGE: [^<]+","</",tag,">",sep = ""),"",met_notes[i], perl = TRUE)
else met_notes[i]<-sub("CHARGE: [^<]+",paste("CHARGE: ",met_charge[i], sep = ""), met_notes[i], perl = TRUE)
}
else if(fbcLevel==0) if(level!=2 && version!=1) met_notes[i]<-gsub("</notes>",paste("<",tag,">","CHARGE: ",met_charge[i],"</",tag,">","\n</notes>",sep = ""),met_notes[i])
}
}
}
}
}
if(("annotation" %in% colnames(react_attr(morg))) && (printAnnos)) react_anno<-as.character(as.list((react_attr(morg)[['annotation']])))
# Merge Notes with "our" Notes and make sure gpr Rules from gpr
if(("notes" %in% colnames(react_attr(morg))) && (printNotes))
{
react_notes<-as.character(as.list((react_attr(morg)[['notes']])))
# using
# SubSystem EXISTIERT nicht colnames(subSys(ec))
for ( i in 1:react_num(morg))
{
# using the for loop
if(modhasubSys)newsubS[i]<- paste(names(which(subSys(morg)[i,])), collapse=", ")
if(nchar(react_notes[i])> 8)
{
# Have Association in Notes? yes: replace no: append
if (regexpr("html:p", react_notes[i], fixed = TRUE) == -1)tag <- "p"
else tag <- "html:p"
havegene<-grepl("GENE[_ ]?ASSOCIATION: [^<]+",react_notes[i])
havesub<-grepl("SUBSYSTEM: [^<]+",react_notes[i])
#Have Gene if not ->no need to write in FBC2
if(havegene)
{
if(fbcLevel==2) react_notes[i]<-sub(paste("<",tag,">","GENE[_ ]?ASSOCIATION: [^<]+","</",tag,">",sep = ""),"",react_notes[i], perl = TRUE)
else react_notes[i]<-sub("GENE[_ ]?ASSOCIATION: [^<]+",paste("GENE_ASSOCIATION: ",gpr(morg)[i], sep = ""), react_notes[i], perl = TRUE)
}