-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_memsat-svm.pl
executable file
·1349 lines (1133 loc) · 40.7 KB
/
run_memsat-svm.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
## MEMSAT-SVM script
##
## *********************************************************
## * MEMSAT-SVM - MEMbrane protein Structure *
## * And Topology using Support Vector Machines *
## * Integral Membrane Protein Topology Prediction Program *
## * Copyright (C) 2008 Tim Nugent *
## *********************************************************
##
## 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 Cwd;
use Getopt::Long;
use GD;
## IMPORTANT : this variable must be set here to the absolute path to the
## library called "lib" and found in the same directory as this script.
use FindBin;
use lib "$FindBin::Bin/lib";
use Math::Round qw(:all);
## IMPORTANT : this variable must be set to the directory containing this
## script either here or using flag '-w' at runtime.
# my $mem_dir = '/cs/research/bioinf/home1/green/dbuchan/Code/memsat-svm';
my $mem_dir = '';
## IMPORTANT : these paths (folder with the NCBI executables and path to the
## database) must be set either here or using the appropriate flags at runtime.
# my $ncbidir = '/scratch0/NOT_BACKED_UP/dbuchan/Applications/blast-2.2.26/bin/';
# my $dbname = '/scratch0/NOT_BACKED_UP/dbuchan/uniref/test_db.fasta';
my $ncbidir = '';
my $dbname = '';
## Executable and input/output paths are initialised in sub get_arguments.
my ($input_path, $output_path, $model_path, $datadir, $svm_classify,
$memsat_svm_bin_path, $globmem_bin_path, $mem_pred_svm_bin_path, $nnsat_bin_path);
my $mtx = 0;
my $remove_files = 0;
my $globmem = 0;
my $graphics = 1;
my $format = 1;
my $runmem3 = 0;
my $cores = 1;
my $globmem_score_threshold = -0.144;
my $global_counter = 0;
my $helix_score = 220;
my $re_helix_score = 178;
my $signal = 1;
my %models = ('MEMSAT-SVM_w27_RE.model'=>27, 'MEMSAT-SVM_w27_SP.model'=>27,
'MEMSAT-SVM_w33_HL.model'=>33, 'MEMSAT-SVM_w35_IO.model'=>35,
'MEMSAT-SVM_w33_GM.model'=>33);
my @windows = ("27", "33", "35", "GM");
my (@mtx, $blast_out, $filename, $svm_all, $memsat_out, $memsat_out_single, $globmem_out,
$png_schematic_out, $png_cartoon_out, $png_cartoon_memsat3_out,
%range, %raw_hl, %raw_io, %raw_re, %raw_sp, %mtx_list, $header, @constraints);
my ($HL_prediction, $IO_prediction, $RE_prediction, $SP_prediction, $GM_prediction, $system);
my %topology = ();
&main();
exit;
# Subroutines
sub main {
print "\nMEMSAT-SVM: Alpha-helical transmembrane protein topology prediction\n";
print "using Support Vector Machines\n\n";
&get_arguments();
# If we've been passed .mtx files rather than fasta files
if ($mtx){
foreach (@ARGV){
if ($_ =~ /constraints/){
push @constraints,$_;
}else{
push @mtx,$_;
}
}
# Otherwise run PSI-BLAST to create .mtx files
}else{
my @fastas;
foreach (@ARGV){
# Deal with constraints
if ($_ =~ /constraints/){
push @constraints,$_;
}else{
push @fastas,$_;
&run_psiblast($_);
}
}
@ARGV = @fastas;
}
# Create filenames for input files if we are processing multiple sequences
if ($mtx[0] ne $mtx[-1]){
my ($x,$y);
if ($mtx[0] =~ /\//){
my @tmp = split(/\//,$mtx[0]);
$x = $tmp[-1];
}else{
$x = $mtx[0];
}
if ($mtx[-1] =~ /\//){
my @tmp = split(/\//,$mtx[-1]);
$y = $tmp[-1];
}else{
$y = $mtx[-1];
}
$header = $x."-".$y;
}else{
if ($mtx[0] =~ /\//){
my @tmp = split(/\//,$mtx[0]);
$header = $tmp[-1];
}else{
$header = $mtx[0];
}
}
$header =~ s/\.mtx//g;
$header =~ s/\.fasta//g;
$header =~ s/\.fa//g;
# Create filenames for SVM prediction files if we are processing multiple sequences
$HL_prediction = $output_path.$header."_SVM_w33_HL.prediction";
$IO_prediction = $output_path.$header."_SVM_w35_IO.prediction";
$RE_prediction = $output_path.$header."_SVM_w27_RE.prediction";
$SP_prediction = $output_path.$header."_SVM_w27_SP.prediction";
$GM_prediction = $output_path.$header."_SVM_w33_GM.prediction";
# Remove input files from previous runs
foreach my $req_win (@windows){
my $input = $input_path.$header."_w".$req_win.".input";
my $erase = `rm $input` if -e $input;
}
print "Generating SVM input files...\n";
# Create SVM classify input files
if($runmem3 != 2)
{
foreach (@mtx){
my @tmp = split(/\./,$_);
$tmp[0] =~ s/output\///;
&create_input($_);
}
}
# Do SVM classification
if($runmem3 != 2)
{
&classify();
}
# Parse predictions and run memsat-svm, globmem-svm and produce graphics
foreach (@mtx){
my @tmp2 = split(/\//, $_);
my @tmp = split(/\./, $tmp2[-1]);
$filename = (defined($memsat_out_single) && (scalar(@mtx) == 1)) ? $memsat_out_single : $tmp[0];
$memsat_out = $output_path.$filename.".memsat_svm";
$globmem_out = $output_path.$filename.".globmem_svm";
$svm_all = $output_path.$filename."_SVM_ALL.out";
$png_schematic_out = $output_path.$filename."_schematic.png";
$png_cartoon_out = $output_path.$filename."_cartoon_memsat_svm.png";
$png_cartoon_memsat3_out = $output_path.$filename."_cartoon_memsat3.png";
&parse_predictions($_) if ($runmem3 != 2);
&run_memsat3($_) if ($runmem3);
&run_memsat($_) unless (($globmem == 2) || ($runmem3 == 2));
print "Written file $globmem_out\n" if ($globmem);
print "\n";
}
# Clean up
$system = `rm $input_path/$header* &> /dev/null` if $remove_files;
$system = `rm $HL_prediction $IO_prediction $RE_prediction $SP_prediction &> /dev/null` if $remove_files;
}
# Process command line arguments
sub get_arguments {
my $result = GetOptions ("n=s" => \$ncbidir,
"mtx=i" => \$mtx,
"d=s" => \$dbname,
"o=s" => \$memsat_out_single,
"i=s" => \$input_path,
"j=s" => \$output_path,
"w=s" => \$mem_dir,
"e=i" => \$remove_files,
"p=i" => \$globmem,
"g=i" => \$graphics,
"m=i" => \$helix_score,
"r=i" => \$re_helix_score,
"f=i" => \$format,
"3=i" => \$runmem3,
"s=i" => \$signal,
"c=i" => \$cores,
"h" => sub {&usage;});
&usage if (!$ARGV[0]);
## Make paths absolute.
my $currentWD = cwd();
foreach my $path ($mem_dir, $input_path, $output_path, $ncbidir, $dbname)
{
next unless (defined($path));
$path = "$currentWD/$path" unless ((substr($path, 0, 1) eq '/') || (substr($path, 0, 1) eq '~'));
}
## Make sure directories end with slash...
foreach my $path ($mem_dir, $input_path, $output_path)
{
next unless (defined($path));
$path .= '/' unless ($path =~ m{/$});
}
## ...but get rid of any trailing slashes to the NCBI directory.
$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 1;
}
}
## 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 parameter or modify\n";
print "the value at the top of the script..\n\n";
exit 1;
}
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 parameter or modify\n";
print "the value at the top of the script.\n\n";
exit 1;
}
unless (-T $dbname){
print "$dbname";
print "The database name for PSI-BLAST searches has not been set correctly.\n";
print "Please pass it using the -d parameter or modify the value at the top of the script.\n\n";
exit 1;
}
}
## Check that $mem_dir is valid and then set all other paths.
my $this_exe = $0;
$this_exe =~ s{^.*/}{};
unless (-e $mem_dir.$this_exe){
print "The path to the main MEMSAT-SVM directory seems to be incorrect.\n";
print "Please pass it using the -w parameter or modify the value at the top of the script.\n\n";
exit 1;
}
$input_path = $mem_dir.'input/' unless (defined($input_path));
$output_path = $mem_dir.'output/' unless (defined($output_path));
unless ((-d $output_path) && (-w $output_path)){
print "Cannot find (or write to) the 'output' directory: ".$output_path."\n";
print "Please pass valid values using the '-j' flag or use the default folder.\n\n";
exit 1;
}
unless ((-d $input_path) && (-w $input_path)){
print "Cannot find (or write to) the 'input' directory: ".$input_path."\n";
print "Please pass valid values using the '-i' flags or use the default folders.\n\n";
exit 1;
}
$model_path = $mem_dir.'models/';
$datadir = $mem_dir.'data/';
$svm_classify = $mem_dir.'bin/svm_classify';
$memsat_svm_bin_path = $mem_dir.'bin/memsat-svm';
$globmem_bin_path = $mem_dir.'bin/globmem';
$mem_pred_svm_bin_path = $mem_dir.'bin/mem_pred';
$nnsat_bin_path = $mem_dir.'bin/nnsat';
unless (-e $memsat_svm_bin_path){
print "Can't find $memsat_svm_bin_path - have you run make?\n\n";
exit 1;
}
unless (-e $svm_classify){
print "Can't find $svm_classify - have you run make?\n\n";
exit 1;
}
}
# Usage
sub usage {
print "Version 1.3\n\n";
print "Usage: run_memsat-svm.pl [options] <fasta file 1> [<constraints file 1>] [<fasta file 2> [<fasta file 3>...]]\n\n";
print "Options:\n\n";
print "-p <0|1|2> Programs to run. memsat-svm predicts topology, globmem-svm\n";
print " discriminates between transmembrane and globular proteins. Default 0.\n";
print " 0 = Run memsat-svm\n";
print " 1 = Run memsat-svm and globmem-svm\n";
print " 2 = Run globmem-svm\n";
print "-s <0|1> Run memsat-svm with signal peptide function. Default: 1\n";
print "-3 <0|1|2> Run memsat version 3. Default 0.\n";
print " 0 = Run memsat-svm\n";
print " 1 = Run memsat-svm and memsat3\n";
print " 2 = Run memsat3\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 "-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> Main MEMSAT-SVM directory that contains run_memsat-svm.pl. Default ''\n";
print "-o <filename> Output filename when running a single sequence. Default:\n";
print " <fasta file>.memsat_svm\n";
print "-f <1|2|3> Output format for topology string. Default: 1\n";
print " 1 = 6-21,40-57,142-172,214-236,276-302\n";
print " 2 = A.6,21;B.40,57;C.142,172;D.214,236;E.276,302\n";
print " 3 = i6-21o40-57i142-172o214-236i276-302o\n";
print "-e <0|1> Erase intermediate files. Default 0.\n";
print "-g <0|1> Draw topology schematic and cartoon. Default 1.\n";
print "-m <int> Minimum score for a transmembrane helix. Default: 220\n";
print "-r <int> Minimum score for a re-entrant helix. Default: 178\n";
print "-h <0|1> Show help. Default 0.\n";
print "-c <int> Number of CPU cores to use for PSI-BLAST. Default 1.\n\n";
print "A contraints file for fasta file XYZ.fa must be named XYZ.constraints\n\n";
print "The constraints file should have the following format, where s,o,m,i\n";
print "are signal peptide, outside loop, membrane and inside loop:\n";
print "s: 1-15\n";
print "o: 1-30\n";
print "m: 37-59,82-100\n";
print "i: 65,80,220-230\n\n";
exit 1;
}
# Run PSI-BLAST
sub run_psiblast {
my $fasta = shift;
my $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 1;
}
if ($fasta =~ /\//){
my @tmp = split(/\//,$fasta);
$mtx = $tmp[-1].".mtx";
}else{
$mtx = $fasta.".mtx";
}
my $out_mtx = $output_path.$mtx;
my $tmp_rootname = 'memsat-svm_tmp';
my $tmp_rootpath = $output_path.$tmp_rootname;
my $blast_out = "$tmp_rootpath.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 $tmp_fasta = "$tmp_rootpath.fasta";
my $tmp_chk = "$tmp_rootpath.chk";
my $system = `cp -f $fasta $tmp_fasta`;
print "$ncbidir/blastpgp -a $cores -j 2 -h 1e-3 -e 1e-3 -b 0 -d $dbname -i $tmp_fasta -C $tmp_chk >& $blast_out\n\n";
$system = `$ncbidir/blastpgp -a $cores -j 2 -h 1e-3 -e 1e-3 -b 0 -d $dbname -i $tmp_fasta -C $tmp_chk >& $blast_out`;
unless (-e $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 1;
}
$system = `echo $tmp_rootname.chk > $tmp_rootpath.pn`;
$system = `echo $tmp_rootname.fasta > $tmp_rootpath.sn`;
$system = `echo "$ncbidir/makemat -P $tmp_rootpath"`;
$system = `$ncbidir/makemat -P $tmp_rootpath`;
$system = `cp $tmp_rootpath.mtx $out_mtx`;
$system = `rm -f ${tmp_rootpath}*`;
unlink('error.log');
}
if (-e $mtx){
$system = `mv $mtx $output_path`;
$mtx = $output_path.$mtx;
push @mtx,$mtx;
}elsif (-e $out_mtx){
$mtx = $out_mtx;
push @mtx,$mtx;
}else{
die "Problem creating $mtx\n";
}
}
# Run memsat-svm and draw images
sub run_memsat {
my $mtx = shift;
my $seq = $mtx_list{$mtx};
my $constrained_prediction = "";
print "Running MEMSAT-SVM...\n";
foreach my $c (@constraints){
my ($x,$y) = split(/\./,$c);
if ($x =~ /$filename/){
$constrained_prediction = "-c ".$c." ";
}
}
my $run_command = "$memsat_svm_bin_path ".$constrained_prediction."-m $helix_score -r $re_helix_score -s $signal -f $format $svm_all > $memsat_out";
print "$run_command\n";
my $system = `$run_command`;
if (-e $memsat_out){
$system = `cat $memsat_out | grep "sequence length is"`;
if ($system){
print "Sequence length must be between 30 and 2000 residues.\n"
}else{
$system = `cat $memsat_out | grep "No transmembrane helices predicted"`;
if ($system){
print "No transmembrane helices predicted.\n"
}else{
## check signal score
$system = `cat $memsat_out | grep "^Signal peptide"`;
my @split = split(/\s+/,$system);
my $pred_signal = 'Not detected.';
if ($split[2] ne 'Not'){
if ($format == 1 || $format == 3){
my ($start,$stop) = split(/-/,$split[2]);
if ($format == 3){
$stop =~ s/o//g;
}
$pred_signal = $stop;
}else{
my ($start,$stop) = split(/,/,$split[2]);
$pred_signal = $stop;
}
}
## check predicted re-entrant region
$system = `cat $memsat_out | grep "^Re-entrant helices:"`;
@split = split(/\s+/,$system);
my @re_helix_split = split(/\D+/,$split[2]);
shift @re_helix_split if $format == 3;
shift @re_helix_split if $format == 2;
my $re_helix_array = \@re_helix_split;
$system = `cat $memsat_out | grep "^N-terminal:"`;
@split = split(/\s+/,$system);
my $pred_n = $split[1];
$system = `cat $memsat_out | grep "^Topology:"`;
@split = split(/\s+/,$system);
my @topology_split = split(/\D+/,$split[1]);
shift @topology_split if $format == 3;
shift @topology_split if $format == 2;
my $topology_array = \@topology_split;
print "\nWritten file $memsat_out\n";
unless(`perl -MGD -e 1 &> /dev/stdout`){
#print "@$topology_array\n$pred_n\n@$re_helix_array\n$pred_signal\n";
&draw_image($filename,$seq,$topology_array,$pred_n, $re_helix_array,$pred_signal) if $graphics;
print "Written file $png_schematic_out\n" if $graphics;
print "Written file $png_cartoon_out\n" if $graphics;
print "Written file $png_cartoon_memsat3_out\n" if $graphics && -e $png_cartoon_memsat3_out && $runmem3;
}else{
print "Couldn't find GD module. Graphics disabled.\n" if $graphics;
}
}
}
}else{
die "Problem running $memsat_svm_bin_path\n";
}
}
sub run_memsat3 {
my $mtx = shift;
my $seq = $mtx_list{$mtx};
print "\nRunning MEMSAT3...\n";
my $rootname = $output_path.$filename;
my $globmem = "$rootname.globmem";
my $nn = "$rootname.nn";
my $memsat3 = "$rootname.memsat3";
my $run_command ="$globmem_bin_path ".$datadir."glob_weights.dat $mtx > $globmem";
print "$run_command\n";
my $system = `$run_command`;
$run_command = "$mem_pred_svm_bin_path ".$datadir."weights.dat $mtx > $nn";
print "$run_command\n";
$system = `$run_command`;
$run_command = "$nnsat_bin_path $nn > $memsat3";
print "$run_command\n";
$system = `$run_command`;
print "\nWritten file $globmem\n";
print "Written file $nn\n";
print "Written file $memsat3\n";
print "\n" unless $runmem3 == 2;
unless (open (MEM3,$memsat3)){
die "Can't open $memsat3!\n";
}
my @memsat3 = <MEM3>;
close MEM3;
my $tag = 0;
foreach my $line (@memsat3){
if ($line =~ /^================/){
$tag++;
next;
}
next unless $tag;
if ($line =~ /^\d+:\s+\((\w+)\)\s(\d+)-(\d+)\s+\(-*\d+\.\d+\)/){
$topology{'MEMSAT3'}{'n_term'} = $1;
push @{$topology{'MEMSAT3'}{'topology_array'}},$2;
push @{$topology{'MEMSAT3'}{'topology_array'}},$3;
}
if ($line =~ /^\d+:\s+(\d+)-(\d+)\s+\(-*\d+\.\d+\)/){
push @{$topology{'MEMSAT3'}{'topology_array'}},$1;
push @{$topology{'MEMSAT3'}{'topology_array'}},$2;
}
}
if ($graphics && $runmem3 == 2){
&draw_image($filename,$seq,\%topology,'',[],0);
print "Written file $png_schematic_out\n";
print "Written file $png_cartoon_memsat3_out\n";
}
}
# Parse SVM output
sub parse_predictions {
my $mtx = shift;
print "Parsing SVM output files...\n";
my $seq = $mtx_list{$mtx};
my @seq = split(//,$seq);
my $length = length $seq;
if ($globmem != 2){
unless (open (HL,$HL_prediction)){
die "Can't open $HL_prediction!\n";
}
my @hl = <HL>;
close HL;
for($global_counter..($global_counter+$length-1)){
$hl[$_] =~ s/\s+//g;
$hl[$_] = Round::nearest_ceil(.001,$hl[$_]);
}
unless (open (IO,$IO_prediction)){
die "Can't open $IO_prediction!\n";
}
my @io = <IO>;
close IO;
for($global_counter..($global_counter+$length-1)){
$io[$_] =~ s/\s+//g;
$io[$_] = Round::nearest_ceil(.001,$io[$_]);
}
unless (open (RE,$RE_prediction)){
die "Can't open $RE_prediction!\n";
}
my @re = <RE>;
close RE;
for($global_counter..($global_counter+$length-1)){
$re[$_] =~ s/\s+//g;
$re[$_] = Round::nearest_ceil(.001,$re[$_]);
}
unless (open (SP,$SP_prediction)){
die "Can't open $SP_prediction!\n";
}
my @sp = <SP>;
close SP;
for($global_counter..($global_counter+$length-1)){
$sp[$_] =~ s/\s+//g;
$sp[$_] = Round::nearest_ceil(.001,$sp[$_]);
}
if (scalar @hl != scalar @io || scalar @hl != scalar @re || scalar @re != scalar @sp){
die "Uneven number of values for $header\n";
}
open (RAW,">$svm_all");
my $seq_counter = 0;
for ($global_counter..($global_counter+$length-1)){
print RAW "$seq[$seq_counter]\t$hl[$_]\t$io[$_]\t$re[$_]\t$sp[$_]\n";
#print "$seq[$_]\t$hl[$_]\t$io[$_]\t$re[$_]\t$sp[$_]\n";
$raw_hl{$seq_counter+1} = $hl[$_];
$raw_io{$seq_counter+1} = $io[$_];
$raw_re{$seq_counter+1} = $re[$_];
$raw_sp{$seq_counter+1} = $sp[$_];
$seq_counter++;
}
close RAW;
}
if ($globmem){
print "Running GLOBMEM-SVM...\n";
unless (open (GM,$GM_prediction)){
die "Can't open $GM_prediction!\n";
}
my @gm = <GM>;
close GM;
my $tm_residues = 0;
my $tm_score = 0;
for($global_counter..($global_counter+$length-1)){
$gm[$_] =~ s/\s+//g;
$tm_residues++ if $gm[$_] > $globmem_score_threshold;
if ($gm[$_] > 0){
$tm_score += $gm[$_];
}
}
if (1){
open (GMO,">$globmem_out");
print GMO "Transmembrane residues found:\t$tm_residues\n";
print GMO "Transmembrane score:\t\t$tm_score\n";
# Using the same threshold as in the benchmarking, see the publication.
if ($tm_residues > 0){
print GMO "This looks like a transmembrane protein.\n";
}else{
print GMO "This looks like a globular protein.\n";
}
close GMO;
}
}
$global_counter += $length;
}
# Run SVM classify
sub classify {
print "Running svm_classify...\n";
foreach my $model (keys %models){
my $type;
if ($model =~ /HL/){
$type = 'HL';
}elsif($model =~ /IO/){
$type = 'IO';
}elsif($model =~ /SP/){
$type = 'SP';
}elsif($model =~ /RE/){
$type = 'RE';
}elsif($model =~ /GM/){
$type = 'GM';
next unless $globmem;
}
if ($globmem == 2){
next unless $type eq 'GM';
}
my $input;
if ($type eq 'GM'){
$input = $input_path.$header."_w".$models{$model}."_GM.input";
}else{
$input = $input_path.$header."_w".$models{$model}."_TM.input";
}
my $prediction = $output_path.$header."_SVM_w".$models{$model}."_".$type.".prediction";;
$model = $model_path.$model;
if (-e $model){
print "$svm_classify -v 0 $input $model $prediction\n" unless -e $prediction;
my $system = `$svm_classify -v 0 $input $model $prediction` unless -e $prediction;
}else{
die "$model doesn't exist.\n";
}
}
print "\n";
}
# Create input files for SVM classify
sub create_input {
my $mtx = shift;
my @mtx = ();
if (-e $mtx){
open (MTX,$mtx);
@mtx = <MTX>;
close MTX;
$mtx[1] =~ s/\s+//g;
$mtx_list{$mtx} = $mtx[1];
}else{
die "Couldn't find $mtx\n";
}
if ($mtx[0] =~ /\>/){
print "This looks like a fasta file! It should be a .mtx file, otherwise\n";
print "pass the -mtx 0 flag.\n\n";
exit 1;
}
my $length = length $mtx_list{$mtx};
my $seq = $mtx_list{$mtx};
my @empty = ();
for (1..20){
push @empty,0;
}
foreach my $reqwin (@windows){
if ($globmem == 2){
next unless $reqwin eq "GM";
}
my ($input,$win);
next if ($globmem == 0 && $reqwin eq "GM");
if ($reqwin eq "GM"){
&get_gm_normalisation_values();
$win = 33;
$input = $input_path.$header."_w".$win."_GM.input";
#print "Creating $mtx $win $input\n";
}else{
get_normalisation_values();
$win = $reqwin;
$input = $input_path.$header."_w".$win."_TM.input";
#print "Creating $mtx $win $input\n";
}
## Add X's to either side of seqeunce
my $x_string;
for (1..($win - 1)/2){
$x_string .= 'X';
}
my $seq_x = $x_string.$seq.$x_string;
## Fill up %profile with lines from @mtx
my %profile = ();
for (1..($length + $win - 1)){
if (($_ <= ($win - 1)/2)||($_ > $length + (($win - 1)/2))){
## Missing
@{$profile{$_}} = @empty;
}else{
## Start on MTX line 15
if (defined $mtx[$_+13-(($win - 1)/2)]){
@{$profile{$_}} = split(/\s+/,$mtx[$_+13-(($win - 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 = Round::nearest_ceil(.00001,$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;
}
}
}
open (IN,">>$input");
for my $pos ((($win - 1)/2) + 1..($length + ($win - 1)/2)){
my $real_pos = $pos - (($win - 1)/2);
my ($vector,$seq,@array);
## Push all the arrays onto @array
for my $w ($pos - (($win - 1)/2) .. $pos + (($win - 1)/2)){
push @array,@{$profile{$w}}
}
## Generate sequence window for the comment
for my $j ($real_pos - 1 - (($win - 1)/2) .. $real_pos - 1 + (($win - 1)/2)){
if ($j < 0 || $j >= $length){
$seq .= 'X';
}else{
$seq .= substr($seq_x,$j+(($win - 1)/2),1);
}
}
my $count = 1;
foreach my $v (@array){
$vector .= $count.":".$v." ";
$count++;
}
print IN "0 $vector # $seq\n";
}
close IN;
}
}
# Draw images
sub draw_image{
my ($header,$seq,$topology,$in_out,$re_region,$signal_region) = @_;
load_module('DrawTransmembraneSchematic');
load_module('DrawTransmembraneCartoon');
my $svm_raw_muliplier = 6;
my $svm_raw_sp_muliplier = 6;
my $svm_raw_prob = 40;
$topology{'MEMSAT-SVM'}{'topology_array'} = $topology;
$topology{'MEMSAT-SVM'}{'n_term'} = $in_out;
if (scalar @$re_region){
$topology{'MEMSAT-SVM'}{'reentrant_array'} = $re_region;
}
if ($signal_region !~ 'detected'){
$topology{'MEMSAT-SVM'}{'signal'} = $signal_region;
}
my @order = ('MEMSAT-SVM');
push @order,'MEMSAT3' if $runmem3 == 1;
if ($runmem3 == 2){
@order = ('MEMSAT3') ;
my $im = DrawTransmembraneSchematic->new(-title=>$header,
-inside_rgb=>,[256,5,50],
-outside_rgb=>,[140,399,70],
-signal_rgb=>,[633,23,46],
-sequence=>$seq,
-topologies=>\%topology,
-draw_consensus=>0,
-draw_key=>1,
-draw_custom_plot_1=>0,
-draw_custom_plot_3=>0,
-draw_custom_plot_5=>0,
-draw_custom_plot_7=>0,
-order=>\@order
);
open(OUTPUT, ">$png_schematic_out");
binmode OUTPUT;
print OUTPUT $im->png;
close OUTPUT;
}else{
my $im = DrawTransmembraneSchematic->new(-title=>$header,
-inside_rgb=>,[256,5,50],
-outside_rgb=>,[140,399,70],
-signal_rgb=>,[633,23,46],
-sequence=>$seq,
-topologies=>\%topology,
-draw_consensus=>0,
-draw_key=>1,
-unknown_label=>'Re-entrant Helix',
-draw_custom_plot_1=>1,
-custom_plot_multiplier_1=>$svm_raw_muliplier,
-custom_plot_label_1=>'SVM H/L Raw',
-custom_plot_data_1=>\%raw_hl,
-draw_custom_plot_3=>1,
-custom_plot_multiplier_3=>$svm_raw_muliplier,
-custom_plot_label_3=>'SVM iL/oL Raw',
-custom_plot_data_3=>\%raw_io,
-draw_custom_plot_5=>1,
-custom_plot_multiplier_5=>$svm_raw_muliplier,
-custom_plot_label_5=>'SVM RE/!RE Raw',
-custom_plot_data_5=>\%raw_re,
-draw_custom_plot_7=>1,
-custom_plot_multiplier_7=>$svm_raw_sp_muliplier,
-custom_plot_label_7=>'SVM SP/!SP Raw',
-custom_plot_data_7=>\%raw_sp,
-order=>\@order
);
open(OUTPUT, ">$png_schematic_out");
binmode OUTPUT;
print OUTPUT $im->png;
close OUTPUT;
}
my %labels;
if (scalar @$re_region){
for (my $r = 0; $r < scalar @$re_region; $r += 2){
$labels{$$re_region[$r]} = $$re_region[$r]."-".$$re_region[$r+1]." Re-entrant helix";
}