From 2f9e9f3b7de073d03c9033798950f825713b351d Mon Sep 17 00:00:00 2001 From: martinahansen Date: Mon, 28 Sep 2009 16:35:07 +0000 Subject: [PATCH] added SAM.pm git-svn-id: http://biopieces.googlecode.com/svn/trunk@686 74ccb610-7750-0410-82ae-013aeee3265d --- code_perl/Maasha/SAM.pm | 127 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 code_perl/Maasha/SAM.pm diff --git a/code_perl/Maasha/SAM.pm b/code_perl/Maasha/SAM.pm new file mode 100644 index 0000000..c900867 --- /dev/null +++ b/code_perl/Maasha/SAM.pm @@ -0,0 +1,127 @@ +package Maasha::SAM; + +# Copyright (C) 2009 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +# Routines to handle SAM and BAM format. +# http://samtools.sourceforge.net/ + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +use warnings; +use strict; +use Maasha::Common; +use Data::Dumper; + +use vars qw ( @ISA @EXPORT ); + +@ISA = qw( Exporter ); + +use constant { + QNAME => 0, + FLAG => 1, + RNAME => 2, + POS => 3, + MAPQ => 4, + CIGAR => 5, + MRNM => 6, + MPOS => 7, + ISIZE => 8, + SEQ => 9, + QUAL => 10, + TAG => 11, + VTYPE => 12, + VALUE => 13, +}; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +sub get_entry +{ + # Martin A. Hansen, September 2009. + + # Parses a SAM entry from a given file handle. + + my ( $fh, # file handle + ) = @_; + + # Returns a list. + + my ( $line, @fields ); + + while ( $line = <$fh> ) + { + chomp $line; + + next if substr( $line, 0, 1 ) eq '@'; + + @fields = split "\t", $line; + + return wantarray ? @fields : \@fields; + } + + return; +} + + +sub sam2biopiece +{ + # Martin A. Hansen, September 2009. + + # Converts a SAM entry to a Biopiece record. + + my ( $entry, # SAM entry + ) = @_; + + # Returns a hashref. + + my ( $record ); + + $record->{ 'REC_TYPE' } = 'SAM'; + $record->{ 'Q_ID' } = $entry->[ QNAME ]; + $record->{ 'FLAG' } = $entry->[ FLAG ]; + $record->{ 'S_ID' } = $entry->[ RNAME ]; + $record->{ 'S_BEG' } = $entry->[ POS ]; + $record->{ 'MAPQ' } = $entry->[ MAPQ ]; + $record->{ 'CIGAR' } = $entry->[ CIGAR ]; + $record->{ 'MRNM' } = $entry->[ MRNM ]; + $record->{ 'S_BEG2' } = $entry->[ MPOS ]; + $record->{ 'ISIZE' } = $entry->[ ISIZE ]; + $record->{ 'SEQ' } = $entry->[ SEQ ]; + $record->{ 'SCORES' } = $entry->[ QUAL ]; + + $record->{ 'S_BEG' } -= 1 if $record->{ 'S_BEG' } != 0; + $record->{ 'S_BEG' } -= 1 if $record->{ 'S_BEG2' } != 0; + + return wantarray ? %{ $record } : $record; +} + + + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +1; -- 2.39.2