BioSIMI-Python/modules/Subsystem.py
def shareSubsystems(self, ListOfSubsystems, ListOfSharedResources, mode = 'volume', combineCall = False):
        '''
        The ListOfSubsystems are merged and all Species are also added to the 
        Subsystem object. The Species in ListOfSharedResources are combined together 
        and so are shared by all Subsystems in the ListOfSubsystems. The Model id is also updated.
        '''
        self.mergeSubsystemModels(ListOfSubsystems)
        model = self.getSubsystemDoc().getModel()
        check(model,'retreiving model in shareSubsystems')
        model_obj = SimpleModel(model)
        mod_id = ''
        total_size = 0
        if combineCall == False:
            total_size = self.getSystem().Size
        else:
            for subsystem in ListOfSubsystems:
                total_size += subsystem.getSubsystemDoc().getModel().getCompartment(0).getSize()

        check(model.getCompartment(0).setSize(total_size), 'setting compartment size in model')
        final_species_hash_map = {}
        if mode == 'volume':
            for subsystem in ListOfSubsystems:
                mod = subsystem.getSubsystemDoc().getModel()
                check(mod,'retreiving subsystem model in shareSubsystems')
                mod_id += '_' + mod.getId()
                # if list of shared resources is empty
                if not ListOfSharedResources:
                    species_list = mod.getListOfSpecies()
                    check(species_list,'retreiving list of species of susbsytem model in shareSubsystems')
                    for species in species_list:
                        ssys_size = 0
                        cumulative_amount = 0
                        species_amount = 0
                        ssys_size = species.getModel().getCompartment(0).getSize()
                        cumulative_amount = (species.getInitialAmount())*ssys_size
                        species_amount = cumulative_amount/total_size
                        check(model.addSpecies(species),'adding species to the model when ListOfSharedResources is empty, in shareSubsystems')
                        check(model.getSpecies(species.getId()).setInitialAmount(species_amount),'setting initial amount to cumulative in shareSubsystems')
                else:
                    # Set the list of reactions in the final subsystem. Get the list of
                    # reactions in the input subsystem and set it to final subsystem
                    species_hash_map = {}
                    for species in mod.getListOfSpecies():
                        ssys_size = 0
                        cumulative_amount = 0
                        species_amount = 0
                        species_name = species.getName()
                        check(species_name,'getting species name in shareSubsystems')
                        if species_name in ListOfSharedResources:
                        # Maintain the dictionary for all species in the input subsystems by their name
                            species_hash_map[species_name] = species
                        else:
                            ssys_size = species.getModel().getCompartment(0).getSize()
                            cumulative_amount = (species.getInitialAmount())*ssys_size
                            species_amount = cumulative_amount/total_size
                            check(model.addSpecies(species),'adding species to the model in shareSubsystems')
                            check(model.getSpecies(species.getId()).setInitialAmount(species_amount),'setting initial amount in shareSubsystems')
                    for species_name in species_hash_map:
                        if final_species_hash_map.get(species_name):
                            #If the final hash map already has that species then append to
                            # the same instead of duplicating
                            final_species_hash_map[species_name].append(
                                species_hash_map[species_name])
                        else:
                            # For all the species in the dictionary not already in the final
                            # hash map, save them to the final hash map dictionary.
                            final_species_hash_map[species_name] = [
                                species_hash_map[species_name]]

            allids = self.getAllIds()
            trans = SetIdFromNames(allids)
            for unique_species_name in final_species_hash_map:
                ssys_size = 0
                cumulative_amount = 0
                species_amount = 0
                if len(final_species_hash_map[unique_species_name]) > 1: 
                    uni_sp = final_species_hash_map[unique_species_name][0]
                        # For any species with same name 
                        # which were present in more than one subsystem
                    count = 0
                    for i in final_species_hash_map[unique_species_name]:
                        ssys_size = i.getModel().getCompartment(0).getSize()
                        cumulative_amount += (i.getInitialAmount())*ssys_size
                        species_amount = cumulative_amount/total_size
                        check(model.addSpecies(i),'add species to model in shareSubsystems')
                        oldid = i.getId()
                        check(oldid,'getting olid in shareSubsystems')
                        newid = trans.getValidIdForName(i.getName()) + '_shared'
                        self.renameSId(oldid, newid)
                        if count >= 1:
                            check(model.removeSpecies(newid),'removing species from the model in shareSubsystems')
                        count += 1
                    sp = model_obj.getSpeciesByName(uni_sp.getName())
                    if type(sp) is list: 
                        for sp_i in sp:
                            check(sp_i.setInitialAmount(species_amount),'setting initial amount to cumulative in shareSubsystems')
                    else:
                        check(sp.setInitialAmount(species_amount),'setting initial amount to cumulative in shareSubsystems')
                else:
                #     # If there are no species with multiple occurence in different subsystems
                #     # then just add the list of all species maintained in the final hash map
                #     # to our new subsystem's list of species.
                    i = final_species_hash_map[unique_species_name][0]
                #     ssys_size = i.getModel().getCompartment(0).getSize()
                #     cumulative_amount = (i.getInitialAmount())*ssys_size
                #     species_amount = cumulative_amount/total_size
                    check(model.addSpecies(i),'adding species to the model in shareSubsystems')
                #     check(i.setInitialAmount(species_amount),'setting initial amount of species in shareSubsystems')
        elif mode == 'virtual':
            for subsystem in ListOfSubsystems:
                mod = subsystem.getSubsystemDoc().getModel()
                check(mod,'retreiving subsystem model in shareSubsystems')
                mod_id += '_' + mod.getId()
                if not ListOfSharedResources:
                    species_list = mod.getListOfSpecies()
                    check(species_list,'retreiving list of species of susbsytem model in shareSubsystems')
                    for species in species_list:
                        check(model.addSpecies(species),'adding species to the model when ListOfSharedResources is empty, in shareSubsystems')
                else:
                    # Set the list of reactions in the final subsystem. Get the list of
                    # reactions in the input subsystem and set it to final subsystem
                    species_hash_map = {}
                    for species in mod.getListOfSpecies():
                        species_name = species.getName()
                        check(species_name,'getting species name in shareSubsystems')
                        if species_name in ListOfSharedResources:
                        # Maintain the dictionary for all species in the input subsystems by their name
                            species_hash_map[species_name] = species
                        else:
                            check(model.addSpecies(species),'adding species to the model in shareSubsystems')
                    for species_name in species_hash_map:
                        if final_species_hash_map.get(species_name):
                            #If the final hash map already has that species then append to
                            # the same instead of duplicating
                            final_species_hash_map[species_name].append(
                                species_hash_map[species_name])
                        else:
                            # For all the species in the dictionary not already in the final
                            # hash map, save them to the final hash map dictionary.
                            final_species_hash_map[species_name] = [
                                species_hash_map[species_name]]

            allids = self.getAllIds()
            trans = SetIdFromNames(allids)
            for unique_species_name in final_species_hash_map:
                cumulative_amount = 0
                if len(final_species_hash_map[unique_species_name]) > 1: 
                        # For any species with same name 
                        # which were present in more than one subsystem
                    count = 0
                    for i in final_species_hash_map[unique_species_name]:
                        check(model.addSpecies(i),'add species to model in shareSubsystems')
                        oldid = i.getId()
                        check(oldid,'getting olid in shareSubsystems')
                        newid = trans.getValidIdForName(i.getName()) + '_shared'
                        self.renameSId(oldid, newid)
                        if count >= 1:
                            check(model.removeSpecies(newid),'removing species from the model in shareSubsystems')
                        count += 1
               # else:
                #     # If there are no species with multiple occurence in different subsystems
                #     # then just add the list of all species maintained in the final hash map
                #     # to our new subsystem's list of species.
                #     i = final_species_hash_map[unique_species_name][0]
                #     ssys_size = i.getModel().getCompartment(0).getSize()
                #     cumulative_amount = (i.getInitialAmount())*ssys_size
                #     species_amount = cumulative_amount/total_size
                #     check(model.addSpecies(i),'adding species to the model in shareSubsystems')
                #     check(i.setInitialAmount(species_amount),'setting initial amount of species in shareSubsystems')
 

        # Updating model id
        check(model.setId('shared_Subsystems_' + mod_id),'setting new model id for shared model')

        The ListOfSubsystems are merged and all Species are also added to the 
        Subsystem object. The Species in ListOfSharedResources are combined together 
        and so are shared by all Subsystems in the ListOfSubsystems. The Model id is also updated.
        
