3 # Copyright (C) 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines to handle SAM and BAM format.
26 # http://samtools.sourceforge.net/
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37 use vars qw ( @ISA @EXPORT );
39 @ISA = qw( Exporter );
59 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
64 # Martin A. Hansen, September 2009.
66 # Parses a SAM entry from a given file handle.
68 my ( $fh, # file handle
73 my ( $line, @fields );
75 while ( $line = <$fh> )
79 next if substr( $line, 0, 1 ) eq '@';
81 @fields = split "\t", $line;
83 return wantarray ? @fields : \@fields;
92 # Martin A. Hansen, September 2009.
94 # Converts a SAM entry to a Biopiece record.
96 my ( $entry, # SAM entry
103 $record->{ 'REC_TYPE' } = 'SAM';
104 $record->{ 'Q_ID' } = $entry->[ QNAME ];
105 $record->{ 'FLAG' } = $entry->[ FLAG ];
106 $record->{ 'S_ID' } = $entry->[ RNAME ];
107 $record->{ 'S_BEG' } = $entry->[ POS ];
108 $record->{ 'MAPQ' } = $entry->[ MAPQ ];
109 $record->{ 'CIGAR' } = $entry->[ CIGAR ];
110 $record->{ 'MRNM' } = $entry->[ MRNM ];
111 $record->{ 'S_BEG2' } = $entry->[ MPOS ];
112 $record->{ 'ISIZE' } = $entry->[ ISIZE ];
113 $record->{ 'SEQ' } = $entry->[ SEQ ];
114 $record->{ 'SCORES' } = $entry->[ QUAL ];
116 $record->{ 'S_BEG' } -= 1 if $record->{ 'S_BEG' } != 0;
117 $record->{ 'S_BEG' } -= 1 if $record->{ 'S_BEG2' } != 0;
119 $record->{ 'S_END' } = $record->{ 'S_BEG' } + length( $record->{ 'SEQ' } ) - 1;
121 if ( $record->{ 'FLAG' } & 0x0010 ) {
122 $record->{ 'STRAND' } = '+';
124 $record->{ 'STRAND' } = '-';
127 return wantarray ? %{ $record } : $record;
133 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<