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;
98 # Martin A. Hansen, September 2009.
100 # Converts a SAM entry to a Biopiece record.
102 my ( $entry, # SAM entry
109 $record->{ 'REC_TYPE' } = 'SAM';
110 $record->{ 'Q_ID' } = $entry->[ QNAME ];
111 $record->{ 'FLAG' } = $entry->[ FLAG ];
112 $record->{ 'S_ID' } = $entry->[ RNAME ];
113 $record->{ 'S_BEG' } = $entry->[ POS ];
114 $record->{ 'MAPQ' } = $entry->[ MAPQ ];
115 $record->{ 'CIGAR' } = $entry->[ CIGAR ];
116 $record->{ 'MRNM' } = $entry->[ MRNM ];
117 $record->{ 'S_BEG2' } = $entry->[ MPOS ];
118 $record->{ 'ISIZE' } = $entry->[ ISIZE ];
119 $record->{ 'SEQ' } = $entry->[ SEQ ];
120 $record->{ 'SCORES' } = $entry->[ QUAL ];
121 $record->{ 'TAG' } = $entry->[ TAG ];
122 $record->{ 'VTYPE' } = $entry->[ VTYPE ];
123 $record->{ 'VALUE' } = $entry->[ VALUE ];
125 $record->{ 'S_BEG' } -= 1 if $record->{ 'S_BEG' } != 0;
126 $record->{ 'S_BEG' } -= 1 if $record->{ 'S_BEG2' } != 0;
128 $record->{ 'S_END' } = $record->{ 'S_BEG' } + length( $record->{ 'SEQ' } ) - 1;
130 $record->{ 'READ_PAIRED' } = $record->{ 'FLAG' } & 0x0001;
131 $record->{ 'READ_MAPPED' } = $record->{ 'FLAG' } & 0x0002;
132 $record->{ 'Q_SEQ_UNMAPPED' } = $record->{ 'FLAG' } & 0x0004;
133 $record->{ 'MATE_UNMAPPED' } = $record->{ 'FLAG' } & 0x0008;
135 if ( $record->{ 'FLAG' } & 0x0010 ) {
136 $record->{ 'STRAND' } = '+';
138 $record->{ 'STRAND' } = '-';
141 $record->{ 'MATE_STRAND' } = $record->{ 'FLAG' } & 0x0020;
142 $record->{ '1ST_READ' } = $record->{ 'FLAG' } & 0x0040;
143 $record->{ '2ND_READ' } = $record->{ 'FLAG' } & 0x0080;
144 $record->{ 'ALIGN_PRIMARY' } = $record->{ 'FLAG' } & 0x0100;
145 $record->{ 'READ_FAIL' } = $record->{ 'FLAG' } & 0x0200;
146 $record->{ 'READ_BAD' } = $record->{ 'FLAG' } & 0x0400;
148 return wantarray ? %{ $record } : $record;
159 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<