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 # Calculate coverage of reads mapped to a backbone sequence.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 use Maasha::Biopieces;
35 use Maasha::UCSC::Wiggle;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $in, $out, $record, $contig_hash, $i, $s_id, $strand, $new_record, $contig, $beg, $end, @array, $min, $max, $mean );
43 $options = Maasha::Biopieces::parse_options(
45 { long => 'bridge', short => 'b', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
49 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
52 while ( $record = Maasha::Biopieces::get_record( $in ) )
54 if ( $record->{ 'S_ID' } and $record->{ 'STRAND' } )
56 if ( $record->{ 'Q_ID' } and $record->{ 'S_BEG' } and $record->{ 'S_END' } )
58 for ( $i = $record->{ 'S_BEG' }; $i <= $record->{ 'S_END' }; $i++ ) {
59 $contig_hash->{ $record->{ 'S_ID' } }->{ $record->{ 'STRAND' } }->[ $i ]++;
63 if ( $record->{ 'Q_ID1' } and $record->{ 'S_BEG1' } and $record->{ 'S_END1' } and
64 $record->{ 'Q_ID2' } and $record->{ 'S_BEG2' } and $record->{ 'S_END2' }
67 if ( $options->{ 'bridge' } )
69 ( $record->{ 'S_BEG1' }, $record->{ 'S_END2' } ) = ( $record->{ 'S_BEG2' }, $record->{ 'S_END1' } ) if ( $record->{ 'S_BEG1' } > $record->{ 'S_END2' } );
71 for ( $i = $record->{ 'S_BEG1' }; $i <= $record->{ 'S_END2' }; $i++ ) {
72 $contig_hash->{ $record->{ 'S_ID' } }->{ $record->{ 'STRAND' } }->[ $i ]++;
77 for ( $i = $record->{ 'S_BEG1' }; $i <= $record->{ 'S_END1' }; $i++ ) {
78 $contig_hash->{ $record->{ 'S_ID' } }->{ $record->{ 'STRAND' } }->[ $i ]++;
81 for ( $i = $record->{ 'S_BEG2' }; $i <= $record->{ 'S_END2' }; $i++ ) {
82 $contig_hash->{ $record->{ 'S_ID' } }->{ $record->{ 'STRAND' } }->[ $i ]++;
88 # Maasha::Biopieces::put_record( $record, $out );
93 foreach $s_id ( keys %{ $contig_hash } )
95 foreach $strand ( keys %{ $contig_hash->{ $s_id } } )
100 while ( ( $beg, $end ) = Maasha::UCSC::Wiggle::fixedstep_scan( $contig_hash->{ $s_id }->{ $strand }, $beg ) )
102 @array = @{ $contig_hash->{ $s_id }->{ $strand } }[ $beg .. $end - 1 ];
104 ( $min, $max ) = Maasha::Calc::minmax( \@array );
105 $mean = Maasha::Calc::mean( \@array );
107 $new_record->{ 'S_ID' } = $s_id;
108 $new_record->{ 'STRAND' } = $strand;
109 $new_record->{ 'S_BEG' } = $beg;
110 $new_record->{ 'S_END' } = $end;
111 $new_record->{ 'CONTIG_ID' } = $contig;
112 $new_record->{ 'CONTIG_LEN' } = $end - $beg + 1;
113 $new_record->{ 'CONTIG_MIN' } = $min;
114 $new_record->{ 'CONTIG_MAX' } = $max;
115 $new_record->{ 'CONTIG_MEAN' } = sprintf "%.2f", $mean;
116 $new_record->{ 'CONTIG' } = join ( ";", @array );
121 Maasha::Biopieces::put_record( $new_record, $out );
126 Maasha::Biopieces::close_stream( $in );
127 Maasha::Biopieces::close_stream( $out );
130 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
135 Maasha::Biopieces::status_set();
141 Maasha::Biopieces::status_log();
145 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<