|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +"""Define the group class.""" |
| 4 | + |
3 | 5 | from __future__ import absolute_import |
4 | 6 |
|
5 | | -from cobra.core.object import Object |
6 | | -from six import string_types |
7 | 7 | from warnings import warn |
8 | 8 |
|
9 | | -kind_types = ["collection", "classification", "partonomy"] |
| 9 | +from six import string_types |
| 10 | + |
| 11 | +from cobra.core.object import Object |
| 12 | + |
| 13 | + |
| 14 | +KIND_TYPES = ("collection", "classification", "partonomy") |
10 | 15 |
|
11 | 16 |
|
12 | 17 | class Group(Object): |
13 | | - """Group is a class for holding information regarding |
14 | | - a pathways, subsystems, or other custom groupings of objects |
15 | | - within a cobra.Model object. |
| 18 | + """ |
| 19 | + Manage groups via this implementation of the SBML group specification. |
| 20 | +
|
| 21 | + `Group` is a class for holding information regarding a pathways, subsystems, |
| 22 | + or other custom groupings of objects within a cobra.Model object. |
16 | 23 |
|
17 | 24 | Parameters |
18 | 25 | ---------- |
19 | | - id : string |
| 26 | + id : str |
20 | 27 | The identifier to associate with this group |
21 | | - name : string |
| 28 | + name : str, optional |
22 | 29 | A human readable name for the group |
23 | | - members : list |
| 30 | + members : iterable, optional |
24 | 31 | A list object containing references to cobra.Model-associated objects |
25 | 32 | that belong to the group. |
26 | | - kind : string |
27 | | - The kind of group, as specified for the Groups feature in the SBML |
28 | | - level 3 package specification. Can be any of "classification", |
29 | | - "partonomy", or "collection". Please consult the SBML level 3 package |
30 | | - specification to ensure you are using the proper value for kind. In |
31 | | - short, members of a "classification" group should have an "is-a" |
32 | | - relationship to the group (e.g. member is-a polar compound, or |
| 33 | + kind : {"collection", "classification", "partonomy"}, optional |
| 34 | + The kind of group, as specified for the Groups feature in the SBML level |
| 35 | + 3 package specification. Can be any of "classification", "partonomy", or |
| 36 | + "collection". The default is "collection". Please consult the SBML level |
| 37 | + 3 package specification to ensure you are using the proper value for |
| 38 | + kind. In short, members of a "classification" group should have an |
| 39 | + "is-a" relationship to the group (e.g. member is-a polar compound, or |
33 | 40 | member is-a transporter). Members of a "partonomy" group should have a |
34 | | - "part-of" relationship (e.g. member is part-of glycolysis). Members of |
35 | | - a "collection" group do not have an implied relationship between the |
| 41 | + "part-of" relationship (e.g. member is part-of glycolysis). Members of a |
| 42 | + "collection" group do not have an implied relationship between the |
36 | 43 | members, so use this value for kind when in doubt (e.g. member is a |
37 | 44 | gap-filled reaction, or member is involved in a disease phenotype). |
| 45 | +
|
38 | 46 | """ |
39 | 47 |
|
40 | | - def __init__(self, id=None, name='', members=[], kind=''): |
| 48 | + def __init__(self, id, name='', members=None, kind=None): |
41 | 49 | Object.__init__(self, id, name) |
42 | 50 |
|
43 | | - self._members = set(members) |
44 | | - self._kind = kind |
45 | | - |
| 51 | + self._members = set() if members is None else set(members) |
| 52 | + self._kind = None |
| 53 | + self.kind = "collection" if kind is None else kind |
46 | 54 | # self.model is None or refers to the cobra.Model that |
47 | 55 | # contains self |
48 | 56 | self._model = None |
49 | 57 |
|
50 | 58 | # read-only |
51 | 59 | @property |
52 | 60 | def members(self): |
53 | | - return getattr(self, "_members", None) |
54 | | - |
55 | | - @members.setter |
56 | | - def members(self, members): |
57 | | - self._members = set(members) |
| 61 | + return self._members |
58 | 62 |
|
59 | 63 | @property |
60 | 64 | def kind(self): |
61 | | - return getattr(self, "_kind", '') |
| 65 | + return self._kind |
62 | 66 |
|
63 | 67 | @kind.setter |
64 | 68 | def kind(self, kind): |
65 | | - if kind in kind_types: |
| 69 | + if kind in KIND_TYPES: |
66 | 70 | self._kind = kind |
67 | 71 | else: |
68 | | - raise ValueError("kind can only by one of: " + str(kind_types)) |
69 | | - |
70 | | - @property |
71 | | - def model(self): |
72 | | - """returns the model the group is a part of""" |
73 | | - return self._model |
| 72 | + raise ValueError( |
| 73 | + "Kind can only by one of: {}.".format(", ".join(KIND_TYPES))) |
74 | 74 |
|
75 | | - def add_members(self, members_list): |
76 | | - """Add objects to the group. |
| 75 | + def add_members(self, new_members): |
| 76 | + """ |
| 77 | + Add objects to the group. |
77 | 78 |
|
78 | 79 | Parameters |
79 | 80 | ---------- |
80 | | - members_to_add : list |
81 | | - list of cobrapy objects to add to the group. |
| 81 | + new_members : list |
| 82 | + A list of cobrapy objects to add to the group. |
| 83 | +
|
82 | 84 | """ |
83 | 85 |
|
84 | | - if isinstance(members_list, string_types) or \ |
85 | | - hasattr(members_list, "id"): |
| 86 | + if isinstance(new_members, string_types) or \ |
| 87 | + hasattr(new_members, "id"): |
86 | 88 | warn("need to pass in a list") |
87 | | - members_list = [members_list] |
88 | | - |
89 | | - new_members = [] |
90 | | - _id_to_members = dict([(x.id, x) for x in self._members]) |
| 89 | + new_members = [new_members] |
91 | 90 |
|
92 | | - # Check for duplicate members in the group |
93 | | - for member in members_list: |
94 | | - # we only need to add the member if it ins't already in the group |
95 | | - if member.id not in _id_to_members: |
96 | | - new_members.append(member) |
| 91 | + self._members.update(new_members) |
97 | 92 |
|
98 | | - self._members = self._members.union(set(new_members)) |
99 | | - |
100 | | - def remove(self, members_list): |
101 | | - """Remove objects from the group. |
| 93 | + def remove(self, to_remove): |
| 94 | + """ |
| 95 | + Remove objects from the group. |
102 | 96 |
|
103 | 97 | Parameters |
104 | 98 | ---------- |
105 | | - members_to_remove : list |
106 | | - list of cobrapy objects to remove from the group |
| 99 | + to_remove : list |
| 100 | + A list of cobrapy objects to remove from the group |
107 | 101 | """ |
108 | 102 |
|
109 | | - if isinstance(members_list, string_types) or \ |
110 | | - hasattr(members_list, "id"): |
| 103 | + if isinstance(to_remove, string_types) or \ |
| 104 | + hasattr(to_remove, "id"): |
111 | 105 | warn("need to pass in a list") |
112 | | - members_list = [members_list] |
| 106 | + to_remove = [to_remove] |
113 | 107 |
|
114 | | - for member in members_list: |
115 | | - self._members.discard(member) |
| 108 | + self._members.difference_update(to_remove) |
0 commit comments