-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
283 lines (247 loc) · 11.3 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import sys
import os
import argparse
from subprocess import *
import json
from Utility import *
from gnrt_pos_true_seqs import DGProcessor
from run_multi_threads_collect_reads import MultiThrdReadsCollector
from run_multi_threads_discordant import DiscordantReadsCollector
from merge_reads import ReadsMerger
from assemble_gaps import GapAssembler
MERGE_FOLDER="merged/"
def usage():
print 'Usage: python {0} -c Options -g configure_file\n'.format(sys.argv[0]),
print 'Options:\n',
print ' Clean Remove all the temporary files\n',
print ' All Run the whole pipeline\n',
print ' Preprocess Get gap positions from draft genomes\n',
print ' Collect Collect and merge the related reads from alignments\n',
print ' Assembly Assemble the collect reads (including collect both unmapped reads)\n',
print 'Example of running the whole pipeline: python main.py -c All -g config.txt'
########################################################################################################################
def get_args():
# Assign description to the help doc
parser = argparse.ArgumentParser(
description='Run the pipeline of GAPPadder')
# Add arguments
parser.add_argument(
'-g', '--config', type=str, help='Configuration file name', required=True)
parser.add_argument(
'-c', '--command', type=str, help='Specific command', required=True)
args = parser.parse_args()
sfconfig=args.config
scommand=args.command
return sfconfig, scommand
def parse_configuration(sf_config):
sf_draft, min_gap_len, flank_len, nthreads, working_folder, vbs="","","","","",""
pbwa, psamtools, pvelvet, pkmc, prefiner, pmerger="","","","","",""
lalgnmt=[]
lkmers=[]
lraw_reads=[]
rtn_msg=""
rtn_value=False
with open(sf_config) as data_file:
data = json.load(data_file)
##first check whether required settings are set
if ("draft_genome"not in data) or ("alignments" not in data) or ("raw_reads" not in data):
return False
##required settings
#input draft genome
sf_draft=data["draft_genome"]["fa"]
if os.path.exists(sf_draft)==False:
rtn_msg="The provided draft genome does not exist, please check!!!"
return rtn_msg, rtn_value
##alignment list
for record in data["alignments"]:
bam=record["bam"]
insert_size=int(record["is"])
std_derivation=int(record["std"])
lalgnmt.append((bam,insert_size,std_derivation))
if os.path.exists(bam)==False:
rtn_msg="The bam file "+bam+" doesn't exist, please check!!!"
return rtn_msg, rtn_value
##optinal settings
#parameters
if "parameters" in data:
min_gap_len=int(data["parameters"]["min_gap_size"])
flank_len=int(data["parameters"]["flank_length"])
nthreads=int(data["parameters"]["nthreads"])
working_folder=data["parameters"]["working_folder"]
if os.path.exists(working_folder)==False:
rtn_msg="The working(output) folder provided doesn't exist, please check!!!"
return rtn_msg, rtn_value
vbs=int(data["parameters"]["verbose"])
#raw reads
for record in data["raw_reads"]:
left_fq=record["left"]
rigth_fq=record["right"]
if os.path.exists(left_fq)==False or os.path.exists(rigth_fq)==False:
rtn_msg="The raw reads files "+ left_fq+" or "+rigth_fq+" do not exist, please check!!!"
return rtn_msg, rtn_value
lraw_reads.append((left_fq, rigth_fq))
##kmer list
if "kmer_length" in data:
for record in data["kmer_length"]:
k=int(record["k"])
for subrecord in record["k_velvet"]:
sub_k=int(subrecord["k"])
lkmers.append((k,sub_k))
#software paths
if "software_path" in data:
pbwa=data["software_path"]["bwa"]
psamtools=data["software_path"]["samtools"]
pvelvet=data["software_path"]["velvet"]
prefiner=data["software_path"]["TERefiner"]
pmerger=data["software_path"]["ContigsMerger"]
pkmc=data["software_path"]["kmc"]
set_software_paths(pbwa, psamtools, prefiner, pmerger, pkmc, pvelvet)
#print flank_len###############################################################################################
set_parameters(sf_draft, nthreads, working_folder , vbs, min_gap_len, flank_len)
set_alignment_list(lalgnmt)
set_raw_reads_list(lraw_reads)
set_kmer_list(lkmers)
return rtn_msg, True
def prepare_sub_folders(spath):
cmd="mkdir {0}scaffold_reads_list_all".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}gap_reads".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}gap_reads_for_alignment".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}gap_reads_high_quality".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}discordant_reads_list".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}discordant_temp".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
def prepare_sub_folders_merged(spath):
if os.path.exists("{0}kmc_temp".format(spath))==True:
return
cmd="mkdir {0}gap_reads".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}gap_reads_for_alignment".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}gap_reads_high_quality".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}kmc_temp".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}temp".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}kmers".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}velvet_temp".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}both_unmapped".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="mkdir {0}unmapped_reads".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
def prepare_folders(algmt_list, working_space):
global MERGE_FOLDER
#first prepare folders
cnt=1
folder_list=[]
for algmt in algmt_list:
folder="{0}_is{1}/".format(cnt, algmt[1])
spath=working_space+folder
folder_list.append(spath)
cnt=cnt+1
if os.path.exists(spath)==False:
cmd="mkdir {0}".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
if os.path.exists("{0}scaffold_reads_list_all".format(spath)):
continue
prepare_sub_folders(spath)
#folder="merged" #prepare for the merged folder
folder=MERGE_FOLDER
spath=working_space+folder
if os.path.exists(spath)==False:
cmd="mkdir {0}".format(spath)
Popen(cmd, shell = True, stdout = PIPE).communicate()
if os.path.exists("{0}scaffold_reads_list_all".format(spath))==False:
prepare_sub_folders_merged(spath)
return folder_list
def clean_all(working_folder):
empty_folder="empty_dir"
if os.path.exists(empty_folder)==False:
cmd="mkdir {0}".format(empty_folder)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="rsync -a --delete {0}/ {1}".format(empty_folder, working_folder)
Popen(cmd, shell = True, stdout = PIPE).communicate()
def main_func(scommand, sf_config):
global MERGE_FOLDER
rtn_msg, brtn=parse_configuration(sf_config)
if brtn==False:
print rtn_msg
return
min_gap_lenth=get_min_gap_length()
flank_length=get_flank_length()
sf_draft=get_draft_genome()
sf_fai=sf_draft+".fai"
samtools_path=get_samtools_path()
if os.path.exists(sf_fai)==False:
cmd="{0} faidx {1}".format(samtools_path, sf_draft)
Popen(cmd, shell = True, stdout = PIPE).communicate()
working_folder=get_output_folder()
if working_folder[-1]!="/":
working_folder=working_folder+"/"
sf_gap_pos=working_folder+"gap_positions.txt"
anchor_mapq=30
clip_dist=250
nthreads=get_threads_num()
algmt_list=get_alignment_list()
if scommand=="Clean" or scommand=="All":
clean_all(working_folder)
if scommand=="Preprocess" or scommand=="All":
dgp=DGProcessor(sf_draft, sf_gap_pos)
dgp.gnrt_gap_positions(min_gap_lenth)
dgp.get_gap_flank_seqs(sf_draft, sf_gap_pos, flank_length, sf_fai, working_folder)
if scommand=="Collect" or scommand=="All":
folder_list=prepare_folders(algmt_list, working_folder)#first collect reads for each alignment
raw_reads_list=get_raw_reads_list()
#print len(algmt_list), len(folder_list), len(raw_reads_list)
if len(algmt_list)!=len(raw_reads_list) and len(folder_list)!=len(raw_reads_list):
print "# of alignment files and # of raw reads do not match!!!!!"
return
idx=0
for algmt in algmt_list:#
folder=folder_list[idx]
raw_reads=raw_reads_list[idx]
idx=idx+1
sf_bam=algmt[0]
insert_size=algmt[1]
derivation=algmt[2]
mtrc=MultiThrdReadsCollector(sf_fai, sf_bam, sf_gap_pos, anchor_mapq)
mtrc.dispath_collect_jobs(nthreads, samtools_path, insert_size, derivation, clip_dist, folder)
##run reads collect for discordant
left_reads=raw_reads[0]
right_reads=raw_reads[1]
if folder[-1]!="/":
folder=folder+"/"
sfout_discord_pos=folder+"discordant_reads_pos.txt"
drc=DiscordantReadsCollector(sf_fai, sf_bam, folder, nthreads)
drc.collect_discordant_regions_v2(sfout_discord_pos)
drc.dispath_collect_jobs()
drc.merge_dispatch_reads_for_gaps_v2(left_reads, right_reads)
#dispatch_reads_for_gaps_to_validate_contigs(left_reads, right_reads)
drc.dispatch_high_quality_reads_for_gaps(left_reads, right_reads)
#####################################
#then merge the reads
rmerger=ReadsMerger()
rmerger.merge_reads_v2(sf_fai, sf_gap_pos, folder_list, "gap_reads", working_folder+MERGE_FOLDER, nthreads)
rmerger.merge_reads_v2(sf_fai, sf_gap_pos, folder_list, "gap_reads_alignment", working_folder+MERGE_FOLDER, nthreads)
rmerger.merge_reads_v2(sf_fai, sf_gap_pos, folder_list, "gap_reads_high_quality", working_folder+MERGE_FOLDER, nthreads)
#
# ##here remove the temporary files???? ##########################################################################
for folder in folder_list:
clean_all(folder)
if scommand=="Assembly" or scommand=="All":
gap_assembler=GapAssembler(sf_fai, sf_gap_pos, nthreads, working_folder+MERGE_FOLDER)
gap_assembler.assemble_pipeline()
return
if __name__ == "__main__":
if len(sys.argv) <= 2:
usage()
raise SystemExit
sfconfig, scommand = get_args()
main_func(scommand,sfconfig)