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