-
Notifications
You must be signed in to change notification settings - Fork 1
/
vsearch_msaout_global_align_muscle.v2.py
302 lines (216 loc) · 7.69 KB
/
vsearch_msaout_global_align_muscle.v2.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
# vsearch_msaout_global_align_muscle.v2.py
# a python script called by assembly_and_mapper_script.sh that re-alignes VSEARCH clusters with MUSCLE and exports a majority consensus sequence
# Copyright (C) 2017, ETH Zurich, Mathias Scharmann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# If you use this code please cite:
#
# "Scharmann M, Grafe TU, Metali F, Widmer A. (2017) Sex-determination
# and sex chromosomes are shared across the radiation of dioecious
# Nepenthes pitcher plants. XXX"
# AND/OR
# "Scharmann M, Metali F, Grafe TU, Widmer A. (2017) Divergence with
# gene flow among multiple sympatric carnivorous Nepenthes pitcher
# plants is linked to trap morphology. XXX"
#
# contact: mathias.scharmann[-at-]env.ethz.ch or msph52[-at-]gmail.com
# Python 2.7.6
# depends on vcftools, muscle!
"""
prepare input like this:
vsearch --fasta_width 0 --threads 10 --id 0.9 --cluster_fast uniq.fasta --msaout vsearch_experiment.txt
"""
import argparse
import os
import random
#import numpy
import math
import subprocess
import multiprocessing as mp
# diese Funktion checkt ob ein file existiert und stoppt das script wenn ein file nicht exisitiert
def extant_file(x):
"""
'Type' for argparse - checks that file exists but does not open.
"""
if not os.path.exists(x):
print "Error: {0} does not exist".format(x)
exit()
x = str(x)
return x
#######
def get_commandline_arguments ():
parser = argparse.ArgumentParser()
parser.add_argument("--vsearch_msa", required=True, type=extant_file,
help="a msaout file made with vsearch clustering", metavar="FILE")
args = parser.parse_args()
return args
def parse_rbdiv (vsearch_msa):
cluster_dict = {}
with open(vsearch_msa, "r") as INFILE:
clstr = 0
jump = False
for line in INFILE:
# print line
if len(line) > 1:
if jump:
continue
if line.startswith(">*"):
clstr += 1
same_clust = True
continue
if same_clust:
if line.startswith(">"):
continue
elif line.startswith(">consensus"):
jump = True
continue
else:
try:
cluster_dict[clstr].append(line.strip("\n"))
except KeyError:
cluster_dict[clstr] = [ line.strip("\n") ]
# print cluster_dict
return cluster_dict
def call_muscle (cluster_dict):
print "there are {0} clusters".format(len(cluster_dict.keys()))
aligned_clusters = {}
for cluster in sorted(cluster_dict.keys(), key=int):
if len(cluster_dict[cluster]) > 200:
print "discarded cluster with {0} sequences".format(len(cluster_dict[cluster]))
continue
print "\rrunning muscle on cluster \t", cluster,
with open("for_muscle.fasta", "w") as OUTFILE:
for idx, seq in enumerate(cluster_dict[cluster]):
OUTFILE.write(">"+str(idx)+"\n"+seq+"\n")
bash_command = "muscle -quiet -in for_muscle.fasta" # -quiet
p = subprocess.Popen(bash_command, shell=True, stdout=subprocess.PIPE) #, stderr=subprocess.STDOUT)
rec = ""
while True:
line = p.stdout.readline()
# print line,
if line == '' and p.poll() != None:
break
if line.startswith(">"):
new_rec = True
if len(rec) > 0:
# print len(rec)
try:
aligned_clusters[cluster].append(rec)
except KeyError:
aligned_clusters[cluster] = [rec]
rec = ""
continue
else:
new_rec = False
if not new_rec:
rec += line.strip("\n")
# print aligned_clusters[cluster]
return aligned_clusters
def make_cluster_consensus(cluster_dict):
# make consensus sequence out of individual sequences in the binned_dict:
consensus_dict = {}
for cluster, seqs in cluster_dict.items():
outseq = []
print "working on cluster {0}".format(cluster)
# print seqs[0]
if len(seqs) > 0:
for column_index in range(0, len(seqs[0])): # to loop over all columns
aligned_column = "".join([ seq[column_index] for seq in seqs ])
# print aligned_column
maxcount = 0
for nuc in ["A","T","G","C"]:
if aligned_column.count(nuc) > maxcount:
maxcount = aligned_column.count(nuc)
consensus = nuc
outseq.append(consensus)
outseqstring = "".join(outseq)
# print outseqstring
consensus_dict[cluster] = outseqstring
return consensus_dict
def write_fasta(fasta_dict):
outlines = [ ]
for contig in sorted(fasta_dict.keys(), key=int):
seq = fasta_dict[contig]
outlines.append( ">"+str(contig)+"_L"+str(len(seq)) )
outlines.append(seq)
with open("vsearch_muscle.fasta", "w") as OUTFILE:
OUTFILE.write("\n".join(outlines))
def MT_wrapper_muscle(cluster_dict, nthreads):
print "there are {0} clusters".format(len(cluster_dict.keys()))
results = {}
pool = mp.Pool(nthreads) #use all available cores, otherwise specify the number you want as an argument
for clust in sorted(cluster_dict.keys(), key=int):
if len(cluster_dict[clust]) > 100:
print "discarded cluster with {0} sequences".format(len(cluster_dict[clust]))
continue
else:
results[clust] = pool.apply_async(call_muscle_MT, args=(cluster_dict[clust], clust))
pool.close()
pool.join()
# Get process results from the output queue
#print output
aligned_clusters = {}
for idx, result in results.items():
# print idx, result
aligned_clusters[idx] = result.get()
# print result.get()
return aligned_clusters
def call_muscle_MT (seqs, clust):
print "\rrunning muscle on cluster \t", clust,
aligned_seqs = []
with open("for_muscle_{0}.fasta".format(clust), "w") as OUTFILE:
for idx, seq in enumerate(seqs):
OUTFILE.write(">"+str(idx)+"\n"+seq+"\n")
bash_command = "muscle -quiet -in for_muscle_{0}.fasta".format(clust) # -quiet
p = subprocess.Popen(bash_command, shell=True, stdout=subprocess.PIPE) #, stderr=subprocess.STDOUT)
rec = ""
while True:
line = p.stdout.readline()
# print line,
if line == '' and p.poll() != None:
break
if line.startswith(">"):
new_rec = True
if len(rec) > 0:
# print len(rec)
aligned_seqs.append(rec)
rec = ""
continue
else:
new_rec = False
if not new_rec:
rec += line.strip("\n")
# print aligned_clusters[cluster]
os.remove("for_muscle_{0}.fasta".format(clust))
return aligned_seqs
def randomise_cluster_ids(cluster_dict):
old_IDs = cluster_dict.keys()
new_IDs = random.sample(old_IDs, len(old_IDs))
out_dict = {}
for old, new in zip(old_IDs, new_IDs):
out_dict[new] = cluster_dict[old]
return out_dict
######### MAIN ########
args = get_commandline_arguments ()
cluster_dict = parse_rbdiv (args.vsearch_msa)
#print cluster_dict
# the following step prevents that clusters are sorted by coverage: if not randomising the ID, it is the order of clusters in the vsearch out format, which orders them by number of sequences in a cluster. When splitting the reference for freebayes, the first few chunks will contain much much more reads than the later ones and run >> longer. This uneveness is avoided if cluster IDs are randomised.
cluster_dict = randomise_cluster_ids(cluster_dict)
#aligned_clusters = call_muscle (cluster_dict)
nthreads = 30
aligned_clusters = MT_wrapper_muscle(cluster_dict, nthreads)
consensus_clusters = make_cluster_consensus(aligned_clusters)
write_fasta(consensus_clusters)
print "Done!"