File: alien_hunter.pl

package info (click to toggle)
alien-hunter 1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 58
  • sloc: perl: 962; java: 358; sh: 22; makefile: 10
file content (349 lines) | stat: -rw-r--r-- 7,780 bytes parent folder | download | duplicates (5)
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

=head1 NAME

alien_hunter.pl

=head1 SYNOPSIS

Prediction of Genomic Islands using Interpolated Variable Order Motifs (IVOMs)

=head1 AUTHOR

George Vernikos <gsv(at)sanger.ac.uk>

=head1 COPYRIGHT

=head1 BUGS

if you witness any bug please contact the author

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

=cut

use FindBin;
use lib $FindBin::Bin;
use POSIX qw(log10);
use Getopt::Long;
use PAI_scripts::help;
use PAI_scripts::MotifMaker;
use PAI_scripts::CutOff;
use PAI_scripts::overlap;
use PAI_scripts::rrna;

use changepointCaller;
use Time::Local;
use Time::HiRes qw (gettimeofday);

$once=0;
$WinSize=5000;
$Overlap=2500;


GetOptions("file_genome=s"=>\$SeqFile,"output=s"=>\$Output,"artemis"=>\$artemis,"help"=>\$help,"changePoint"=>\$changePoint);

	if($help|!$SeqFile|!$Output){
	help();
	goto END;	
	}

	open file1, $SeqFile
	or die "dead";

	print"\n reading sequence...\n";
	
	while(<file1>){  								
	chomp($_);
		if (m#>#){
		}
		else {
		$GenSeq.=$_;
		}
	}
	close file1;

	#runs rrna method in rrna.pm to find rrna operons
	rrna($GenSeq);
	
	$GenLen=length($GenSeq);
		
	if($GenLen<20000){
	print "\n$GenLen bp; too short sequence!\n\n";
	goto END;
	}
	
	#builds the motif vectors
	MMaker();
		
	#scans genome sequence for all kth order motifs
	print" scanning genome for motifs...\n";
	($amb_base,$count8merswithN)=scan($GenSeq,0);	
	
	if($amb_base==1){
	print "\n\nambiguous bases in the sequence!!!\n\n";
	
	#check percentage of total 8mers in the sequence that contain ambiquous bases (if > 30% stop) 
	$per8merswithN=$count8merswithN/($GenLen-8+1);
		if($per8merswithN > 0.3){
		print "\n\nmore than 30% of 8mers contain ambiguous bases -- cannot continue!!!\n\n";
		goto END;
		}
	}
		
	#builds the IVOM vectors
	print"\n building genome IVOM vectors...\n";
	$GenIVOMref=IvomBuild();	
	
		foreach $p ($GenIVOMref){
			foreach $key (keys %$p){
			$GenIVOM{$key}="$p->{$key}";
			}
		}
			
	print"\n sliding window running - calculate relative entropy (KL)...\n";
	
	#how many overlapping windows
	$HowMany=($GenLen/$Overlap)-1; 				
		
	$start=gettimeofday();
	
	for ($i=0;$i<=$HowMany-1;$i++){
	
		$from=$i*$WinSize-($i*$Overlap);
		$to=$from+$WinSize;
		
	#so as for the last window if != winsize, to take different 
	#start point to the end of the genome, but again with size=winsize
		if(($HowMany-1)-$i<=1){
		$to=$GenLen;
		$from=$GenLen-$WinSize;					
		}
	
		$Query=substr($GenSeq,$from,$to-$from);
		
	#scans each sliding window for all kth order motifs
		($amb_base,$count8merswithN)=scan($Query,1);
		#if the current sliding window contains at least 1 ambiguous base skip it and go to the next one
		if($amb_base==1){
		$counter++;	
		goto HERE;	
		}
	
	#builds the IVOM vectors for each sliding window
		$QueryIVOM=IvomBuild();
		$counter++;
		
	# to convert base 0.. into 1..
		if($i==0){					
		$from=1;	
		}
				
	#calculates relative entropy (Kullback-Leibler)
		foreach $k (keys %GenIVOM){
			
			$w=${$QueryIVOM}{$k};
			$G=$GenIVOM{$k};
			
			#print "$k $w $G\n";
			$Score+=($w*log10($w/$G));
					
		}
		
		$AllScores{"$from..$to"}=$Score;
	
			
	#keeps track of process completed			
		$PercentComplete=($counter/$HowMany)*100; 	
		$PercentComplete=sprintf("%.1f",$PercentComplete);

		if($PreviousValue ne $PercentComplete){
			print "completed ... $PercentComplete%\n";
		
			if($PercentComplete>=1){
			
				#calculating finish time
				if($once==0){
				$once=1;
				$dif=gettimeofday()-$start;
				$remain=$HowMany/$counter;
				$dif=$dif*$remain;
				$estimated=$start+$dif;
				print "\n\t\testimated finish time:\n\t\t";
				print scalar(localtime($estimated));
				print "\n\n";
				}
			
			}
		}
		$PreviousValue= $PercentComplete;
		
	HERE:
	$Score=0;	
	}#for how many
		
	print"\n determining score threshold...\n";
			
		($cutoff,$ScaledScores)= Cutoff(\%AllScores);
		$cutoff=sprintf("%.3f",$cutoff);

	########### to print all scores ##############
	open file4, ">$Output.sco"
	or die "dead";
	
	foreach $h ($ScaledScores){
		foreach $key (keys %$h){
		$temp_sc{$key}="$h->{$key}";
		}
	}

	foreach $k (keys %temp_sc){
	$v = $temp_sc{$k};
	print file4 "FT   misc_feature    $k\nFT                   /score=$v\n";
	}

	close file4;
	#############################################
	
	
	$NumKeys= keys %temp_sc;
	if($NumKeys<2){
		goto END;
	}
	

	print"\n merging predictions...\n";
	
		$joinedScoresRef=overlap($cutoff,$ScaledScores);
	
		foreach $h ($joinedScoresRef){
			foreach $key (keys %$h){
		 	$joinedScores{$key}="$h->{$key}";
			}
		}
	
	print"\n writing predictions in embl format...\n";
	
	#find the max joined_window score
			@keys = sort {					
			$joinedScores{$a} <=> $joinedScores{$b}		
			} keys %joinedScores;
			$NumKeys = keys %joinedScores;
			$max=$joinedScores{$keys[$NumKeys-1]};
	
	open file2, ">$Output"
	or die "dead";	

	if($cutoff>0){
	open file3, ">$Output.plot"
	or die "dead";
	}
	
	$st=1;
				
	#sorts the scores based on their keys (from..to) in order for the artemis 
	#plot to print the regions in the order they occur in the genome
	@keys = sort { 
	$a<=>$b
	}keys %joinedScores;
					
	foreach $k (@keys) {
	$res=0;
   	$v = $joinedScores{$k};
    	#all scores above the cutoff are coloured scaled: red->white (max->min)
		if($v>=$cutoff){					 
		$x=($v-$cutoff)/($max-$cutoff);
		$green=255-($x*255);
		$blue=255-($x*255);
		$green=sprintf("%.0f",$green);
		$blue=sprintf("%.0f",$blue);
		$red=255;	
		$color="$red $green $blue";
		#$label="Alien";	
		}

	#calls rrna.pm to annotate regions overlapping rrnas
	($rfrom,$rto)=split/\.\./,$k;
	$res=overlapRNA($rfrom,$rto);
	
	if($res==1){
print file2 "FT   misc_feature    $k
FT                   /colour=$color
FT                   /algorithm=\"alien_hunter\"
FT                   /note=\"threshold: $cutoff; probably region overlapping rRNA operon\"
FT                   /score=$v\n";
	}
	else{
print file2 "FT   misc_feature    $k
FT                   /colour=$color
FT                   /algorithm=\"alien_hunter\"
FT                   /note=\"threshold: $cutoff\"
FT                   /score=$v\n";
	}
		#writes scores for artemis plot
		#check if cutoff>0 else it doen's write plot
		if($cutoff>0){
						
			($from,$to)=split /\.\./, $k;
			#begin -> from
			for($i=$st;$i<$from;$i++){
			print file3 "0\n";
			}
			#from -> to
			for($i=$from;$i<=$to;$i++){
			print file3 "$v\n";
			}
			$st=$to+1;
			
		}
	
	}
	close file2;

	#to -> end
		if($cutoff>0){		
			for($i=$st;$i<=$GenLen;$i++){
			print file3 "0\n";
			}
			close file3;
			
	#optimizing the boundaries
			if($changePoint){
			print"\n optimizing predicted boundaries...\n\n";
			changepointCaller($SeqFile,$Output);
			$Output.=".opt";
			}
		}

	#calculate time elapsed
	$finish=gettimeofday();
	$diff=$finish-$start;
	$sec=$diff % 60;
	$diff=($diff-$sec) /60;
	$min=$diff % 60;
	$diff=($diff-$min) /60;
	$hours=$diff % 24;
	$diff=($diff-$hours) /24;
	print "\ntime elapsed: $hours:$min:$sec\n";	
	
	#it runs artemis; if also -c then it will load the optimized predictions
	if($artemis){ 					
	print"\n loading predictions into artemis ...\n\n\n";
	exec "art $SeqFile + $Output";
	}
	
#end of code
END: