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;
