-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_mempack.pl
executable file
·1205 lines (1009 loc) · 32.1 KB
/
run_mempack.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
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
##
## ********************************************************
## * MEMPACK - Predicting transmembrane helix packing *
## * arrangements using residue contacts and *
## * a force-directed algorithm. *
## * Copyright (C) 2009 Timothy Nugent and David T. Jones *
## ********************************************************
##
## This program is copyright and may not be distributed without
## permission of the author unless specifically permitted under
## the terms of the license agreement.
##
## THIS SOFTWARE MAY ONLY BE USED FOR NON-COMMERCIAL PURPOSES. PLEASE
## CONTACT THE AUTHOR IF YOU REQUIRE A LICENSE FOR COMMERCIAL USE.
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use Round qw(:all);
use Getopt::Long;
## NCBI / Database paths - these need to be set!
my $ncbidir = '';
my $dbname = '';
## Executable and input/output paths
my $mem_dir = '';
my $input_path = $mem_dir.'input/';
my $output_path = $mem_dir.'output/';
my $model_path = $mem_dir.'models/';
my $datadir = $mem_dir.'data/';
my $svm_classify = $mem_dir.'bin/svm_classify';
my $mtx = 0;
my $remove_files = 0;
my $globmem = 0;
my $graphics = 1;
my $format = 1;
my $cores = 1;
my $erase_previous = 0;
my $draw_rr_contacts = 1;
my (@mtx,$blast_out,$svm_all,%range,$header);
my ($system);
my %topology = ();
my ($sequence,@topology,%profile,@empty,$topology_string,$length,%aa,%aa3,@all_contact);
my $window = 7;
my $dp = 0.0001;
my $verbose = 0;
my $distance = 1;
my $def = 1;
my $kk_plot = $mem_dir.'bin/kk_plot';
my $font = 'lib/arial.ttf';
&main();
exit;
sub main{
print "\n********************************************************\n";
print "* MEMPACK - Predicting transmembrane helix packing *\n";
print "* arrangements using residue contacts and *\n";
print "* a force-directed algorithm. *\n";
print "* Copyright (C) 2009 Timothy Nugent and David T. Jones *\n";
print "********************************************************\n\n";
&get_arguments();
&get_normalisation_values();
# If we've been passed .mtx files rather than fasta files
if ($mtx){
foreach (@ARGV){
push @mtx,$_;
$_ =~ s/\.mtx|\.lmtx//g;
$header = $_;
}
if ($erase_previous){
my $erase_string = $input_path.$header."*";;
if($header =~ /\//){
my @split = split(/\//,$header);
$erase_string = $input_path.$split[-1]."*";
}
$system = `rm $erase_string &> /dev/null`;
$erase_string =~ s/^$input_path/$output_path/;
$system = `rm $erase_string &> /dev/null`;
}
# Otherwise run PSI-BLAST to create .mtx files
}else{
my @fastas;
foreach (@ARGV){
$header = $_;
$header =~ s/\.fasta//g;
$header =~ s/\.fa//g;
if ($erase_previous){
my $erase_string = $input_path.$header."*";;
if($header =~ /\//){
my @split = split(/\//,$header);
$erase_string = $input_path.$split[-1]."*";
}
$system = `rm $erase_string`;
$erase_string =~ s/^$input_path/$output_path/;
$system = `rm $erase_string`;
}
push @fastas,$_;
&run_psiblast($_);
}
@ARGV = @fastas;
}
if($header =~ /\//){
my @split = split(/\//,$header);
$header = $split[-1];
}
$system = `rm $input_path/$header* &> /dev/null` if $erase_previous;
$system = `rm $output_path/$header* &> /dev/null` if $erase_previous
&load_mtx($header);
## Predict lipid exposure
my $input_file = $input_path.$header."_LIPID_EXP.dat";
if (-e $input_file){
if (-s $input_file){
`rm $input_file`;
}else{
exit;
}
}
open (INPUT,">$input_file");
my (%lipid_scores,@positions,@residues);
## Loop through all the helices
my $helix1_count = 1;
for (my $h = 0; $h < scalar @topology; $h+=2){
#print "TMH $helix1_count :\t$topology[$h] - $topology[$h+1]\n";
my $helix1_length = $topology[$h+1] - $topology[$h] + 1;
## Loop through all positions in this helix
for my $p1 ($topology[$h]..$topology[$h+1]){
my ($array,$p1_seq) = &create_lipid_input($p1);
print INPUT "0 ";
my $feature = 1;
foreach my $v (@{$array}){
print INPUT $feature.":".$v." ";
$feature++;
}
push @positions,$p1;
push @residues,substr($p1_seq,3,1);
my $comment = $p1."_".$p1_seq;
print INPUT "# $comment\n";
}
$helix1_count++;
}
close INPUT;
my $model = $model_path."LIPID_EXPOSURE_ALL.model";
my $prediction = $output_path.$header."_LIPID_EXPOSURE.predictions";
if (-e $model){
if (-e $prediction){
print "$prediction exists!\n\n";
}else{
print "$svm_classify -v 0 $input_file $model $prediction\n";
$system = `$svm_classify -v 0 $input_file $model $prediction`;
}
}else{
die "$model doesn't exist.\n";
}
my $lipid_out = $output_path.$header."_LIPID_EXPOSURE.results";
if (-e $lipid_out){
print "$lipid_out exists!\n\n";
unless(open(LIPID,$prediction)){
die "Couldn't open $prediction\n";
}else{
my $count = 0;
while(<LIPID>){
my $value = $_;
$value =~ s/\s+//g;
$lipid_scores{$positions[$count]} = $value;
$count++;
}
}
}else{
unless(open(LIPID_OUT,">$lipid_out")){
die "Couldn't write to $lipid_out\n";
}
print LIPID_OUT "Pos\tRes\tScore\n";
unless(open(LIPID,$prediction)){
die "Couldn't open $prediction\n";
}else{
if (scalar @positions != scalar @residues && scalar @residues != scalar keys %lipid_scores){
die "Predictions do not equal number of residues!\n\n";
}
my $count = 0;
while(<LIPID>){
my $value = $_;
$value =~ s/\s+//g;
$lipid_scores{$positions[$count]} = $value;
print LIPID_OUT "$positions[$count]\t$residues[$count]\t$value\n";
$count++;
}
}
close LIPID_OUT;
print "Written $lipid_out\n\n";
}
## Generate SVM input files for residue-residue contact prediction
$input_file = $input_path.$header."_CONTACT.dat";
$helix1_count = 1;
for (my $h = 0; $h < scalar @topology; $h+=2){
## Loop through all positions in this helix
my $helix1_pos = 1;
for my $p1 ($topology[$h]..$topology[$h+1]){
## Loop through all the helices after current one
my $helix2_count = $helix1_count + 1;
for (my $nh = $h+2; $nh < scalar @topology; $nh+=2){
my $helix2_pos = 1;
for my $p2 ($topology[$nh]..$topology[$nh+1]){
my $tmp_res = $p1."-".$p2;
push @all_contact,$tmp_res;
$helix2_pos++;
}
$helix2_count++;
}
$helix1_pos++;
}
$helix1_count++;
}
if (!-e $input_file){
open (INPUT,">$input_file");
## Loop through all the helices
$helix1_count = 1;
for (my $h = 0; $h < scalar @topology; $h+=2){
#print "TMH $helix1_count :\t$topology[$h] - $topology[$h+1]\n";
my $helix1_length = $topology[$h+1] - $topology[$h] + 1;
## Loop through all positions in this helix
my $helix1_pos = 1;
for my $p1 ($topology[$h]..$topology[$h+1]){
## Loop through all the helices after current one
my $helix2_count = $helix1_count + 1;
for (my $nh = $h+2; $nh < scalar @topology; $nh+=2){
my $helix2_length = $topology[$nh+1] - $topology[$nh] + 1;
## Loop through all positions in next helix
my $helix2_pos = 1;
for my $p2 ($topology[$nh]..$topology[$nh+1]){
print "$p1 (TMH ".$helix1_count.") --> $p2 (TMH ".$helix2_count.")\n" if $verbose;
my $distance = $p2 - $p1;
# Get windows around these two residues
my ($array,$p1_p2_seq) = &create_contact_input($p1,$p2);
## Global features
print "Helix $helix1_count length = $helix1_length\n" if $verbose;
print "Residue 1 pos in helix = $helix1_pos\n" if $verbose;
my ($relative_pos1,$relative_pos2);
if ($helix1_count % 2){
$relative_pos1 = $helix1_pos/$helix1_length;
}else{
$relative_pos1 = 1 - ($helix1_pos/$helix1_length);
}
print "Residue 1 relative pos in helix = $relative_pos1\n" if $verbose;
print "Helix $helix2_count length = $helix2_length\n" if $verbose;
print "Residue 2 pos in helix = $helix2_pos\n" if $verbose;
if ($helix2_count % 2){
$relative_pos2 = $helix2_pos/$helix2_length;
}else{
$relative_pos2 = 1 - ($helix2_pos/$helix2_length);
}
print "Residue 2 relative pos in helix = $relative_pos2\n" if $verbose;
print "Distance = $distance\n\n" if $verbose;
my $s1 = substr($sequence,$p1-1,1);
my $s2 = substr($sequence,$p2-1,1);
my $res1 = $aa3{$s1};
my $res2 = $aa3{$s2};
my $label = 0;
print INPUT "$label ";
my $feature = 1;
foreach my $v (@{$array}){
print INPUT $feature.":".$v." ";
$feature++;
}
## Relative position in helix 1
print INPUT $feature.":",nearest_ceil($dp,$relative_pos1)," ";
$feature++;
## Relative position in helix 2
print INPUT $feature.":",nearest_ceil($dp,$relative_pos2)," ";
$feature++;
## Distance between residues
if ($distance <= 25){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 25 && $distance <= 50){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 50 && $distance <= 75){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 75 && $distance <= 100){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 100 && $distance <= 125){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 125 && $distance <= 150){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 150 && $distance <= 175){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 175 && $distance <= 200){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
if ($distance > 200){
print INPUT $feature.":1 ";
}else{
print INPUT $feature.":0 ";
}
$feature++;
## Add lipid exposure scores
print INPUT $feature.":".$lipid_scores{$p1}." ";
$feature++;
print INPUT $feature.":".$lipid_scores{$p2}." ";
$feature++;
my $comment = $p1."_".$p2."_".$p1_p2_seq."_".$res1."_".$res2;
print INPUT "# $comment\n";
$helix2_pos++;
}
$helix2_count++;
}
$helix1_pos++;
}
$helix1_count++;
}
close INPUT;
}else{
print "$input_file exits!\n\n";
}
$model = $model_path."CONTACT_ALL_DEF1.model";
$prediction = $output_path.$header."_CONTACT_DEF1.predictions";
my $output = $output_path.$header."_CONTACT_DEF1.results";
my $graph_out = $output_path.$header."_graph.out";
if($def == 3){
$model = $model_path."CONTACT_ALL_DEF3.model";
$prediction = $output_path.$header."_CONTACT_DEF3.predictions";
$output = $output_path.$header."_CONTACT_DEF3.results";
}elsif($def == 2){
$model = $model_path."CONTACT_ALL_DEF2.model";
$prediction = $output_path.$header."_CONTACT_DEF2.predictions";
$output = $output_path.$header."_CONTACT_DEF2.results";
}
if (-e $model){
if (-e $prediction){
print "$prediction exists!\n\n";
}else{
print "$svm_classify -v 0 $input_file $model $prediction\n";
$system = `$svm_classify -v 0 $input_file $model $prediction`;
}
}else{
die "$model doesn't exist.\n";
}
## Construct results file
my %residue_to_helix;
my $helix = 1;
for (my $h = 0;$h < scalar @topology; $h += 2){
my $r_start = $topology[$h];
my $r_stop = $topology[$h+1]+1;
for my $r ($r_start..$r_stop){
$residue_to_helix{$r} = $helix;
}
$helix++;
}
my %contact_scores;
my @predicted_contact = ();
my %pred_hh;
if (-e $output){
print "$output exists!\n\n";
unless(open(OUTPUT,$output)){
die "Couldn't open $output\n";
}else{
while(<OUTPUT>){
next if $_ =~ /Topology/;
my @split = split(/\s+/,$_);
push @predicted_contact,$split[0];
$pred_hh{$split[1]} = 1;
}
close OUTPUT;
}
}else{
open(OUT,">$output");
my $top_string = "";
print OUT "# Topology:\t";
foreach my $t (@topology){
$top_string = $top_string.$t.",";
}
chop $top_string;
print OUT "$top_string\n";
unless(open(CONTACT,$prediction)){
die "Couldn't open $prediction\n";
}else{
my $count = 0;
while(<CONTACT>){
my $value = $_;
$value =~ s/\s+//g;
if($value > 0){
my @split = split(/-/,$all_contact[$count]);
if (exists $residue_to_helix{$split[0]} && exists $residue_to_helix{$split[1]}){
print OUT "$all_contact[$count]\t",$residue_to_helix{$split[0]},"-",$residue_to_helix{$split[1]},"\t$value\n";
push @predicted_contact,$all_contact[$count];
my $hh = $residue_to_helix{$split[0]}."-".$residue_to_helix{$split[1]};
$pred_hh{$hh} = 1;
}
}
$count++;
}
close CONTACT;
}
close OUT;
print "Written $output\n\n";
}
exit unless $graphics;
## Draw layout
unless (scalar keys %pred_hh){
print "\nNo predicted contacts!\n\n";
exit;
}
print "Generating layout...\n";
my $system = `rm $graph_out` if -e $graph_out;
print "$kk_plot $output > $graph_out\n\n";
$system = `$kk_plot $output > $graph_out`;
if (!-e $graph_out){
die "Couldn't plot layout!\n\n";
}
my @rotations = ();
my $image_no = 1;
my %kk_graph;
if (-e $graph_out && $graphics){
if(load_module('DrawHelicalWheel')){
die "Couldn't load module DrawHelicalWheel (requires GD) - graphical output disabled.\n\n";
}
if(load_module('Image::Magick')){
die "Couldn't load module Image::Magick - graphical output disabled.\n\n";
}
open(KK,$graph_out);
my @kk = <KK>;
close KK;
my $tag = 0;
foreach my $k (@kk){
if ($k =~ /===/){
my @selected_helices = keys %kk_graph;
my $im;
if($draw_rr_contacts){
$im = DrawHelicalWheel->new(-title=>$header,
-sequence=>$sequence,
-helices=>\@topology,
-ttf_font=>$font,
-hh_contacts=>\%pred_hh,
-graph_centres=>\%kk_graph,
-residue_contacts=>\@predicted_contact,
-rotations=>\@rotations,
-helix_diameter=>235,
-only_these_helices=>,\@selected_helices
);
}else{
$im = DrawHelicalWheel->new(-title=>$header,
-sequence=>$sequence,
-helices=>\@topology,
-ttf_font=>$font,
-hh_contacts=>\%pred_hh,
-graph_centres=>\%kk_graph,
-rotations=>\@rotations,
-helix_diameter=>235,
-only_these_helices=>,\@selected_helices
);
}
my $svg = $output_path.$header.'_Kamada-Kawai.svg';
open(OUTPUT, ">$svg");
binmode OUTPUT;
print OUTPUT $im->svg;
close OUTPUT;
my $png = $output_path.$header.'_Kamada-Kawai_'.$image_no.'.png';
print "Generating JPG image $png\n\n";
my $p = Image::Magick->new(magick=>'png',quality=>100);
$p->Read($svg);
$p->Write($png);
$system = `rm $svg`;
$image_no++;
@rotations = ();
%kk_graph = ();
}
next unless $k =~ /\(/;
my @split = split(/\s+/,$k);
my $x = 0;
my $y = 0;
if ($split[1] =~ /\((-?\d+\.?\d*e?-?\d*),(-?\d+\.?\d*e?-?\d*)\)/){
$x = $1;
$y = $2;
}
$kk_graph{$split[0]} = [$x,$y];
push @rotations,$split[2];
}
}else{
if ($graphics){
die "Couldn't create $graph_out!\n";
}
}
# Clean up
$system = `rm $input_path/$header* &> /dev/null` if $remove_files;
}
# Create input files for SVM classify
sub create_contact_input {
my ($pos1,$pos2) = @_;
my $p1_p2_seq = "";
my $array = ();
## Get window for 1st residude
print "$pos1\t- central position in SW sequence\n" if $verbose;
$pos1--; ## Since position 1 in @profile is 0
## Push all the arrays onto @array
for my $w ($pos1 - (($window- 1)/2) .. $pos1 + (($window- 1)/2)){
print "$w\t- window positions required from profile\n" if $verbose;
#if (defined @{$profile{$w}} ){
if (@{$profile{$w}} ){
push @{$array},@{$profile{$w}};
}else{
push @{$array},@empty;
}
}
## Generate sequence window for the comment
my $window_seq1 = "";
for my $j ($pos1 - (($window- 1)/2) .. $pos1 + (($window- 1)/2)){
print "j = $j\t- substring position required from sequence\n" if $verbose;
if ($j < 0 || $j >= $length){
$window_seq1 .= 'X';
}else{
$window_seq1 .= substr($sequence,$j,1);
}
}
print "$window_seq1\n\n" if $verbose;
## Get window for 2md residude
print "$pos2\t- central position in SW sequence\n" if $verbose;
$pos2--; ## Since position 1 in @profile is 0
## Push all the arrays onto @array
for my $w ($pos2 - (($window- 1)/2) .. $pos2 + (($window- 1)/2)){
print "$w\t- window positions required from profile\n" if $verbose;
push @{$array},@{$profile{$w}}
}
## Generate sequence window for the comment
my $window_seq2 = "";
for my $j ($pos2 - (($window- 1)/2) .. $pos2 + (($window- 1)/2)){
print "j = $j\t- substring position required from sequence\n" if $verbose;
if ($j < 0 || $j >= $length){
$window_seq2 .= 'X';
}else{
$window_seq2 .= substr($sequence,$j,1);
}
}
print "$window_seq2\n\n" if $verbose;
$p1_p2_seq = $window_seq1."-".$window_seq2;
return($array,$p1_p2_seq);
}
# Create input files for SVM classify
sub create_lipid_input {
my $pos1 = shift;
my $array = ();
$pos1--; ## Since position 1 in @profile is 0
for my $w ($pos1 - (($window- 1)/2) .. $pos1 + (($window- 1)/2)){
#print "$w\t- window positions required from profile\n";
#if (defined @{$profile{$w}} ){
if (@{$profile{$w}} ){
push @{$array},@{$profile{$w}};
}else{
push @{$array},@empty;
}
}
## Generate sequence window for the comment
my $window_seq1 = "";
for my $j ($pos1 - (($window- 1)/2) .. $pos1 + (($window- 1)/2)){
#print "j = $j\t- substring position required from sequence\n" if $verbose;
if ($j < 0 || $j >= $length){
$window_seq1 .= 'X';
}else{
$window_seq1 .= substr($sequence,$j,1);
}
}
#print "$window_seq1\n\n" if $verbose;
return($array,$window_seq1);
}
# Load PSI-BLAST mtx file
sub load_mtx{
my $header = shift;
my $mtx = $output_path.$header.".mtx";
my $lmtx = $output_path.$header.".lmtx";
if (-e $mtx[0]){
$mtx = $mtx[0];
}
if (-e $mtx){
open (MTX,$mtx);
@mtx = <MTX>;
close MTX;
}elsif(-e $lmtx){
open (MTX,$lmtx);
@mtx = <MTX>;
close MTX;
}
else{
die "Couldn't find $mtx or $lmtx\n";
}
$length = $mtx[0];
$sequence = $mtx[1];
$length =~ s/\s+//g;
$sequence =~ s/\s+//g;
#print "$sequence\n";
for (1..($length + $window- 1)){
if (($_ <= ($window- 1)/2)||($_ > $length + (($window- 1)/2))){
## Missing
@{$profile{$_}} = @empty;
}else{
## Start on MTX line 15
if (defined $mtx[$_+13-(($window- 1)/2)]){
@{$profile{$_}} = split(/\s+/,$mtx[$_+13-(($window- 1)/2)]);
## Now do the Z score normalisation
## Not normalised
#print "Not normalised: @{$profile{$_}}\n";
my $z_count = 0;
foreach my $z (@{$profile{$_}}){
## Normalise to Z score
$z= ($z - $range{$z_count}{'mean'})/$range{$z_count}{'sd'};
## Scale
$z = ($z + (-1 * $range{$z_count}{'lower'}))/$range{$z_count}{'range'};
## 5 Decimal places
$z = nearest_ceil($dp,$z);
$z_count++;
}
## Get 20 residues rather than 28
my @aa_20 = (${$profile{$_}}[1],${$profile{$_}}[3],${$profile{$_}}[4],${$profile{$_}}[5],${$profile{$_}}[6],
${$profile{$_}}[7],${$profile{$_}}[8],${$profile{$_}}[9],${$profile{$_}}[10],${$profile{$_}}[11],
${$profile{$_}}[12],${$profile{$_}}[13],${$profile{$_}}[14],${$profile{$_}}[15],${$profile{$_}}[16],
${$profile{$_}}[17],${$profile{$_}}[18],${$profile{$_}}[19],${$profile{$_}}[21],${$profile{$_}}[22]);
@{$profile{$_}} = @aa_20;
}
}
}
}
# Get arguments
sub get_arguments{
# Process command line arguments
if (!$ARGV[0]){
&usage;
}else{
my $result = GetOptions ("n=s" => \$ncbidir,
"mtx=i" => \$mtx,
"d=s" => \$dbname,
"j=s" => \$output_path,
"i=s" => \$input_path,
"t=s" => \$topology_string,
"w=s" => \$mem_dir,
"e=i" => \$remove_files,
"a=i" => \$def,
"g=i" => \$graphics,
"f=i" => \$erase_previous,
"c=i" => \$cores,
"r=i" => \$draw_rr_contacts,
"h" => sub {&usage;});
## Get rid of trailing slashes
$ncbidir =~ s/\/+$//;
unless($mtx){
## Check the NCBI directory
unless (-d $ncbidir){
## Look for the NCBI directory
my $system = `which blastpgp`;
if ($system =~ /(.*)\/blastpgp$/){
$ncbidir = $1;
$ncbidir =~ s/\s+//g;
}
unless (-d $ncbidir){
print "NCBI directory $ncbidir doesn't exist. Please pass it using\n";
print "the -n paramater or modifiy the value at the top of the script.\n\n";
exit;
}
}
## Make sure we can find blastpgp & makemat
my $psiblast = $ncbidir."/blastpgp";
my $makemat = $ncbidir."/makemat";
unless (-e $psiblast){
print "Can't find the program blastpgp in the NCBI directory $ncbidir\n";
print "Please pass the correct NCBI location using the -n paramater or modifiy\n";
print "the value at the top of the script..\n\n";
exit;
}
unless (-e $makemat){
print "Can't find the program makemat in the NCBI directory $ncbidir\n";
print "Please pass the correct NCBI location using the -n paramater or modifiy\n";
print "the value at the top of the script..\n\n";
exit;
}
if ($dbname eq ''){
print "The database name for PSI-BLAST searches has not been set. Please pass it using\n";
print "the -d paramater or modifiy the value at the top of the script.\n\n";
exit;
}
}
if (defined $topology_string){
@topology = split(/,/,$topology_string);
#shift @topology;
if(scalar @topology % 2){
print "Uneven number of helix boundaries detected.\n\n";
exit;
}
}else{
print "Topology for this sequence was not provided. Please pass it using\n";
print "the -t paramater.\n\n";
exit;
}
if (defined $mem_dir){
$svm_classify = $mem_dir.'bin/svm_classify';
$kk_plot = $mem_dir.'bin/kk_plot';
# $input_path = $mem_dir.'input/';
my $new_lib = $mem_dir."lib";
$font = $mem_dir.$font;
unshift @INC, $new_lib;
}
if($output_path =~ /^output\/$/)
{
$output_path = $mem_dir.'output/';
}
$model_path = $mem_dir.'models/';
$datadir = $mem_dir.'data/';
unless (-e $svm_classify){
print "Can't find $svm_classify - have you run make?\n\n";
exit;
}
}
}
# Usage
sub usage {
print "Version 1.0\n\n";
print "Usage: run_mempack.pl [options] <fasta file>\n\n";
print "Options:\n\n";
print "-a <1|2|3> Distance between atoms in interacting residue pair. Default 1.\n";
print " 1 = Less than 8 angstroms between C-beta atoms (C-alpha for glycine).\n";
print " 2 = Less than the sum of their van der Waals radii plus a threshold of 0.6 angstroms.\n";
print " 3 = Less than 5.5 angstroms between sidechain or backbone heavy atoms.\n";
print "-mtx <0|1> Process PSI-BLAST .mtx files instead of fasta files. Default 0.\n";
print "-n <directory> NCBI binary directory (location of blastpgp and makemat)\n";
print "-d <path> Database for running PSI-BLAST.\n";
print "-t <topology> Transmembrane topology helix boundaries in the form: 20,35,50,70,91,108\n";
print "-i <path> Path to folder for SVM classify input files. Default input/\n";
print "-j <path> Output path for all files. Default: output/\n";
print "-w <path> Directory that contains mempack. Default ''\n";
print "-e <0|1> Erase intermediate files. Default 0.\n";
print "-f <0|1> Erase files from previous runs. Default 0.\n";
print "-g <0|1> Draw schematic. Default 1.\n";
print "-r <0|1> Draw residue-residue contacts. Default 1.\n";
print "-c <int> Number of CPU cores to use for PSI-BLAST. Default 1.\n";
print "-h <0|1> Show help. Default 0.\n\n";
exit;
}
# Run PSI-BLAST
sub run_psiblast {
my $fasta = shift;
my $mtx;
if ($fasta =~ /\//){
my @tmp = split(/\//,$fasta);
$mtx = $tmp[-1].".mtx";
}else{
$mtx = $fasta.".mtx";
}
if ($fasta =~ /mtx/){
print "This looks like an .mtx file! It should be a fasta file, otherwise\n";
print "pass the -mtx 1 flag.\n\n";
exit;
}
$mtx =~ s/\.fa//;
$mtx =~ s/\.fasta//;
my $out_mtx = $output_path.$mtx;
my $blast_out = "mempack_tmp.out";
die "Fasta file $fasta doesn't exist!\n" unless -e $fasta;
unless (-e $out_mtx || -e $mtx){
print "Running PSI-BLAST: $fasta\n";
my $system = `cp -f $fasta mempack_tmp.fasta`;
print "$ncbidir/blastpgp -a $cores -j 2 -h 1e-3 -e 1e-3 -b 0 -d $dbname -i mempack_tmp.fasta -C mempack_tmp.chk >& $blast_out\n\n";
$system = `$ncbidir/blastpgp -a $cores -j 2 -h 1e-3 -e 1e-3 -b 0 -d $dbname -i mempack_tmp.fasta -C mempack_tmp.chk >& $blast_out`;
unless (-e "mempack_tmp.chk"){
print "There was an error running PSI-BLAST. Did you set the database path correctly?\n\n";
open(ERROR,$blast_out);
my @error = <ERROR>;
close ERROR;
foreach my $line (@error){
print $line;
}
print "\n";
exit;
}
$system = `echo mempack_tmp.chk > mempack_tmp.pn`;
$system = `echo mempack_tmp.fasta > mempack_tmp.sn`;
$system = `echo "$ncbidir/makemat -P mempack_tmp"`;
$system = `$ncbidir/makemat -P mempack_tmp`;
$system = `cp mempack_tmp.mtx $mtx`;
$system = `rm -f mempack_tmp*`;
$system = `rm -f error.log` if -e "error.log";
}
if (-e $mtx){
$system = `mv $mtx $output_path`;
$mtx = $output_path.$mtx;
push @mtx,$mtx;;
}elsif (-e $out_mtx){