-
Notifications
You must be signed in to change notification settings - Fork 7
/
4-FinalAssembly
executable file
·386 lines (328 loc) · 9.26 KB
/
4-FinalAssembly
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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;
use File::Basename;
#####################################################################################
# a script to clean up assemblies, which are redundant and make them less redundant #
# external dependencies: blat (v34), cap3, cd-hit-est #
# written by Sonal Singhal, sonal.singhal1 [at] gmail.com, 29 Dec 2011 #
# v2; updated on 13 May 2012; made file naming more robust so that multiple #
# instances can be run on the same machine #
# added command line flags by Ke Bi #
#####################################################################################
die (qq/
4-FinalAssembly.pl [options]
external dependencies: blat (v34), cap3, cd-hit-est
Options:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-a DIR Path to a folder with raw assemblies
-b INT Min length for a contig to be kept [200]
-c INT How much memory you can dedicate to
these external dependencies, in MB [4000]
-d FLOAT The difference level at which we can
start to try to cluster contigs [0.99]
-e FLOAT Two contigs need to overlap over e% of
their potential overlap in order for
us to even try to assemble them [0.95]
-f CHAR The particular assembly that you like to
process, if "all" is specified, each and
all assemblies will be processed [all]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\n\n/) unless (@ARGV);
my %opts = (a=>undef, b=>200, c=>4000, d=>0.99, e=>0.95, f=>"all");
getopts('a:b:c:d:e:f:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/ ){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my @files;
if ($opts{f} eq "all") {
@files = <$dir*>;
print "\n","OK! Now processing all assemblies!", "\n";
}
else {
@files = <$dir*$opts{f}*>;
print "\n","OK! Now processing assemblies $opts{f}!", "\n";
}
my $minLength = $opts{b};
#how much memory you can dedicate to these external dependencies, in MB
my $mem = $opts{c};
#the difference level at which we can start to try to cluster contigs
my $dist = $opts{d};
#two contigs need to overlap over $overCut% of their potential overlap in order for us to even try to assemble them
my $overCut = $opts{e};
###########################
# run the subroutines #
###########################
foreach my $assembly (@files) {
#my $lib =$1 if basename($assembly) =~m/\S+.fa/;
my $orig = $assembly . ".original";
my $call1 = system("cp $assembly $orig");
#first need to rename contigs
renameContigs($assembly);
#then need to cluster at 100%; also remove contigs shorter than $minLength
cluster100($assembly);
#then cluster at 99%, and use cluster info to run cap3
my $cl99a = cluster99($assembly);
#then take clustered contigs and do blat 1x at 99%
my $cl99b = clusterBlat($cl99a,'.cl99b');
$dist = $dist - 0.01;
#then take clustered contigs and do blat 2x at 98%
my $cl98a = clusterBlat($cl99b,'.cl98a');
my $cl98b = clusterBlat($cl98a,'.cl98b');
my $clustered = $cl98b . ".clustered";
cluster($cl98b,$clustered);
my $final = $assembly . ".final";
my $call2 = system("cp $clustered $final");
}
###########################
# behold the subroutines #
###########################
sub cluster {
my ($assembly,$out) = @_;
my $tmp = $assembly . '.tmp';
my $call1 = system("cd-hit-est -i $assembly -o $tmp -c 0.99 -l $minLength -M $mem -r 1 -B 1");
rename($tmp,$out);
my $call2 = system("rm $tmp" . "*");
}
sub clusterBlat {
my ($seq,$index) = @_;
my $clusters = $seq . '.blatSelCheck' . '.out' ;
my $call = system("blat $seq $seq $clusters -noHead -dots=100");
my (%seq, $id);
open(SEQ, "<$seq");
while(<SEQ>) {
chomp(my $line = $_);
if ($line =~ m/^>(\S+)/) {
$id = $1;
}
else {
$seq{$id} .= $line;
}
}
close(SEQ);
my (%clusters, %revClusters);
my $tracker = 1;
open(IN, "<$clusters");
while(<IN>){
chomp(my $line = $_);
my @d = split(/\t/,$line);
unless($d[9] eq $d[13]) {
my $overlap;
#calculate potential overlap
if ($d[11] < 0.05*$d[10]) {
#query is hanging off the right end of the hit
if ($d[15] + $d[10] > $d[14]) {
$overlap = $d[14] - $d[15];
}
#query is entirely within the hit
else {
$overlap = $d[10];
}
}
else {
#hit is entirely within the query
if ($d[11] + $d[15] < $d[10]) {
$overlap = $d[14];
}
#query is hanging off the left end of the hit
else {
$overlap = $d[10] - $d[11];
}
}
if (($d[0]+$d[1])/$overlap >= $overCut && $d[0]/($d[0] + $d[1]) >= $dist) {
#overlap is good enough that I am willing to try to assemble these...
#complicated note-taking because of weird data-structure i have created...
my ($query, $hit) = 0;
$query = $revClusters{$d[9]} if $revClusters{$d[9]};
$hit = $revClusters{$d[13]} if $revClusters{$d[13]};
if ($query && $hit) {
if ($query ne $hit) {
#need to combine these two clusters
foreach my $hit_id (keys %{$clusters{$hit}}) {
$clusters{$query}{$hit_id}++;
$revClusters{$hit_id} = $query;
}
delete($clusters{$hit});
}
}
elsif ($query) {
$clusters{$query}{$d[13]}++;
$revClusters{$d[13]} = $query;
}
elsif ($hit) {
$clusters{$hit}{$d[9]}++;
$revClusters{$d[9]} = $hit;
}
else {
$clusters{$tracker}{$d[9]}++; $clusters{$tracker}{$d[13]}++;
$revClusters{$d[9]} = $tracker; $revClusters{$d[13]} = $tracker;
$tracker++;
}
}
}
}
unlink($clusters);
#make cluster files here
my $out = $1 . $index if $seq =~ m/(.*)\.[a-z|0-9]+/i;
$tracker = 1;
open(OUT, ">$out");
my $temp = $seq . ".localAssembly.fa";
foreach my $cluster (keys %clusters) {
open(TEMP, ">$temp");
foreach my $seqid (keys %{$clusters{$cluster}}) {
print TEMP ">", $seqid, "\n", $seq{$seqid}, "\n";
delete($seq{$seqid});
}
close(TEMP);
my $call = system("cap3 " . $temp . " -z 1 -o 16 -e 11 -s 251");
my $assembled = $temp . ".cap.contigs";
my $singlets = $temp . ".cap.singlets";
open(SIN, "<$singlets");
while(<SIN>) {
chomp(my $line = $_);
if ($line =~ m/>/) {
print OUT ">contig", $tracker, "\n";
$tracker++;
}
else {
print OUT $line, "\n";
}
}
close(SIN);
open(CON, "<$assembled");
while(<CON>) {
chomp(my $line = $_);
if ($line =~ m/>/) {
print OUT ">contig", $tracker, "\n";
$tracker++;
}
else {
print OUT $line, "\n";
}
}
close(CON);
}
foreach (keys %seq) {
print OUT ">contig", $tracker, "\n", $seq{$_}, "\n";
$tracker++;
}
close(OUT);
my $call3 = system("rm $temp" . "*");
return($out);
}
sub cluster99 {
my ($assembly) = @_;
my $out = $assembly . '_99';
my $call1 = system("cd-hit-est -i $assembly -o $out -c 0.99 -M $mem -r 1 -d 30 -B 1");
my $clusters = $out . ".clstr";
#get all the cluster info
my %clusters; my $c;
open(IN, "<$clusters");
while(<IN>) {
chomp(my $line = $_);
if ($line =~ m/>(Clus.*)/) {
$c = $1; $c =~ s/\s//g;
}
else {
push(@{$clusters{$c}},$1) if $line =~ m/>([A-Z|0-9]+)/i;
}
}
close(IN);
my $call2 = system("rm $out" . "*");
#get the sequence info
my (%seq, $id);
open(SEQ, "<$assembly");
while(<SEQ>) {
chomp(my $line = $_);
if ($line =~ m/^>(\S+)/) {
$id = $1;
}
else {
$seq{$id} .= $line;
}
}
close(SEQ);
my $cl99 = $assembly . ".cl99a";
my $tracker = 1;
open(OUT, ">$cl99");
my $temp = $assembly . ".localAssembly.fa";
#now go through each cluster and do a "local assembly"
foreach my $c (keys %clusters) {
my @contigs = @{$clusters{$c}};
#loner
if (scalar(@contigs) == 1) {
print OUT ">contig", $tracker, "\n", $seq{$contigs[0]}, "\n";
$tracker++;
}
#more than one
else {
open(TMP, ">$temp");
foreach my $id (@contigs) {
print TMP ">", $id, "\n", $seq{$id}, "\n";
}
close(TMP);
#now need to assemble this...
my $call = system("cap3 " . $temp . " -z 1 -o 16 -e 11 -s 251");
my $assembled = $temp . ".cap.contigs";
my $singlets = $temp . ".cap.singlets";
open(SIN, "<$singlets");
while(<SIN>) {
chomp(my $line = $_);
if ($line =~ m/>/) {
print OUT ">contig", $tracker, "\n";
$tracker++;
}
else {
print OUT $line, "\n";
}
}
close(SIN);
open(CON, "<$assembled");
while(<CON>) {
chomp(my $line = $_);
if ($line =~ m/>/) {
print OUT ">contig", $tracker, "\n";
$tracker++;
}
else {
print OUT $line, "\n";
}
}
close(CON);
}
}
close(OUT);
my $call3 = system("rm $temp" . "*");
return($cl99);
}
sub cluster100 {
my ($assembly) = @_;
my $out = $assembly . '_100';
my $call1 = system("cd-hit-est -i $assembly -o $out -c 1.00 -l $minLength -M $mem -r 1 -B 1");
rename($out,$assembly);
my $call2 = system("rm $out" . "*");
}
sub renameContigs {
my ($assembly) = @_;
my $out = $assembly . '2';
open(IN, "<$assembly");
open(OUT, ">$out");
my $contig = 1;
while(<IN>) {
chomp(my $line = $_);
if ($line =~ m/>/) {
print OUT ">contig", $contig, "\n";
$contig++;
}
else {
print OUT $line, "\n";
}
}
close(IN); close(OUT);
rename($out,$assembly);
}