package Maasha::BGB::Common;

# 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


# Common routines for manipulating the Biopieces Genome Browser.


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


use warnings;
use strict;
use Data::Dumper;
use Maasha::Common;
use Maasha::Filesys;
use Maasha::Biopieces;

use vars qw( @ISA @EXPORT );

@ISA = qw( Exporter );


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


sub list_user_dir
{
    # Martin A. Hansen, December 2009.

    # List all users directories in the ~/Data/Users
    # directory with full path.

    # Returns a list.

    my ( @dirs, @users );

    Maasha::Common::error( 'BP_WWW not set in environment' ) if not $ENV{ 'BP_WWW' };

    @dirs = Maasha::Filesys::ls_dirs( "$ENV{ 'BP_WWW' }/Data/Users" );

    @users = grep { $_ !~ /\/\.\.?$/ } @dirs;

    return wantarray ? @users : \@users;
}


sub list_users
{
    # Martin A. Hansen, December 2009.

    # List all users in ~/Data/Users

    # Returns a list.
    
    my ( @dirs, $dir, @users );

    @dirs = list_user_dir();

    foreach $dir ( @dirs ) {
        push @users, ( split "/", $dir )[ -1 ];
    }

    return wantarray ? @users : \@users;
}


sub list_clade_dir
{
    # Martin A. Hansen, December 2009.

    # List all clades for a given user in ~/Data/Users

    my ( $user,   # user for which to return clades
       ) = @_;

    # Returns a list.

    my ( @dirs, @clades );

    Maasha::Common::error( 'BP_WWW not set in environment' ) if not $ENV{ 'BP_WWW' };
    Maasha::Common::error( 'no user specified' ) if not $user;

    @dirs = Maasha::Filesys::ls_dirs( "$ENV{ 'BP_WWW' }/Data/Users/$user" );

    @clades = grep { $_ !~ /\/\.\.?$/ } @dirs;

    return wantarray ? @clades : \@clades;
}


sub list_clades
{
    # Martin A. Hansen, December 2009.

    # List all clades for a given user in ~/Data/Users

    my ( $user,   # user for which to return clades
       ) = @_;

    # Returns a list.
    
    my ( @dirs, $dir, @clades );

    @dirs = list_clade_dir( $user );

    foreach $dir ( @dirs ) {
        push @clades, ( split "/", $dir )[ -1 ];
    }

    return wantarray ? @clades : \@clades;
}


sub list_genome_dir
{
    # Martin A. Hansen, December 2009.

    # List all genomes for a given user and clade in ~/Data/Users

    my ( $user,    # user for which to return genomes
         $clade,   # clade for which to return genomes
       ) = @_;

    # Returns a list.

    my ( @dirs, @genomes );

    Maasha::Common::error( 'BP_WWW not set in environment' ) if not $ENV{ 'BP_WWW' };
    Maasha::Common::error( 'no user specified' ) if not $user;
    Maasha::Common::error( 'no clade specified' ) if not $clade;

    @dirs = Maasha::Filesys::ls_dirs( "$ENV{ 'BP_WWW' }/Data/Users/$user/$clade" );

    @genomes = grep { $_ !~ /\/\.\.?$/ } @dirs;

    return wantarray ? @genomes : \@genomes;
}


sub list_genomes
{
    # Martin A. Hansen, December 2009.

    # List all genomes for a given user and clade in ~/Data/Users

    my ( $user,    # user for which to return genomes
         $clade,   # clade for which to return genomes
       ) = @_;

    # Returns a list.
    
    my ( @dirs, $dir, @genomes );

    @dirs = list_genome_dir( $user, $clade );

    foreach $dir ( @dirs ) {
        push @genomes, ( split "/", $dir )[ -1 ];
    }

    return wantarray ? @genomes : \@genomes;
}


sub list_assembly_dir
{
    # Martin A. Hansen, December 2009.

    # List all assemblies for a given user and clade and genome in ~/Data/Users

    my ( $user,     # user for which to return assemblies
         $clade,    # clade for which to return assemblies
         $genome,   # genome for which to return assemblies
       ) = @_;

    # Returns a list.

    my ( @dirs, @assemblies );

    Maasha::Common::error( 'BP_WWW not set in environment' ) if not $ENV{ 'BP_WWW' };
    Maasha::Common::error( 'no user specified' ) if not $user;
    Maasha::Common::error( 'no clade specified' ) if not $clade;
    Maasha::Common::error( 'no genome specified' ) if not $genome;

    @dirs = Maasha::Filesys::ls_dirs( "$ENV{ 'BP_WWW' }/Data/Users/$user/$clade/$genome" );

    @assemblies = grep { $_ !~ /\/\.\.?$/ } @dirs;

    return wantarray ? @assemblies : \@assemblies;
}


