]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_matches
removed variables bulk type
[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
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
85
86
87 sub dotplot_matches
88 {
89     # Martin A. Hansen, August 2007.
90
91     # Generates a dotplot from a list of matches using Gnuplot.
92
93     my ( $matches,   # list of hashrefs.
94          $options,   # options hash
95          $tmp_dir,   # temporary directory
96        ) = @_;
97
98     # Returns list.
99
100     my ( $forward_file, $backward_file, $pid, $fh_forward, $fh_backward,
101          $fh_in, $fh_out, $cmd, $match, $line, @lines, $q_max, $s_max );
102
103     $forward_file  = "$tmp_dir/match_f.tab";
104     $backward_file = "$tmp_dir/match_r.tab";
105
106     $fh_forward  = Maasha::Filesys::file_write_open( $forward_file );
107     $fh_backward = Maasha::Filesys::file_write_open( $backward_file );
108
109     $q_max = 0;
110     $s_max = 0;
111
112     foreach $match ( @{ $matches } )
113     {
114         if ( $match->{ "DIR" } =~ /^f/ )
115         {
116             print $fh_forward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
117             print $fh_forward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_END" } + 1 ), "\n";
118             print $fh_forward "\n\n";
119         }
120         else
121         {
122             print $fh_backward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_END" } + 1 ), "\n";
123             print $fh_backward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
124             print $fh_backward "\n\n";
125         }
126
127         $q_max = $match->{ "Q_END" } if $match->{ "Q_END" } > $q_max;
128         $s_max = $match->{ "S_END" } if $match->{ "S_END" } > $s_max;
129     }
130
131     $q_max++;
132     $s_max++;
133
134     close $fh_forward;
135     close $fh_backward;
136
137     $cmd  = "gnuplot";
138
139     $pid = open2( $fh_out, $fh_in, $cmd );
140     
141     print $fh_in "set terminal $options->{ 'terminal' }\n";
142     print $fh_in "set xrange [1:$q_max]\n";
143     print $fh_in "set yrange [1:$s_max]\n";
144     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
145     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
146     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
147     print $fh_in "unset key\n";
148
149     if ( $options->{ "terminal" } ne "dumb" )
150     {
151         print $fh_in "set style line 1 linetype 1 linecolor rgb \"green\" linewidth 2 pointtype 6 pointsize default\n";
152         print $fh_in "set style line 2 linetype 1 linecolor rgb \"red\" linewidth 2 pointtype 6 pointsize default\n";
153     }
154
155     print $fh_in "set xtics border out\n";
156     print $fh_in "set ytics border out\n";
157     print $fh_in "set grid\n";
158
159     if ( $options->{ "direction" } =~ /^b/ ) {
160         print $fh_in qq(plot "$forward_file" with lines ls 1, "$backward_file" with lines ls 2\n);
161     } elsif ( $options->{ "direction" } =~ /^f/ ) {
162         print $fh_in qq(plot "$forward_file" with lines ls 1\n);
163     } elsif ( $options->{ "direction" } =~ /^r/ ) {
164         print $fh_in qq(plot "$backward_file" with lines ls 2\n);
165     }
166
167     close $fh_in;
168
169     while ( $line = <$fh_out> )
170     {
171         chomp $line;
172
173         push @lines, $line;
174     }
175
176     close $fh_out;
177
178     waitpid $pid, 0;
179
180     unlink $forward_file;
181     unlink $backward_file;
182
183     return wantarray ? @lines : \@lines;
184 }
185
186
187 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
188
189
190 BEGIN
191 {
192     $run_time_beg = Maasha::Biopieces::run_time();
193
194     Maasha::Biopieces::log_biopiece();
195 }
196
197
198 END
199 {
200     Maasha::Biopieces::close_stream( $in );
201     Maasha::Biopieces::close_stream( $out );
202
203     $run_time_end = Maasha::Biopieces::run_time();
204
205     Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
206 }
207
208
209 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
210
211
212 __END__
213