#!/Users/runner/bioconda/conda-bld/maker_1616698748543/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_place/bin/perl 

eval 'exec /Users/runner/bioconda/conda-bld/maker_1616698748543/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_place/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

use warnings;
use strict;
use Getopt::Long;
use File::Temp qw(tempfile);

my $usage = "

Synopsis:

fasta_merge -d maker_datastore_index.log
fasta_merge -o genome.all -i <fasta1> <fasta2> ...

Descriptions:

This script will take a MAKER datastore index log file, extract all
the relevant fasta files and create fasta files with relevant
categories of sequence (i.e. transcript, protein, GeneMark protien,
etc.).  For this to work properly you need to be in the same directory
as the datastore index.

Options:

  -d The location of the MAKER datastore index log.
  -o Alternate base name for the output files.
  -i A optional list of files to process along with or instead of the
     datastore.

";

my $datastore;
my @files;
my $outfile;

GetOptions ("datastor|d=s" => \$datastore,
	    "i=s" => \@files,
	    "o=s" => \$outfile,
	    "help|?" => sub{print $usage; exit();}
	    );

if(! $datastore){
    print $usage;
    exit();
}

if (! $outfile){
    ($outfile) = $datastore =~ /([^\/]+)$/;
    $outfile =~ s/_master_datastore_index.log//;
    $outfile = "genome" if(! defined $outfile);
}

die "ERROR: The file \'$datastore\' does not exist\n" if ($datastore && ! -r $datastore);
if ($datastore){
    open(IN, '<', $datastore) or die "Can't open $datastore for reading\n";

    #uniq the entries
    my %entries;
    @entries{@{[<IN>]}} = ();

    my @dirs;
    foreach my $e (keys %entries){
	next unless ($e =~ /FINISHED/);
	chomp $e;
	my ($id, $dir, $status) = split("\t", $e);
	$dir =~ s/\/$//;
	push(@dirs, $dir);
    }

    foreach my $dir (@dirs){
	if (! -d $dir){
	    my $base = $datastore;
	    $base =~ s/[^\/]+$/\./;
	    $dir = "$base/$dir";
	}

	my @f_files = <$dir/*.transcripts.fasta>;
	push(@files, @f_files);
	@f_files = <$dir/*.proteins.fasta>;
	push(@files, @f_files);
    }
}

my %groups;

foreach my $file (@files){
    if($file =~ /([^\.]+)\.transcripts\.fasta$/){
	my $key = $1;
	push(@{$groups{$key}{transcripts}}, $file);
    }
    elsif($file =~ /([^\.]+)\.proteins\.fasta$/){
	my $key = $1;
	push(@{$groups{$key}{proteins}}, $file);
    }
    elsif($file =~ /([^\.]+)\.noncoding\.fasta$/){
	my $key = $1;
	push(@{$groups{$key}{noncoding}}, $file);
    }
    else{
	push(@{$groups{all}}, $file);
    }
}

while(my $key = each %groups){
    if($key eq 'all'){
	my $all = [sort @{$groups{$key}}];
	dump_it($all, "$outfile.fasta");
	next;
    }

    #maker standard naming convention
    my $source = 'maker';
    $source .= ".$key" if($key ne 'maker');

    #protein and transcript files

    if($groups{$key}{transcripts}){
	my $trans = [sort @{$groups{$key}{transcripts}}];
	dump_it($trans, "$outfile.all.$source.transcripts.fasta");
	warn "WARNING: Transcipt to protein mismatch for $key\n"
	    if(!$groups{$key}{proteins})
    }
    if($groups{$key}{proteins}){
	my $prot  = [sort @{$groups{$key}{proteins}}];
	dump_it($prot, "$outfile.all.$source.proteins.fasta");
	warn "WARNING: Transcipt to protein mismatch for $key\n"
	    if(!$groups{$key}{transcripts})
    }
    if($groups{$key}{noncoding}){
	my $non  = [sort @{$groups{$key}{noncoding}}];
	dump_it($non, "$outfile.all.$source.noncoding.fasta");
    }
}

sub dump_it {
    my $files = shift;
    my $name = shift;

    open(OUT, "> $name");
    close(OUT);

    foreach my $file (@{$files}){
	die "ERROR: The file \'$file\' does not exist\n" if (! -e $file);
	system("cat $file >> $name");
    }
}