sub list_assemblies
{
    # Martin A. Hansen, December 2009.

    # List all assemblies for a given user and clade and genome in ~/Data/Users

    my ( $user,     # user for which to return assemblies
         $clade,    # clade for which to return assemblies
         $genome,   # genome for which to return assemblies
       ) = @_;

    # Returns a list.
    
    my ( @dirs, $dir, @assemblies );

    @dirs = list_assembly_dir( $user, $clade, $genome );

    foreach $dir ( @dirs ) {
        push @assemblies, ( split "/", $dir )[ -1 ];
    }

    return wantarray ? @assemblies : \@assemblies;
}


sub list_contig_dir
{
    # Martin A. Hansen, December 2009.

    # List all assemblies for a given user->clade->genome->assembly in ~/Data/Users

    my ( $user,       # user for which to return contigs
         $clade,      # clade for which to return contigs
         $genome,     # genome for which to return contigs
         $assembly,   # assembly for which to return contigs
       ) = @_;

    # Returns a list.

    my ( @dirs, @contigs );

    Maasha::Common::error( 'BP_WWW not set in environment' ) if not $ENV{ 'BP_WWW' };
    Maasha::Common::error( 'no user specified' ) if not $user;
    Maasha::Common::error( 'no clade specified' ) if not $clade;
    Maasha::Common::error( 'no genome specified' ) if not $genome;
    Maasha::Common::error( 'no assembly specified' ) if not $assembly;

    @dirs = Maasha::Filesys::ls_dirs( "$ENV{ 'BP_WWW' }/Data/Users/$user/$clade/$genome/$assembly" );

    @contigs = grep { $_ !~ /\/\.\.?$/ } @dirs;

    return wantarray ? @contigs : \@contigs;
}


sub list_contigs
{
    # Martin A. Hansen, December 2009.

    # List all contigs for a given user->clade->genome->assembly in ~/Data/Users

    my ( $user,       # user for which to return contigs
         $clade,      # clade for which to return contigs
         $genome,     # genome for which to return contigs
         $assembly,   # assembly for which to return contigs
       ) = @_;

    # Returns a list.
    
    my ( @dirs, $dir, @contigs );

    @dirs = list_contig_dir( $user, $clade, $genome, $assembly );

    foreach $dir ( @dirs ) {
        push @contigs, ( split "/", $dir )[ -1 ];
    }

    return wantarray ? @contigs : \@contigs;
}


sub list_track_dir
{
    # Martin A. Hansen, December 2009.

    # List all tracks for a given user->clade->genome->assembly->contig in ~/Data/Users

    my ( $user,       # user for which to return tracks
         $clade,      # clade for which to return tracks
         $genome,     # genome for which to return tracks
         $assembly,   # assembly for which to return tracks
         $contig,     # contig for which to return tracks
       ) = @_;

    # Returns a list.

    my ( @dirs, @tracks );

    Maasha::Common::error( 'BP_WWW not set in environment' ) if not $ENV{ 'BP_WWW' };
    Maasha::Common::error( 'no user specified' ) if not $user;
    Maasha::Common::error( 'no clade specified' ) if not $clade;
    Maasha::Common::error( 'no genome specified' ) if not $genome;
    Maasha::Common::error( 'no assembly specified' ) if not $assembly;
    Maasha::Common::error( 'no contig specified' ) if not $contig;

    if ( -d "$ENV{ 'BP_WWW' }/Data/Users/$user/$clade/$genome/$assembly/$contig/Tracks" ) {
        @dirs = Maasha::Filesys::ls_dirs( "$ENV{ 'BP_WWW' }/Data/Users/$user/$clade/$genome/$assembly/$contig/Tracks" );
    }

    @tracks = grep { $_ !~ /\/\.\.?$/ } @dirs;

    return wantarray ? @tracks : \@tracks;
}


sub list_tracks
{
    # Martin A. Hansen, December 2009.

    # List all tracks for a given user->clade->genome->assembly->contig in ~/Data/Users

    my ( $user,       # user for which to return tracks
         $clade,      # clade for which to return tracks
         $genome,     # genome for which to return tracks
         $assembly,   # assembly for which to return tracks
         $contig,     # contig for which to return tracks
       ) = @_;

    # Returns a list.
    
    my ( @dirs, $dir, @tracks );

    @dirs = list_track_dir( $user, $clade, $genome, $assembly, $contig );

    foreach $dir ( @dirs ) {
        push @tracks, ( split "/", $dir )[ -1 ];
    }

    return wantarray ? @tracks : \@tracks;
}


sub max_track
{
    # Martin A. Hansen, December 2009.
    
    # Traverses all contigs for a given user->clade->genome->assembly and
    # returns the maximum track's prefix value eg. 20 for 20_Genbank.

    my ( $user,
         $clade,
         $genome,
         $assembly
       ) = @_;

    # Returns an integer
    
    my ( @contigs, $contig, @tracks, $max );

    @contigs = list_contigs( $user, $clade, $genome, $assembly );

    foreach $contig ( @contigs ) {
        push @tracks, list_tracks( $user, $clade, $genome, $assembly, $contig );
    }

    @tracks = sort @tracks;

    if ( scalar @tracks > 0 and $tracks[ -1 ] =~ /^(\d+)/ ) {
        $max = $1;
    } else {
        $max = 0;
    }

    return $max;
}


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


1;
