blib/script/self_dot_plot.pl untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 173 CC 9 SCAR 29.9
CC
9
Cov
0%
main
56.0
print_dots
6.0
print_png
6.0
CRAP 19.9
1 #!/usr/bin/env perl
2 # NOTE: YOU MUST CHANGE THE LINE ABOVE TO POINT TO
3 # THE FULL PATH OF THE PERL EXECUTABLE ON YOUR SYSTEM.
4  
5 # Please see copyright notice and system requirements
6 # in this document.
7  
8 # This program used to produce symmetric dot plots for:
9 #
10 # Tomoko Kuroda-Kawaguchi, Helen Skaletsky, Laura G. Brown,
11 # Patrick J. Minx, Holland S. Cordum, Robert H. Waterston,
12 # Richard K. Wilson, Sherman Silber, Robert Oates, Steve
13 # Rozen & David C. Page. The AZFc region of the Y chromosome
14 # features massive palindromes and uniform recurrent
15 # deletions in infertile men. Nature Genetics, 29(3)
16 # (Nov. 2001), in press.
17  
18 # Copyright (c) 2001 Helen Skaletsky and Whitehead Institute
19 #
20 # Redistribution and use in source and binary forms, with or
21 # without modification, are permitted provided that the
22 # following conditions are met:
23 #
24 # 1. Redistributions must reproduce the above copyright
25 # notice, this list of conditions and the following
26 # disclaimer in the documentation and/or other materials
27 # provided with the distribution. Redistributions of source
28 # code must also reproduce this information in the source code
29 # itself.
30 #
31 # 2. If the program is modified, redistributions must
32 # include a notice (in the same places as above) indicating
33 # that the redistributed program is not identical to the
34 # version distributed by Whitehead Institute.
35 #
36 # 3. All advertising materials mentioning features or use
37 # of this software must display the following
38 # acknowledgment:
39 #
40 # This product includes software developed by
41 # Helen Skaletsky and the Whitehead Institute
42 # for Biomedical Research.
43 #
44 # 4. The name of the Whitehead Institute may not be used to
45 # endorse or promote products derived from this software
46 # without specific prior written permission.
47 #
48 # THIS SOFTWARE IS PROVIDED BY THE WHITEHEAD INSTITUTE AND
49 # HELEN SKALETSKY ``AS IS'' AND ANY EXPRESS OR IMPLIED
50 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE WHITEHEAD
53 # INSTITUTE OR HELEN SKALETSKY BE LIABLE FOR ANY DIRECT,
54 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
58 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
61 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  
63 # System requirements: requires perl and perl module GD.pm,
64 # available from CPAN (author LDS --- Lincoln D. Stein).
65
66 # For usage information, run this program with the flag
67 # -h.
68  
69 require 5.10.0;
70  
71 use warnings;
72 use strict;
73  
74 use Data::Dumper;
75 use Carp;
76  
77 use 5.010;
78  
79  
80 use GD;
81 use strict;
82 use vars qw/$VERSION/;
83  
84 our $VERSION = '1.0';
85  
86 use Getopt::Long;
87  
88 sub usage();
89 sub main();
90 sub print_dots ($$$$$$$$);
91 sub print_png ($$);
92  
93 main();
94  
95 sub usage() {
96 print qq/
97 USAGE: $0 -w <word size> -s <step length>
98 -i <in file> -o <out file> -d <dot file>
99 -t <title>
100 [ -h ]
101  
102 Create a PNG ("portable network graphics") file
103 that displays a triangular dot plot of the input
104 sequence against itself.
105  
106 <word size> is the word size for a match. A dot
107 is printed if there is a perfect match
108 of length <word size>.
109  
110 <step length> is the number of bases to move the
111 word for each dot.
112  
113 <in file> is a fasta format file from which the
114 sequence is taken.
115  
116 <out file> is the PNG file created.
117  
118 <dot file> contains 0 based positions of perfect
119 matches of length <word size>.
120 (E.g., the line "251 1077" means that
121 substrings of length <word size>
122 starting at 251 and at 1077 are
123 identical.)
124  
125 <title> is a title to place in the output.
126  
127 -h causes this message to printed.
128  
129 (Version $VERSION)
130  
131 /;
132 }
133  
134 sub main() {
135  
136 my ($seqfile, $word, $step, $outfile, $title, $dotfile, $help);
137  
138 if (!GetOptions('infile=s' => \$seqfile,
139 'wordlen=i' => \$word,
140 'step=i' => \$step,
141 'outfile=s' => \$outfile,
142 'title=s' => \$title,
143 'dotfile=s' => \$dotfile,
144 'help' => \$help)) {
145 usage;
146 exit -1;
147 }
148 if ($help) {
149 usage;
150 exit;
151 }
152  
153 if (!defined $word
154 || !defined $step
155 || !defined $seqfile
156 || !defined $outfile
157 || !defined $dotfile) {
158 usage; exit -1;
159 }
160
161 $title = '' unless defined $title;
162  
163 if ($word <= 0) {
164 print STDERR "$0 -w <word size> must be >= 0\n";
165 exit -1;
166 }
167  
168 if ($step <= 0) {
169 print STDERR "$0 -s <step length> must be >= 0\n";
170 exit -1;
171 }
172  
173  
174 open(IN, $seqfile) || die "Cannot open $seqfile: $!";
175 <IN>;
176 my $seq = '';
177 while(<IN>){
178 chop; $seq .= uc($_);
179 }
180 close IN;
181 my $m = length($seq);
182  
183 my $k = 0;
184 my $n = $m - $word;
185  
186 open(OUT, ">$dotfile")
187 || die "Cannot write $dotfile: $!\n";
188  
189 while($k < $n) {
190 my $s = substr($seq, $k, $word);
191 while($seq =~ m/$s/g){
192 my $t = pos $seq; $t -= $word;
193 print OUT "$k\t$t\n";
194 }
195 my $s1 = reverse($s);
196 $s1 =~ tr/ACGTURYMWSKDHVBN/TGCAAYRKWSMHDBVN/;
197 while($seq =~ m/$s1/g){
198 my $t = pos $seq; $t -= $word;
199 print OUT "$k\t$t\n";
200 }
201 $k += $step;
202 }
203 close OUT;
204  
205 # Create and print the output.
206 my $width = 700; my $height = 730; my $x0 = 30;
207 my $img = new GD::Image($width, $height);
208 my $white = $img->colorAllocate(255,255,255);
209 my $black = $img->colorAllocate(0,0,0);
210 my $gray = $img->colorAllocate(187,187,187);
211 $img->interlaced('true');
212  
213 $x0 = 60;
214 $img->string(gdLargeFont, $x0, 30,
215 "$title -w=$word -s=$step",
216 $black);
217 $img->string(gdLargeFont, $x0, 0.5*$width-10,
218 "$m bp",
219 $black);
220 print_dots($img, $width, $height, $x0, $black, $gray, $m, $dotfile);
221 print_png($img, $outfile)
222 }
223  
224 sub print_dots ($$$$$$$$) {
225 my ($img, $width, $height, $x0, $black, $gray, $m, $dotfile) = @_;
226 open(IN, $dotfile) || die "Cannot open $dotfile: $!\n";
227 my ($x1, $y1, $del);
228 $del = ($width - 80)/$m;
229 my $count = 0;
230 while(<IN>){
231 chop;
232 my @x = split '\t';
233 next if $x[1] < $x[0];
234 # For progress report on very long run
235 # if(int($count/10000)*10000 == $count){
236 # print STDERR "$x[0] $x[1]\n";
237 # }
238 $x1 = $x0 + $del*$x[0];
239 $y1 = $x0 + $del*$x[1];
240 $img->setPixel(0.5*($x1+$y1),
241 $width-0.5*($y1-$x1)-20-0.5*$width,
242 $black);
243 $count++;
244 }
245 close IN;
246 $img->line($x0, 0.5*$width-20, $width-20,
247 0.5*$width-20, $gray);
248 $img->line(0.5*($x0+$width-20), 0.5*($x0-20),
249 $width-20, 0.5*$width-20, $gray);
250 $img->line(0.5*($x0+$width-20), 0.5*($x0-20),
251 $x0, 0.5*$width-20, $gray);
252 }
253  
254 sub print_png ($$) {
255 my ($img, $outfile) = @_;
256 if ($outfile) {
257 open(OUT, ">$outfile")
258 || die "Cannot write $outfile: $!\n";
259 print OUT $img->png;
260 close OUT;
261 } else {
262 print $img->png;
263 }
264 }