package Maasha::GFF;


# Copyright (C) 2007-2008 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 'Generic Feature Format' - GFF.


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


use strict;
use Data::Dumper;
use Maasha::Common;

use vars qw( @ISA @EXPORT_OK );

require Exporter;

@ISA = qw( Exporter );


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


sub get_entry
{
    # Martin A. Hansen, February 2008.

    # Reads a single entry from a filehandle to a GFF file.

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

    # Returns hashref.

    my ( $line, @fields, %entry, $q_beg, $q_end, @atts, $att, $key, $val );

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

        @fields = split "\t", $line;
    
        if ( @fields == 9 )
        {
            $q_beg = $fields[ 3 ] - 1;
            $q_end = $fields[ 4 ] - 1;

            ( $q_beg, $q_end ) = ( $q_end, $q_beg ) if $q_beg > $q_end;

            %entry = (
                Q_ID       => $fields[ 0 ],
                SOURCE     => $fields[ 1 ],
                TYPE       => $fields[ 2 ],
                Q_BEG      => $q_beg,
                Q_END      => $q_end,
                SCORE      => $fields[ 5 ],
                STRAND     => $fields[ 6 ],
                PHASE      => $fields[ 7 ],
                ATT        => $fields[ 8 ],
            );

            @atts = split ";", $fields[ 8 ];

            foreach $att ( @atts )
            {
                ( $key, $val ) = split "=", $att;
            
                $entry{ "ATT_" . uc $key } = $val;
            }

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


sub get_entries
{
    # Martin A. Hansen, February 2008.

    # Reads GFF file and returns a list of entries.

    my ( $path,   # full path to GFF file.
       ) = @_;

    # Returns a list.

    my ( $fh, $entry, @entries );

    $fh = Maasha::Common::read_open( $path );

    while ( $entry = get_entry( $fh ) ) {
        push @entries, $entry;
    }

    close $fh;

    return wantarray ? @entries : \@entries;
}


sub put_entry
{


}


sub put_entries
{


}


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

1;
