package Maasha::Fastq;

# Copyright (C) 2006-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 for manipulation of FASTQ files and FASTQ entries.

# http://maq.sourceforge.net/fastq.shtml


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


use warnings;
use strict;
use Data::Dumper;
use Maasha::Calc;
use vars qw( @ISA @EXPORT );

@ISA = qw( Exporter );

use constant {
    SEQ_NAME => 0,
    SEQ      => 1,
    SCORES   => 2,
};


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


sub get_entry
{
    # Martin A. Hansen, July 2009.

    # Gets the next FASTQ entry from a given filehandle.

    my ( $fh,   # filehandle
       ) = @_;

    # Returns a list

    my ( $seq, $seq_name, $qual, $qual_name );

    $seq_name  = <$fh>;
    $seq       = <$fh>;
    $qual_name = <$fh>;
    $qual      = <$fh>;

    return unless $seq;

    chomp $seq;
    chomp $seq_name;
    chomp $qual;
    chomp $qual_name;

    $seq_name =~ s/^@//;

    return wantarray ? ( $seq_name, $seq, $qual ) : [ $seq_name, $seq, $qual ];
}


sub put_entry
{
    # Martin A. Hansen, July 2009.

    # Output a FASTQ entry to STDOUT or a filehandle.

    my ( $entry,   # FASTQ entry
         $fh,      # filehandle - OPTIONAL
       ) = @_;

    # Returns nothing.

    $fh ||= \*STDOUT;

    print $fh "@" . $entry->[ SEQ_NAME ] . "\n";
    print $fh $entry->[ SEQ ] . "\n";
    print $fh "+\n";
    print $fh $entry->[ SCORES ] . "\n";
}


sub fastq2biopiece
{
    # Martin A. Hansen, July 2009.
    
    # Converts a FASTQ entry to a Biopiece record, where
    # the FASTQ quality scores are converted to numerics.

    my ( $entry,     # FASTQ entry,
       ) = @_;

    # Returns a hash.

    my ( $record );

    $record->{ 'SEQ' }        = $entry->[ SEQ ];
    $record->{ 'SEQ_NAME' }   = $entry->[ SEQ_NAME ];
    $record->{ 'SCORES' }     = $entry->[ SCORES ];

    $record->{ 'SCORES' }     =~ s/(.)/ord( $1 ) - 33 . ";"/ge; # http://maq.sourceforge.net/fastq.shtml
    $record->{ 'SCORE_MEAN' } = sprintf( "%.2f", Maasha::Calc::mean( [ split /;/, $record->{ 'SCORES' } ] ) );

    return wantarray ? %{ $record } : $record;
}


sub biopiece2fastq
{
    # Martin A. Hansen, July 2009.
    
    # Converts a Biopiece record to a FASTQ entry.

    my ( $record,   # Biopiece record
       ) = @_;

    # Returns a list.

    my ( $list );

    if ( exists $record->{ 'SEQ' } and exists $record->{ 'SEQ_NAME' } and exists $record->{ 'SCORES' } )
    {
        $list->[ SEQ_NAME ] = $record->{ 'SEQ_NAME' };
        $list->[ SEQ ]      = $record->{ 'SEQ' };
        $list->[ SCORES ]   = $record->{ 'SCORES' };
    
        $list->[ SCORES ] =~ s/(\d+);/chr( ( $1 <= 93 ? $1 : 93 ) + 33 )/ge;

        return wantarray ? @{ $list } : $list;
    }

    return;
}


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
