package Maasha::KISS::IO;

# 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 for parsing and emitting KISS records.


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


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

@ISA = qw( Exporter );

use constant {
    S_ID        => 0,
    S_BEG       => 1,
    S_END       => 2,
    Q_ID        => 3,
    SCORE       => 4,
    STRAND      => 5,
    HITS        => 6,
    ALIGN       => 7,
    BLOCK_COUNT => 8,
    BLOCK_BEGS  => 9,
    BLOCK_LENS  => 10,
};

#      0         1         2
#      012345678901234567890
#      ---------------------   S.aur complete genome
#         -===__===-           TAG_000001
#         0123456789
#
#    S_ID        = 'S.aur complete genome'
#    S_BEG       = 3
#    S_END       = 12
#    Q_ID        = 'TAG_000001'
#    SCORE       => 1
#    STRAND      => +
#    HITS        => 31
#    ALIGN       => 0:A>T,3:G>C
#    BLOCK_COUNT => 2
#    BLOCK_BEGS  => 1,6
#    BLOCK_LENS  => 3,3
#
#
# 'S.aur complete genome'   3   12  'TAG_000001'    1   +   31   2   1,6 3,3


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


sub kiss_entry_get
{
    my ( $fh,    # file handle
       ) = @_;

    # Returns a hashref.

    my ( $line, @fields, %entry );

    while ( $line = <$fh> )
    {
        chomp $line;

        next if $line =~ /^$|^#/;

        @fields = split /\t/, $line;

        Maasha::Common::error( qq( BAD kiss entry: $line) ) if not @fields == 11;
        
        $entry{ 'S_ID' }        = $fields[ S_ID ];
        $entry{ 'S_BEG' }       = $fields[ S_BEG ];
        $entry{ 'S_END' }       = $fields[ S_END ];
        $entry{ 'Q_ID' }        = $fields[ Q_ID ];
        $entry{ 'SCORE' }       = $fields[ SCORE ];
        $entry{ 'STRAND' }      = $fields[ STRAND ];
        $entry{ 'HITS' }        = $fields[ HITS ];
        $entry{ 'ALIGN' }       = $fields[ ALIGN ];
        $entry{ 'BLOCK_COUNT' } = $fields[ BLOCK_COUNT ];
        $entry{ 'BLOCK_BEGS' }  = $fields[ BLOCK_BEGS ];
        $entry{ 'BLOCK_LENS' }  = $fields[ BLOCK_LENS ];

        return wantarray ? %entry : \%entry;
    }
}


sub kiss_entry_put
{
    my ( $entry,   # KISS entry to output
         $fh,      # file handle  -  OPTIONAL
       ) = @_;

    # Returns nothing.
    
    my ( @fields );

    $fh ||= \*STDOUT;

    $fields[ S_ID ]   = $entry->{ 'S_ID' };
    $fields[ S_BEG ]  = $entry->{ 'S_BEG' };
    $fields[ S_END ]  = $entry->{ 'S_END' };
    $fields[ Q_ID ]   = $entry->{ 'Q_ID' };
    $fields[ SCORE ]  = $entry->{ 'SCORE' };
    $fields[ STRAND ] = $entry->{ 'STRAND' };
    $fields[ HITS ]   = $entry->{ 'HITS' };
    $fields[ ALIGN ]  = $entry->{ 'ALIGN' };
    $fields[ BLOCK_COUNT ] = $entry->{ 'BLOCK_COUNT' };
    $fields[ BLOCK_BEGS ]  = $entry->{ 'BLOCK_BEGS' };
    $fields[ BLOCK_LENS ]  = $entry->{ 'BLOCK_LENS' };

    print $fh join( "\t", @fields ), "\n";
}


sub kiss2biopiece
{
    my ( $entry,   # KISS entry
       ) = @_;

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


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

    $record->{ 'HITS' }        ||= ".";
    $record->{ 'BLOCK_COUNT' } ||= ".";
    $record->{ 'BLOCK_BEGS' }  ||= ".";
    $record->{ 'BLOCK_LENS' }  ||= ".";
    $record->{ 'ALIGN' }       ||= $record->{ 'DESCRIPTOR' } || ".";

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


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

1;


