def get_species_id_list_libsbml(model_file = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'):
    """ Return a list of species IDs from the input model."""
    
    reader = SBMLReader()
    document = reader.readSBMLFromFile(model_file)
    model = document.getModel()
    
    species_id_list = []
    for metabolite in model.getListOfSpecies():
        species_id_list.append((metabolite.getId(), metabolite.getName()))
    
    return species_id_list
def get_species_id_dict(model_file = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'):
    """ Return a dictionary of species IDs from IDs and names from the input model."""
    
    reader = SBMLReader()
    document = reader.readSBMLFromFile(model_file)
    model = document.getModel()
    
    species_id_dict = {}
    ambiguous_synonyms = []
    covered_metabolites = []
    for metabolite in model.getListOfSpecies():
        
        metabolite_id = metabolite.getId()
        
        #print metabolite_id
        
        
        if metabolite_id[0:2] == "M_":
            met_id = metabolite_id[2:].lower().strip()
            met_id_orig = metabolite_id[2:]
        else:
            met_id = metabolite_id.lower().strip()
            met_id_orig = metabolite_id
        met_name = metabolite.getName().lower().strip()
        
        if met_id in species_id_dict:
            print("Ambiguous ID: {} - {} ({})".format(met_id, met_name, species_id_dict[met_id]))
            ambiguous_synonyms.append(met_id)
        else:
            species_id_dict[met_id] = met_id_orig
            
        if met_name in species_id_dict:
            if met_name != met_id:
                print("Ambiguous name: {} - {} ({})".format(met_name, met_id, species_id_dict[met_id]))
                ambiguous_synonyms.append(met_name)
        else:
            species_id_dict[met_name + metabolite_id[-2:]] = met_id_orig
        
    for ambiguous_synonym in ambiguous_synonyms:
        species_id_dict.pop(ambiguous_synonym, None)
        
        
    return species_id_dict
def get_species_id_list(model_file = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'):
    """ Return a list of species IDs from an SBML model.  Use COBRA.
    
    Also, return whether each metabolite is an orphan
    """
    
    cobra_model = create_cobra_model_from_sbml_file(model_file)
    
    reader = SBMLReader()
    document = reader.readSBMLFromFile(model_file)
    model = document.getModel()
    
    species_id_list = []
    print("Model orphans:\n")
    for metabolite in cobra_model.metabolites:
        ## Is metabolite an orphan (and internal)?
         
        reactions_list = metabolite.get_reaction()
         
        
        if len(reactions_list) == 1:
            orphan = True
            print metabolite.id
        else:
            orphan = False
         
        met_id = re.sub("__","-",metabolite.id)
        met_name = re.sub("__","-",metabolite.name)
             
        species_id_list.append((met_id, met_name, orphan))
         
#     species_id_list = []
#     for metabolite in model.getListOfSpecies():
#         species_id_list.append((metabolite.getId(), metabolite.getName(), False))
         
    return species_id_list
Exemplo n.º 4
0
from pathlib import Path
import pkg_resources

from libsbml import SBMLReader 


model_file_name = "example-network.xml"

model_file = Path(pkg_resources.resource_filename("libsbml_draw", "model/libs/" + model_file_name))

print("model file: ", model_file)
    
reader = SBMLReader()    

doc = reader.readSBMLFromFile(str(model_file))    

model = doc.getModel()

speciesList = model.getListOfSpecies()

print("num species: ", model.getNumSpecies())
print("num species: ", len(speciesList))


def getBoundarySpeciesIds(doc):

        model = doc.getModel()
        speciesList = model.getListOfSpecies()    
    
        boundarySpeciesIds = list()
Exemplo n.º 5
0



if __name__ == '__main__':
    
    gpr_file = '/Users/wbryant/Dropbox/UG Project - COGzymes/model data/iAH991_reactions_revised.csv'
    f_in = open(gpr_file,'r')
    gpr_dict = {}
    for line in f_in:
        cols = line.split("\t")
        gpr_dict[cols[1]] = cols[4]
    
    infile = '/Users/wbryant/Dropbox/UG Project - COGzymes/model data/BTH_iah991.xml'
    reader = SBMLReader()
    document = reader.readSBMLFromFile(infile)
    model = document.getModel()
    
    notes_string = '  <body xmlns="http://www.w3.org/1999/xhtml">\n    <p>GENE_ASSOCIATION: %s</p>\n  </body>'
    
    for reaction in model.getListOfReactions():
        
        id_rxn = reaction.getId()
        
        id_rxn = re.sub("^R_","",id_rxn)
        id_rxn = re.sub("_LPAREN_","(",id_rxn)
        id_rxn = re.sub("_RPAREN_",")",id_rxn)
        id_rxn = re.sub("_DASH_","-",id_rxn)        
        
        if id_rxn in gpr_dict:
            gpr = gpr_dict[id_rxn]
Exemplo n.º 6
0
    def readFromFile(self, path, omex=False):
        '''
        Reads EnzymeML document to an object layer EnzymeMLDocument class.
        
        Args:
            String path: Path to .omex container or folder destination for plain .xml
            Boolean omex: Determines whether reader handles an .omex file or not
        '''

        self.omex = omex
        self.__path = path

        if self.omex:
            self.archive = CombineArchive()
            self.archive.initializeFromArchive(self.__path)

            sbmlfile = self.archive.getEntry(0)
            content = self.archive.extractEntryToString(sbmlfile.getLocation())

        reader = SBMLReader()

        if self.omex:
            document = reader.readSBMLFromString(content)

        else:
            document = reader.readSBMLFromFile(self.__path + '/experiment.xml')

        document.getErrorLog().printErrors()

        model = document.getModel()

        enzmldoc = EnzymeMLDocument(model.getName(), model.getLevel(),
                                    model.getVersion())

        # Fetch references
        self.__getRefs(model, enzmldoc)

        # Fetch meta data
        try:
            creators = self.__getCreators(model)
            enzmldoc.setCreator(creators)
        except AttributeError:
            enzmldoc.setCreator(Creator("UNKNOWN", "UNKNOWN", "UNKNOWN"))

        try:
            model_hist = model.getModelHistory()
            enzmldoc.setCreated(model_hist.getCreatedDate().getDateAsString())
            enzmldoc.setModified(
                model_hist.getModifiedDate().getDateAsString())
        except AttributeError:
            enzmldoc.setCreated("2020")
            enzmldoc.setModified("2020")

        # Fetch units
        unitDict = self.__getUnits(model)
        enzmldoc.setUnitDict(unitDict)

        # Fetch Vessel
        vessel = self.__getVessel(model)
        enzmldoc.setVessel(vessel, use_parser=False)

        # Fetch Species
        proteinDict, reactantDict = self.__getSpecies(model)
        enzmldoc.setReactantDict(reactantDict)
        enzmldoc.setProteinDict(proteinDict)

        # fetch reaction
        reactionDict = self.__getReactions(model, enzmldoc)
        enzmldoc.setReactionDict(reactionDict)

        del self.__path

        return enzmldoc
Exemplo n.º 7
0
def import_SBML(SBML_filename, rxn_notes_fields = [], met_notes_fields = []):
    """Import file C{SBML_filename} and convert it into a bipartite metabolic network."""
    
    print '\n\nImporting SBML model ...'
    
    # Import SBML model
    reader = SBMLReader()
    document = reader.readSBMLFromFile(SBML_filename)
    model = document.getModel()
    print 'Model being imported: %s ...' % (model.getId())
    
    # Initialize NetworkX model and populate with metabolite and reaction nodes.
    # At the same time, create an id / node_idx dictionary for use when creating
    # edges.
    G = nx.DiGraph()
    G.model_id = model.getId()
    node_idx = 0
    node_id_dictionary = {}
   
    for metabolite in model.getListOfSpecies():
        node_idx += 1
        G.add_node(node_idx)
        G.node[node_idx]['name'] = metabolite.getName().strip()
        G.node[node_idx]['id'] = metabolite.getId()
        G.node[node_idx]['type'] = 'metabolite'
        node_id_dictionary[metabolite.getId()] = node_idx

    for reaction in model.getListOfReactions():
        node_idx += 1
        G.add_node(node_idx)
        G.node[node_idx]['name'] = reaction.getName().strip()
        G.node[node_idx]['id'] = reaction.getId()
        G.node[node_idx]['type'] = 'reaction'
        node_id_dictionary[reaction.getId()] = node_idx
        
        #print node_idx
        #print G.node[node_idx]['name']
        
        notes = reaction.getNotesString()
        
        genelist = []
        genes = re.search('GENE[_ ]ASSOCIATION\:([^<]+)<',notes)
        if genes is not None:
            for gene in re.finditer('([^\s\&\|\(\)]+)', genes.group(1)):
                if not gene.group(1) == 'and' and not gene.group(1) == 'or' and not gene.group(1) == 'none':
                    genelist.append(gene.group(1))
            G.node[node_idx]['gpr'] = genes.group(1)
        else:
            G.node[node_idx]['gpr'] = 'None'
        G.node[node_idx]['genelist'] = list(set(genelist))
        
        # Cycle through all reactants and products and add edges
        #print 'REACTANTS:'
        for reactant in reaction.getListOfReactants():
            #print reactant.getSpecies()
            reactant_idx = node_id_dictionary[reactant.getSpecies()]
            reactant_stoichiometry = -1*reactant.getStoichiometry()
            #print reactant_idx
            G.add_edge(reactant_idx,node_idx,coeff=reactant_stoichiometry)
            #print G.edges(reactant_idx)
        #print 'PRODUCTS:'
        for product in reaction.getListOfProducts():
            #print product.getSpecies()
            product_idx = node_id_dictionary[product.getSpecies()]
            product_stoichiometry = product.getStoichiometry()
            
            G.add_edge(node_idx,product_idx,coeff=product_stoichiometry)
            
            #print G.edges(node_idx)
        #print '\n'
    # Add degree of each metabolite as 'weight' attribute
    for node in G.nodes():
        if G.node[node]['type'] == 'metabolite':
            G.node[node]['weight'] = float(G.degree(node))
            G.node[node]['score'] = -1*float(G.degree(node))
    print 'Finished model import.'
    
    return G
Exemplo n.º 8
0
 def handle(self, *args, **options):
     
     """ COMMAND DESCRIPTION
         
     """ 
         
     
     SBML_filename = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'
     
     
     ## Clear any previous import from this file and create Source record 
     source_name = SBML_filename.split("/")[-1]
     if Source.objects.filter(name=source_name).count() > 0:
         Source.objects.filter(name=source_name).delete()
         
     source = Source(name=source_name)
     source.save()
     
     """Import Joern's model: assume model has correct stoichiometry, so override DB.
     
     For each non-MetaNetX reaction, try to find MNX metabolites 
     to do best integration possible, then add reaction and 
     non-MNX metabolites."""
     
     ### Use SBML to pull out reaction details (met details are implied)
     
     ## Read in SBML file
     reader = SBMLReader()
     document = reader.readSBMLFromFile(SBML_filename)
     model = document.getModel()
     
     idx = 0
     counter = loop_counter(len(model.getListOfReactions()))
     
     for rxn in model.getListOfReactions():
         
         idx += 1
         counter.step()
         
         
         ## Get reaction reversibility (and direction)
         reversible = rxn.getReversible()
         if reversible == True:
             direction_id = "both"
         else:
             direction_id = "ltr"
         direction = Direction.objects.get(direction=direction_id)
         
         
         ## Get "SOURCE: " and "REACTION: " from notes and find best ID
         note_source_id = get_note(rxn, "SOURCE")
         note_reaction_name = get_note(rxn, "REACTION")
         if note_reaction_name is not None:
             ## Has MNX ID
             reaction_name = note_reaction_name
         elif note_source_id is not None:
             ## Has alternative ID
             reaction_name = note_source_id
         else:
             reaction_name = rxn.getId()
         reaction_name = reaction_name.split(";")[0]
         reaction_name = re.sub("^R\_","",reaction_name)
         
         
         ## Create new reaction
         new_reaction = Reaction(
             name=reaction_name,
             source=source,
         )
         new_reaction.save()
         
         ## Get all old synonyms for this reaction ID and point them towards the model reactions
         for synonym in Reaction_synonym.objects.filter(reaction__name=reaction_name):
             synonym.reaction = new_reaction
             synonym.save()
         
         try:
             synonym = Reaction_synonym.objects.get(synonym=reaction_name)
             synonym.reaction = new_reaction
             synonym.save()
         except:    
             new_reaction_synonym = Reaction_synonym(
                 synonym = reaction_name,
                 reaction = new_reaction,
                 source = source
             )
             new_reaction_synonym.save()
             
         
         ## Create list of all participants in reaction
         metabolites_list = []
         for reactant in rxn.getListOfReactants():
             metabolites_list.append(("reactant",reactant))                     
         for product in rxn.getListOfProducts():
             metabolites_list.append(("product",product))
               
          
         ### For each reactant and product, create stoichiometry
         for metabolite_tuple in metabolites_list:
               
             met = metabolite_tuple[1]
             met_type = metabolite_tuple[0] 
             species_id = met.getSpecies()
               
             ## Establish species ID and compartment
             try:
                 u = species_id.split("__64__")
                 metabolite_id = u[0]
                 compartment_id = u[1]
             except:
                 metabolite_id = species_id
                 print(" - '%s' ID without compartment?" % species_id)
               
             ## Get or create metabolite
             if Metabolite.objects.filter(id=metabolite_id).count() == 1:
                 metabolite = Metabolite.objects.get(id=metabolite_id)
             elif Metabolite.objects.filter(metabolite_synonym__synonym=metabolite_id).distinct().count() == 1:
                 metabolite = Metabolite.objects.filter(metabolite_synonym__synonym=metabolite_id).distinct()[0]
             else:
                 ## There may be multiple entries with the same synonym, so eliminate the synonyms
                 Metabolite_synonym.objects.filter(synonym=metabolite_id).delete()
                 
                 ## Create new metabolite with ID metabolite_id
                 metabolite = Metabolite(
                     id=metabolite_id,
                     name=metabolite_id,
                     source=source
                 )
                 metabolite.save()
                 metabolite_synonym = Metabolite_synonym(
                     synonym = metabolite_id,
                     metabolite = metabolite,
                     source=source
                 )
                 metabolite_synonym.save()                    
              
              
             ## Get compartment 
             try:
                 compartment = Compartment.objects.get(id=compartment_id)
             except:
                 compartment = Compartment(
                     id = compartment_id,
                     name = compartment_id,
                     source = source
                 )
                 try:
                     compartment.save()
                 except:
                     print compartment.id
                     print compartment.name
                     print compartment.source
                     sys.exit(1)
                  
                  
             ## Get stoichiometric coefficient     
             coeff = met.getStoichiometry()
             if met_type == "reactant":
                 coeff = -1*coeff
              
             ## Create stoichiometry for reaction
             stoichiometry = Stoichiometry(
                 reaction = new_reaction,
                 metabolite = metabolite,
                 source = source,
                 compartment = compartment,
                 stoichiometry = coeff
             )
             stoichiometry.save()
         
         ## Now that reaction is imported, add to Metabolic_model with directionality
         model_reaction = Metabolic_model(
             source = source,
             reaction = new_reaction,
             direction = direction
         )
         model_reaction.save()
     
     counter.stop()    
     
     
     
     
     
     
     
     
     
Exemplo n.º 9
0
    def handle(self, *args, **options):
        
        """ Export a model to SBML for a given source ID for exchange.
            
        """ 
        
        
        ## Cycle through command line arguments
        num_args = 0
        for arg in args:
            num_args += 1
        
        model_source_id = args[0]
        try:
            source = Source.objects.get(name=model_source_id)
        except:
            print("There is no such source as '%s', please check ID ..." % model_source_id)
            sys.exit(1) 
        
        
        
        ## Create model container
#         document = SBMLDocument()
#         document.setLevelAndVersion(2,4)
        reader = SBMLReader()
        document = reader.readSBMLFromFile('/Users/wbryant/work/BTH/level2v4_template.xml')
        model = document.getModel()
        model.setName(model_source_id)
        model.setId(model_source_id)
        
        
        ## Get reactions from Metabolic_model
        model_reactions = Metabolic_model.objects.filter()
        
        
        ## Create compartments
        compartments = Compartment.objects.filter(stoichiometry__source=source).distinct()
        
        for compartment in compartments:
            c_model = model.createCompartment()
            c_model.setName(str(compartment.name))
            c_model.setId(str(compartment.id))
            
        
        ## Create Species
        stoichiometries = Stoichiometry.objects.filter(source=source)\
            .values_list(
                "metabolite__id",
                "metabolite__name",
                "compartment__id",
            )\
            .distinct()
        print("There are %d metabolites (with compartments) ..." % len(stoichiometries))
        
        print("Populating model with metabolites ...")
        counter = loop_counter(len(stoichiometries))
        
        
        for met in stoichiometries:
            counter.step()
            species = model.createSpecies()
            species.setName(met[1].encode("utf-8"))
            species.setId(str(met[0] + "__in__" + met[2]))
            species.setCompartment(str(met[2]))
            
        counter.stop()
        
        
        ## Create Reactions (get list according to 'Metabolic_model')
        model_reactions = Metabolic_model.objects.filter(source=source).prefetch_related()
        num_reactions = len(model_reactions)
        
        print("There are %d reactions ..." % num_reactions)
        
        print("Populating model with reactions ...")
        counter = loop_counter(num_reactions)
        
        for mod in model_reactions:
            counter.step()
            reaction = model.createReaction()
            reaction.setName(mod.reaction.name.encode("utf-8"))
            reaction.setId(mod.reaction.name.encode("utf-8"))
            
            if mod.direction.direction == "both":
                reversible = True
            else:
                reversible = False
            reaction.setReversible(reversible)
        
            for sto in mod.reaction.stoichiometry_set.all():
                if sto.stoichiometry > 0:
                    met = reaction.createReactant()
                else:
                    met = reaction.createProduct()
                
                species_id = sto.metabolite.id + "__in__"\
                                + sto.compartment.id
                
                met.setSpecies(str(species_id))
                met.setStoichiometry(abs(sto.stoichiometry))
        
        counter.stop()
            
        ## Set and save the model
        
        print("Writing model to SBML ...")
        document.setModel(model)
#         document.setLevelAndVersion(2,4)
        writeSBMLToFile(document, "/Users/wbryant/work/cogzymes/incognito/test_libsbml.xml")