forked from graph-genome/component_segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmentation.py
More file actions
355 lines (280 loc) · 13.3 KB
/
segmentation.py
File metadata and controls
355 lines (280 loc) · 13.3 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""
Tasks
Get example output for tests - Ted
Parser for ODGI Bin file format - Ted
Component Segmentation Detection - Josiah and Joerg
Python memory object model - Josiah
Output format
"""
from typing import List, Tuple, Set
from pathlib import Path as osPath
from datetime import datetime
from sortedcontainers import SortedDict
from DNASkittleUtils.Contigs import read_contigs
from matrixcomponent.matrix import Path, Component, LinkColumn, Bin
from matrixcomponent.PangenomeSchematic import PangenomeSchematic
import matrixcomponent.utils as utils
import os
import logging
import argparse
import matrixcomponent
import matrixcomponent.JSONparser as JSONparser
import numpy as np
import pandas as pd
MAX_COMPONENT_SIZE = 100 # automatic calculation from cells_per_file did not go well
LOGGER = logging.getLogger(__name__)
"""logging.Logger: The logger for this module"""
def populate_component_occupancy(schematic: PangenomeSchematic):
for component in schematic.components:
# are matrix paths in the same order as schematic.path_names?
# side effect instead of return
component.occupants = [any([bin.coverage > 0.1 for bin in bins if bin])
for bins in component.matrix]
print("Populated Occupancy per component per path.")
def populate_component_matrix(paths: List[Path], schematic: PangenomeSchematic):
# the loops are 1) paths, and then 2) schematic.components
# paths are in the same order as schematic.path_names
for i, path in enumerate(paths):
sorted_bins = SortedDict((bin.bin_id, bin) for bin in path.bins)
values = list(sorted_bins.values())
for component in schematic.components:
from_id = sorted_bins.bisect_left (component.first_bin)
to_id = sorted_bins.bisect_right(component.last_bin)
relevant = values[from_id:to_id]
padded = []
if relevant:
padded = [[]] * (component.last_bin - component.first_bin + 1)
for bin in relevant:
padded[bin.bin_id - component.first_bin] = \
Bin(bin.coverage, bin.inversion_rate, bin.first_nucleotide, bin.last_nucleotide)
component.matrix.append(padded) # ensure there's always 1 entry for each path
print("Populated Matrix per component per path.")
populate_component_occupancy(schematic)
def segment_matrix(matrix: List[Path], bin_width, cells_per_file, pangenome_length) -> PangenomeSchematic:
from matrixcomponent import JSON_VERSION
print(f"Starting Segmentation process on {len(matrix)} Paths.")
schematic = PangenomeSchematic(JSON_VERSION,
bin_width,
1,
1,
[], [p.name for p in matrix], 1, pangenome_length)
connections, dividers = dividers_with_max_size(matrix, cells_per_file)
component_by_first_bin = {}
component_by_last_bin = {}
start_pos = 0
for valid_start in dividers:
if valid_start != 0:
current = Component(start_pos, valid_start - 1)
# current.active_members = 1
schematic.components.append(current)
component_by_first_bin[start_pos] = current
component_by_last_bin[valid_start - 1] = current
start_pos = valid_start
print(f"Created {len(schematic.components)} components")
# populate Component occupancy per Path
populate_component_matrix(matrix, schematic)
connections_array = connections.to_numpy()
groups = utils.find_groups(connections_array[:, :2])
path_indices = connections.path_index.to_numpy()
participants_mask = np.zeros(len(schematic.path_names), dtype=bool)
nLinkColumns = 0
for (start, end) in groups:
row = connections_array[start]
src, dst = int(row[0]), int(row[1])
participants_mask[:] = False
participants_mask[path_indices[start:end]] = True
phase_dots = participants_mask.tolist()
link_column = LinkColumn(src, dst, participants=phase_dots)
src_component = component_by_last_bin.get(src)
dst_component = component_by_first_bin.get(dst)
if src_component:
src_component.departures.append(link_column)
nLinkColumns += 1
if dst_component:
dst_component.arrivals.append(link_column)
nLinkColumns += 1
for i in range(len(schematic.components)-1):
component, next_component = schematic.components[i],schematic.components[i+1]
add_adjacent_connector_column(component, next_component, schematic)
print(f"Created {nLinkColumns} LinkColumns")
return schematic
def dividers_with_max_size(matrix: List[Path], cells_per_file: int):
"""Adds in additional dividers to ensure very large components are split into
multiple components with no Links."""
connections, dividers = find_dividers(matrix)
# estimate number of paths, x10 because most paths are empty
dividers_extended = []
prev = 0
for div in dividers:
gap_size = div - prev
if gap_size > MAX_COMPONENT_SIZE:
for i in range(prev + MAX_COMPONENT_SIZE, div, MAX_COMPONENT_SIZE):
dividers_extended.append(i) # add a series of dividers spaced ^ apart
prev = div
dividers_extended.append(div)
return connections, dividers_extended
def add_adjacent_connector_column(component, next_component, schematic):
"""The last Departure LinkColumn is to the adjacent component
Use logic to decide on which rows need adjacent connectors
Start with the easy subtractive case of occupancy - departures and move to more complex,
multiple copy cases."""
adjacents = []
for row in range(len(schematic.path_names)):
connection_exists = False
if component.occupants[row] and next_component.occupants[row]: # occupant present
# n_arrivals = sum([column.participants[row] for column in component.arrivals])
departed = any([column.participants[row] for column in component.departures]) # no need to compute sum
# connection_exists = n_arrivals + 1 > departed
connection_exists = not departed # didn't depart
adjacents.append(connection_exists)
component.departures.append(LinkColumn( # LinkColumn for adjacents
component.last_bin,
component.last_bin + 1,
participants=adjacents))
def find_dividers(matrix: List[Path]) -> Tuple[pd.DataFrame, Set[int]]:
max_bin = 1
self_loops = [] # track self loops just in case component gets cut in half
connection_dfs = [] # pandas dataframe with columns (from, to, path [name])
n_remaining_links = 0
for i, path in enumerate(matrix):
bin_ids = np.array([b.bin_id for b in path.bins])
bin_ids.sort()
if bin_ids.size > 0:
max_bin = max(max_bin, int(bin_ids[-1]))
links = path.links
if links.size == 0:
continue
# we don't want these to become dividers
boundary_mask = utils.path_boundaries(links)
self_loops_mask = utils.self_loops(links)
if np.any(self_loops_mask):
self_loops.append(links[self_loops_mask])
links = links[~(boundary_mask | self_loops_mask)]
path_dividers_mask = utils.path_dividers(links, bin_ids)
path_dividers = links[path_dividers_mask]
if path_dividers.size == 0:
continue
df = pd.DataFrame.from_dict({
'from': path_dividers[:, 0], # aka upstream
'to': path_dividers[:, 1], # aka downstream
'path_index': i
})
n_remaining_links = n_remaining_links + len(df)
df = utils.sort_and_drop_duplicates(df) # early deduplication saves lots of runtime memory
connection_dfs.append(df)
# <old comments applicable to each divider>
#
# if (upstream + 1) in leaving.keys() :
# print(f"Found inherited rearrangement {upstream+1}")
#
# TODO: insert prevarications about exact position
# Divider should be somewhere in here
# Tolerable range?
# Stack up others using the same LinkColumn
df = pd.concat(connection_dfs)
df = utils.sort_and_drop_duplicates(df)
n_uniq_links = len(df)
# all start positions of components
# (max_bin + 1) is end of pangenome
dividers = np.concatenate([[1, max_bin + 1], df["from"] + 1, df["to"]])
dividers = np.unique(dividers).tolist()
print(f"Largest bin_id was {max_bin}\n"
f"Found {len(dividers)} dividers.")
if self_loops:
n_self_loops = np.unique(np.concatenate(self_loops), axis=0).shape[0]
print(f"Eliminated {n_self_loops} self-loops")
n_links = sum([len(p.links) for p in matrix])
print(f"Input has {n_links} listed Links. "
f"Segmentation eliminated {(1-n_remaining_links/n_links)*100}% of them.")
print(f"Found {n_uniq_links} unique links")
return df, dividers
def setup_logging():
"""Setup the logging, add a log file"""
log_name = osPath(args.json_file).with_suffix('.log')
if args.output_folder:
log_name = osPath(args.output_folder).joinpath('log')
os.makedirs(args.output_folder, exist_ok=True)
t = datetime.now()
timestr = f"{t.year}{t.month:02}{t.day:02}-{t.hour:02}-{t.minute:02}-{t.second:02}"
log_name = str(log_name) + '.' + timestr + '.log'
handler = logging.FileHandler(os.path.join(log_name))
handler.setLevel(args.log_level)
handler.setFormatter(logging.Formatter(matrixcomponent.LOGGING_FORMAT_STR,
datefmt=matrixcomponent.LOGGING_DATE_FORMAT))
logging.getLogger().addHandler(handler)
# Helper class to allow multi-line help messages for argparse user parameters:
class SmartFormatter(argparse.HelpFormatter):
def _split_lines(self, text, width):
if text.startswith('R|'):
return text[2:].splitlines()
# this is the RawTextHelpFormatter._split_lines
return argparse.HelpFormatter._split_lines(self, text, width)
def write_files(folder, odgi_fasta: Path, schematic: PangenomeSchematic):
os.makedirs(folder, exist_ok=True) # make directory for all files
fasta = None
if odgi_fasta:
fasta = read_contigs(odgi_fasta)[0]
bin2file_mapping = schematic.split_and_write(args.cells_per_file, folder, fasta)
schematic.write_index_file(folder, bin2file_mapping)
def get_arguments():
"""Create the command line interface and return the command line arguments
Returns
-------
Namespace
The command line arguments
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="Example Command:\n"
"--json-file=data/run1.B1phi1.i1.seqwish.w100.json --cells-per-file=5000 --fasta=data/run1.B1phi1.i1.seqwish.fasta")
parser.add_argument('-j', '--json-file',
dest='json_file',
required=True,
help='input JSON file')
parser.add_argument('-f', '--fasta',
dest='fasta',
help='Optional: Fasta file containing the pangenome sequence generated by '
'odgi for this Graph.')
parser.add_argument('-o', '--out-folder',
dest='output_folder',
help='output folder')
parser.add_argument('-c', '--cells-per-file',
dest='cells_per_file',
default=5000,
type=int,
help='Tip: Adjust this number to get chunk files output close to 2MB. '
'Number of cells per file (#bins per file = #cells / #paths)')
parser.add_argument('-l', '--log-level',
default='DEBUG',
choices=('DEBUG', 'INFO', 'WARNING', 'ERROR'),
help='level of logging verbosity. DEBUG is most verbose')
parser.add_argument('-p', '--parallel-cores',
dest='parallel_cores',
default=os.cpu_count(),
type=int,
help='Tip: do not set this one to more than available CPU cores)')
args = parser.parse_args()
if not args.output_folder:
# directory with the same name as the json
args.output_folder = osPath(args.json_file).parent.joinpath(osPath(args.json_file).stem)
else:
args.output_folder = osPath(args.output_folder)
os.makedirs(args.output_folder, exist_ok=True)
if (args.parallel_cores <= 0):
args.parallel_cores = os.cpu_count()
return args
def main():
global args
args = get_arguments()
setup_logging()
LOGGER.info(f'reading {osPath(args.json_file)}...\n')
paths, pangenome_length, bin_width = JSONparser.parse(args.json_file, args.parallel_cores)
schematic = segment_matrix(paths, bin_width, args.cells_per_file, pangenome_length)
del paths
# this one spits out json and optionally other output files (fasta, ttl)
write_files(args.output_folder, args.fasta, schematic)
if __name__ == '__main__':
main()
#--json-file=data/run1.B1phi1.i1.seqwish.w100.json --cells-per-file=5000
# --fasta=data/run1.B1phi1.i1.seqwish.fasta