pythonsbml

How to remove members of a group with libSBML file in python


I cannot remove members of a ListOfMembers() in a group in an SBML file.

I try to remove some species of an sbml file. So I delet them, and store their IDs in a set removed_species :

plugin = model.getPlugin("groups")
if plugin is not None:
    for group in plugin.getListOfGroups():
        num = group.getNumMembers()
        for member in group.getListOfMembers():
            if member.getIdRef() in removed_species:
                print("Member removed from group", group.getId(), member.getIdRef())
                group.removeMember(member.getIdRef())
        if group.getNumMembers() == num:
            print("No member removed from group", group.getId())

Members are deleted in the last loop. I checked with print but when it come back at the group loop everything is like at the start.

This is the output :

Member removed from group Sos_471_0_ Sos_dot_Ras_GEF_cplx_486_0_
Member removed from group Sos_471_0_ Sos_dot_Ras_GEF_cplx_486_0_
No member removed from group Sos_471_0_
No member removed from group Ras_548_0_
Member removed from group EGFR_560_0_ phosph_Shc_cplx_575_0_
Member removed from group EGFR_560_0_ phosph_Shc_cplx_575_0_
No member removed from group EGFR_560_0_

Solution

  • I found a solution using removeFromParentAndDelete():

    plugin = model.getPlugin("groups")
        if plugin is not None:
            for group in plugin.getListOfGroups():
                member_list = [member for member in group.getListOfMembers()]
                for member in member_list:
                    if member.getIdRef() in removed_species:
                        member.removeFromParentAndDelete()