-
Notifications
You must be signed in to change notification settings - Fork 12
/
cp_rasmol_gradesPE_and_pipe.pm
2404 lines (2274 loc) · 90.5 KB
/
cp_rasmol_gradesPE_and_pipe.pm
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 -w
package cp_rasmol_gradesPE_and_pipe;
use Fcntl ':flock'; # import LOCK_* constants
#use strict;
#************************************************************************************************
my %consurf_rasmol_colors=("1"=>"[16,200,209]","2"=>"[140,255,255]","3"=>"[215,255,255]","4"=>"[234,255,255]",
"5"=>"[255,255,255]","6"=>"[252,237,244]","7"=>"[250,201,222]","8"=>"[240,125,171]",
"9"=>"[160,37,96]","10"=>"[255,255,150]");
my %consurf_colors_chimera=("0"=>" 255 255 150","1"=>" 16 200 209","2"=>" 140 255 255","3"=>" 215 255 255","4"=>" 234 255 255","5"=>" 255 255 255","6"=>" 252 237 244","7"=>" 250 201 222","8"=>" 240 125 171","9"=>" 160 37 96");
my $bayesInterval=3;
my %ColorScale = (0 => 9, 1 => 8, 2 => 7, 3 => 6, 4 => 5, 5 => 4, 6 => 3, 7 => 2, 8 => 1);
my %tr_aa;
$tr_aa{LYS}="K";$tr_aa{ARG}="R";$tr_aa{HIS}="H";$tr_aa{ASP}="D";$tr_aa{GLU}="E";
$tr_aa{TYR}="Y";$tr_aa{TRP}="W";$tr_aa{SER}="S";$tr_aa{THR}="T";$tr_aa{PHE}="F";
$tr_aa{LEU}="L";$tr_aa{ILE}="I";$tr_aa{MET}="M";$tr_aa{CYS}="C";$tr_aa{ASN}="N";
$tr_aa{GLN}="Q";$tr_aa{ALA}="A";$tr_aa{VAL}="V";$tr_aa{PRO}="P";$tr_aa{GLY}="G";
my $CHIMERA_SAVING_FIGURE_LINK="http://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/print.html";
my %consurf_html_colors=( "1"=>"#10C8D1", #less conserved
"2"=>"#8CFFFF",
"3"=>"#D7FFFF",
"4"=>"#EAFFFF",
"5"=>"#FFFFFF", #average
"6"=>"#FCEDF4",
"7"=>"#FAC9DE",
"8"=>"#F07DAB",
"9"=>"#A02560", #most conserved
"ISD"=>"yellow"); #InSufficient Data
#############
#subroutines#
#############
#************************************************************************************************
# read the rate4site output into an array.
# calculates layers according to the max and min grades from the output
# $Output : a pointer to array ; assigns each position in the MSA (array index) with a grade (arracy value)
sub assign_colors_according_to_r4s_layers{
my ($rate4site_filename, $Output) = @_;
unless (open RATE4SITE, $rate4site_filename){
return ("assign_colors_according_to_r4s_layers : can't open $rate4site_filename","PANIC");}
my $line;
my $i = 0;
while (<RATE4SITE>) {
$line = $_;
chomp $line;
# baysean
if ($line =~ /^\s+(\d+)\s+(\w)\s+(\S+)\s+\[\s*(\S+),\s*(\S+)\]\s+\S+\s+(\d+)\/(\d+)/){
$Output->[$i]{POS} = $1;
$Output->[$i]{SEQ} = $2;
$Output->[$i]{GRADE} = $3;
$Output->[$i]{INTERVALLOW} = $4;
$Output->[$i]{INTERVALHIGH} = $5;
$Output->[$i]{MSA_NUM}=$6;
$Output->[$i]{MSA_DENUM}=$7;
$i++;
}
# Maximum likelihood
elsif($line=~m/(\d+)\s+(\w)\s+(\S+)\s+(\d+)\/(\d+)/){
$Output->[$i]{POS} = $1;
$Output->[$i]{SEQ} = $2;
$Output->[$i]{GRADE} = $3;
$Output->[$i]{INTERVALLOW} = $3;
$Output->[$i]{INTERVALHIGH} = $3;
$Output->[$i]{MSA_NUM}=$4;
$Output->[$i]{MSA_DENUM}=$5;
$i++;
}
}
close RATE4SITE;
my $element;
my $max_cons = $Output->[0]{GRADE};
my $ConsColorUnity; #unity of conservation to be colored
foreach $element (@$Output){
if ($$element{GRADE} < $max_cons) {$max_cons = $$element{GRADE};}
}
$ConsColorUnity = $max_cons / 4.5 * -1;
if ($max_cons !~ /^\-/){$ConsColorUnity = $max_cons;}
#calculates the grades for each color
my $NoLayers = 9;
my @ColorLayers;
for (my $i = 0; $i <= $NoLayers; $i++) {
$ColorLayers[$i] = $max_cons + ($i * $ConsColorUnity);
}
#gives the color to the interval
my $Count = 0;
foreach my $element (@$Output) {
for (my $i = 0; $i <= $#ColorLayers; $i++){
if( ($i==$#ColorLayers) and !exists $$element{INTERVALLOWCOLOR}){
$$element{INTERVALLOWCOLOR} = 8;
}
elsif ($$element{INTERVALLOW} >= $ColorLayers[$i] and $$element{INTERVALLOW} < $ColorLayers[$i + 1]) {
$$element{INTERVALLOWCOLOR} =$i;
}
elsif ( ($$element{INTERVALLOW} < $ColorLayers[$i]) and !exists $$element{INTERVALLOWCOLOR}){
$$element{INTERVALLOWCOLOR} = 0;
}
if (($i == $#ColorLayers) and !exists $$element{INTERVALHIGHCOLOR}){
$$element{INTERVALHIGHCOLOR} = 8;
}
elsif ($$element{INTERVALHIGH} >= $ColorLayers[$i] and $$element{INTERVALHIGH} < $ColorLayers[$i + 1]) {
$$element{INTERVALHIGHCOLOR} =$i;
}
elsif ( ($$element{INTERVALHIGH} < $ColorLayers[$i]) and !exists $$element{INTERVALHIGHCOLOR}){
$$element{INTERVALHIGHCOLOR} = 0;
}
} # END FOR
} # END FOREACH
#give the color for each position based on the grades
# match the colors to the grades
foreach my $element (@$Output){
for (my $i = 0; $i <= $#ColorLayers; $i++) {
if ($i == $#ColorLayers) {
$$element{COLOR} = $ColorScale{$i-1};
}
elsif ($$element{GRADE} >= $ColorLayers[$i] && $$element{GRADE} < $ColorLayers[$i + 1]) {
$$element{COLOR} = $ColorScale{$i};
last;
}
}
if ((($$element{INTERVALHIGHCOLOR}-$$element{INTERVALLOWCOLOR})>$bayesInterval ) or ($$element{MSA_NUM} <= 5)){
$$element{ISD}=1;
}
else{$$element{ISD}=0;}
}
return ("OK");
}
#************************************************************************************************
#printing the the gradesPE file
sub create_gradesPE{
my ($Output,$ref_match, $ref_residue_freq, $no_isd_residue_color, $isd_residue_color, $gradesPE_file) = @_;
my ($seq3d_grades_isd, $seq3d_grades);
# open file
unless (open PE, ">$gradesPE_file" ){
return ("create_gradesPE : can't open '$gradesPE_file'","PANIC");}
print PE "\t Amino Acid Conservation Scores\n";
print PE "\t===============================\n\n";
print PE "- POS: The position of the AA in the SEQRES derived sequence.\n";
print PE "- SEQ: The SEQRES derived sequence in one letter code.\n";
print PE "- 3LATOM: The ATOM derived sequence in three letter code, including the AA's positions as they appear in the PDB file and the chain identifier.\n";
print PE "- SCORE: The normalized conservation scores.\n";
print PE "- COLOR: The color scale representing the conservation scores (9 - conserved, 1 - variable).\n";
print PE "- CONFIDENCE INTERVAL: When using the bayesian method for calculating rates, a confidence interval is assigned to each of the inferred evolutionary conservation scores.\n";
print PE "- CONFIDENCE INTERVAL COLORS: When using the bayesian method for calculating rates. The color scale representing the lower and upper bounds of the confidence interval.\n";
print PE "- MSA DATA: The number of aligned sequences having an amino acid (non-gapped) from the overall number of sequences at each position.\n";
print PE "- RESIDUE VARIETY: The residues variety at each position of the multiple sequence alignment.\n\n";
print PE " POS\t SEQ\t 3LATOM\tSCORE\t\tCOLOR\tCONFIDENCE INTERVAL\tCONFIDENCE INTERVAL COLORS\tMSA DATA\tRESIDUE VARIETY\n";
print PE " \t \t \t(normalized)\t \t \n";
foreach my $elem (@$Output){
my $pos = $$elem{POS};
my $var = "";
my $atom_3L=$$ref_match{$pos};
my $score = $$elem{COLOR};
printf (PE "%4d", $pos);
printf (PE "\t%4s", "$$elem{SEQ}");
printf (PE "\t%10s", $atom_3L);
printf (PE "\t%6.3f", "$$elem{GRADE}");
if($$elem{ISD}==1){
printf (PE "\t\t%3d", "$$elem{COLOR}");
printf (PE "%1s", "*");
}
else{printf (PE "\t\t%3d", "$$elem{COLOR}");}
printf (PE "\t%6.3f", "$$elem{INTERVALLOW}");
printf (PE "%1s", ",");
printf (PE "%6.3f", "$$elem{INTERVALHIGH}");
printf (PE "\t\t\t%5d", "$ColorScale{$$elem{INTERVALLOWCOLOR}}");
printf (PE "%1s", ",");
printf (PE "%1d\t\t", "$ColorScale{$$elem{INTERVALHIGHCOLOR}}");
printf (PE "\t%8s", "$$elem{MSA_NUM}\/$$elem{MSA_DENUM}");
for my $_aa (keys %{$ref_residue_freq->{($pos)}}){
$var.= "$_aa,";
}
chop($var) if ($var =~ /,$/);
print PE "\t$var\n";
# the amino-acid in that position, must be part of the residue variety in this column
if ($var !~ /$$elem{SEQ}/){
close PE;
return ("create_gradesPE : in position $pos, the amino-acid ".$$elem{SEQ}." does not match the residue variety: $var.","PANIC");}
#printing the residue to the rasmol script
#assigning grades to $seq3d strings
if($atom_3L !~ m/\-/){
$atom_3L =~ m/(.+):/;
$atom_3L = $1;
if($score=~ m/(\d)/){
my $color = $1;
push @{ $no_isd_residue_color->[$color] }, $atom_3L;
#if($score=~ m/\*/){
if ($$elem{ISD}==1){
push @{ $isd_residue_color->[10] }, $atom_3L;
$seq3d_grades_isd.="0";
}
else{
push @{ $isd_residue_color->[$color] }, $atom_3L;
$seq3d_grades_isd.="$color";
}
$seq3d_grades.="$color";
}
}
else{
$seq3d_grades_isd.=".";
$seq3d_grades.=".";
}
}
print PE "\n\n*Below the confidence cut-off - The calculations for this site were performed on less than 6 non-gaped homologue sequences,\nor the confidence interval for the estimated score is equal to- or larger than- 4 color grades.\n";
close(PE);
return ("OK",$seq3d_grades_isd, $seq3d_grades);
}
#************************************************************************************************
#printing the the ConSeq gradesPE file
sub create_gradesPE_ConSeq{
my ($Output, $ref_residue_freq, $ref_Solv_Acc_Pred, $gradesPE_file) = @_;
# open file
unless (open PE, ">$gradesPE_file" ){
return ("create_gradesPE_Conseq : can't open '$gradesPE_file'","PANIC");}
print PE "\t Amino Acid Conservation Scores\n";
print PE "\t===============================\n\n";
print PE "- POS: The position of the AA in the SEQRES derived sequence.\n";
print PE "- SEQ: The SEQRES derived sequence in one letter code.\n";
print PE "- SCORE: The normalized conservation scores.\n";
print PE "- COLOR: The color scale representing the conservation scores (9 - conserved, 1 - variable).\n";
print PE "- CONFIDENCE INTERVAL: When using the bayesian method for calculating rates, a confidence interval is assigned to each of the inferred evolutionary conservation scores.\n";
print PE "- CONFIDENCE INTERVAL COLORS: When using the bayesian method for calculating rates. The color scale representing the lower and upper bounds of the confidence interval.\n";
# print PE "- B/E: Burried (b) or Exposed (e) residue.\n";
# print PE "- FUNCTION: functional (f) or structural (s) residue (f - highly conserved and exposed, s - highly conserved and burried).\n";
print PE "- MSA DATA: The number of aligned sequences having an amino acid (non-gapped) from the overall number of sequences at each position.\n";
print PE "- RESIDUE VARIETY: The residues variety at each position of the multiple sequence alignment.\n\n";
# print PE " POS\t SEQ\tSCORE\t\tCOLOR\tCONFIDENCE INTERVAL\tCONFIDENCE INTERVAL COLORS\tB\/E\tFUNCTION\tMSA DATA\tRESIDUE VARIETY\n";
# print PE " \t \t(normalized)\t \t \n";
print PE " POS\t SEQ\tSCORE\t\tCOLOR\tCONFIDENCE INTERVAL\tCONFIDENCE INTERVAL COLORS\tMSA DATA\tRESIDUE VARIETY\n";
print PE " \t \t(normalized)\t \t \n";
foreach my $elem (@$Output){
my $pos = $$elem{POS};
my $var = "";
my $Solv_Acc_Pred=" ";
if (exists $$ref_Solv_Acc_Pred{$pos})
{
$Solv_Acc_Pred=$$ref_Solv_Acc_Pred{$pos};
}
my $score = $$elem{COLOR};
printf (PE "%4d", $pos);
printf (PE "\t%4s", "$$elem{SEQ}");
printf (PE "\t%6.3f", "$$elem{GRADE}");
if($$elem{ISD}==1){
printf (PE "\t\t%3d", "$$elem{COLOR}");
printf (PE "%1s", "*");
}
else{printf (PE "\t\t%3d", "$$elem{COLOR}");}
printf (PE "\t%6.3f", "$$elem{INTERVALLOW}");
printf (PE "%1s", ",");
printf (PE "%6.3f", "$$elem{INTERVALHIGH}");
printf (PE "\t\t\t%5d", "$ColorScale{$$elem{INTERVALLOWCOLOR}}");
printf (PE "%1s", ",");
printf (PE "%1d\t\t", "$ColorScale{$$elem{INTERVALHIGHCOLOR}}");
printf (PE "\t%3s", $Solv_Acc_Pred);
## FUNCT/STRUCT COL
if ($Solv_Acc_Pred eq "e"){
if ($$elem{COLOR} == 9 || $$elem{COLOR} == 8 ){
printf (PE "\t%8s", "f");
}
else {
printf (PE "\t%8s", " ");
}
}
else {
if (($$elem{COLOR} == 9) and ($Solv_Acc_Pred ne " ")){
printf (PE "\t%8s", "s");
}
}
printf (PE "\t%8s", "$$elem{MSA_NUM}\/$$elem{MSA_DENUM}");
for my $_aa (keys %{$ref_residue_freq->{($pos)}}){
$var.= "$_aa,";
}
chop($var) if ($var =~ /,$/);
print PE "\t$var\n";
# the amino-acid in that position, must be part of the residue variety in this column
if ($var !~ /$$elem{SEQ}/i){
close PE;
return ("create_gradesPE_ConSeq : in position $pos, the amino-acid ".$$elem{SEQ}." does not match the residue variety: $var.","PANIC");}
#printing the residue to the rasmol script
#assigning grades to $seq3d strings
}
print PE "\n\n*Below the confidence cut-off - The calculations for this site were performed on less than 6 non-gaped homologue sequences,\nor the confidence interval for the estimated score is equal to- or larger than- 4 color grades.\n";
close(PE);
return ("OK");
}
#************************************************************************************************
#matches the position in the seqres/msa sequence to the position in the pdb
sub match_seqres_pdb{
my ($seqres_atom_aln, $atom_position, $chain, $ref_fas2pdb) = @_;
my (@seqres, @atoms, $length_of_seqres, $length_of_atom);
my $pdbseq="";
my $query_seq="";
#creating arrays containing each sequences
unless(open ALN, $seqres_atom_aln){return ("match_seqres_pdb : Could not open the file $seqres_atom_aln for reading $!", "PANIC");}
flock ALN, LOCK_EX;
while(<ALN>){
if($_=~ m/^ATOM_\S+\s+(\S+)/){
$pdbseq=$pdbseq.$1;
}
elsif($_=~ m/^SEQRES_\S+\s+(\S+)/){
$query_seq=$query_seq.$1;
}
elsif($_=~ m/^MSA_\S+\s+(\S+)/){
$query_seq=$query_seq.$1;
}
}
flock ALN,LOCK_UN;
close(ALN);
#arrays that conatin the sequences from the pairwise for each sequneces (including gaps).
#the sequnces start from position 1 in the array!
$pdbseq="X".$pdbseq;
$query_seq="X".$query_seq;
@atoms=split("",$pdbseq);
@seqres=split("",$query_seq);
#creating the hash that matches the position in the ATOM fasta to its position
#in the pdb file and also the fasta ATOM position to the correct residue
unless(open MATCH, $atom_position){return ("match_seqres_pdb : Could not open the file $atom_position for reading $!", "PANIC");}
flock MATCH, LOCK_EX;
my %match_ATOM;
my %res_ATOM;
while(<MATCH>){
$_=~ m/(\w{3})\s+(\d+)\s+(\S+)/;
my $res=$1;
my $fas_atom=$2;
my $pdb_atom=$3;
$match_ATOM{$fas_atom}=$res.$pdb_atom.":".$chain;
}
flock MATCH,LOCK_UN;
close MATCH;
#creating a hash in which the key is the position in the aln (i.e the
#position in the @seqres) and the value is the correct position in the fas.
my $pos_count=0;
my %aln_fas_seqres;
for(my $n=1;$n<@seqres+0;$n++){
# if($seqres[$n] !~ m/\-/){ #HAIM
if(($seqres[$n] !~ m/\-/) and ($seqres[$n] !~ m/X/)){
$pos_count++;
$aln_fas_seqres{$n}=$pos_count;
}
}
$length_of_seqres = $pos_count;
#creating a hash in which the key is the position in the aln (i.e the
#position in the @atoms) and the value is the correct position in the fas.
$pos_count=0;
my %aln_fas_atoms;
for(my $n=1;$n<@atoms+0;$n++){
# if($atoms[$n] !~ m/-/){ #HAIM
if(($atoms[$n] !~ m/-/) and ($atoms[$n] !~ m/X/)){
$pos_count++;
$aln_fas_atoms{$n}=$pos_count;
}
}
$length_of_atom = $pos_count;
for(my $i=1;$i<@seqres+0;$i++){
my $fas_pos=$aln_fas_seqres{$i};
if($seqres[$i]!~m/-/){
# if($atoms[$i] eq "-"){ #HAIM
if(($atoms[$i] eq "-") or ($atoms[$i] eq "X")){
$ref_fas2pdb->{$fas_pos}="-";
}
else{
my $match=$match_ATOM{$aln_fas_atoms{$i}};
$ref_fas2pdb->{$fas_pos}=$match;
}
}
}
return ("OK",$length_of_seqres, $length_of_atom);
}
#************************************************************************************************
#adds the 3LATOM colomn to the new gradesPE file. also creates a rasmol script.
# its input is a reference to a hash where key is the position in the SEQRES sequence, and the value is that 3LATOM according to the PDB sequence
sub add_pdb_gradesPE{
my ($pre_gradesPE, $outPE, $ref_match, $no_isd_residue_color, $isd_residue_color, $ref_residue_freq)=@_;
my ($seq3d_grades_isd, $seq3d_grades, $aa_position);
my ($grade, $score, $high, $low, $gradel, $gradeh, $msa_m, $msa_d, $var);
my $seq = "";
unless (open GRADESPE, $pre_gradesPE){return ("add_pdb_gradesPE : Could not open the file $pre_gradesPE for reading $!", "PANIC");}
unless(open PE, ">$outPE"){return ("add_pdb_gradesPE : Could not open the file $outPE for writing $!", "PANIC");}
while(<GRADESPE>){
if($_!~ m/\s+\d+\s+\w\s+atom_res/){
print PE "$_";
next;
}
else{
$_=~m/^\s+(\d+)\s+/;
my $pos=$1;
my $atom_3L=$$ref_match{$pos};
$atom_3L=~ m/(\w{3})/;
my $aa=$1;
$_=~ m/\s+(\d+)\s+(\w)\s+atom_res\s+(\S+)\s+(\S+)\s+(\S+),\s*(\S+)\s+(\S+),\s*(\S+)\s+(\d+)\/(\d+)/;
$pos=$1; $seq = $2;
$grade=$3; $score=$4; $high=$5;$low=$6;$gradel=$7;
$gradeh=$8; $msa_m=$9; $msa_d=$10; $var="";
printf (PE "%4d", "$pos");
printf (PE "\t%4s", "$seq");
printf (PE "\t%10s", "$atom_3L");
printf (PE "\t%6.3f", "$grade");
if($score=~m/\*/){
printf (PE "\t\t%3d", "$score");
printf (PE "%1s", "\*");
}
else{printf (PE "\t\t%3d", "$score");}
printf (PE "\t%6.3f", "$high");
printf (PE "%1s", ",");
printf (PE "%6.3f", "$low");
printf (PE "\t\t\t%5d", "$gradel");
printf (PE "%1s", ",");
printf (PE "%1d\t\t", "$gradeh");
printf (PE "\t%8s", "$msa_m\/$msa_d");
for my $_aa (keys %{$ref_residue_freq->{$pos}}){
$var.= "$_aa,";
}
chop($var) if ($var =~ /,$/);
print PE "\t$var\n";
#printing the residue to the rasmol script
#assigning grades to $seq3d strings
if($atom_3L !~ m/\-/){
$atom_3L =~ m/(.+):/;
$atom_3L = $1;
if($score=~ m/(\d)/){
my $color = $1;
push @{ $no_isd_residue_color->[$color] }, $atom_3L;
if($score=~ m/\*/){
push @{ $isd_residue_color->[10] }, $atom_3L;
$seq3d_grades_isd.="0";
}
else{
push @{ $isd_residue_color->[$color] }, $atom_3L;
$seq3d_grades_isd.="$color";
}
$seq3d_grades.="$color";
}
}
else{
$seq3d_grades_isd.=".";
$seq3d_grades.=".";
}
}
}
close PE;
close GRADESPE;
return ("OK",$seq3d_grades_isd, $seq3d_grades);
}
#************************************************************************************************
sub print_rasmol{
my ($out_file,$rasmol_isd,$ref_colors_array,$chain, $proteopedia) = @_;
$chain = '' if ($chain eq 'NONE');
# print out new format of rasmol
unless (open OUT, ">".$out_file) {return ("print_rasmol : Could not open the file $out_file for writing $!", "PANIC");}
print OUT "select all\ncolor [200,200,200]\n\n" if $proteopedia ne "yes";
for (my $i=$#{$ref_colors_array}+1; $i>0;$i--){
next if ($i==10 and $rasmol_isd eq "no");
if ($#{$ref_colors_array->[$i]}>-1){
print OUT print_selected(\@{ $ref_colors_array->[$i] }, "no");
print OUT "\nselect selected and :$chain\n";
print OUT "color $consurf_rasmol_colors{$i}\nspacefill\n";
print OUT "define CON".($i)." selected\n\n";
}
}
# print OUT
close OUT;
return ("OK");
}
#************************************************************************************************
sub print_selected{
my $arr_ref = shift;
my $print_for_pipe = shift;
my @all_aa = @$arr_ref;
my $string = "";
my $total_text = "";
if ($print_for_pipe eq "yes"){
$string = "! select ";}
else{
$string = "select ";}
my $total_length = length $string;
foreach (@all_aa){
my $_aa = $_;
$total_length+=length $_aa;
#print "aa is: $_aa total is: $total_length\n";
if ($total_length >80){
if ($string =~ m/, $/){
chop $string; chop $string;
}
$total_text .= $string."\n";
if ($print_for_pipe eq "yes"){
$string="! select selected or $_aa, ";}
else{
$string="select selected or $_aa, ";}
$total_length = length $string;
}
else{
$string .="$_aa, ";
$total_length+=2; # for the ',' and ' '
}
$_aa = "";
}
if ($string =~ m/, $/){
chop $string; chop $string;
$total_text.= $string;
}
return $total_text;
}
#************************************************************************************************
#input: $input_pdb_file, $ref_return_arr
#output: ("OK"/"ERR",$header_line,$title_lines,$compnd_lines)
sub extract_data_from_pdb{
my $input_pdb_file = shift;
my @return_arr = ();
unless (open PDB, $input_pdb_file){
$return_arr[0] = "extract_data_from_pdb : Could not open the file $input_pdb_file $!";
$return_arr[1] = "PANIC";
}
else{
flock PDB, 2;
while (<PDB>){
if(/^HEADER/) {
$_ =~ s/\s+$//;
$return_arr[1] = $_;
}
elsif (/^TITLE\s+/){
$_ =~ s/\s+$//;
$_ =~ /^TITLE\s+\d*\s(.*)\s*/;
$return_arr[2].=$1." ";
}
elsif (/^COMPND\s+/) {
$_ =~ s/\s+$//;
$_ =~ /^COMPND\s+\d*\s(.*)/;
$return_arr[3].=$1." ";
}
elsif (/^SOURCE/ || /^KEYWDS/ || /^AUTHOR/ || /^SEQRES/ || /^ATOM/) {# no nead to go over all the pdb
last;
}
}
flock PDB, 8;
close PDB;
$return_arr[0] = "OK";
}
return @return_arr;
}
#************************************************************************************************
# take a string aaaaaaa and returns it in this format: ! "aaa" +\n! "aa";\n
sub design_string_for_pipe{
my $string_to_format = shift;
my $part = $string_to_format;
my $newPart = "";
while (length $part > 73){
$newPart .= "! \"".(substr $part, 0, 73)."\" +\n";
$part = substr $part, 73;
}
$newPart .= "! \"".$part."\" ;";
return $newPart;
}
#************************************************************************************************
sub design_string_with_spaces_for_pipe{
my $part = shift;
my @words = split /\s+/, $part;
my $newPart = "! \"".$words[0];
$part = "";
for (my $i=1; $i<@words; $i++){
# if adding another word to the string will yeild a too long string - we cut it.
if ((length($words[$i]) +1+ length($newPart)) > 76){
$part .= $newPart."\" +\n" ;
$newPart="! \"".$words[$i];
}
else{
$newPart.=" $words[$i]";
}
}
$part .= $newPart."\" ;";
return $part;
}
#************************************************************************************************
# input : 1 - a path to a directory where sub-directories hold every chain,
# 2 - a reference to hash that will store chains and their identical chains as a string
# please note: if chain A and B are identical, $ref_identical_chains->{"A"} will hold only "B" and $ref_identical_chains->{"B"} will hold only "A"
sub find_identical_chains{
my ($all_identical,$ref_identical_chains) = @_;
my %chain_seq = ();
my $seq = "";
opendir PROTEIN, $protein_path;
while (my $chain = readdir PROTEIN){
next if ($chain !~ /^[A-Za-z]$/);
my $seq_file = $protein_path.$chain."/seq.fas";
if (-e $seq_file){
open SEQ, $seq_file;
<SEQ>;
$seq = <SEQ>;
close SEQ;
next if ($seq !~ /\w+/);
if (exists $chain_seq{$seq}){
if (length $chain_seq{$seq} > 1){
my @chains = split //, $chain_seq{$seq};
foreach (@chains){
$ref_identical_chains->{$chain}.=$_;
$ref_identical_chains->{$_}.=$chain;
}
}
else{
$ref_identical_chains->{$chain}.=$chain_seq{$seq};
$ref_identical_chains->{$chain_seq{$seq}} .= $chain;
}
$chain_seq{$seq}.=$chain;
}
else{
$chain_seq{$seq} = $chain;
}
}
}
closedir PROTEIN;
}
#************************************************************************************************
# the routine creates 2 hashes, according to information it reads from a MSA
# for each position in the query sequence :
# 1. it collects all the residues that are aligned to this positions. it also counts the number of time each residue appeared in that position in the MSA
# 2. it counts the total number of residues which aligned to this position
sub read_residue_variety
{
my ($msa_filename, $msa_ref_sequence, $ref_residue_frequency, $ref_position_totalAA) = @_;
my $line;
my $position = 1;
my $num_of_seqs = 0;
my $flag=0;
my $data_length;
my @elements_to_remove = ();
my $ret_seq = "";
# open msa file
unless (open MSA, $msa_filename) {return ("Could not open the file $msa_filename for reading $!", "PANIC");}
flock MSA, LOCK_EX;
# read header lines
$line = <MSA>;
$line = <MSA>;
while ($line = <MSA>)
{
if ($line =~ /^(.+) +([A-Za-z-]+)$/)
{
$num_of_seqs++ if ($flag==0); #count the number of sequences, only in the first block
if($flag==1){
#inc position and read next (blank) line
$position += $data_length;
$flag=2;
}
my $seq_name = $1;
my $seq_data = $2;
$seq_name =~ s/\s+$//; # remove spaces
$data_length = length($seq_data);
for (my $position_offset = 0;$position_offset < length($seq_data);$position_offset++)
{
# get residue at this position
my $current_aa = substr($seq_data,$position_offset,1);
# if this is the query seq and no aa is found we need to save
# this position in order to erase it in the end
if ($seq_name eq $msa_ref_sequence and $current_aa !~ /^[ABCDEFGHIKLMNPQRSTVWY]$/){
push @elements_to_remove,$position+$position_offset;
$ret_seq .= "in seq $seq_name ignored $current_aa in position ".($position+$position_offset)." " if ($current_aa !~ /^[-Xx]$/);
}
elsif ($current_aa !~ /^-$/){
# save residue value
if (!exists $ref_residue_frequency->{$position+$position_offset}){
$ref_residue_frequency->{$position+$position_offset}->{$current_aa} = 1;
}
elsif (!exists $ref_residue_frequency->{$position+$position_offset}->{$current_aa}){
$ref_residue_frequency->{$position+$position_offset}->{$current_aa} = 1;
}
else{
$ref_residue_frequency->{$position+$position_offset}->{$current_aa}++;
}
$ref_position_totalAA->{$position+$position_offset}++;
}
}
}
elsif($line =~ /^\s+/){
$flag=1;
}
}
flock MSA, LOCK_UN;
close MSA;
# remove all positions where the query seq was skipped
my $index = pop(@elements_to_remove);
while (defined($index))
{
residues_shift_left($index, $ref_residue_frequency);
residues_shift_left($index, $ref_position_totalAA);
$index = pop(@elements_to_remove);
}
# If there was an unexpected character in the query sequence in the MSA, report it and exit
if ($ret_seq ne "")
{return ($ret_seq, 'PANIC');}
else {return ("OK", $num_of_seqs);}
}
#************************************************************************************************
sub residues_shift_left
{
my ($start_position, $ref_residue_frequency) = @_;
my $index = $start_position;
while (exists($ref_residue_frequency->{$index+1}))
{
$ref_residue_frequency->{$index} = $ref_residue_frequency->{$index+1};
$index++;
}
delete($ref_residue_frequency->{$index});
}
#************************************************************************************************
# for each position, calculate the % for each residue which is found in the MSA in that position
# the output is written in "Cvs" format, meaning there are ',' signs for each tab carachter
sub print_precentage{
my ($ref_residue_freq,$ref_position_totalAA, $out_file, $ref_ConSurfGrades) = @_;
my @aa_arr = ("A","C","D","E","F","G","H","I","K","L","M","N","P","Q","R","S","T","V","W","Y","OTHER");
my ($val, $aa_found, $total, @aa_in_position, $other, $other_val);
unless (open OUT, ">".$out_file) {return ("print_precentage : Could not open the file $out_file for writing $!", 'PANIC');}
print OUT "\"The table details the residue variety in % for each position in the query sequence.\"\n\"Each column shows the % for that amino-acid, found in position ('pos') in the MSA.\"\n\"In case there are residues which are not a standard amino-acid in the MSA, they are represented under column 'OTHER'\"\n\n";
print OUT "pos";
print OUT ",$_" foreach (@aa_arr);
print OUT ",MAX AA,ConSurf Grade\n";
# order the lines according to AA position in the sequence
for my $position (sort {$a <=> $b} keys %$ref_residue_freq){
$total=0; $aa_found = "no"; $other="";$other_val=0;
my $max_precent=0;my $max_AA="";
print OUT $position;
# the total number of animo acids found in the MSA for that position
$total += $ref_position_totalAA->{$position};
#For each position , sort the amino acids variety
@aa_in_position = sort keys %{$ref_residue_freq->{$position}};
my $j=0;
$aa = $aa_in_position[$j];
$val = ($ref_residue_freq->{$position}->{$aa})/$total*100;
# in order to print a table, we go by the sorted array
my $i=0;
while ($i<@aa_arr){
# if there are non standart aa, we calculate its total value seperately and add it under column OTHER
while ($aa !~ /^[KRHDEYWSTFLIMCNQAVPG]$/i and $j < @aa_in_position){
$other_val+=$val;
$j+=1;
if ($j < @aa_in_position){
$aa = $aa_in_position[$j];
$val = ($ref_residue_freq->{$position}->{$aa})/$total*100;
}
}
if ($aa_arr[$i] eq 'OTHER' and $other_val!=0){
$other = $other_val;
print OUT ",$other";
}
elsif ($aa_arr[$i] eq uc ($aa)){
if ($val>$max_precent)
{
$max_precent=$val;
$max_AA=$aa;
}
elsif ($val==$max_precent)
{
$max_AA.=$aa;
}
print OUT ",".sprintf("%.3f",$val);
$j+=1;
if ($j < @aa_in_position){
$aa = $aa_in_position[$j];
$val = ($ref_residue_freq->{$position}->{$aa})/$total*100;
}
}
else{
print OUT ",";
}
$i++;
}
print OUT ",$max_AA ".sprintf("%.3f",$max_precent);
if (defined $ref_ConSurfGrades->[($position-1)])
{
my $GradesPE_elem=$ref_ConSurfGrades->[($position-1)];
print OUT ",$$GradesPE_elem{COLOR}";
if($$GradesPE_elem{ISD}==1){
print OUT "*";
}
}
print OUT "\n";
}
close OUT;
return ("OK");
}
#************************************************************************************************
# for each position, calculate the % for each residue which is found in the MSA in that position
# the values in the line might not sum to exactly 100%, as we round the value to be written with
# 2 digits after the '.'
sub print_precentage_text{
my ($ref_residue_freq,$ref_position_totalAA, $out_file) = @_;
my @aa_arr = ("A","C","D","E","F","G","H","I","K","L","M","N","P","Q","R","S","T","V","W","Y","OTHER");
my ($val, $aa_found, $total, @aa_in_position, $other, $other_val);
unless (open OUT, ">".$out_file) {return ("Could not open the file $out_file for writing $!", 'PANIC');}
print OUT "The table details the residue variety in % for each position in the query sequence.\nEach column shows the % for that amino-acid, found in position (\"pos\") in the MSA.\nIn case there are residues which are not a standard amino-acid, they are represented under column \"OTHER\"\n\n";
print OUT "pos | ";
print OUT " -$_- " foreach (@aa_arr);
print OUT "\n" ;
print OUT "---------" foreach (@aa_arr);
print OUT "\n";
# order the lines according to AA position in the sequence
for my $position (sort {$a <=> $b} keys %$ref_residue_freq){
$total=0; $aa_found = "no"; $other="";$other_val=0;
printf OUT '%4s', $position;
print OUT " | ";
# the total number of animo acids found in the MSA for that position
$total += $ref_position_totalAA->{$position};
#For each position , sort the amino acids variety
@aa_in_position = sort keys %{$ref_residue_freq->{$position}};
my $j=0;
$aa = $aa_in_position[$j];
$val = ($ref_residue_freq->{$position}->{$aa})/$total*100;
# in order to print a table, we go by the sorted array
my $i=0;
while ($i<@aa_arr){
# if there are non standart aa, we calculate its total value seperately and add it under column OTHER
while ($aa !~ /^[KRHDEYWSTFLIMCNQAVPG]$/ and $j < @aa_in_position){
$other_val+=$val;
$j+=1;
if ($j < @aa_in_position){
$aa = $aa_in_position[$j];
$val = ($ref_residue_freq->{$position}->{$aa})/$total*100;
}
}
if ($aa_arr[$i] eq 'OTHER' and $other_val!=0){
$other = sprintf '%*2$.2f', $other_val;
print OUT " ".$other;
}
elsif ($aa_arr[$i] eq $aa){
printf OUT '%*2$.2f', $val, 8;
$j+=1;
if ($j < @aa_in_position){
$aa = $aa_in_position[$j];
$val = ($ref_residue_freq->{$position}->{$aa})/$total*100;
}
}
else{
print OUT " ";
}
$i++;
}
print OUT "\n";
}
close OUT;
return ("OK");
}
#************************************************************************************************
sub round{
my $in = shift;
if ($in =~ /(\d+)\.(\d)(\d)(\d)\d*/){
$x = $1; $y = $2; $z = $3; $w = $4;
if (less_than(5,$w)){return $x.".".$y.$z;}
else{
if(less_than(9,$z)){return $x.".".$y.($z+1);}
else{
if(less_than(9,$y)){return $x.".".($y+1)."0";}
else{return ($x+1).".00";}
}
}
}
else{
return $in;
}
}
sub less_than{
my $_val = shift;
my $in = shift;
if ($in>=0 and $in<$_val) {return 1;}
else{return 0;}
}
#************************************************************************************************
# creating part of the pipe file, which contains all the non-unique information.
# each chain will use this file to construct the final pdb_pipe file, to be viewed with FGiJ
sub create_part_of_pipe{
my ($pipe_file, $num_of_seq_in_msa, $IN_db_file, $seq3d_grades_isd, $seq3d_grades, $length_of_seqres, $length_of_atom, $ref_isd_residue_color, $ref_no_isd_residue_color) = @_;
#############################################
# collect data for printing the "pipe" file #
#############################################
# database where sequences collected from:
unless (open DB, $IN_db_file){return ("cannot open the file $IN_db_file for reading $!", 'PANIC');}
<DB> =~ /Database: (.+)/;
if ($1 =~ /sprot/i or $1 =~ /swissprot/i){
$db = "SWISS-PROT";}
else{
$db = "UNIPROT";}
close DB;
# design the seq3d to be printted out to the pipe file
$seq3d_grades_isd = cp_rasmol_gradesPE_and_pipe::design_string_for_pipe($seq3d_grades_isd);
$seq3d_grades = cp_rasmol_gradesPE_and_pipe::design_string_for_pipe($seq3d_grades);
# creating the frequencies array which corresponds the number of residues in each grade
my ($consurf_grade_freqs_isd, $consurf_grade_freqs) = freq_array($ref_isd_residue_color, $ref_no_isd_residue_color);
##########################
# write to the pipe file #
##########################
unless (open PIPE, ">".$pipe_file){return ("cannot open the file $pipe_file for writing $!", 'PANIC');}
print PIPE <<EndOfPipe;
! consurf_psi_blast_e_value = 0.001;
! consurf_psi_blast_database = "$db";
! consurf_psi_blast_iterations = 3;
! consurf_max_seqs = 300;
! consurf_alignment = "MUSCLE";
! consurf_method = "bayesian";
! consurf_substitution_model = "JTT";
!
! consurf_seqres_length = $length_of_seqres;
! consurf_atom_seq_length = $length_of_atom;
! consurf_unique_seqs = $num_of_seq_in_msa;
! consurf_grade_freqs_isd = $consurf_grade_freqs_isd;
! consurf_grade_freqs = $consurf_grade_freqs;
!
! seq3d_grades_isd =
$seq3d_grades_isd
!
! seq3d_grades =
$seq3d_grades
!
!
!! ====== CONTROL PANEL OPTIONS SECTION ======
!js.init
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! pipe_title_enlarged = false;
! pipe_background_color = "white";
!
!! Specify the custom consurf control panel
!!
! pipe_cp1 = "consurf/consurf.htm";
!
!! If you want the frontispiece to be reset every time you enter this
!! page, use false. If this is a one-page presentation (no contents)
!! and you want to be able to return from QuickViews without resetting
!! the view, use true.
!!
! frontispiece_conditional_on_return = true;
!
!! Open the command input slot/message box to 30% of window height.
!!
! pipe_show_commands = true;
! pipe_show_commands_pct = 30;
!
!! Don't show the PiPE presentation controls in the lower left frame.
!!
! pipe_hide_controls = true;
!
!! Hide development viewing mode links at the bottom of the control panel.
!!
! pipe_tech_info = false;
!
!! pipe_start_spinning = true; // default is PE's Preference setting.
!! top.nonStopSpin = true; // default: spinning stops after 3 min.
!!
!! ====== COLORS SECTION ======
!!
!color color_carbon C8C8C8
!color color_sulfur FFC832
!
!! Ten ConSurf color grades follow:
!!
!color color_grade0 FFFF96 insufficient data yellow
!color color_grade1 10C8D1 turquoise variable
!color color_grade2 8CFFFF
!color color_grade3 D7FFFF
!color color_grade4 EAFFFF