/
collections.py
321 lines (271 loc) · 9.57 KB
/
collections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import gfapy
class Collections:
@property
def comments(self):
"""List of the comment lines (lines starting with #).
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
d = self._records["#"]
return list(d.values())
@property
def gaps(self):
"""List of the gap (G) lines. The list is empty in GFA1.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
d = self._records["G"]
return list(d.values())
@property
def sets(self):
"""List of the set (U) lines. The list is empty in GFA1.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
d = self._records["U"]
return list(d.values())
@property
def segments(self):
"""List of the segment (S) lines.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
d = self._records["S"]
return list(d.values())
@property
def edges(self):
"""List of the edge lines.
Edge lines are L and C lines in GFA1 and E lines in GFA2.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
if self._version == "gfa1":
return self._gfa1_links + self._gfa1_containments
elif self._version == "gfa2":
return self._gfa2_edges
else:
return self._gfa1_links + self._gfa1_containments + self._gfa2_edges
@property
def dovetails(self):
"""List of the dovetail edge lines.
Dovetail edge lines are L lines in GFA1 and E lines representing dovetail
overlaps in GFA2.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
if self._version == "gfa1":
return self._gfa1_links
elif self._version == "gfa2":
return [ e for e in self._gfa2_edges if e.is_dovetail() ]
else:
return self._gfa1_links + \
[ e for e in self._gfa2_edges if e.is_dovetail() ]
@property
def containments(self):
"""List of the containment edge lines.
Containment edge lines are C lines in GFA1 and E lines representing
containments in GFA2.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
if self._version == "gfa1":
return self._gfa1_containments
elif self._version == "gfa2":
return [ e for e in self._gfa2_edges if e.is_containment() ]
else:
return self._gfa1_containments + \
[ e for e in self._gfa2_edges if e.is_containment() ]
@property
def paths(self):
"""List of the path lines (P in GFA1, O in GFA2).
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
return self._gfa1_paths + self._gfa2_paths
@property
def fragments(self):
"""List of the fragment (F) lines. The list is empty in GFA1.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
d = self._records["F"]
return [f for e in d.values() for f in e.values()]
@property
def custom_records(self):
"""List of the custom records of the GFA. The list is empty for GFA1.
All records with a non-standard first field (record type) are considered
custom records.
Note:
adding or removing elements to the list, does not add or removes
lines from the Gfa instance. For this, the add_line() and rm() methods
shall be used. Calling disconnect() on a line of the list, however,
removes the line from the instance.
"""
cr = []
for k in self.custom_record_keys:
collection = self._records[k]
cr += list(collection.values())
return cr
@property
def _gfa1_containments(self):
d = self._records["C"]
return list(d.values())
@property
def _gfa1_links(self):
d = self._records["L"]
return list(d.values())
@property
def _gfa2_edges(self):
d = self._records["E"]
return list(d.values())
@property
def _gfa2_paths(self):
d = self._records["O"]
return list(d.values())
@property
def gap_names(self):
"""List of the names of the gap (G) lines. The list is empty in GFA1.
"""
d = self._records["G"]
return list([k for k in d.keys() if isinstance(k, str)])
@property
def set_names(self):
"""List of the names of the set (U) lines. The list is empty in GFA1.
"""
d = self._records["U"]
return list([k for k in d.keys() if isinstance(k, str)])
@property
def segment_names(self):
"""List of the names of the segment (S) lines.
"""
d = self._records["S"]
return list(d.keys())
@property
def edge_names(self):
"""List of the names of the edge (E, L, C) lines.
For the L and C lines, the content of the custom tag id
is taken as name.
"""
if self._version == "gfa1":
return self._link_names + self._containment_names
elif self._version == "gfa2":
return self._gfa2_edge_names
else:
return self._gfa2_edge_names + self._link_names + self._containment_names
@property
def path_names(self):
"""List of the names of the path lines (P for GFA1, O for GFA2).
"""
return self._gfa1_path_names + self._gfa2_path_names
@property
def names(self):
"""All identifiers in the GFA identifiers namespace.
Notes:
GFA1: in Gfapy the P and S namespaces are joined (i.e. paths with
the same name as segments are not accepted). Furthermore, to simplify
the conversion to/from GFA2, the ID tag is used in L and C lines,
and their content is also included in the same namespace as the S/P
identifiers. GFA2: the namespace for identifiers is described in
the specification and includes all the S, E, G, U and O lines; the
external sequence identifiers in F lines are not included.
"""
return self.segment_names + \
self.edge_names + \
self.gap_names + \
self.path_names + \
self.set_names
def unused_name(self):
"""Compute a GFA identifier not yet in use in the Gfa object."""
self._max_int_name += 1
return str(self._max_int_name)
@property
def external_names(self):
"""List of the identifiers of external sequences mentioned in F records.
The list is empty in GFA1.
"""
return list(self._records["F"].keys())
@property
def _gfa2_edge_names(self):
d = self._records["E"]
return list([k for k in d.keys() if isinstance(k, str)])
@property
def _link_names(self):
d = self._records["L"]
return list([k for k in d.keys() if isinstance(k, str)])
@property
def _containment_names(self):
d = self._records["C"]
return list([k for k in d.keys() if isinstance(k, str)])
@property
def _gfa2_path_names(self):
d = self._records["O"]
return list([k for k in d.keys() if isinstance(k, str)])
@property
def _gfa1_paths(self):
d = self._records["P"]
return list(d.values())
@property
def _gfa1_path_names(self):
d = self._records["P"]
return list(d.keys())
GFA1_ONLY_KEYS = ["L", "C", "P"]
NONCUSTOM_GFA2_KEYS = ["H", "#", "F", "S", "E", "G", "U", "O", "\t"]
@property
def custom_record_keys(self):
"""Record types of the custom records.
Returns:
list of str
"""
if self._version == "gfa1":
return []
else:
keys = [k for k,v in self._records.items() if v]
if self._version == "gfa2":
return [k for k in keys if k not in self.NONCUSTOM_GFA2_KEYS]
else:
return [k for k in keys \
if k not in self.NONCUSTOM_GFA2_KEYS and k not in self.GFA1_ONLY_KEYS]
def custom_records_of_type(self, record_type):
"""List of custom records of the specified type."""
if record_type not in self.custom_record_keys:
return []
return list(self._records[record_type].values())
@property
def lines(self):
"""All the lines of the GFA"""
return self.comments + \
self.headers + \
self.segments + \
self.edges + \
self.paths + \
self.sets + \
self.gaps + \
self.fragments + \
self.custom_records