-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBidirectionalBLAST.pl
969 lines (618 loc) · 33.5 KB
/
BidirectionalBLAST.pl
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
#!usr/bin/perl -w
#Written by: Wheaton Schroeder
#READ ME!
#In order to perform a bi-directional blast to determine if EC numbers are present in the Exophiala dermatitidis genome.
#Using KEGG, EC numbers are translated to an AA sequence for an organism closely related to Exophiala dermatitidis (uses
#closest available organism from a defined heirarch). Then this AA sequence is blasted against the E. dermatitidis genome
#to determine if that enzyme is present in E. dermatitidis. Then, the blast direction is reversed to confirm the match.
#there are identical identity and e-value cutoffs in both directions. Technically storing protein gene accession numbers
#because not all E. dermatitidis genes have a name. This program does require the installation of the LWP package for perl
#does require a file structure in the directory in which the code is contained:
#must have folder "BackwardBLASTs"
#must have folder "blastResults"
#must have folder "FASTAs" with subfolder "Target"
#must edit "require" line for correct filepath for common_functions.pl
#must have file "BlastSpecs.txt", check file already given, formatting and line order is important!
# the first line is always the scientific name of the target organism
# the second line is always your filepath to where this code resides, and where the appropriate file structure is
# the third line is always a header and is essentially ignored
# the fourth line onward is the KEGG organism code [space] scientific name of that organism, the order here denotes
# the prefered order to check for those as the related organism, so usually the closest organism phylogenetically
# is at line 4, the second closest at line 5, and so on and so forth
#Latest version: 10/23/19
#make a requirement LWP and the common_functions library
use LWP::UserAgent; #imports package for web-based queries
use strict;
require "common_functions.pl"; #require our script of useful functions
my $userAgent = LWP::UserAgent->new; #create a user agent for the search
#organism which to direct all blast queries against, defined in BlastInput
my $queryOrg = " ";
#blast cutoffs, default, will be redefined by BlastSpecs.txt file
my $eCutOff = 1;
my $perPosSubCutOff = 0;
#stores home folder filepath, defined in BlastInput
my $filepath = " ";
#read the EC list
if (! open ECLIST, "<EClist_1.txt") {
die "could not open/read EClist_1.txt, reason $!";
}
#have a dedicated abbreviated log
open(ABBLOG, ">AbbreviatedLog_1.log") or die "could not open AbbreviatedLog.log, reason $!";
#have an error log
open(ERRORLOG, ">error_1.log") or die "could not open error.log, reason $!";
#have a dedicated full log
open(FULLLOG, ">FullLog_1.log") or die "could not open FullLog.log, reason $!";
select ABBLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Start time: %s\n", ×tamp;
select ERRORLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Start time: %s\n", ×tamp;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Start time: %s\n", ×tamp;
select STDOUT;
#output file for all matches which meet the cutoff
open(ECBLASTOUT, ">ECBlastInfo_1.csv") or die "could not create/write ECBlastInfo.csv, reason $!";
#chomp and store the EC list, will need to remove the comma later, if present
chomp(my @EC = <ECLIST>);
#stores hash of EC to matched genes in target, each match seperated by a comma
my %ECtoMatches = ( );
#creates a hash to get AA sequence from accession
my %accToAAseq = ( );
#creates a hash to get name from accession
my %accToName = ( );
#creates hash get organism from accession
my %accToOrg = ( );
#hash to store EC number to match numbers, forward
my %AcctoMatchNum = ( );
#hash to store EC number to match numbers, backward
#match number counter forward
my $matchNum = 0;
#array to hold blast information of good matches. Element format:
#organism used,acession used,acession matched,protein name,e-value,%ID
my @goodMatchInfo = ( );
#stores just the target organism accessions
my @matchAccession = ( );
#stores backwards match information, should be same size as @goodMatchInfo
my @backMatchInfo = ( );
#allows use of files accross the program, will be redefined frequently
my $file = " ";
my $file2 = " ";
my $file3 = " ";
my $file4 = " ";
#stores organism name that gene that was found
my $orgName = " ";
#stores match accession that was found
my $matchAcc = " ";
#stores related organims accession that was found,this is the protein accession, the hash is for the uniprot id
#(because for some reason the uniprot ID is occassionally given in blast reports).
my $NCBIID = " ";
my %NCBItoUniProt = ( );
#stores lines to write in an array, first element is header
my @writeLines = ("EC,Related Organism Used,Relate Protein Name,Related Accession,Related Length,Match Protein Name,Match Accession,Match Length,Forward e-val,Forward %ID, Forward %+ subs, Forward % gaps,Backward e-val,Backward %ID,Backward %+ subs,Backward % gaps,Weigted sum");
select ECBLASTOUT;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "%s\n", $writeLines[$#writeLines];
select STDOUT;
#hash with keys "$EC[$i].$matchAcc", and element is the writeLines for that match
my %ECandMatchtoLine = ( );
#stores a numerical counter to create a unique filename for files which are unable to be opened/edited
my $errFiles = 0;
#stores current value of sum used to determine significance of a match
my $sum = 0;
#create a hash to store organism code to scientific name of related organism. Note, that this does not store in a fixed
#order, so an array must also be created to give order preference to these species. This is crafted using the KEGG organism
#catalog and selecting all fungi in the same family first, then using "The Ascomycota Tree of Life: A Phylum-Wide Phylogeny
#Clarifies the Origin and Evolution of Fundamental Reproductive and Ecological Traits" by Schoch et. al. 2009 selected to
#order in which to include the orders (each order in same order as KEGG)
#this hash is necessary for the backwards blast
#so far just ascomycetes
my %prefOrgtoName = ( );
#array to keep order of preferred species
my @prefOrg = ( );
#write the prefOrg and prefOrgtoName array and hash respectively from the input file
open(BLASTIN, "<BlastSpecs.txt") or die "could not open/read BlastSpecs.txt, necessary file for calibrating this program for the target organism, reason: $!";
chomp(my @specs = <BLASTIN>);
#for each line of input file
for (my $u = 0; $u <= $#specs; $u++) {
#if first line, has target organism
if ($u == 0) {
#remove "Target: " in front
$specs[$u] =~ s/^Target:\s//;
#name query organism
$queryOrg = $specs[$u];
#go to next line
next;
} elsif ($u == 1) { #if on second line, has filepath
#remove the "Filepath: " in front
$specs[$u] =~ s/^Filepath:\s//;
#define the filepath
$filepath = $specs[$u];
#go to next line
next;
} elsif ($u == 2) { #if on the Expect cut-off line
my ($label, $cutOff) = split /:/, $specs[$u];
$eCutOff = $cutOff;
printf "Expect Cut-Off: %s\n", $eCutOff;
select FULLLOG;
printf "Expect Cut-Off: %s\n", $eCutOff;
select STDOUT;
next;
} elsif ($u == 3) { #if on the percent positive substitution cut-off line
my ($label, $cutOff) = split /:/, $specs[$u];
$perPosSubCutOff = &percenttoDecimal($cutOff);
printf "Percent Positive Substitution Cut-Off: %s\n", $perPosSubCutOff;
select FULLLOG;
printf "Percent Positive Substitution Cut-Off: %s\n", $perPosSubCutOff;
select STDOUT;
next;
} elsif ($u == 4) { #if on third line, has header for related species in order
#don't do anything with this line
next;
}
#now we are in the part where the related organism codes and scientific names are given
my ($orgCode, $genusName, $speciesName) = split /\s/, $specs[$u];
#add organism code to end of prefOrg array
push @prefOrg, $orgCode;
#define hash element to go from code to scientific name
$prefOrgtoName{$orgCode} = $genusName." ".$speciesName;
}
for (my $i = 0; $i <= $#EC; $i++) {
select ABBLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Begin EC %s at %s----------------------------------------------------------------------\n", $EC[$i], ×tamp;
select STDOUT;
$EC[$i] =~ s/,//g;
printf "\n\n%s-------------------------------------------------------------------------------------\n",$EC[$i];
printf "Searching %s genome for probable %s genes\n", $queryOrg, $EC[$i];
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "\n\n%s-------------------------------------------------------------------------------------\n",$EC[$i];
printf "Searching %s genome for probable %s genes\n", $queryOrg, $EC[$i];
select STDOUT;
#go to the EC page, get its conent
my $ECurl = "http://rest.kegg.jp/get/".$EC[$i];
my $ECpage = $userAgent->get($ECurl);
$ECpage = $ECpage->content;
#split page by \n character, so that each line is a new element in the array, allows us to split a line by \s into an array later
my @ECpage = split /\n/, $ECpage;
#need to get protein name
my $protName = "";
if ($ECpage =~ /NAME\s+(.+)/) {
$protName = $1;
$protName =~ s/;$//g; #remove trailing semi-colon if present
$protName =~ s/,/;/g; #replace all commas with semi-colons for now, to avoid errors
} #should not have a protein without a name
#for each organism, in the order of preference, determine if that EC corresponds to a gene in that organism
for (my $y = 0; $y <= $#prefOrg; $y++) {
printf "\nUsing Organism: %s\n",$prefOrgtoName{$prefOrg[$y]};
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "\nUsing Organism: %s\n",$prefOrgtoName{$prefOrg[$y]};
select STDOUT;
#for each line in the EC page, check if that organism code is there with associated genes
for (my $t = 0; $t <= $#ECpage; $t++) {
#search page for proper organism code
if ($ECpage[$t] =~ /\s+$prefOrg[$y]:\s/) {
#remove "GENES" if the organism is the first gene listed
$ECpage[$t] =~ s/^GENES//ig;
#remove excess spaces
$ECpage[$t] =~ s/^\s+//;
$ECpage[$t] =~ s/\s+$//;
printf "Found gene for related organism %s:%s\n", $prefOrg[$y], $prefOrgtoName{$prefOrg[$y]};
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Found gene for related organism %s:%s\n", $prefOrg[$y], $prefOrgtoName{$prefOrg[$y]};
select STDOUT;
#then @prefOrg[$y] has a gene for EC[$i], get the list of genes by splitting into an array for each gene
my @gene = split /\s/, $ECpage[$t];
shift @gene; #remove first element of gene, which is the organism code
#for each gene, get the amino acid sequence
for (my $e = 0; $e <= $#gene; $e++) {
#remove the gene name from behind the gene code
$gene[$e] =~ s/\(.+\)//ig;
printf "%s-------------------------------------------------------------------------------------\n",$gene[$e];
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "%s-------------------------------------------------------------------------------------\n",$gene[$e];
select STDOUT;
#get connected to the gene page
my $AAurl = "http://rest.kegg.jp/get/".lc($prefOrg[$y]).":".$gene[$e];
my $AApage = $userAgent->get($AAurl);
$AApage = $AApage->content;
########################################################################################################################################
#GET THE AMINO ACID SEQUENCE!
#do this by matching all text between AASEQ and NTSEQ, capturing all intevening alphabetical characters
if ($AApage =~ /AASEQ\s+[0-9]+\n\s+(([A-Z]+\s+)+)NTSEQ/) {
#so we have an AA sequence, build the fasta
my $AAseq = $1;
#remove spaces from AAsequence, leave newlines
$AAseq =~ s/\s//ig;
#need to grab NCBI protein ID
$NCBIID = "";
if ($AApage =~ /NCBI-ProteinID:\s+(\w+)/) {
$NCBIID = $1;
}
if ($AApage =~ /UniProt:\s+(\w+?)(\s|$)/) {
$NCBItoUniProt{$NCBIID} = $1;
} else { #if it doesn't have a UniProt ID
$NCBItoUniProt{$NCBIID} = $NCBIID;
}
printf "Building FASTA report for gene %s (Accession: %s)\n", $gene[$e], $NCBIID;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Building FASTA report for gene %s (Accession: %s)\n", $gene[$e], $NCBIID;
select STDOUT;
$accToAAseq{$NCBIID} = $AAseq;
my $origLenth = length($AAseq);
#get the scientific name of the organism
$orgName = $prefOrgtoName{$prefOrg[$y]};
$accToOrg{$NCBIID} = $orgName;
$accToName{$NCBIID} = $protName;
########################################################################################################################################
#BUILD THE FASTA! first create the file. Note, .fasta files should open in notepad, or failing that
#they will open in notepad++
$file = $filepath."FASTAs\\".$NCBIID.".fasta";
open(FASTA, ">".$file) or die "could not create/write $file, reason $!";
#then write the header line
select FASTA;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf ">%s %s [%s]\n", $NCBIID, $accToName{$NCBIID}, $accToOrg{$NCBIID};
printf "%s", $accToAAseq{$NCBIID};
close FASTA;
#need to return the output to the standard output to prevent errors
select STDOUT;
########################################################################################################################################
#DO THE BLAST AND WRITE THE RESULTS!
#at this point I have the FASTAs built, so I have all the blast inputs, perform the forward blast
#built FASTA to queryOrg looking for best match
my $blastResults_f = &web_blast("blastp","nr",$queryOrg,$file);
my @BlastLines = split /\n/, $blastResults_f;
#print out the blast results
$file2 = $filepath."ForwardBLASTs/".$NCBIID.".txt";
if (! open BLASTRES, ">".$file2) {
print ERRORLOG "could not create/write $file2, reason $!";
open BLASTRES, ">ForwardBLASTs/".$NCBIID."num".$errFiles.".txt";
$errFiles++;
}
select BLASTRES;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
#basically clean the blast results of unnecessary spaces
#at the end of the line
for(my $zz = 0; $zz <= $#BlastLines; $zz++) {
$BlastLines[$zz] =~ s/\s+$//g;
printf "%s\n", $BlastLines[$zz];
}
close BLASTRES;
select STDOUT;
#########################################################################################################################################
#GET THE MATCHES, GET MATCHED GENES FASTAS!
#get the best forward matches (based on e-value cut offs).
#split blast results into seperate lines
#for each line of the forward blast
for (my $p = 0; $p <= $#BlastLines; $p++) {
#read in alignment information, if there
#split by spaces, if accession is there,
my @line = split /\s+/, $BlastLines[$p];
#get the first element of the
my $startLine = shift @line;
if (defined $startLine) {
chomp($startLine);
}
#if the line starts with a valid accession
#Accession numbers are RefSeq accession numbers, of the pattern shown below
if ((defined $startLine) && ($startLine =~ /^[A-Z][A-Z]_/)) {
#then we must be in the "Sequences producing significant alignments" table, get the significant alignment
#name fragment (use to find name), accession number, E-value, lose score, keep only if significant
$matchAcc = $startLine;
#get identity percentage
my $id_per = pop @line;
#get expect value
my $eVal = pop @line;
$eVal =~ s/\s+//g;
#pop score without saving it, we won't use it
pop @line;
#put together name fragment
my $name = join " ", @line;
#remove spaces at either end
$name =~ s/^\s//;
$name =~ s/\s$//;
#remove commas, replace with semicolons
$name =~ s/,/;/g;
#remove "..." indicating a long name, and everything after the three dots, as that indicates the end of the name
$name =~ s/\.\.\..+$//;
#check if full name (don't include organism name in protein name)
#right square bracket indicates start of organism
if ($name =~ /(.+)\[/) {
#remove organism by getting only everything before the square bracket
$name = $1;
$name =~ s/\s$//;
} else { #otherwise missing part of name, need to search rest of document for full name
#get all results matching the name fragment
#need to do a reformat to the name to use it as a search string
$name = &readyForMatch($name);
my @nameFragMatch = ($blastResults_f =~ /$name.+/g);
#second match to name fragment will be full name
$name = $nameFragMatch[1];
#remove organism name
$name =~ s/\[.+\]//ig;
}
if ((($eVal =~ /^(\d|e|-|\.)+$/) || ($eVal =~ /^(\d+\.\d+)$/)) && ($eVal < $eCutOff)) {
printf "matched e val\n";
my $perPosSub = 0;
my $latestAcc = " ";
for (my $z = $p + 1; $z <= $#BlastLines; $z++) {
#record the latest accession, if changed
if ($BlastLines[$z] =~ /^>(.+?)\s/) {
$latestAcc = $1;
}
#find and turn the percent positive substitution into a decimal
#make sure we are in the correct accession alignment report
if (($latestAcc eq $matchAcc) && ($BlastLines[$z] =~ /Positives\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/)) {
$perPosSub = &percenttoDecimal($1);
printf "accession: %s, fraction positive substitution: %s\n", $matchAcc, $perPosSub;
select FULLLOG;
printf "accession: %s, fraction positive substitution: %s\n", $matchAcc, $perPosSub;
select STDOUT;
$z = $#BlastLines + 1;
}
}
if ($perPosSub < $perPosSubCutOff) {
next;
}
$sum = 0;
printf "Found a significant match, eval: %s\n", $eVal;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "Found a significant match, eval: %s\n", $eVal;
select STDOUT;
#fill hashes with this matched accession
$accToName{$matchAcc} = $accToName{$NCBIID};
$accToOrg{$matchAcc} = $queryOrg;
$AcctoMatchNum{$matchAcc} = $matchNum;
$matchNum++;
#time the capture the fasta of the match
my $MatchURL = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=protein&id=".$matchAcc."&rettype=fasta&retmode=text";
my $MatchFasta = $userAgent->get($MatchURL);
$MatchFasta = $MatchFasta->content;
#get AA sequence
my @fasta = split /\n/, $MatchFasta;
shift @fasta;
my $matchAAseq = join "", @fasta;
$accToAAseq{$matchAcc} = $matchAAseq;
my $length = length($matchAAseq);
push @goodMatchInfo, $orgName.",".$accToName{$NCBIID}.",".$NCBIID.",".$origLenth.",".$name.",".$matchAcc.",".$length.",".$eVal.",";
#BUILD THE FASTA! first create the file. Note, .fasta files should open in notepad, or failing that
#they will open in notepad++
printf "building FASTA report for %s gene %s\n", $queryOrg, $matchAcc;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "building FASTA report for %s gene %s\n", $queryOrg, $matchAcc;
select STDOUT;
$file3 = $filepath."FASTAs\\Target\\".$matchAcc.".fasta";
if (! open FASTA, ">".$file3) {
die "could not create/write $file3, reason $!\n";
}
#then write the header line
select FASTA;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "%s", $MatchFasta;
close FASTA;
#need to return the output to the standard output to prevent errors
select STDOUT;
#search for the % identity in the accession section
for (my $n = $p + 1; $n <= $#BlastLines; $n++) {
#get new search line
my @line2 = split /s+/, $BlastLines[$n];
#get new start line
my $startLine2 = shift @line2;
if ((defined $startLine2) && ($startLine2 =~/^>$matchAcc/)) {
#advance to percent identity and store it
my $percentID = "";
while (! ($BlastLines[$n] =~ /Identities\s=/)) {
$n++;
}
if ($BlastLines[$n] =~ /Identities\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/) { #note should not fail, just trying to capture text
$percentID = $1;
}
my $posSubs = " ";
#get percentage of positive substitutions
if ($BlastLines[$n] =~ /Positives\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/) {
$posSubs = $1;
}
my $gapsPer = " ";
#get percentage of positive substitutions
if ($BlastLines[$n] =~ /Gaps\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/) {
$gapsPer = $1;
}
#append to goodMatchInfo, if the accession is a good match (will filter out bad matches caught earlier
if (exists $AcctoMatchNum{$matchAcc}) {
$goodMatchInfo[$AcctoMatchNum{$matchAcc}] = $goodMatchInfo[$AcctoMatchNum{$matchAcc}].$percentID.",".$posSubs.",".$gapsPer.",";
}
#########################################################################################################################################################################################
#DO THE BACKWARDS BLAST AND WRITE THE RESULTS! to confirm acceptable by blasting best forward match sequences
#against original organism
#at this point I have the FASTAs built, so I have all the blast inputs, perform the backward blast using
#retrieved FASTA against the original organism
printf "confirming match significance for %s gene %s\n", $queryOrg, $matchAcc;
my $blastResults_b = &web_blast("blastp","nr",$orgName,$file3);
#print out the blast results
$file4 = $filepath."BackwardBLASTs\\".$matchAcc."on".$NCBIID.".txt";
if (! open BLASTRES, ">".$file4) {
print ERRORLOG "could not create/write $file4, reason $!\n";
open BLASTRES, ">BackwardBLASTs\\".$matchAcc."on".$NCBIID."num".$errFiles.".txt";
$errFiles++;
}
select BLASTRES;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
#split lines and loop to remove trailing spaces
my @BlastLines_b = split /\n/, $blastResults_b;
for (my $aa = 0; $aa <= $#BlastLines_b; $aa++) {
$BlastLines_b[$aa] =~ s/\s+$//g;
printf "%s\n", $BlastLines_b[$aa];
}
close BLASTRES;
select STDOUT;
#Search for accession number of related organism
my $confirmedMatch = 0;
for ($b = 0; $b <= $#BlastLines_b; $b++) {
#split the blast line into words
my @search = split /\s+/, $BlastLines_b[$b];
#the accession is always the first element in the line, so check to see if it is the accession we are looking for
my $first = shift @search;
#get the percent identity and e-value
my $id_per2 = pop @search;
my $eVal2 = pop @search;
#don't save the score
pop @search;
my $UniProt = $NCBItoUniProt{$NCBIID};
#if first element is defined, is a legal ID for the related protein, and we don't have a confirmed match
if ((defined $first) && (($first =~ /^$NCBIID/) || ($first =~ /^$UniProt/)) && ($confirmedMatch == 0)) { #should only be true once
#found a match, this means we are in the "sequences producing significant alignments" table
#the last element of this line should be the e-value
if (($eVal2 =~ /^(\d|e|-|\.)+$/) && ($eVal2 < $eCutOff)) {
my $perPosSubb = 0;
my $latestAccb = " ";
for (my $x = $b + 1; $x <= $#BlastLines_b; $x++) {
#record the latest accession, if changed
if ($BlastLines_b[$x] =~ /^>(.+?)\s/) {
$latestAcc = $1;
}
#find and turn the percent positive substitution into a decimal
#make sure we are in the correct accession alignment report
if ((($latestAcc eq $NCBIID) || ($latestAcc eq $UniProt)) && ($BlastLines_b[$x] =~ /Positives\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/)) {
$perPosSub = &percenttoDecimal($1);
printf "accession: %s, fraction positive substitution: %s\n", $matchAcc, $perPosSub;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "accession: %s, fraction positive substitution: %s\n", $matchAcc, $perPosSub;
select STDOUT;
$x = $#BlastLines_b + 1;
}
}
if ($perPosSub < $perPosSubCutOff) {
printf "match not confirmed.\n";
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "match not confirmed.\n";
select STDOUT;
next;
}
#we have a good backwards and forwards match
push @backMatchInfo, $eVal2.",";
#search for percent identity, iterate through the lines after the current one
#since alignment reports after the sequences table
for (my $r = $b + 1; $r <= $#BlastLines_b; $r++) {
my @findID = split /\s+/, $BlastLines_b[$r];
my $findAcc = shift @findID;
if ((defined $findAcc) && (($findAcc =~ /^>$NCBIID/) || ($findAcc =~ /^>$UniProt/))) {
$findAcc =~ s/^>//;
#match is confirmed!
printf "match confirmed! E-value: %s\n",$eVal2;
printf "For gene: %s onto gene: %s\n", $findAcc, $matchAcc;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "match confirmed! E-value: %s\n",$eVal2;
printf "For gene: %s onto gene: %s\n", $findAcc, $matchAcc;
select STDOUT;
$confirmedMatch = 1;
while (! ($BlastLines_b[$r] =~ /Identities\s=/)) {
$r++;
}
my $percentIDb = " ";
if ($BlastLines_b[$r] =~ /Identities\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/g) { #note should not fail, just trying to capture text
$percentIDb = $1;
}
my $posSubsb = " ";
if ($BlastLines_b[$r] =~ /Positives\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/g) { #note should not fail, just trying to capture text
$posSubsb = $1;
}
my $gapsPerb = " ";
if ($BlastLines_b[$r] =~ /Gaps\s=\s[0-9]+\/[0-9]+\s\(([0-9]+\%)\)/g) { #note should not fail, just trying to capture text
$gapsPerb = $1;
}
#remove parentheses
$percentIDb =~ s/\(//g;
$percentIDb =~ s/\)//g;
#append to goodMatchInfo
$backMatchInfo[$#backMatchInfo] = $backMatchInfo[$#backMatchInfo].$percentIDb.",".$posSubsb.",".$gapsPerb.",";
#have confirmed a good matchand got percent identity, add to writeLines
push @writeLines,$EC[$i].",".$goodMatchInfo[$AcctoMatchNum{$matchAcc}].$backMatchInfo[$#backMatchInfo];
#since this is a long program, write to the output file while running so that can check periodically
select ECBLASTOUT;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "%s\n", $writeLines[$#writeLines];
select ABBLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "\n%s: Match Report:\n%s\n", ×tamp, $writeLines[$#writeLines];
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "\n\n%s: Match Report:\n%s\n\n", ×tamp, $writeLines[$#writeLines];
select STDOUT;
printf "\n\n%s: Match Report:\n%s\n\n", ×tamp, $writeLines[$#writeLines];
}
}
} else {
printf "match not confirmed.\n";
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "match not confirmed.\n";
select STDOUT;
}
} #otherwise keep searching for related accession
}
} #otherwise go to next line
}
} #otherwise don't bother, not a significant match
}
}
} else {
#there is no AA sequence so nothing to blast, don't build FASTA, etc.
}
}
}
}
}
select ABBLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "finished EC %s\n", $EC[$i];
select STDOUT;
}
select ERRORLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "end time: %s\n", ×tamp;
select ABBLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "end time: %s\n", ×tamp;
select FULLLOG;
#make the perl unbuffered, forces it to write to files realtime
$| = 1;
printf "end time: %s\n", ×tamp;
select STDOUT;