3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Generate a dotplot of matches in the stream.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 my ( $options, $in, $out, $default, $terminals, $record, @data, $fh, $result, %data_hash, $tmp_dir );
42 $default = "plot_matches";
43 $terminals = "dumb,x11,aqua,post,svg";
45 $options = Maasha::Biopieces::parse_options(
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 },
57 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
60 while ( $record = Maasha::Biopieces::get_record( $in ) )
62 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
66 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
69 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
70 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
72 $tmp_dir = Maasha::Biopieces::get_tmpdir();
74 $result = dotplot_matches( \@data, $options, $tmp_dir );
76 $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
78 print $fh "$_\n" foreach @{ $result };
82 Maasha::Biopieces::close_stream( $in );
83 Maasha::Biopieces::close_stream( $out );
86 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
91 # Martin A. Hansen, August 2007.
93 # Generates a dotplot from a list of matches using Gnuplot.
95 my ( $matches, # list of hashrefs.
96 $options, # options hash
97 $tmp_dir, # temporary directory
102 my ( $forward_file, $backward_file, $pid, $fh_forward, $fh_backward,
103 $fh_in, $fh_out, $cmd, $match, $line, @lines, $q_max, $s_max );
105 $forward_file = "$tmp_dir/match_f.tab";
106 $backward_file = "$tmp_dir/match_r.tab";
108 $fh_forward = Maasha::Filesys::file_write_open( $forward_file );
109 $fh_backward = Maasha::Filesys::file_write_open( $backward_file );
114 foreach $match ( @{ $matches } )
116 if ( ($match->{ "DIR" } and $match->{ "DIR" } =~ /^f/) or ($match->{ "STRAND" } and $match->{ "STRAND" } eq '+') )
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";
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";
129 $q_max = $match->{ "Q_END" } if $match->{ "Q_END" } > $q_max;
130 $s_max = $match->{ "S_END" } if $match->{ "S_END" } > $s_max;
139 $cmd = "gnuplot -persist";
141 $pid = open2( $fh_out, $fh_in, $cmd );
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";
151 if ( $options->{ "terminal" } ne "dumb" )
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";
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";
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);
171 while ( $line = <$fh_out> )
182 unlink $forward_file;
183 unlink $backward_file;
185 return wantarray ? @lines : \@lines;
189 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194 Maasha::Biopieces::status_set();
200 Maasha::Biopieces::status_log();
204 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<