]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_matches
fixed seq qual length check
[biopieces.git] / bp_bin / plot_matches
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Generate a dotplot of matches in the stream.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Plot;
33 use Maasha::Filesys;
34 use IPC::Open2;
35
36
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38
39
40 my ( $options, $in, $out, $default, $terminals, $record, @data, $fh, $result, %data_hash, $tmp_dir );
41
42 $default   = "plot_matches";
43 $terminals = "dumb,x11,aqua,post,svg,png";
44
45 $options = Maasha::Biopieces::parse_options(
46     [
47         { long => 'no_stream', short => 'x', type => 'flag',   mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
48         { long => 'data_out',  short => 'o', type => 'file',   mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
49         { long => 'terminal',  short => 't', type => 'string', mandatory => 'no',  default => 'dumb',   allowed => $terminals,             disallowed => undef },
50         { long => 'direction', short => 'd', type => 'string', mandatory => 'no',  default => 'both',   allowed => 'both,forward,reverse', disallowed => undef },
51         { long => 'title',     short => 'T', type => 'string', mandatory => 'no',  default => $default, allowed => undef,                  disallowed => undef },
52         { long => 'xlabel',    short => 'X', type => 'string', mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
53         { long => 'ylabel',    short => 'Y', type => 'string', mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
54     ]   
55 );
56
57 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
59
60 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
61 {
62     if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
63         push @data, $record;
64     }
65
66     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
67 }
68
69 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
70 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
71
72 $tmp_dir = Maasha::Biopieces::get_tmpdir();
73
74 $result = dotplot_matches( \@data, $options, $tmp_dir );
75
76 $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
77
78 print $fh "$_\n" foreach @{ $result };
79
80 close $fh;
81
82 Maasha::Biopieces::close_stream( $in );
83 Maasha::Biopieces::close_stream( $out );
84
85
86 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
87
88
89 sub dotplot_matches
90 {
91     # Martin A. Hansen, August 2007.
92
93     # Generates a dotplot from a list of matches using Gnuplot.
94
95     my ( $matches,   # list of hashrefs.
96          $options,   # options hash
97          $tmp_dir,   # temporary directory
98        ) = @_;
99
100     # Returns list.
101
102     my ( $forward_file, $backward_file, $pid, $fh_forward, $fh_backward,
103          $fh_in, $fh_out, $cmd, $match, $line, @lines, $q_max, $s_max );
104
105     $forward_file  = "$tmp_dir/match_f.tab";
106     $backward_file = "$tmp_dir/match_r.tab";
107
108     $fh_forward  = Maasha::Filesys::file_write_open( $forward_file );
109     $fh_backward = Maasha::Filesys::file_write_open( $backward_file );
110
111     $q_max = 0;
112     $s_max = 0;
113
114     foreach $match ( @{ $matches } )
115     {
116         if ( ($match->{ "DIR" } and $match->{ "DIR" } =~ /^f/) or ($match->{ "STRAND" } and $match->{ "STRAND" } eq '+') )
117         {
118             print $fh_forward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
119             print $fh_forward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_END" } + 1 ), "\n";
120             print $fh_forward "\n\n";
121         }
122         else
123         {
124             print $fh_backward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_END" } + 1 ), "\n";
125             print $fh_backward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
126             print $fh_backward "\n\n";
127         }
128
129         $q_max = $match->{ "Q_END" } if $match->{ "Q_END" } > $q_max;
130         $s_max = $match->{ "S_END" } if $match->{ "S_END" } > $s_max;
131     }
132
133     $q_max++;
134     $s_max++;
135
136     close $fh_forward;
137     close $fh_backward;
138
139     $cmd  = "gnuplot -persist";
140
141     $pid = open2( $fh_out, $fh_in, $cmd );
142     
143     print $fh_in "set terminal $options->{ 'terminal' }\n";
144     print $fh_in "set xrange [1:$q_max]\n";
145     print $fh_in "set yrange [1:$s_max]\n";
146     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
147     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
148     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
149     print $fh_in "unset key\n";
150
151     if ( $options->{ "terminal" } ne "dumb" )
152     {
153         print $fh_in "set style line 1 linetype 1 linecolor rgb \"green\" linewidth 2 pointtype 6 pointsize default\n";
154         print $fh_in "set style line 2 linetype 1 linecolor rgb \"red\" linewidth 2 pointtype 6 pointsize default\n";
155     }
156
157     print $fh_in "set xtics border out\n";
158     print $fh_in "set ytics border out\n";
159     print $fh_in "set grid\n";
160
161     if ( $options->{ "direction" } =~ /^b/ ) {
162         print $fh_in qq(plot "$forward_file" with lines ls 1, "$backward_file" with lines ls 2\n);
163     } elsif ( $options->{ "direction" } =~ /^f/ ) {
164         print $fh_in qq(plot "$forward_file" with lines ls 1\n);
165     } elsif ( $options->{ "direction" } =~ /^r/ ) {
166         print $fh_in qq(plot "$backward_file" with lines ls 2\n);
167     }
168
169     close $fh_in;
170
171     while ( $line = <$fh_out> )
172     {
173         chomp $line;
174
175         push @lines, $line;
176     }
177
178     close $fh_out;
179
180     waitpid $pid, 0;
181
182     unlink $forward_file;
183     unlink $backward_file;
184
185     return wantarray ? @lines : \@lines;
186 }
187
188
189 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190
191
192 BEGIN
193 {
194     Maasha::Biopieces::status_set();
195 }
196
197
198 END
199 {
200     Maasha::Biopieces::status_log();
201 }
202
203
204 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
205
206
207 __END__