The ListOfSubsystems are merged and all Species are also added to the
Subsystem object. The Species in ListOfSharedResources are combined together
and so are shared by all Subsystems in the ListOfSubsystems. The Model id is also updated.
The ListOfSubsystems are merged and all Species are also added to the Subsystem object . The Species in ListOfSharedResources are combined together and so are shared by all Subsystems in the ListOfSubsystems . The Model id is also updated .
def shareSubsystems(self, ListOfSubsystems, ListOfSharedResources, mode = 'volume', combineCall = False):
        self.mergeSubsystemModels(ListOfSubsystems)
        model = self.getSubsystemDoc().getModel()
        check(model,'retreiving model in shareSubsystems')
        model_obj = SimpleModel(model)
        mod_id = ''
        total_size = 0
        if combineCall == False:
            total_size = self.getSystem().Size
        else:
            for subsystem in ListOfSubsystems:
                total_size += subsystem.getSubsystemDoc().getModel().getCompartment(0).getSize()
        check(model.getCompartment(0).setSize(total_size), 'setting compartment size in model')
        final_species_hash_map = {}
        if mode == 'volume':
            for subsystem in ListOfSubsystems:
                mod = subsystem.getSubsystemDoc().getModel()
                check(mod,'retreiving subsystem model in shareSubsystems')
                mod_id += '_' + mod.getId()
                if not ListOfSharedResources:
                    species_list = mod.getListOfSpecies()
                    check(species_list,'retreiving list of species of susbsytem model in shareSubsystems')
                    for species in species_list:
                        ssys_size = 0
                        cumulative_amount = 0
                        species_amount = 0
                        ssys_size = species.getModel().getCompartment(0).getSize()
                        cumulative_amount = (species.getInitialAmount())*ssys_size
                        species_amount = cumulative_amount/total_size
                        check(model.addSpecies(species),'adding species to the model when ListOfSharedResources is empty, in shareSubsystems')
                        check(model.getSpecies(species.getId()).setInitialAmount(species_amount),'setting initial amount to cumulative in shareSubsystems')
                else:
                    species_hash_map = {}
                    for species in mod.getListOfSpecies():
                        ssys_size = 0
                        cumulative_amount = 0
                        species_amount = 0
                        species_name = species.getName()
                        check(species_name,'getting species name in shareSubsystems')
                        if species_name in ListOfSharedResources:
                            species_hash_map[species_name] = species
                        else:
                            ssys_size = species.getModel().getCompartment(0).getSize()
                            cumulative_amount = (species.getInitialAmount())*ssys_size
                            species_amount = cumulative_amount/total_size
                            check(model.addSpecies(species),'adding species to the model in shareSubsystems')
                            check(model.getSpecies(species.getId()).setInitialAmount(species_amount),'setting initial amount in shareSubsystems')
                    for species_name in species_hash_map:
                        if final_species_hash_map.get(species_name):
                            final_species_hash_map[species_name].append(
                                species_hash_map[species_name])
                        else:
                            final_species_hash_map[species_name] = [
                                species_hash_map[species_name]]
            allids = self.getAllIds()
            trans = SetIdFromNames(allids)
            for unique_species_name in final_species_hash_map:
                ssys_size = 0
                cumulative_amount = 0
                species_amount = 0
                if len(final_species_hash_map[unique_species_name]) > 1: 
                    uni_sp = final_species_hash_map[unique_species_name][0]
                    count = 0
                    for i in final_species_hash_map[unique_species_name]:
                        ssys_size = i.getModel().getCompartment(0).getSize()
                        cumulative_amount += (i.getInitialAmount())*ssys_size
                        species_amount = cumulative_amount/total_size
                        check(model.addSpecies(i),'add species to model in shareSubsystems')
                        oldid = i.getId()
                        check(oldid,'getting olid in shareSubsystems')
                        newid = trans.getValidIdForName(i.getName()) + '_shared'
                        self.renameSId(oldid, newid)
                        if count >= 1:
                            check(model.removeSpecies(newid),'removing species from the model in shareSubsystems')
                        count += 1
                    sp = model_obj.getSpeciesByName(uni_sp.getName())
                    if type(sp) is list: 
                        for sp_i in sp:
                            check(sp_i.setInitialAmount(species_amount),'setting initial amount to cumulative in shareSubsystems')
                    else:
                        check(sp.setInitialAmount(species_amount),'setting initial amount to cumulative in shareSubsystems')
                else:
                    i = final_species_hash_map[unique_species_name][0]
                    check(model.addSpecies(i),'adding species to the model in shareSubsystems')
        elif mode == 'virtual':
            for subsystem in ListOfSubsystems:
                mod = subsystem.getSubsystemDoc().getModel()
                check(mod,'retreiving subsystem model in shareSubsystems')
                mod_id += '_' + mod.getId()
                if not ListOfSharedResources:
                    species_list = mod.getListOfSpecies()
                    check(species_list,'retreiving list of species of susbsytem model in shareSubsystems')
                    for species in species_list:
                        check(model.addSpecies(species),'adding species to the model when ListOfSharedResources is empty, in shareSubsystems')
                else:
                    species_hash_map = {}
                    for species in mod.getListOfSpecies():
                        species_name = species.getName()
                        check(species_name,'getting species name in shareSubsystems')
                        if species_name in ListOfSharedResources:
                            species_hash_map[species_name] = species
                        else:
                            check(model.addSpecies(species),'adding species to the model in shareSubsystems')
                    for species_name in species_hash_map:
                        if final_species_hash_map.get(species_name):
                            final_species_hash_map[species_name].append(
                                species_hash_map[species_name])
                        else:
                            final_species_hash_map[species_name] = [
                                species_hash_map[species_name]]
            allids = self.getAllIds()
            trans = SetIdFromNames(allids)
            for unique_species_name in final_species_hash_map:
                cumulative_amount = 0
                if len(final_species_hash_map[unique_species_name]) > 1: 
                    count = 0
                    for i in final_species_hash_map[unique_species_name]:
                        check(model.addSpecies(i),'add species to model in shareSubsystems')
                        oldid = i.getId()
                        check(oldid,'getting olid in shareSubsystems')
                        newid = trans.getValidIdForName(i.getName()) + '_shared'
                        self.renameSId(oldid, newid)
                        if count >= 1:
                            check(model.removeSpecies(newid),'removing species from the model in shareSubsystems')
                        count += 1
        check(model.setId('shared_Subsystems_' + mod_id),'setting new model id for shared model')
