This repository has been archived by the owner on Jul 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (227 loc) · 9.4 KB
/
main.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
import argparse
import math
import time
import json
import os
from modules.CandidateFinder import CandidateFinder
from modules.BamHandler import BamHandler
from modules.FastaHandler import FastaHandler
from modules.AlleleFinder import AlleleFinder
from multiprocessing import Process
"""
alignmentPolish finds possible variant sites in given bam file.
It requires three parameters:
- bam_file_path: path to a bam file
- reference_file_path: path to a reference file
Creates:
- CandidateFinder object that contains windows of possible variants.
Also, the terms "window" and "region" are NOT interchangeable.
Region: A genomic region of interest where we want to find possible variant candidate
Window: A window in genomic region where there can be multiple alleles
A region can have multiple windows and each window belongs to a region.
Example Usage:
python3 main.py --bam [path_to_bam] --ref [path_to_reference_fasta_file] --chromosome_name chr3 --max_threads [max_number_of_threads] --test [True/False] --json [True/False] --output_dir [path_to_JSON_output]
"""
DEBUG_PRINT_WINDOWS = False
DEBUG_PRINT_CANDIDATES = False
class ComplexEncoder(json.JSONEncoder):
"""
JSON encoder for class attributes
"""
def default(self, obj):
if hasattr(obj, 'reprJSON'):
return obj.reprJSON()
else:
return json.JSONEncoder.default(self, obj)
class AllCandidatesInRegion:
"""
Creates a list of candidates in a region.
"""
def __init__(self, chromosome_name, start_position, end_position):
"""
Initialize object
:param chromosome_name: Name of the chromosome
:param start_position: Region start
:param end_position: Region end
"""
self.chromosome_name = chromosome_name
self.start_position = start_position
self.end_position = end_position
self.all_candidates = []
def add_candidate_to_list(self, alignment_candidates_object):
"""
Add a candidate to the list
:param alignment_candidates_object: Candidate object to add
:return:
"""
self.all_candidates.append(alignment_candidates_object)
def reprJSON(self):
"""
Report all attributes of this object as a dictionary that can be saved as a JSON
:return: A dictionary with key value to be saved in json format
"""
return dict(chromosome_name=self.chromosome_name, start_position=self.start_position,
end_position=self.end_position, all_candidates=self.all_candidates)
class View:
"""
Works as a main class and handles user interaction with different modules.
"""
def __init__(self, chromosome_name, bam_file_path, reference_file_path, output_file_path):
# --- initialize handlers ---
self.bam_handler = BamHandler(bam_file_path)
self.fasta_handler = FastaHandler(reference_file_path)
self.output_dir = output_file_path
# --- initialize parameters ---
self.chromosome_name = chromosome_name
def write_json(self, start, end, all_candidate_lists):
"""
Create a json output of all candidates found in the region
:param start: Candidate region start
:param end: Candidate region end
:param all_candidate_lists: Candidate list to be saved
:return:
"""
if not os.path.exists(self.output_dir + "json_output/"):
os.mkdir(self.output_dir + "json_output/")
json_file = open(self.output_dir + "json_output/" + "Candidates" + '_' + self.chromosome_name + '_'
+ str(start) + '_' + str(end) + ".json", 'w')
json_file.write(json.dumps(all_candidate_lists.reprJSON(), cls=ComplexEncoder, indent=4, sort_keys=True))
def parse_region(self, start_position, end_position, json_out):
"""
Find possible candidate windows.
- All candidate lists
"""
reads = self.bam_handler.get_reads(chromosome_name=self.chromosome_name,
start=start_position,
stop=end_position)
candidate_finder = CandidateFinder(reads=reads,
fasta_handler=self.fasta_handler,
chromosome_name=self.chromosome_name,
region_start_position=start_position,
region_end_position=end_position)
# parse reads to find candidate positions
candidate_finder.parse_reads(reads=reads)
# merge candidate positions
candidate_finder.merge_positions()
# print the windows we got
if DEBUG_PRINT_WINDOWS:
candidate_finder.print_windows()
candidate_windows = candidate_finder.get_candidate_windows()
all_candidate_lists = AllCandidatesInRegion(self.chromosome_name, start_position, end_position)
# for each window find list of possible alleles
for chr_name, window_start, window_end in candidate_windows:
# get the reference sequence
reference_sequence = self.fasta_handler.get_sequence(chr_name, window_start, window_end+1)
# get all pileup columns in that window
pileup_columns = self.bam_handler.get_pileupcolumns_aligned_to_a_region(chr_name, window_start, window_end+1)
allele_finder = AlleleFinder(chr_name, window_start, window_end, pileup_columns, reference_sequence)
# generate base dictionaries
allele_finder.generate_base_dictionaries()
# generate candidate allele list
candidate_list = allele_finder.generate_candidate_allele_list()
if DEBUG_PRINT_CANDIDATES:
candidate_list.print_all_candidates()
# add alleles to candidate
all_candidate_lists.add_candidate_to_list(candidate_list)
if json_out:
self.write_json(start_position, end_position, all_candidate_lists)
def test(self, json_out):
"""
Run a test
:param json_out:
:return:
"""
self.parse_region(start_position=100000, end_position=200000, json_out=json_out)
def do_parallel(chr_name, bam_file, ref_file, json_out, output_dir, max_threads=5):
"""
Split chromosome in different ranges for parallel processing
:param chr_name: Chromosome name
:param bam_file: Bam file path
:param ref_file: Reference file path
:param output_dir: Directory for saving output
:param json_out: JSON out flag
:param max_threads: Maximum number of threads
:return:
"""
# entire length of chromosome
fasta_handler = FastaHandler(ref_file)
whole_length = fasta_handler.get_chr_sequence_length(chr_name)
# expected length of each segment
each_segment_length = int(math.ceil(whole_length / max_threads))
for i in range(max_threads):
# parse window of the segment. Use a 1000 overlap for corner cases.
view = View(chromosome_name=chr_name,
bam_file_path=bam_file,
reference_file_path=ref_file,
output_file_path=output_dir
)
start_position = i*each_segment_length
end_position = (i+1) * each_segment_length + 1000
p = Process(target=view.parse_region, args=(start_position, end_position, json_out))
p.start()
if __name__ == '__main__':
'''
Processes arguments and performs tasks to generate the pileup.
'''
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument(
"--ref",
type=str,
required=True,
help="Reference corresponding to the BAM file."
)
parser.add_argument(
"--bam",
type=str,
required=True,
help="BAM file containing reads of interest."
)
parser.add_argument(
"--chromosome_name",
type=str,
default="3",
help="Desired chromosome number E.g.: 3"
)
parser.add_argument(
"--max_threads",
type=int,
default=5,
help="Number of maximum threads for this region."
)
parser.add_argument(
"--test",
type=bool,
default=False,
help="If true then a dry test is run."
)
parser.add_argument(
"--json",
type=bool,
default=False,
help="If true then output will be in a json file in json folder."
)
parser.add_argument(
"--output_dir",
type=str,
default="output/",
help="If true then output will be in a json file in json folder."
)
FLAGS, unparsed = parser.parse_known_args()
# process the output directory
if FLAGS.output_dir[-1] != '/':
FLAGS.output_dir += '/'
if not os.path.exists(FLAGS.output_dir):
os.mkdir(FLAGS.output_dir)
view = View(chromosome_name=FLAGS.chromosome_name,
bam_file_path=FLAGS.bam,
reference_file_path=FLAGS.ref,
output_file_path=FLAGS.output_dir)
if FLAGS.test is True:
view = View(chromosome_name=FLAGS.chromosome_name,
bam_file_path=FLAGS.bam,
reference_file_path=FLAGS.ref,
output_file_path=FLAGS.output_dir)
view.test(FLAGS.json)
else:
do_parallel(FLAGS.chromosome_name, FLAGS.bam, FLAGS.ref, FLAGS.json, FLAGS.output_dir, FLAGS.max_threads)