forked from MVZSEQ/SCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembly_evaluation.pl
More file actions
1345 lines (1075 loc) · 36.4 KB
/
assembly_evaluation.pl
File metadata and controls
1345 lines (1075 loc) · 36.4 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use warnings;
use strict;
use File::Temp;
use POSIX;
use Getopt::Std;
use File::Basename;
use List::Util qw[min max];
# Modified based on Sonal Singhal's brilliant and famous *7evaluateAssembly.pl*.
# To use COVERAGE and FIX, you will have to generate alignment pileup output by SamTools first
# For questions please contact either Sonal Singhal (sonal.singhal1 [at] gmail.com) or Ke Bi (kebi@berkeley.edu)
&main;
exit;
sub main {
&usage if (@ARGV<1);
my $command = shift(@ARGV);
my %fun = (BASIC=>\&BASIC, COVERAGE=>\&COVERAGE, FIX=>\&FIX, CEGMA=>\&CEGMA, ORF=>\&ORF, CONTIGUITY=>\&CONTIGUITY, ANNOTATABLE=>\&ANNOTATABLE, ACCURACY=>\&ACCURACY, COMPARE=>\&COMPARE, READSUSED=>\&READSUSED);
die("Unknown command \"$command\"\n") if (!defined($fun{$command}));
&{$fun{$command}};
}
sub usage {
die(qq/
Usage: Assembly_evaluation.pl <command> [<arguments>]\n
Command:
ACCURACY: The percentage of the correctly assembled bases estimated using the set
of expressed reference transcripts
ANNOTATABLE: Calculates the percentage of the matches in reference. Also calculate
average percentage of matched bp and mismatches among the matched genes
BASIC: Reports basic stats: mean, median, total length, gc%, N50 etc. Also
generates a histogram of contig lengths for each assembly (bin=100)
CEGMA: Number of hits by mapping to a set of 458 core proteins that are present
in a wide range of taxa
COMPARE: Compares assemblies pairwisely to report % total aligned bp, % alignment
accuracy, % uniquely assembled contigs, % shared and % uniquely assembled
contigs hit reference protein database
CONTIGUITY: Calculates assembly contiguity (the percentage of expressed reference
transcripts covered by a single, longest-assembled contig) and completeness
(the percentage of expressed reference transcripts covered by all the
assembled transcripts)
COVERAGE: Calculates error rate, average quality score of the aliged bases and its
varinace\/std, and average base coverage. Need to do alignment first
FIX: Fixes assembly errors
ORF: The percentage of the tested contigs (bp) that contains open reading frames
READSUSED: Maps a subset of reads to the assembly to estimate % of reads used for making
these assemblies etc.
\n/);
}
# %master is not used in most functions. Keep it for comparison purpose.
####################################################################################################################################################
sub READSUSED {
die (qq/
Usage: Assembly_evaluation.pl READSUSED [options]
Options:
-a CHR directory with all assemblies
-b CHR directory with all libraries
-c INT how many reads do you want to use for the test? [100000]
Note: the naming for libraries and assemblies has to be consistent.
Library: ABC_1_final.txt, ABC_2_final.txt
Assembly: ABC.fasta
\n/) if !@ARGV;
my %opts = (a=>undef, b=>undef, c=>100000);
getopts('a:b:c:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my $read;
if ($opts{b} =~ m/\/$/){
$read = $opts{b};
}
else {
$read = $opts{b} . "/";
}
my @assemblies = <$dir*fasta>;
my @reads = <$read*_1_final.txt>;
my $randomReads = $opts{c};
my $out = $dir.'readsUsed.out';
open (OUT, ">", $out);
print OUT "Assemblies", "\t","numReadsMatched(%)", "\t","numPairsMatched(%)", "\t", "avgDist(bp)", "\t", "chimera(%)", "\n";
foreach my $file1 (@reads) {
my $lib = $1 if basename($file1) =~ m/(\S+)_1_final.txt/;
my $file2 = $read . $lib . '_2_final.txt';
my $nLines = $randomReads * 4;
my $reads1 = $read . $lib. "_testReads1.fastq";
my $reads2 = $read . $lib. "_testReads2.fastq";
system("head -n $nLines $file1 > $reads1");
system("head -n $nLines $file2 > $reads2");
foreach my $seq (@assemblies) {
my $id = $1 if basename($seq) =~ m/(\S+).fasta/;
if ($id eq $lib) {
print "processing assembly ",$lib, "." , "\n";
readsUsed($seq,$id,$reads1,$reads2, $randomReads);
}
}
}
close OUT;
sub readsUsed {
my ($seq,$id,$reads1,$reads2, $randomReads) = @_;
my %master;
my $call1 = system("bowtie-build $seq $id -q");
my $out = "bowtie.out";
my $call2 = system("bowtie $id -1 $reads1 -2 $reads2 -k 1 -5 5 -3 5 --un un.fastq > $out");
my $totDist;
my $matches;
open(OUT2, "<$out");
while(<OUT2>) {
$matches++;
my $line1 = $_;
my $line2 = <OUT2>;
my @d1 = split(/\s+/, $line1);
my @d2 = split(/\s+/, $line2);
$totDist += abs($d1[3] - $d2[3]);
}
close(OUT2);
$master{$id}{'numPairsMatched'} = $matches / $randomReads;
$master{$id}{'avgDist'} = $totDist/ $matches;
$master{$id}{'numReadsMatched'} = $matches*2;
my $un1 = 'un_1.fastq';
my $un2 = 'un_2.fastq';
my $un_out1 = 'un1.out';
my $un_out2 = 'un2.out';
my $call3 = system("bowtie $id $un1 -k 3 --best -5 5 -3 5 > $un_out1");
my $call4 = system("bowtie $id $un2 -k 3 --best -5 5 -3 5 > $un_out2");
my %r1; my %d;
open(IN, "<$un_out1");
while(<IN>) {
my @d = split(/\t/,$_);
my $id1 = $1 if $d[0] =~ m/(\S+)/;
$r1{$id1}{$d[2]}++;
$d{$d[0]}++;
}
close(IN);
my %r2;
open(IN, "<$un_out2");
while(<IN>) {
my @d = split(/\t/,$_);
my $id1 = $1 if $d[0] =~ m/(\S+)/;
$r2{$id1}{$d[2]}++;
$d{$d[0]}++;
}
close(IN);
my $chimera = 0;
foreach my $id (keys %r1) {
#potential chimera
my $match = 0;
if ($r2{$id}) {
foreach my $c (keys %{$r1{$id}}) {
$match++ if $r2{$id}{$c}
}
$chimera++ if $match == 0;
}
}
$master{$id}{'numReadsMatched'} = $master{$id}{'numReadsMatched'} + scalar(keys %d);
$master{$id}{'chimera'} = $chimera / ($master{$id}{'numReadsMatched'} / 2);
$master{$id}{'numReadsMatched'} = $master{$id}{'numReadsMatched'} / ( $randomReads * 2 );
my $call5 = system("rm $id"."*ebwt");
print OUT $id, "\t";
printf OUT "%.2f", $master{$id}{'numReadsMatched'}*100;
print OUT "\t";
printf OUT "%.2f", $master{$id}{'numPairsMatched'}*100;
print OUT "\t";
printf OUT "%.2f", $master{$id}{'avgDist'};
print OUT "\t";
printf OUT "%.2f", $master{$id}{'chimera'}*100;
print OUT "\n";
unlink($un1,$un2,$un_out1,$un_out2,$out);
}
}
####################################################################################################################################################
sub COMPARE {
die (qq/
Usage: Assembly_evaluation.pl COMPARE [options]
Options:
-a CHR directory with all assemblies
-b INT how many assembled contigs do you want to test [1000]
-c FILE protein reference
-d FLOAT evalue significance level when comparing two different assemblies [1e-100]
-e INT number of processors for blast search? [2]
-f FLOAT evalue significance level when comparing refernce protein [1e-20]
-g INT to see if the unique stuff is any good [100]
\n/) if !@ARGV;
my %opts = (a=>undef, b=>100, c=>undef, d=>1e-100, e=>2, f=>1e-20,g=>100);
getopts('a:b:c:d:e:f:g:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my $testSet = $opts{g};
my $prot_db = $opts{c};
my $evalue1 = $opts{d};
my $evalue2 = $opts{f};
my $proc = $opts{e};
my @assemblies = <$dir*fasta>;
my $contigs = $opts{b};
my $out = $dir.'compareAssemblies.out';
open (OUT, ">", $out);
print OUT "Assemblies", "\t","totAl(%)", "\t","accuracy(%)","\t","unique(%)", "\t","shared(%)","\t","legitUniq(%)", "\n";
foreach my $seq (@assemblies) {
my $id = $1 if basename($seq) =~ m/(\S+).fasta/;
my $random1 = bootstrap($seq,$contigs);
print "start processing assembly $id!", "\n";
foreach my $seq2 (@assemblies) {
if ($seq2 ne $seq) {
compare($id,$seq2,$seq,$random1,$evalue1,$prot_db, $proc, $evalue2,$contigs,$testSet);
}
}
}
close OUT;
#my $out2 = $dir.'compareAssemblies_final.out';
#open (IN1, "<", $out);
#open (OUT1, ">", $out2);
#my $header = <IN1>;
#print OUT1 "Assemblies", "\t","totAl(%)", "\t","accuracy(%)","\t","unique(%)", "\t","legitUniq(%)", "\n";
#foreach (<IN1>) {
# chomp (my @line1 = split /\t/, $_);
# my $lib1 = $1 if $line1[0] =~ m/(\S+)_vs_(\S+)/;
# my $lib2 = $2;
# my $next = $lib2 . "_vs_" . $lib1;
# print OUT1 join ("\t", @line1), "\n";
# open (IN2, "<", $out);
# foreach (<IN2>) {
# chomp (my @line2 = split /\t/, $_);
# if ($line2[0] eq $next ) {
# print OUT1 $next, "\t", "NA","\t", "NA", "\t", $line2[3], "\t", $line2[4], "\n";
# last;
# }
# }
#}
#close IN1;
#close IN2;
#close OUT1;
sub compare {
my ($id,$a1,$a2,$random,$evalue1,$prot_db, $proc, $evalue2,$contigs,$testSet) = @_;
my %master;
my %seq;
#$a1 = $seq2;
#$a2 = $seq;
open(IN, "<$a1");
my $c;
while(<IN>) {
chomp(my $line = $_);
if ($line =~ m/>(\S+)/) {
$c = $1;
}
else {
$seq{$c} .= $line;
}
}
close(IN);
my %rand;
my $temp1 = "randomSeq.fa";
open(OUT3, ">$temp1");
for (my $i = 0; $i < scalar(@{$random}); $i++) {
print OUT3 ">contig", $i, "\n", $random->[$i], "\n";
$rand{'contig' . $i} = $random->[$i];
}
close(OUT3);
my $db = $1 if basename($a1) =~ m/(\S+).fasta/i;
my $out = $id . $db . ".out";
my $blatCall = system("blat $a1 $temp1 $out -noHead -out=blast8");
open(IN, "<$out");
my %match;
while(<IN>) {
chomp(my $line = $_);
my @d = split(/\t/,$line);
#match already exists
if ($match{$d[0]}) {
#continuing the best part match
if ($d[1] eq $match{$d[0]}{'match'}) {
$match{$d[0]}{'error'} += ($d[4] + $d[5]);
$match{$d[0]}{'al'} += $d[3];
}
}
#either a new match or a contig for which there isn't a good match
else {
if ($d[10] < $evalue1) {
$match{$d[0]} = { 'match' => $d[1] , 'error' => ($d[4] + $d[5]), 'al' => $d[3] };
}
}
}
close(IN);
my $accuracy; my $totLength; my $totAl; my $numSeq = scalar( keys %{$seq{$a2}});
foreach my $c (keys %match) {
$accuracy += $match{$c}{'error'};
$totLength += $match{$c}{'al'};
#$totAl += ($match{$c}{'al'} * 2) / ( length($seq{$match{$c}{'match'}}) + length($rand{$c})); #$totAl is defined confusingly. Sonal, what you want to do here?
$totAl += ($match{$c}{'al'}) / (length($rand{$c}));
delete($rand{$c});
}
$totAl = $totAl / ( scalar(keys %match) );
$accuracy = 1 - ($accuracy / $totLength);
my $unique = ($contigs - scalar(keys %match)) / $contigs;
my $shared = scalar(keys %match) / $contigs;
my $legitUniq = 'NA';
unless (scalar(keys %rand) < 10) {
my $testUnique = 'testUnique.fa';
open(OUT2, ">$testUnique");
my @uniq = keys %rand;
for (my $i = 0; $i < $testSet; $i++) {
print OUT2 ">contig" . "$i" . "\n" . $rand{$uniq[int rand($#uniq)]} . "\n";
}
close(OUT2);
unless (-f $prot_db . ".pin") {
my $call = system("makeblastdb -in $prot_db -dbtype prot");
}
my @call = `blastall -p blastx -d $prot_db -i $testUnique -m 8 -a $proc -b 1 -e $evalue2 | cut -f 1 | uniq`;
$legitUniq = scalar(@call)/$testSet;
unlink($testUnique);
}
$master{$id}{$db . '.totAl'} = $totAl;
$master{$id}{$db . '.accuracy'} =$accuracy;
$master{$id}{$db . '.unique'} = $unique;
$master{$id}{$db . '.legitUniq'} = $legitUniq;
unlink($out); unlink($temp1);
print OUT $id,"_vs_",$db, "\t";
printf OUT "%.2f",$totAl*100;
print OUT "\t";
printf OUT "%.2f",$accuracy*100;
print OUT "\t";
printf OUT "%.2f",$unique*100;
print OUT "\t";
printf OUT "%.2f",$shared*100;
print OUT "\t";
printf OUT "%.2f",$legitUniq*100;
print OUT "\n";
}
}
####################################################################################################################################################
sub ACCURACY {
die (qq/
Usage: Assembly_evaluation.pl ACCURACY [options]
Options:
-a CHR directory with all assemblies
-b INT how many assembled contigs do you want to test [1000]
-c FILE protein reference
-d FLOAT evalue significance level when comparing to reference cDNA [1e-20]
-e INT number of processors for blast search? [4]
\n/) if !@ARGV;
my %opts = (a=>undef, b=>100, c=>undef, d=>1e-20, e=>2);
getopts('a:b:c:d:e:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my $prot_db = $opts{c};
my $evalue2 = $opts{d};
my $proc = $opts{e};
my @assemblies = <$dir*fasta>;
my $testSet = $opts{b};
my $out = $dir.'accuracy.out';
open (OUT, ">", $out);
print OUT "Assemblies", "\t","stop codon(%)", "\t","gaps(%)", "\n";
foreach my $seq (@assemblies) {
my $id = $1 if basename($seq) =~ m/(\S+).fasta/;
my $random2 = bootstrap($seq,$testSet);
print "start processing assembly $id!", "\n";
acc($id,$random2,$prot_db, $evalue2, $proc);
}
close OUT;
sub acc {
my ($id,$random, $prot_db, $evalue2, $proc) = @_;
my %master;
my $temp1 = 'query.fa';
open(OUT2, ">$temp1");
for (my $i = 0; $i < scalar(@{$random}); $i++) {
print OUT2 ">contig", $i, "\n", $random->[$i], "\n";
}
close(OUT2);
unless (-f $prot_db . ".pin") {
my $call = system("makeblastdb -in $prot_db -dbtype prot");
}
my @call = `blastall -p blastx -d $prot_db -i $temp1 -m 8 -a $proc -b 1 -e $evalue2`;
my %a; my $gap = 0; my $stop = 0;
foreach my $line (@call) {
my @d = split(/\t/,$line);
unless($a{$d[0]}) {
$a{$d[0]}++; my $subseq;
if ($d[6] < $d[7]) {
my $start = $d[6] - 1;
my $length = $d[7] - $d[6] + 1;
$subseq = substr $random->[$1], $start, $length if $d[0] =~ m/contig(\S+)/;
}
else {
my $start = $d[7] - 1;
my $length = $d[6] - $d[7] + 1;
$subseq = substr $random->[$1], $start, $length if $d[0] =~ m/contig(\S+)/;
$subseq = reverse($subseq);
$subseq =~ tr/atgcATGC/tacgTACG/;
}
#check to see if there looks to be a gap
$gap++ if length($subseq) % 3;
#check to see if there is a nonsense mutation
my $stop1 = (translate($subseq) =~ tr/\*/\*/);
$stop++ unless($stop1 == 0);
}
}
$master{$id}{'nonsenseMu'} = $stop/scalar(keys %a);
$master{$id}{'gapMu'} = $gap/scalar(keys %a);
unlink($temp1);
print OUT $id, "\t";
printf OUT "%.3f", $master{$id}{'nonsenseMu'}*100;
print OUT "\t";
printf OUT "%.3f", $master{$id}{'gapMu'}*100;
print OUT "\n";
}
}
####################################################################################################################################################
sub ANNOTATABLE {
die (qq/
Usage: Assembly_evaluation.pl ANNOTATABLE [options]
Options:
-a CHR directory with all assemblies
-b INT how many cDNA sequences do you want to test [1000]
-c FILE cDNA reference
-d FLOAT evalue significance level when comparing to reference cDNA [1e-20]
\n/) if !@ARGV;
my %opts = (a=>undef, b=>1000, c=>undef, d=>1e-20);
getopts('a:b:c:d:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my $cdna_db = $opts{c};
my $evalue2 = $opts{d};
my @assemblies = <$dir*fasta>;
my $contigs = $opts{b};
my $out = $dir.'annotatable.out';
open (OUT, ">", $out);
print OUT "Assemblies", "\t","total matches(%)", "\t","matched bases(%)", "\t", "avg accuracy(%)", "\n";
foreach my $seq (@assemblies) {
my $id = $1 if basename($seq) =~ m/(\S+).fasta/;
my $random1 = bootstrap($seq,$contigs);
print "start processing assembly $id!", "\n";
anno($id, $random1, $cdna_db, $evalue2);
}
close OUT;
sub anno {
my ($id, $random, $cdna_db, $evalue2) = @_;
my %master;
my $temp1 = 'query.fa';
my $temp2 = 'blatout';
open(OUT2, ">$temp1");
for (my $i = 0; $i < scalar(@{$random}); $i++) {
print OUT2 ">contig", $i, "\n", $random->[$i], "\n";
}
close(OUT2);
my $call = system("blat $cdna_db $temp1 $temp2 -noHead -t=dnax -q=dnax -out=blast8");
my %a; my $matches;
open(IN, "<$temp2");
while(<IN>){
chomp(my $line = $_);
my @d = split(/\t/, $line);
unless($a{$d[0]}) {
if ($d[10] < $evalue2) {
#it is a match
#how long is the match
my $length = length($random->[$1]) if $d[0] =~ m/contig(\d+)/;
$a{$d[0]}{'length'} = $d[3] / $length;
#how accurate is the match
$a{$d[0]}{'accuracy'} = $d[2];
#how many matches do we have
$matches++;
}
}
}
my $length; my $accuracy;
foreach my $c (keys %a) {
$length += $a{$c}{'length'};
$accuracy += $a{$c}{'accuracy'};
}
$length = $length / $matches;
$accuracy = $accuracy / $matches;
$master{$id}{'anno'} = $matches/scalar(@{$random});
$master{$id}{'lengthAnno'} = $length;
$master{$id}{'accuAnno'} = $accuracy;
unlink($temp1); unlink($temp2);
print OUT $id, "\t", $master{$id}{'anno'}*100, "\t", $master{$id}{'lengthAnno'}*100, "\t", $master{$id}{'accuAnno'} = $accuracy, "\n";
}
}
####################################################################################################################################################
sub CONTIGUITY {
die (qq/
Usage: Assembly_evaluation.pl CONTIGUITY [options]
Options:
-a CHR directory with all assemblies
-b INT how many cDNA sequences do you want to test [1000]
-c FILE cDNA reference
-d FLOAT evalue significance level when comparing to reference cDNA [1e-20]
\n/) if !@ARGV;
my %opts = (a=>undef, b=>1000, c=>undef, d=>1e-20);
getopts('a:b:c:d:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my $cdna_db = $opts{c};
my $evalue2 = $opts{d};
my @assemblies = <$dir*fasta>;
my $contigs = $opts{b};
my $out = $dir.'Contiguity.out';
open (OUT, ">", $out);
print OUT "Assemblies", "\t","complete(%)", "\t","contiguity(%)", "\n";
foreach my $seq (@assemblies) {
my $id = $1 if basename($seq) =~ m/(\S+).fasta/;
my $random1 = bootstrap($cdna_db,$contigs);
print "start processing assembly $id!", "\n";
con($seq, $id, $random1, $evalue2);
}
close OUT;
sub con {
my ($seq, $id, $random, $evalue2) = @_;
my %master;
my $temp1 = 'query.fa';
my $temp2 = 'blatout';
my %seq;
open(OUT2, ">$temp1");
for (my $i = 0; $i < scalar(@{$random}); $i++) {
print OUT2 ">cdna", $i, "\n", $random->[$i], "\n";
$seq{'cdna' . $i}{'length'} = length($random->[$i]);
}
close(OUT2);
my %contiguity; my %complete;
my $call = system("blat $seq $temp1 $temp2 -noHead -out=blast8");
open(IN, $temp2);
while(<IN>) {
my @d = split(/\t/,$_);
if ($d[10] < $evalue2) {
my $min = (sort { $a <=> $b } ($d[6], $d[7]))[0];
my $max = (sort { $a <=> $b } ($d[6], $d[7]))[1];
unless ($contiguity{$d[0]}) {
for (my $i = $min; $i <= $max; $i++) {
$contiguity{$d[0]}{$i}++;
}
}
for (my $i = $min; $i <= $max; $i++) {
$complete{$d[0]}{$i}++;
}
}
}
close IN;
my $complete; my $contiguity;
foreach my $c (%seq) {
if ($complete{$c}) {
$complete += scalar( keys %{$complete{$c}} ) / $seq{$c}{'length'};
$contiguity += scalar( keys %{$contiguity{$c}} ) / $seq{$c}{'length'};
}
}
$complete = $complete/scalar(keys %complete);
$contiguity = $contiguity/scalar(keys %complete);
$master{$id}{'complete'} = $complete*100;
$master{$id}{'contiguity'} = $contiguity*100;
print OUT $id, "\t", $master{$id}{'complete'}, "\t", $master{$id}{'contiguity'}, "\n";
unlink($temp1); unlink($temp2);
}
}
####################################################################################################################################################
sub CEGMA {
die (qq/
Usage: Assembly_evaluation.pl CEGMA [options]
Options:
-a CHR directory with all assemblies
-b FILE location of cegma file
-c FLOAT blast search evalue [1e-20]
\n/) if !@ARGV;
my %opts = (a=>undef,b=>undef, c=>1e-20);
getopts('a:b:c:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/ ){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my @assemblies = <$dir*fasta>;
my $final = $dir. 'cegma.blast.out';
open (OUT, ">", $final);
print OUT "Assemblies", "\t", "cegma hit", "\n";
foreach my $seq (@assemblies) {
my $id = $1 if (basename($seq) =~ m/(\S+).fasta/);
my $out = $seq . '.cegma.blast.out';
my $call1 = system("makeblastdb -in $seq -dbtype nucl");
my $call2 = system("blastall -p tblastn -d $seq -i $opts{b} -e $opts{c} -m 8 -o $out -b 1");
my @call3 = `cut -f 1 $out | uniq | wc`;
my $count = $1 if $call3[0] =~ m/^\s+(\d+)/;
print OUT $id, "\t", $count,"\n";
unlink ($out); my $call4 = system("rm $seq" . ".n*");
}
close OUT;
}
####################################################################################################################################################
sub ORF {
die (qq/
Usage: Assembly_evaluation.pl ORF [options]
Options:
-a CHR directory with all assemblies
-b INT how many contigs do you want to test [1000]
\n/) if !@ARGV;
my %opts = (a=>undef, b=>1000);
getopts('a:b:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my @assemblies = <$dir*fasta>;
my $contigs = $opts{b};
my $out = $dir.'ORFfinder.out';
open (OUT, ">", $out);
print OUT "Assemblies", "\t","ORF(%)", "\n";
foreach my $seq (@assemblies) {
my $id = $1 if basename($seq) =~ m/(\S+).fasta/;
my $random1 = bootstrap($seq,$contigs);
print "start processing assembly $id!", "\n";
ORFfinder($id,$random1,$contigs);
}
close OUT;
sub ORFfinder {
my ($id,$array_ref,$contigs) = @_;
my %master;
$master{$id}{'orf'} = '0';
foreach (@$array_ref) {
my $seq = $_;
my @longest;
my $revcomp = reverse($seq);
$revcomp =~ tr/ATGCatgc/GACTgact/;
my %seq = ('1' => $seq, '2' => substr($seq,1), '3' => substr($seq,2), '-1' => $revcomp, '-2' => substr($revcomp,1), '-3' => substr($revcomp,2));
foreach(keys %seq) {
my @orfs = translate($seq{$_}) =~ m/([A-Z]+)/g;
@orfs = sort{length($a) <=> length($b)} @orfs;
push(@longest, length($orfs[-1]));
}
@longest = sort{$a <=> $b} @longest;
$master{$id}{'orf'} += $longest[-1]*3/length($seq);
}
$master{$id}{'orf'} = sprintf("%.3f", $master{$id}{'orf'}/$contigs);
print OUT $id, "\t", $master{$id}{'orf'}, "\n";
}
sub translate {
my ($string) = @_;
$string = uc($string);
my @codons = $string =~ m/(\S\S\S)/g;
my %codons = ( 'ATG'=>'M','ACG'=>'T','CTG'=>'L','CCG'=>'P','GTG'=>'V','GCG'=>'A','TTG'=>'L','TCG'=>'S',
'ATA'=>'I','ACA'=>'T','CTA'=>'L','CCA'=>'P','GTA'=>'V','GCA'=>'A','TTA'=>'L','TCA'=>'S',
'ATC'=>'I','ACC'=>'T','CTC'=>'L','CCC'=>'P','GTC'=>'V','GCC'=>'A','TTC'=>'F','TCC'=>'S',
'ATT'=>'I','ACT'=>'T','CTT'=>'L','CCT'=>'P','GTT'=>'V','GCT'=>'A','TTT'=>'F','TCT'=>'S',
'AGG'=>'R','AAG'=>'K','CGG'=>'R','CAG'=>'Q','GGG'=>'G','GAG'=>'E','TGG'=>'W','TAG'=>'*',
'AGA'=>'R','AAA'=>'K','CGA'=>'R','CAA'=>'Q','GGA'=>'G','GAA'=>'E','TGA'=>'*','TAA'=>'*',
'AGC'=>'S','AAC'=>'N','CGC'=>'R','CAC'=>'H','GGC'=>'G','GAC'=>'D','TGC'=>'C','TAC'=>'Y',
'AGT'=>'S','AAT'=>'N','CGT'=>'R','CAT'=>'H','GGT'=>'G','GAT'=>'D','TGT'=>'C','TAT'=>'Y');
my $translate;
foreach(@codons) {
if ($codons{$_}) {
$translate = $translate . $codons{$_};
}
else {
$translate = $translate . 'X';
}
}
return($translate);
}
sub bootstrap {
my ($seq,$num) = @_;
my @seq;
my $c = -1;
open(SEQ, "<$seq");
while(<SEQ>) {
chomp(my $line = $_);
if ($line =~ m/^>(\S+)/) {
$c++;
}
else {
$seq[$c] .= $line;
}
}
close(SEQ);
my @random;
for (my $i = 0; $i < $num; $i++) {
$random[$i] = $seq[int($#seq*rand())];
}
return \@random;
}
}
####################################################################################################################################################
sub BASIC {
die (qq/
Usage: Assembly_evaluation.pl BASIC [options]
Options:
-a CHAR directory with all assemblies
\n/) if !@ARGV;
my %opts = (a=>undef);
getopts('a:', \%opts);
my $dir;
if ($opts{a} =~ m/\/$/ ){
$dir = $opts{a};
}
else {
$dir = $opts{a} . "/";
}
my @assemblies = <$dir*fasta>;
my $out = $dir . "basic_evaluation.out";
open (OUT, ">", $out);
print OUT "Assembly","\t", "TotalLength","\t", "TotalContigs","\t", "Mean","\t", "Median", "\t", "Max","\t", ">2000bp","\t", ">1000bp","\t", ">500bp","\t", "N25", "\t","N50", "\t", "N90", "\t", "GC%", "\n";
foreach my $assembly (@assemblies) {
my %master;
my %seq;
print "Processing","\t", basename($assembly), "\n";
open (IN,"<", $assembly);
my $id =$1, if basename($assembly) =~ m/(\S+).fasta/;
my $histogram = $dir . $id . ".hist";
my $con;
while (<IN>) {
chomp(my $line = $_);
if ($line =~ m/^>(\S+)/) {
$con = $1;
}
else {
$seq{$con} .= $line;
}
}
close IN;
open (HIST, ">", $histogram);
#codes from Joseph Fass (UC Davis), starts here
my $int;
my %len;
foreach my $id (keys %seq) {
$int = floor(length($seq{$id})/100);
if( !defined($len{$int}) ) {
$len{$int} = 1;
} else {
$len{$int}++;
}
}
my @ints = sort { $a <=> $b } keys(%len);
for(my $i=$ints[0]; $i <= $ints[-1]; $i++) {
$len{$i} = 0 if(!defined($len{$i}));
printf HIST "%d:%d \t$len{$i}\n", ( ($i*100), ($i*100+99) );
}
close HIST;
#ends here
my @length;
foreach my $c (sort {$a cmp $b} keys %seq) {
push(@length, length($seq{$c}));
$master{$id}{'totLength'} += length($seq{$c});
$master{$id}{'GC'} += ($seq{$c} =~ tr/gGcC/gGcC/);
}
@length = sort {$a <=> $b} (@length);
$master{$id}{'mean'} = int($master{$id}{'totLength'}/scalar(@length));
$master{$id}{'median'} = $length[int($#length/2)];
$master{$id}{'totContigs'} = scalar(@length);
$master{$id}{'max'} = $length[$#length];
my $track;
$master{$id}{'n2000'} = 0;
$master{$id}{'n1000'} = 0;
$master{$id}{'n500'} = 0;
for (my $i = 0; $i < scalar(@length); $i++) {
$track += $length[$i];
if ($length[$i] > 2000) {
$master{$id}{'n2000'}++;
$master{$id}{'n1000'}++;
$master{$id}{'n500'}++;
}
elsif ($length[$i] > 1000) {
$master{$id}{'n1000'}++;
$master{$id}{'n500'}++;
}
elsif ($length[$i] > 500) {
$master{$id}{'n500'}++;
}
if ($track/$master{$id}{'totLength'} > 0.1) {
$master{$id}{'n90'} = $length[$i] unless $master{$id}{'n90'};
}
if ($track/$master{$id}{'totLength'} > 0.5) {
$master{$id}{'n50'} = $length[$i] unless $master{$id}{'n50'};
}
if ($track/$master{$id}{'totLength'} > 0.75) {
$master{$id}{'n25'} = $length[$i] unless $master{$id}{'n25'};
}
}
print OUT $id,"\t", $master{$id}{'totLength'},"\t", $master{$id}{'totContigs'},"\t", $master{$id}{'mean'},"\t", $master{$id}{'median'}, "\t", $master{$id}{'max'},"\t", $master{$id}{'n2000'},"\t", $master{$id}{'n1000'},"\t", $master{$id}{'n500'},"\t", $master{$id}{'n25'},"\t", $master{$id}{'n50'},"\t", $master{$id}{'n90'},"\t";