def shareSubsystems ( self , ListOfSubsystems , ListOfSharedResources , mode = 'volume' , combineCall = False ) : self . mergeSubsystemModels ( ListOfSubsystems ) model = self . getSubsystemDoc ( ) . getModel ( ) check ( model , 'retreiving model in shareSubsystems' ) model_obj = SimpleModel ( model ) mod_id = '' total_size = 0 if combineCall == False : total_size = self . getSystem ( ) . Size else : for subsystem in ListOfSubsystems : total_size += subsystem . getSubsystemDoc ( ) . getModel ( ) . getCompartment ( 0 ) . getSize ( ) check ( model . getCompartment ( 0 ) . setSize ( total_size ) , 'setting compartment size in model' ) final_species_hash_map = { } if mode == 'volume' : for subsystem in ListOfSubsystems : mod = subsystem . getSubsystemDoc ( ) . getModel ( ) check ( mod , 'retreiving subsystem model in shareSubsystems' ) mod_id += '_' + mod . getId ( ) if not ListOfSharedResources : species_list = mod . getListOfSpecies ( ) check ( species_list , 'retreiving list of species of susbsytem model in shareSubsystems' ) for species in species_list : ssys_size = 0 cumulative_amount = 0 species_amount = 0 ssys_size = species . getModel ( ) . getCompartment ( 0 ) . getSize ( ) cumulative_amount = ( species . getInitialAmount ( ) ) * ssys_size species_amount = cumulative_amount / total_size check ( model . addSpecies ( species ) , 'adding species to the model when ListOfSharedResources is empty, in shareSubsystems' ) check ( model . getSpecies ( species . getId ( ) ) . setInitialAmount ( species_amount ) , 'setting initial amount to cumulative in shareSubsystems' ) else : species_hash_map = { } for species in mod . getListOfSpecies ( ) : ssys_size = 0 cumulative_amount = 0 species_amount = 0 species_name = species . getName ( ) check ( species_name , 'getting species name in shareSubsystems' ) if species_name in ListOfSharedResources : species_hash_map [ species_name ] = species else : ssys_size = species . getModel ( ) . getCompartment ( 0 ) . getSize ( ) cumulative_amount = ( species . getInitialAmount ( ) ) * ssys_size species_amount = cumulative_amount / total_size check ( model . addSpecies ( species ) , 'adding species to the model in shareSubsystems' ) check ( model . getSpecies ( species . getId ( ) ) . setInitialAmount ( species_amount ) , 'setting initial amount in shareSubsystems' ) for species_name in species_hash_map : if final_species_hash_map . get ( species_name ) : final_species_hash_map [ species_name ] . append ( species_hash_map [ species_name ] ) else : final_species_hash_map [ species_name ] = [ species_hash_map [ species_name ] ] allids = self . getAllIds ( ) trans = SetIdFromNames ( allids ) for unique_species_name in final_species_hash_map : ssys_size = 0 cumulative_amount = 0 species_amount = 0 if len ( final_species_hash_map [ unique_species_name ] ) > 1 : uni_sp = final_species_hash_map [ unique_species_name ] [ 0 ] count = 0 for i in final_species_hash_map [ unique_species_name ] : ssys_size = i . getModel ( ) . getCompartment ( 0 ) . getSize ( ) cumulative_amount += ( i . getInitialAmount ( ) ) * ssys_size species_amount = cumulative_amount / total_size check ( model . addSpecies ( i ) , 'add species to model in shareSubsystems' ) oldid = i . getId ( ) check ( oldid , 'getting olid in shareSubsystems' ) newid = trans . getValidIdForName ( i . getName ( ) ) + '_shared' self . renameSId ( oldid , newid ) if count >= 1 : check ( model . removeSpecies ( newid ) , 'removing species from the model in shareSubsystems' ) count += 1 sp = model_obj . getSpeciesByName ( uni_sp . getName ( ) ) if type ( sp ) is list : for sp_i in sp : check ( sp_i . setInitialAmount ( species_amount ) , 'setting initial amount to cumulative in shareSubsystems' ) else : check ( sp . setInitialAmount ( species_amount ) , 'setting initial amount to cumulative in shareSubsystems' ) else : i = final_species_hash_map [ unique_species_name ] [ 0 ] check ( model . addSpecies ( i ) , 'adding species to the model in shareSubsystems' ) elif mode == 'virtual' : for subsystem in ListOfSubsystems : mod = subsystem . getSubsystemDoc ( ) . getModel ( ) check ( mod , 'retreiving subsystem model in shareSubsystems' ) mod_id += '_' + mod . getId ( ) if not ListOfSharedResources : species_list = mod . getListOfSpecies ( ) check ( species_list , 'retreiving list of species of susbsytem model in shareSubsystems' ) for species in species_list : check ( model . addSpecies ( species ) , 'adding species to the model when ListOfSharedResources is empty, in shareSubsystems' ) else : species_hash_map = { } for species in mod . getListOfSpecies ( ) : species_name = species . getName ( ) check ( species_name , 'getting species name in shareSubsystems' ) if species_name in ListOfSharedResources : species_hash_map [ species_name ] = species else : check ( model . addSpecies ( species ) , 'adding species to the model in shareSubsystems' ) for species_name in species_hash_map : if final_species_hash_map . get ( species_name ) : final_species_hash_map [ species_name ] . append ( species_hash_map [ species_name ] ) else : final_species_hash_map [ species_name ] = [ species_hash_map [ species_name ] ] allids = self . getAllIds ( ) trans = SetIdFromNames ( allids ) for unique_species_name in final_species_hash_map : cumulative_amount = 0 if len ( final_species_hash_map [ unique_species_name ] ) > 1 : count = 0 for i in final_species_hash_map [ unique_species_name ] : check ( model . addSpecies ( i ) , 'add species to model in shareSubsystems' ) oldid = i . getId ( ) check ( oldid , 'getting olid in shareSubsystems' ) newid = trans . getValidIdForName ( i . getName ( ) ) + '_shared' self . renameSId ( oldid , newid ) if count >= 1 : check ( model . removeSpecies ( newid ) , 'removing species from the model in shareSubsystems' ) count += 1 check ( model . setId ( 'shared_Subsystems_' + mod_id ) , 'setting new model id for shared model' )
The ListOfSubsystems are merged and all Species are also added to the
Subsystem object.
The ListOfSubsystems are merged and all Species are also added to the Subsystem object .
'''
        The ListOfSubsystems are merged and all Species are also added to the 
        Subsystem object. The Species in ListOfSharedResources are combined together 
        and so are shared by all Subsystems in the ListOfSubsystems. The Model id is also updated.
        ''' # if list of shared resources is empty # Set the list of reactions in the final subsystem. Get the list of # reactions in the input subsystem and set it to final subsystem # Maintain the dictionary for all species in the input subsystems by their name #If the final hash map already has that species then append to # the same instead of duplicating # For all the species in the dictionary not already in the final # hash map, save them to the final hash map dictionary. # For any species with same name  # which were present in more than one subsystem #     # If there are no species with multiple occurence in different subsystems #     # then just add the list of all species maintained in the final hash map #     # to our new subsystem's list of species. #     ssys_size = i.getModel().getCompartment(0).getSize() #     cumulative_amount = (i.getInitialAmount())*ssys_size #     species_amount = cumulative_amount/total_size #     check(i.setInitialAmount(species_amount),'setting initial amount of species in shareSubsystems') # Set the list of reactions in the final subsystem. Get the list of # reactions in the input subsystem and set it to final subsystem # Maintain the dictionary for all species in the input subsystems by their name #If the final hash map already has that species then append to # the same instead of duplicating # For all the species in the dictionary not already in the final # hash map, save them to the final hash map dictionary. # For any species with same name  # which were present in more than one subsystem # else: #     # If there are no species with multiple occurence in different subsystems #     # then just add the list of all species maintained in the final hash map #     # to our new subsystem's list of species. #     i = final_species_hash_map[unique_species_name][0] #     ssys_size = i.getModel().getCompartment(0).getSize() #     cumulative_amount = (i.getInitialAmount())*ssys_size #     species_amount = cumulative_amount/total_size #     check(model.addSpecies(i),'adding species to the model in shareSubsystems') #     check(i.setInitialAmount(species_amount),'setting initial amount of species in shareSubsystems') # Updating model id
self
ListOfSubsystems
ListOfSharedResources
mode
combineCall