-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
395 lines (355 loc) · 14.4 KB
/
models.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# to make numpy divisions show decimal values by default:
# https://stackoverflow.com/questions/1799527/numpy-show-decimal-values-in-array-results
from __future__ import division
import csv
import os
from os import path
import multiprocessing as mp
import numpy as np
from scipy import stats
import string
import utils
class MicroSatelliteProfiler:
"""
Class that aids in the detection of microsatellite instability (MSI) from
sequencing data
"""
BED_FILE_ERROR_MESSAGE = (
"Bed file containing heterozygous SNPs does not exist."
)
VALID_REPEAT_UNITS = [1, 2, 3, 4, 5, 6]
CHROMOSOMES = [str(i) for i in range(1, 23)]
CHROMOSOMES.extend(["X", "Y","chr1","chr2","chr3","chr4","chr5","chr6","chr7","chr8","chr9","chr10","chr11","chr12","chr13","chr14","chr15","chr16","chr17","chr18","chr19","chr20","chr21","chr22","chrX","chrY"])
CHROMOSOMES_ERROR_MESSAGE = (
"Valid chromosomes are: {}".format(CHROMOSOMES)
)
FASTA_DIRECTORY_ERROR_MESSAGE = (
"Fasta directory correspoding to the reference genome does not exist."
)
FASTA_FILE_ERROR_MESSAGE = "Fasta file: {} does not exist."
NORMAL = "normal"
NORMAL_BAM_ERROR_MESSAGE = "Normal bam file does not exist."
NUMBER_OF_PROCESSORS_ERROR_MESSAGE = (
"The value of the argument `nprocs` needs to be at least 1"
)
PHASED = "phased"
REFERENCE_SET_ERROR_MESSAGE = "Reference set file does not exist."
REPEAT_UNITS_ERROR_MESSAGE = (
"Valid repeat_units are {} or any "
"combination thereof".format(VALID_REPEAT_UNITS)
)
TUMOR = "tumor"
TUMOR_BAM_ERROR_MESSAGE = "Tumor/case bam file does not exist."
UNPHASED = "unphased"
VALID_MODES = [PHASED, UNPHASED]
VALID_MODES_ERROR_MESSAGE = (
'The mode argument needs to be one of the following: {}.'.format(
VALID_MODES
)
)
def __init__(self, arguments):
"""
Constructor for MicroSatelliteProfiler
:param arguments: parsed argparse.ArgumentParser() object
"""
self.bed_filename = arguments.bed
self.chromosomes = np.sort(arguments.chromosomes)
self.fasta_dict = {}
self.fasta_directory = arguments.fasta
self.flank_size = arguments.flank_size
self.mapping_quality = arguments.mapping_quality
self.max_microsatellite_length = arguments.max_MS_length
self.min_coverage = arguments.min_coverage
self.min_microsatellite_length = arguments.min_MS_length
self.mode = arguments.mode
self.normal_bam = arguments.normal_bam
self.number_of_processors = arguments.nprocs
self.output_prefix = arguments.output_prefix
self.reference_set_base_dir = arguments.reference_set
self.repeat_units = set(arguments.rus)
self.tolerated_mismatches = arguments.tolerated_mismatches
#self.notation = arguments.notation
self.tumor_bam = arguments.tumor_bam
self.reference_sets = None
self.sites = None
self.reference_set_dict = {}
self.reference_set_ini_end_dict = {}
self.read_lengths_normal = []
self.read_lengths_tumor = []
self.read_lengths_normal_unphased = []
self.read_lengths_tumor_unphased = []
self._validate_arguments()
self._set_fasta_dict()
self._populate_reference_sets()
if self.is_phased:
with open(self.bed_filename) as bed:
reader = csv.reader(bed, delimiter="\t")
self.sites = list(reader)
self.chunk_size = int(len(self.sites) / self.number_of_processors)
if self.is_unphased:
self.chunk_size = int(
len(self.reference_sets) / self.number_of_processors
)
@property
def is_phased(self):
return True if self.mode == self.PHASED else False
@property
def is_unphased(self):
return True if self.mode == self.UNPHASED else False
def _check_bams(self):
if not path.exists(self.tumor_bam):
raise RuntimeError(self.TUMOR_BAM_ERROR_MESSAGE)
if not path.exists(self.normal_bam):
raise RuntimeError(self.NORMAL_BAM_ERROR_MESSAGE)
def _check_bed_filename(self):
if not path.exists(self.bed_filename) and self.mode == self.PHASED:
raise RuntimeError(self.BED_FILE_ERROR_MESSAGE)
def _check_chromosomes(self):
for chromosome in self.chromosomes:
if chromosome not in self.CHROMOSOMES:
raise RuntimeError(self.CHROMOSOMES_ERROR_MESSAGE)
def _check_fasta_filename(self):
if not path.exists(self.fasta_directory):
raise RuntimeError(self.FASTA_DIRECTORY_ERROR_MESSAGE)
def _check_mode(self):
if self.mode not in self.VALID_MODES:
raise RuntimeError(self.VALID_MODES_ERROR_MESSAGE)
def _check_processors(self):
if self.number_of_processors is None:
self.number_of_processors = mp.cpu_count()
if self.number_of_processors == 0:
raise RuntimeError(self.NUMBER_OF_PROCESSORS_ERROR_MESSAGE)
def _check_reference_set(self):
if not path.exists(self.reference_set_base_dir) and self.mode == self.UNPHASED:
raise RuntimeError(self.REFERENCE_SET_ERROR_MESSAGE)
def _check_repeat_units(self):
for repeat_unit in self.repeat_units:
if repeat_unit not in self.VALID_REPEAT_UNITS:
raise RuntimeError(self.REPEAT_UNITS_ERROR_MESSAGE)
def _conclude_run(self):
if self.number_of_processors > 1:
all_normal = {}
all_tumor = {}
for i in range(0, self.number_of_processors*len(self.chromosomes)):
if self.is_phased:
all_normal.update(self.read_lengths_normal[i])
all_tumor.update(self.read_lengths_tumor[i])
else:
all_normal.update(self.read_lengths_normal_unphased[i])
all_tumor.update(self.read_lengths_tumor_unphased[i])
else:
all_normal = {}
all_tumor = {}
if self.is_phased:
for i in range(0, len(self.chromosomes)):
all_normal.update(self.read_lengths_normal[i])
all_tumor.update(self.read_lengths_tumor[i])
else:
for i in range(0, len(self.chromosomes)):
all_normal.update(self.read_lengths_normal_unphased[i])
all_tumor.update(self.read_lengths_tumor_unphased[i])
keys_normal = set(all_normal)
keys_tumor = set(all_tumor)
common_keys = sorted(keys_tumor.intersection(keys_normal))
with open('{}_{}.txt'.format(self.output_prefix, self.mode), 'w') as f:
if self.is_phased:
self._write_phased_output(
f,
common_keys,
all_normal,
all_tumor
)
else:
self._write_unphased_output(
f,
common_keys,
all_normal,
all_tumor
)
print "{} microsatellites writen to: {}_{}.txt".format(
self.mode.title(),
self.output_prefix,
self.mode
)
print (
"Calculation of the {} microsatellites finished successfully."
.format(self.mode)
)
def _log_normal_result(self, result):
self.read_lengths_normal.append(result)
def _log_tumor_result(self, result):
self.read_lengths_tumor.append(result)
def _log_unphased_normal_result(self, result):
self.read_lengths_normal_unphased.append(result)
def _log_unphased_tumor_result(self, result):
self.read_lengths_tumor_unphased.append(result)
def _populate_reference_sets(self, refsets=None):
for chromosome in self.chromosomes:
refsetgen = utils.loadcsv(
os.path.join(
self.reference_set_base_dir,
"reference_set_{}_sorted.txt".format(string.strip(chromosome,"chr"))
),
self.min_microsatellite_length,
self.max_microsatellite_length,
self.repeat_units
)
refsets = [x for x in refsetgen]
self.reference_set_dict[chromosome] = refsets
# get the index positions
refset_ini_end = [x[1] for x in refsets]
self.reference_set_ini_end_dict[chromosome] = refset_ini_end
self.reference_sets = refsets
def run(self):
"""
Public method that wraps phased/unphased runs and executes them in
their multiprocess Pools
"""
if self.is_phased:
# Phased Normal run
self._run_in_pool(
utils.phased,
self.normal_bam,
self.sites,
self._log_normal_result,
self.NORMAL
)
# Phased Tumor run
self._run_in_pool(
utils.phased,
self.tumor_bam,
self.sites,
self._log_tumor_result,
self.TUMOR
)
if self.is_unphased:
for chromo in self.chromosomes:
print "Processing chromosome: ", chromo
# Unphased Normal run
self._run_in_pool(
utils.unphased,
self.normal_bam,
self.reference_set_dict[chromo],
self._log_unphased_normal_result,
self.NORMAL
)
# Unphased Tumor run
self._run_in_pool(
utils.unphased,
self.tumor_bam,
self.reference_set_dict[chromo],
self._log_unphased_tumor_result,
self.TUMOR
)
self._conclude_run()
def _run_in_pool(self,
func_to_run,
bam_file,
sites,
logging_method,
tumor_type):
"""
Run phased/unphased MSI detection for normal/tumor bamfiles in a
multiprocessing Pool
"""
print "{}: Extracting MS repeats from {} bam file..\n".format(
self.mode.upper(),
tumor_type
)
if self.number_of_processors == 1:
logging_method(func_to_run(self, sites, bam_file))
else:
pool = mp.Pool(
self.number_of_processors,
initializer=utils.multiprocessing_lock_init,
initargs=(mp.Lock(),)
)
for index in np.arange(0, self.number_of_processors):
if index != (self.number_of_processors - 1):
pool.apply_async(
func_to_run,
args=(
self,
sites[
index *
self.chunk_size:(index + 1) *
self.chunk_size
],
bam_file,
),
callback=logging_method
)
else:
pool.apply_async(
func_to_run,
args=(
self,
sites[
index *
self.chunk_size: len(sites)
],
bam_file,
),
callback=logging_method
)
pool.close()
pool.join()
print "{}: {} bam file processed correctly..\n".format(
self.mode.upper(),
tumor_type
)
def _set_fasta_dict(self):
for chromosome in self.chromosomes:
chromosome=string.strip(chromosome,"chr")
fasta_path = os.path.join(
self.fasta_directory,
"chr{}.fa".format(chromosome)
)
assert os.path.exists(fasta_path), \
self.FASTA_FILE_ERROR_MESSAGE.format(fasta_path)
self.fasta_dict[chromosome] = "{}chr{}.fa".format(
self.fasta_directory,
chromosome
#string.strip(chromosome,"chr")
)
def _validate_arguments(self):
"""
Validate the contents of our arg parser argument values
:raises RuntimeError
"""
self._check_processors()
self._check_repeat_units()
self._check_chromosomes()
self._check_mode()
self._check_bams()
self._check_reference_set()
self._check_bed_filename()
self._check_fasta_filename()
def _write_phased_output(self, outf, common_keys, all_normal, all_tumor):
for name in common_keys:
nor = all_normal[name]
canc = all_tumor[name]
if isinstance(nor, int) == False and isinstance(canc,
int) == False:
if len(nor) >= self.min_coverage and len(
canc) >= self.min_coverage:
pval_ks = stats.ks_2samp(nor, canc)[1]
outf.write(name + "\t" + ",".join(
[str(x) for x in nor]) + "\t" + ",".join(
[str(x) for x in canc]) + "\t" + str(pval_ks) + "\n")
def _write_unphased_output(self, outf, common_keys, all_normal, all_tumor):
for name in common_keys:
nor = all_normal[name]
canc = all_tumor[name]
if isinstance(nor, int) == False and \
isinstance(canc, int) == False:
if len(nor) >= self.min_coverage and \
len(canc) >= self.min_coverage:
pval = stats.ks_2samp(nor, canc)[1]
mo = stats.mode(nor).mode ## now scipy also returns the count for the mode!!
percentage = (nor == mo).sum() / len(nor)
confidence = "high" if percentage >= .7 else "low"
outf.write(name + "\t" + ",".join(
[str(x) for x in nor]) + "\t" + ",".join(
[str(x) for x in canc]) + "\t" + str(
pval) + "\t" + confidence + "\n")