-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateData.py
367 lines (280 loc) · 12.1 KB
/
generateData.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
from Bio import AlignIO, SeqIO, Phylo
from Bio.Align import AlignInfo
from Bio.Cluster import distancematrix
from Bio.Phylo.BaseTree import Tree, Clade
from cStringIO import StringIO
from run_ext_prog import runExtProg
import Bio
import numpy as np
import os
import re
import shutil
MATCH_TREE_NODE = re.compile("(\d+\.)(\d+)(\.\d+)")
projectDir = "/home/steven/Postdoc/Project_Srp/"
dataDir = projectDir + "simulateData/"
softwareDir = projectDir + "software/"
metaSimDir = softwareDir + "metaSim/"
artDir = softwareDir + ""
seqgenDir = softwareDir + "SeqGen/"
shrimpDir = softwareDir + "SHRiMP/bin/"
bsscDir = softwareDir + "BSSC/"
def runBSSC(workingFile, srp_tree_file, debug):
bssc = runExtProg(bsscDir + "BSSC_original", pdir=bsscDir, length=3)
bssc.set_param_at("-f", 1)
bssc.set_param_at(workingFile + "_BSSC.par", 2)
bssc.set_param_at(1, 3)
bssc_paup_result = workingFile + "_BSSC.paup"
# bssc_tree_result = workingFile + "_true_trees.trees"
try:
os.remove(bssc_paup_result)
# os.remove(workingFile + "_0.pau")
except OSError:
pass
while not os.path.exists(bssc_paup_result):
bssc.run()
# input_handle = open(bssc_tree_result, "rU")
input_handle = open(bssc_paup_result, "rU")
for line in input_handle:
if line.find("tree true_tree_1") > 0:
line = line.strip()
start = line.index("U]") + 3
treeString = line[start:]
tree = Phylo.read(StringIO(treeString), 'newick')
input_handle.close()
for clade in tree.find_clades():
if clade.name:
match = re.match(MATCH_TREE_NODE, clade.name)
if match:
index = match.group(2)
clade.name = "hap_" + str(int(index) - 1)
input_handle = open(bssc_paup_result, "rU")
sequences = AlignIO.read(input_handle, "nexus")
input_handle.close()
seq = sequences[0]
ref_handle = open(workingFile + ".cons", "w")
ref_handle.write(">%s\n%s\n" % ("Ref", seq.seq))
ref_handle.close()
output_handle = open(workingFile + "_seqgen.phylip", "w")
output_handle.write("1 1200\n")
output_handle.write("%s %s\n" % ("ancestor", seq.seq))
output_handle.write("1\n")
Phylo.write(tree, output_handle, "newick")
output_handle.close()
output_handle = open(srp_tree_file, "w")
Phylo.write(tree, output_handle, "newick")
output_handle.close()
# print os.path.exists(bssc_paup_result)
# print os.remove(bssc_paup_result)
# print os.path.exists(bssc_paup_result)
def runSeqGen(workingFile, srp_hap_file, srp_tree_file, debug):
seqgen_infile = workingFile + "_seqgen.phylip"
seqgen = runExtProg(seqgenDir + "./seq-gen", pdir=seqgenDir, length=3)
seqgen.set_param_at("-mHKY", 1)
seqgen.set_param_at("-t2", 2)
seqgen.set_param_at("-k1", 3)
# seqgen.set_param_at("-d0.1", 4)
# seqgen.set_param_at("-s0.00001", 4)
seqgen.set_stdin(seqgen_infile)
all_unique = False
repeat = 0
while not all_unique:
if repeat == 100:
runBSSC(workingFile, srp_tree_file, debug)
print "==========rerun BSSC========"
repeat = 0
repeat += 1
# print repeat
seqgen.run(0)
all_unique = check_unique_sequences(seqgen)
temp_handle = open(workingFile + "_seqgen_out.phylip", "w")
temp_handle.write(seqgen.output)
temp_handle.close()
SeqIO.convert(workingFile + "_seqgen_out.phylip", "phylip", workingFile + ".fasta", "fasta")
shutil.copy(workingFile + ".fasta", srp_hap_file)
# ./seq-gen -mHKY -t2 -k1 < H7.fasta > zz runBSSC(workingFile, srp_tree_file).fasta
def check_unique_sequences(seqgen):
# create our hash table to add the sequences
sequences = {}
data = AlignIO.read(StringIO(seqgen.output), "phylip")
for seq_record in data:
sequence = str(seq_record.seq).upper()
if sequence not in sequences:
sequences[sequence] = seq_record.id
else:
return False
return True
def runMetaSim(workingFile, workingDir, debug):
metaSim_infile = workingFile + ".fasta"
if os.path.exists(workingFile + "-454.fna"):
os.remove(workingFile + "-454.fna")
metaSim = runExtProg(metaSimDir + "./MetaSim", pdir=metaSimDir, length=9)
metaSim.set_param_at("cmd", 1)
metaSim.set_param_at("--454", 2)
metaSim.set_param_at("-f250", 3)
metaSim.set_param_at("-t25", 4)
metaSim.set_param_at("-r2000", 5)
metaSim.set_param_at("-c", 6)
metaSim.set_param_at("-d", 7)
metaSim.set_param_at(workingDir, 8)
metaSim.set_param_at(metaSim_infile, 9)
metaSim.run(debug)
# ./MetaSim cmd --454 -f 250 -t 25 -r300 -c -d /home/sw167/Postdoc/Project_Srp/simulateData/H6a /home/sw167/Postdoc/Project_Srp/simulateData/H6a/H6a.fasta
def runART(workingFile, art_output_prefix, error_free_model=True, debug=False):
art_infile = workingFile + ".fasta"
art = runExtProg(artDir + "./art_illumina", pdir=artDir)
art.set_param_at("-sam", 1)
art.add_switch("-i %s" % art_infile)
art.add_switch("-l 75")
art.add_switch("-f 20")
art.add_param("-o %s%s" % (workingFile, art_output_prefix))
if error_free_model:
art.add_switch("-ef")
# art.add_param("-na")
# print art.get_extract_switch()
# art.set_param_at("-c", 6)
# art.set_param_at("-d", 7)
# art.set_param_at(workingDir, 8)
# art.set_param_at(art_infile, 9)
art.run(debug)
# art_illumina -sam -i reference.fa -l 50 -f 10 -o single_dat
def runSam2Fasta(workingFile, result_file_prefix, samfile_prefix, srp_read_prefix, error_free_model=True, debug=False):
reference_file = workingFile + ".cons"
srp_temp_file = workingFile + "_temp.fasta"
sam_outfile = workingFile + samfile_prefix + ".sam"
# sam2fasta = runExtProg(softwareDir + "./sam2fasta.py", length=3)
sam2fasta = runExtProg("./sam2fasta_mod.py", length=3)
sam2fasta.set_param_at(reference_file, 1)
sam2fasta.set_param_at(sam_outfile, 2)
sam2fasta.set_param_at(srp_temp_file, 3)
sam2fasta.run(debug)
shutil.copy(sam_outfile, result_file_prefix + samfile_prefix + ".sam")
shutil.copy(srp_temp_file, result_file_prefix + srp_read_prefix + samfile_prefix + ".fasta")
if error_free_model:
sam_outfile = workingFile + samfile_prefix + "_errFree.sam"
sam2fasta.set_param_at(sam_outfile, 2)
sam2fasta.run(debug)
shutil.copy(sam_outfile, result_file_prefix + samfile_prefix + "_errFree.sam")
shutil.copy(srp_temp_file, result_file_prefix + srp_read_prefix + samfile_prefix + "_errFree.fasta")
def runShrimp(workingFile, shrimp_infile_prefix, shrimp_prefix, debug):
reference_file = workingFile + ".cons"
shrimp_infile = workingFile + shrimp_infile_prefix
sam_outfile = workingFile + shrimp_prefix + ".sam"
shrimp = runExtProg(shrimpDir + "gmapper-ls", length=3)
shrimp.set_param_at(shrimp_infile, 1)
shrimp.set_param_at(reference_file, 2)
shrimp.set_param_at("-h 25%", 3)
shrimp.add_switch("--qv-offset 33") # # for ART output
# shrimp.add_switch("--ignore-qvs")
# shrimp.set_param_at("-P", 3)
shrimp.run(debug)
outfile_handle = open(sam_outfile, "w")
outfile_handle.write(shrimp.output)
#
# def runSam2FastaShrimp(workingFile, samfile_prefix, srp_file, debug=False):
#
# reference_file = workingFile + ".cons"
# srp_temp_file = workingFile + "_temp.fasta"
# sam_outfile = workingFile + samfile_prefix + ".sam"
#
# # sam2fasta = runExtProg(softwareDir + "./sam2fasta.py", length=3)
# sam2fasta = runExtProg("./sam2fasta_mod.py", length=3)
# sam2fasta.set_param_at(reference_file, 1)
# sam2fasta.set_param_at(sam_outfile, 2)
# sam2fasta.set_param_at(srp_temp_file, 3)
# sam2fasta.run(1)
#
# temp_handle = open(srp_temp_file, "rU")
# srp_handle = open(srp_file, "w")
# # print reference_file, sam_outfile, srp_temp_file
#
# last_line = 0
# for i, line in enumerate(temp_handle):
# if i > 1:
# srp_handle.write(line)
# last_line = i
# # print last_line / 2
# temp_handle.close()
# srp_handle.close()
def main():
prefix = "H10"
working_directory = os.path.join(dataDir, prefix , "")
working_file_prefix = os.path.join(working_directory, prefix)
# base_bssc_infile = working_file_prefix + ".par"
srp_read_prefix = "_ShortRead"
art_prefix = "_ART"
shrimp_prefix = "_Shrimp"
print working_directory, working_file_prefix
for index in range(0, 10):
str_index = str(index)
# if not os.path.exists(working_directory):
# os.mkdir(working_directory)
# shutil.copy(base_bssc_infile, working_file_prefix + ".par")
prefix_index = prefix + "_" + str_index
result_dir = os.path.join(working_directory, prefix_index, "")
result_file_prefix = os.path.join(result_dir, prefix_index)
print index, result_dir, result_file_prefix
if not os.path.exists(result_dir):
os.mkdir(result_dir)
srp_tree_file = result_file_prefix + "_FullTree.tree"
srp_hap_file = result_file_prefix + "_FullHaplotype.fasta"
# srp_read_file_prefix = result_file_prefix + srp_read_prefix
debug = 0
error_free_model = True
runBSSC(working_file_prefix, srp_tree_file, debug)
runSeqGen(working_file_prefix, srp_hap_file, srp_tree_file, debug)
#
runART(working_file_prefix, art_prefix, error_free_model, debug)
runSam2Fasta(working_file_prefix, result_file_prefix, art_prefix, srp_read_prefix, error_free_model, debug)
shrimp_infile = art_prefix + ".fq"
runShrimp(working_file_prefix, shrimp_infile, shrimp_prefix, debug)
error_free_model = False
runSam2Fasta(working_file_prefix, result_file_prefix, shrimp_prefix, srp_read_prefix, error_free_model, debug)
##### old steps
# runMetaSim(working_file_prefix, working_directory, debug)
# shrimp_infile = "-454.fna" # metaSim
# runShrimp(working_file_prefix, shrimp_infile, "_metaSim", debug)
# runBSSC(working_file_prefix, srp_tree_file, debug)
# runSeqGen(working_file_prefix, srp_hap_file, srp_tree_file, debug)
# runMetaSim(working_file_prefix, working_directory, debug)
# runShrimp(working_file_prefix, srp_file, debug)
def mainFromAlignment():
prefix = "H7"
workingDir = dataDir + prefix + "/"
workingFile = workingDir + prefix
# base_bssc_infile = workingFile + ".par"
# for index in [12, 18, 19, 23, 26, 33, 36, 41, 43, 54, 60, 67, 68, 70, 73, 75, 89, 93]: #
for index in range(59, 60):
str_index = str(index)
# workingDir = dataDir + prefix + "/" + prefix + "_" + str_index + "/"
# workingFile = workingDir + prefix + "_" + str_index
# if not os.path.exists(workingDir):
# os.mkdir(workingDir)
# shutil.copy(base_bssc_infile, workingFile + ".par")
prefix_index = prefix + "_" + str_index
resultDir = workingDir + prefix_index + "/"
# if not os.path.exists(resultDir):
# os.mkdir(resultDir)
workingDir = "/home/sw167/Postdoc/Project_Srp/simulateData/H7/H7_" + str_index + "/"
workingFile = workingDir + "H7_" + str_index + "_Srp_fullHaplotype"
srp_file = workingDir + "H7_" + str_index + "_Srp50x.fasta"
# srp_tree_file = resultDir + prefix_index + "_Srp.tree"
# srp_hap_file = resultDir + prefix_index + "_Srp_fullHaplotype.fasta"
# print index, resultDir
debug = 1
# runBSSC(workingFile, srp_tree_file, debug)
# runSeqGen(workingFile, srp_hap_file, srp_tree_file, debug)
input_handle = open(workingFile + ".fasta", "rU")
sequences = AlignIO.read(input_handle, "fasta")
input_handle.close()
seq = sequences[0]
ref_handle = open(workingFile + ".cons", "w")
ref_handle.write(">%s\n%s\n" % ("Ref", seq.seq))
ref_handle.close()
runMetaSim(workingFile, workingDir, debug)
runShrimp(workingFile, srp_file, debug)
if __name__ == '__main__':
main()
# mainFromAlignment()
# workingFile = "/home/sw167/Postdoc/Project_Srp/test/H1_100bp"
# srp_file = workingFile + "out.fasta"
# runShrimp(workingFile, srp_file, 1);