]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/mate_pair_dist
fixed seq qual length check
[biopieces.git] / bp_bin / mate_pair_dist
1 #!/usr/bin/env perl
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 # Determines the distance between mapped mate pair sequence matches.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Biopieces;
33
34
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36
37
38 my ( $options, $in, $out, $record, %data, $q_id, $mate, $contig, $strand, @mates1, @mates2, $mate1, $mate2, $new_record );
39
40 $options = Maasha::Biopieces::parse_options();
41
42 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
43 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
44
45 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
46 {
47     if ( exists $record->{ 'S_ID' }   and
48          exists $record->{ 'Q_ID' }   and 
49          exists $record->{ 'STRAND' } and 
50          exists $record->{ 'S_BEG' }
51     )
52     {
53         ( $q_id, $mate ) = split "/", $record->{ 'Q_ID' }, 2;
54
55         if ( $mate )
56         {
57             if ( $mate == 2 )
58             {
59                 if ( $record->{ 'STRAND' } eq '+' ) {
60                     $record->{ 'STRAND' } = '-';
61                 } else {
62                     $record->{ 'STRAND' } = '+';
63                 }
64             }
65
66             push @{ $data{ $record->{ 'S_ID' } }{ $record->{ 'STRAND' } }{ $q_id }{ $mate } }, $record; 
67         }
68     }
69
70     Maasha::Biopieces::put_record( $record, $out );
71 }
72
73 # print Dumper( \%data );   # DEBUG
74
75 foreach $contig ( keys %data )
76 {
77     foreach $strand ( keys %{ $data{ $contig } } )
78     {
79         foreach $q_id ( keys %{ $data{ $contig }{ $strand } } )
80         {
81             next if not exists $data{ $contig }{ $strand }{ $q_id }{ 1 };
82             next if not exists $data{ $contig }{ $strand }{ $q_id }{ 2 };
83
84             @mates1 = @{ $data{ $contig }{ $strand }{ $q_id }{ 1 } };
85             @mates2 = @{ $data{ $contig }{ $strand }{ $q_id }{ 2 } };
86
87             @mates1 = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } } @mates1;
88             @mates2 = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } } @mates2;
89
90             foreach $mate1 ( @mates1 )
91             {
92                 foreach $mate2 ( @mates2 )
93                 {
94                     $new_record = {
95                         S_ID     => $contig,
96                         Q_ID1    => $mate1->{ 'Q_ID' },
97                         Q_ID2    => $mate2->{ 'Q_ID' },
98                         S_BEG1   => $mate1->{ 'S_BEG' },
99                         S_BEG2   => $mate2->{ 'S_BEG' },
100                         S_END1   => $mate1->{ 'S_END' },
101                         S_END2   => $mate2->{ 'S_END' },
102                         SEQ_LEN1 => $mate1->{ 'SEQ_LEN' },
103                         SEQ_LEN2 => $mate2->{ 'SEQ_LEN' },
104                         STRAND   => $mate1->{ 'STRAND' },
105                         DIST   => abs( $mate2->{ 'S_BEG' } - $mate1->{ 'S_BEG' } ),
106                     };
107
108                     Maasha::Biopieces::put_record( $new_record, $out );
109                 }
110             }
111         }
112     }
113 }
114
115 Maasha::Biopieces::close_stream( $in );
116 Maasha::Biopieces::close_stream( $out );
117
118
119 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
120
121
122 BEGIN
123 {
124     Maasha::Biopieces::status_set();
125 }
126
127
128 END
129 {
130     Maasha::Biopieces::status_log();
131 }
132
133
134 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
135
136
137 __END__