-
Notifications
You must be signed in to change notification settings - Fork 2
/
AMPA.pl
executable file
·334 lines (277 loc) · 10.2 KB
/
AMPA.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
#!/usr/bin/perl -w
use strict;
use Math::CDF;
use Math::Round;
#
# The default input parameters
#
my $debug = 0;
my $noplot = 0;
my $gnuplot = "";
my $input_file = "protein.txt";
my $window_size = 7;
my $threshold = 0.225;
my $graph_file = "";
my $result_file = "results.$$";
my $data_file = "sliding.$$";
my $_prompt = "";
#
# Find out gnuplot
#
if( exists $ENV{GNUPLOT_BIN}) {
$gnuplot = $ENV{GNUPLOT_BIN};
}
if( $gnuplot eq "" ) {
$gnuplot = `which gnuplot`;
chomp($gnuplot);
}
if( $gnuplot eq "" ) {
print "AMPA requires gnuplot to be installed on your system.";
exit 2;
}
#
# Parse the command
#
my $cl=join( " ", @ARGV);
if (($cl=~/-h/) ||($cl=~/-H/) ) {
# print usage
print "Usage: AMPA.pl -in=<input fasta file> [other options]\n";
print "\n";
print "Available options:\n";
print "-in Fasta sequence input file\n";
print "-t Threshold value (default: 7)\n";
print "-w Window size value (default: 0.225)\n";
print "-rf File where store the program result\n";
print "-df File where store the produced plot data\n";
print "-gf File where store the generated plot\n";
print "-noplot Skip plot creation step\n";
exit 1;
}
#
# If not command line is provided switch to interactive mode
#
if ($#ARGV==-1 ) {
# the input file name
print "Enter the name of the sequence file and then press Enter [$input_file]: ";
$_prompt = <STDIN>;
chomp($_prompt);
if( $_prompt ne "" ) { $input_file=$_prompt };
# the window size
print "Enter the desired window size as an odd number and then press Enter [$window_size]: ";
$_prompt = <STDIN>;
chomp($_prompt);
if( $_prompt ne "" ) { $window_size = $_prompt };
# the threshold
print "Enter the desired threshold and then press Enter [$threshold]: ";
$_prompt = <STDIN>;
chomp($_prompt);
if( $_prompt ne "" ) { $threshold = $_prompt };
}
# option '-in': input file name
if ( ($cl=~/-in=\s*(\S+)/)) {
$input_file = $1;
}
# option '-w': window size
if ( ($cl =~ /-w=\s*(\S+)/)) {
$window_size = $1;
}
# option '-t': threshold value
if ( ($cl =~ /-t=\s*(\S+)/)) {
$threshold = $1;
}
# option '-gf': output graph file
if ( ($cl =~ /-gf=\s*(\S+)/)) {
$graph_file = $1;
}
# option '-rf': output result file
if ( ($cl =~ /-rf=\s*(\S+)/)) {
$result_file = $1;
}
# option '-df': output sliding file
if ( ($cl =~ /-df=\s*(\S+)/)) {
$data_file = $1;
}
# option '-debug': enable debug mode
if ( ($cl =~ /-debug/)) {
$debug = 1;
}
# option '-noplot': threshold value
if ( ($cl =~ /-noplot/)) {
$noplot = 1;
}
if( $debug != 0 ) {
print "Window size : $window_size\n";
print "Threshold value : $threshold\n";
print "Input file name : $input_file\n";
print "Graph file name : $graph_file\n";
print "Result file name : $result_file\n";
print "Data file name : $data_file\n";
print "Gnuplot bin : $gnuplot\n";
}
#
# Main script start here
#
my(@names,@lengths,@sequences,$sequence_number,$protein);
open(PROTEIN, "$input_file") or die "Cannot open '$input_file': $!\n";
#The program should read the user input sequence
$/ = ">";
for(my $c=1; $c<(($window_size + 1)/2); $c++) {
push (@sequences, 'X');
}
while (my $sequence_record = <PROTEIN>) {
if ($sequence_record eq ">") {
next;
}
my $sequence_name = "";
if ($sequence_record =~ m/([^\n]+)/) {
$sequence_name = $1;
} else {
$sequence_name = "No title was found!";
}
$sequence_record =~ s/[^\n]+//;
push (@names, $sequence_name);
$sequence_record =~ s/[^ACDEFGHIKLMNPQRSTVWYacdefghiklmnpqrstvwy]//g;
push (@sequences, $sequence_record);
push (@lengths, length($sequence_record));
my $sequence_length = length($sequence_record);
}
for(my $c=1; $c<(($window_size + 1)/2); $c++) {
push (@sequences, 'X');
}
my $sequences = join("",@sequences);
close(PROTEIN) or die "Cannot close the file: $!\n";
#The program calls the subroutines to analyze the sequence
&sliding($sequences);
if(!$noplot) {
&gnuplot_sld();
}
### Subroutines
# Defines the value at the window center (window_size+1)/2
sub sliding {
open(KD,">$data_file") or die("Could not open '$data_file': $!\n");
open(RS,">$result_file") or die("Could not open '$result_file': $!\n");
my($center,$length);
my $half = (($window_size + 1)/2);
# These are the antimicrobial propensity values for each amino acid
my %sliding = (
'A', 0.307,
'R', 0.106,
'N', 0.240,
'D', 0.479,
'C', 0.165,
'Q', 0.248,
'E', 0.449,
'G', 0.265,
'H', 0.202,
'I', 0.198,
'L', 0.246,
'K', 0.111,
'M', 0.265,
'F', 0.246,
'P', 0.327,
'S', 0.281,
'T', 0.242,
'W', 0.172,
'Y', 0.185,
'V', 0.200,
'X', $threshold
);
$protein = shift;
print KD "# The window size is currently set at $window_size.\n";
print KD "# Here are the AMSI values for this protein:\n";
print KD "# $names[0]\n";
print KD "# Pos\ APV\n";
print KD "# ---\t-------------------\n";
my $acc1=0;
my $acc2=0;
my $bacindex=0;
my $amvalue=0;
my $bacvalue=0;
my $prob=0;
for(my $i=0;$i<=(length($protein)-$window_size);$i++) {
my $window = substr($protein, $i, $window_size);
my $sum=0;
for(my $j=0; $j<$window_size; $j++) {
my $PV = 0;
my $residue = substr($window, $j, 1);
$PV = $sliding{$residue};
$sum+=$PV;
}
$center = $i + $half;
my $position = $center - 3;
my $APV=$sum/$window_size;
$APV = sprintf "%.3f", $APV;
print KD "$position\t$APV\n";
$amvalue=$amvalue+$APV;
if($acc1==3) {
if($acc2>=10){
my $init=$position-$acc2-2;
my $last=$position-1;
my $bacvalue=(($bacvalue)/($last-$init+1));
$prob = &Math::CDF::pnorm(($bacvalue-0.2584)/0.02479);
$prob*=100;
my $rbacvalue = nearest(.001, $bacvalue);
my $rprob = nearest(1, $prob);
print RS "Antimicrobial stretch found in $init to $last. Propensity value $rbacvalue ($rprob %) \n";
$bacindex++;
$acc2=0;
$acc1=0;
$bacvalue=0;
$prob=0;
}
if($acc2<10){
$acc1=0;
$acc2=0;
$bacvalue=0;
}
}
if($acc1!=3){
if($acc2==0) {
$acc1=0;
$bacvalue=0;
}
if($APV<=$threshold) {
$acc2++;
$bacvalue+=$APV
}
if($APV>$threshold){
$acc1++;
$bacvalue+=$APV
}
}
if($position==(length($protein)-(($window_size)-1)) && $acc2>=10) {
my $init=$position-$acc2-2;
my $last=$position-1;
my $bacvalue=(($bacvalue)/($last-$init+1));
$bacindex++;
$prob = &Math::CDF::pnorm(($bacvalue-0.2584)/0.02479);
$prob*=100;
my $rbacvalue = nearest(.001, $bacvalue);
my $rprob = nearest(1, $prob);
print RS "Antimicrobial stretch found in $init to $last. Propensity value $rbacvalue ($rprob %) \n";
}
}
print RS "# This protein has $bacindex bactericidal stretches \n";
my $manvalue = nearest(.001, $amvalue/(length($protein)-6));
print RS "# This protein has a mean antimicrobial value of $manvalue \n";
close(KD) or die("Could not close file: $!\n");
}
sub gnuplot_sld {
my $cmdline = "|$gnuplot -persist";
open (GP, $cmdline) or die "no gnuplot";
# force buffer to flush after each write
use FileHandle;
GP->autoflush(1);
print GP "set title \"Antimicrobial Profile\"\n";
print GP "set xlabel \"Position\"\n";
print GP "set ylabel \"Score\"\n";
print GP "set grid\n";
print GP "set data style lines\n";
# if an outpout has been specified output to it
if( $graph_file ne "" ) {
print GP "set terminal png large enhanced size 800 600\n";
print GP "set output '$graph_file' \n";
}
print GP "plot \"$data_file\" u 1:2 t \"Antimicrobial profile \" w lines\n";
close GP;
}