]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_matches
implementing status solution
[biopieces.git] / bp_bin / plot_matches
1 #!/usr/bin/env perl -w
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 strict;
30 use Maasha::Biopieces;
31 use Maasha::Plot;
32 use Maasha::Filesys;
33 use IPC::Open2;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $in, $out, $default, $terminals, $record, @data, $fh, $result, %data_hash, $tmp_dir );
40
41 $default   = "plot_matches";
42 $terminals = "dumb,x11,aqua,post,svg";
43
44 $options = Maasha::Biopieces::parse_options(
45     [
46         { long => 'no_stream', short => 'x', type => 'flag',   mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
47         { long => 'data_out',  short => 'o', type => 'file',   mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
48         { long => 'terminal',  short => 't', type => 'string', mandatory => 'no',  default => 'dumb',   allowed => $terminals,             disallowed => undef },
49         { long => 'direction', short => 'd', type => 'string', mandatory => 'no',  default => 'both',   allowed => 'both,forward,reverse', disallowed => undef },
50         { long => 'title',     short => 'T', type => 'string', mandatory => 'no',  default => $default, allowed => undef,                  disallowed => undef },
51         { long => 'xlabel',    short => 'X', type => 'string', mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
52         { long => 'ylabel',    short => 'Y', type => 'string', mandatory => 'no',  default => undef,    allowed => undef,                  disallowed => undef },
53     ]   
54 );
55
56 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
57 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
58
59 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
60 {
61     if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
62         push @data, $record;
63     }
64
65     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
66 }
67
68 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
69 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
70
71 $tmp_dir = Maasha::Biopieces::get_tmpdir();
72
73 $result = dotplot_matches( \@data, $options, $tmp_dir );
74
75 $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
76
77 print $fh "$_\n" foreach @{ $result };
78
79 close $fh;
80
81 Maasha::Filesys::dir_remove( $tmp_dir );
82
83 Maasha::Biopieces::close_stream( $in );
84 Maasha::Biopieces::close_stream( $out );
85
86
87 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
88
89
90 sub dotplot_matches
91 {
92     # Martin A. Hansen, August 2007.
93
94     # Generates a dotplot from a list of matches using Gnuplot.
95
96     my ( $matches,   # list of hashrefs.
97          $options,   # options hash
98          $tmp_dir,   # temporary directory
99        ) = @_;
100
101     # Returns list.
102
103     my ( $forward_file, $backward_file, $pid, $fh_forward, $fh_backward,
104          $fh_in, $fh_out, $cmd, $match, $line, @lines, $q_max, $s_max );
105
106     $forward_file  = "$tmp_dir/match_f.tab";
107     $backward_file = "$tmp_dir/match_r.tab";
108
109     $fh_forward  = Maasha::Filesys::file_write_open( $forward_file );
110     $fh_backward = Maasha::Filesys::file_write_open( $backward_file );
111
112     $q_max = 0;
113     $s_max = 0;
114
115     foreach $match ( @{ $matches } )
116     {
117         if ( $match->{ "DIR" } =~ /^f/ )
118         {
119             print $fh_forward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
120             print $fh_forward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_END" } + 1 ), "\n";
121             print $fh_forward "\n\n";
122         }
123         else
124         {
125             print $fh_backward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_END" } + 1 ), "\n";
126             print $fh_backward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
127             print $fh_backward "\n\n";
128         }
129
130         $q_max = $match->{ "Q_END" } if $match->{ "Q_END" } > $q_max;
131         $s_max = $match->{ "S_END" } if $match->{ "S_END" } > $s_max;
132     }
133
134     $q_max++;
135     $s_max++;
136
137     close $fh_forward;
138     close $fh_backward;
139
140     $cmd  = "gnuplot";
141
142     $pid = open2( $fh_out, $fh_in, $cmd );
143     
144     print $fh_in "set terminal $options->{ 'terminal' }\n";
145     print $fh_in "set xrange [1:$q_max]\n";
146     print $fh_in "set yrange [1:$s_max]\n";
147     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
148     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
149     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
150     print $fh_in "unset key\n";
151
152     if ( $options->{ "terminal" } ne "dumb" )
153     {
154         print $fh_in "set style line 1 linetype 1 linecolor rgb \"green\" linewidth 2 pointtype 6 pointsize default\n";
155         print $fh_in "set style line 2 linetype 1 linecolor rgb \"red\" linewidth 2 pointtype 6 pointsize default\n";
156     }
157
158     print $fh_in "set xtics border out\n";
159     print $fh_in "set ytics border out\n";
160     print $fh_in "set grid\n";
161
162     if ( $options->{ "direction" } =~ /^b/ ) {
163         print $fh_in qq(plot "$forward_file" with lines ls 1, "$backward_file" with lines ls 2\n);
164     } elsif ( $options->{ "direction" } =~ /^f/ ) {
165         print $fh_in qq(plot "$forward_file" with lines ls 1\n);
166     } elsif ( $options->{ "direction" } =~ /^r/ ) {
167         print $fh_in qq(plot "$backward_file" with lines ls 2\n);
168     }
169
170     close $fh_in;
171
172     while ( $line = <$fh_out> )
173     {
174         chomp $line;
175
176         push @lines, $line;
177     }
178
179     close $fh_out;
180
181     waitpid $pid, 0;
182
183     unlink $forward_file;
184     unlink $backward_file;
185
186     return wantarray ? @lines : \@lines;
187 }
188
189
190 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
191
192
193 BEGIN
194 {
195     Maasha::Biopieces::status_set();
196 }
197
198
199 END
200 {
201     Maasha::Biopieces::status_log();
202 }
203
204
205 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
206
207
208 __END__