#!/usr/bin/env perl
#
# PROGRAM: ace2contigs.
#
#  $Revision: 1.10 $
#
# Parses ace file and creates a fasta file of the contig sequences.
#
# S.Trong 07/23/2004 - initial version
# S.Trong 1/10/2006 - added -q option to create qual file.
#
#***********************************************************************#

use FindBin;
use warnings;
use lib "$FindBin::Bin/../lib";
use strict;
use PGF::Parsers::ParseAce;
use PGF::Utilities::FastaSeq qw(formatSeq);
use File::Basename;
use Getopt::Long;
use vars qw( $opt_h $opt_q );

unless ( GetOptions( 
                  "h"=>\$opt_h,
                  "q"=>\$opt_q,
           )
       ) {
    printHelp();
    exit 1;
}

my $makeQualFile = defined $opt_q ? 1:0;

if ( @ARGV != 2 ) {
    printHelp();
    exit;
}

my ( $aceFile, $outputFilename ) = @ARGV;

if ( !-e $aceFile ) {
    print "Cannot find your ace file.\n";
    exit;
}

my $tag = basename($aceFile);
$tag =~ s/\.ace.*$//;

print "\nReading ace file ...\n";
my $aceObj = PGF::Parsers::ParseAce->parse($aceFile);

$outputFilename =~ s/\.fasta$//; # remove .fasta extension if exist.
my $fastaFile = "$outputFilename.fasta";
my $qualFile = "$outputFilename.qual";

open FOFILE, ">$fastaFile" or die "Cannot create file $fastaFile: $!\n";
if ( $makeQualFile ) {
    open QOFILE, ">$qualFile" or die "Cannot create file $qualFile: $!\n";
}

foreach ( $aceObj->allContigs() ) {
    my $attrib = $aceObj->contig($_);
    my $seq = uc formatSeq($attrib->{sequence_unpadded},50);
    next unless $seq =~ /[ATGCN]/;
    print FOFILE ">$_\n$seq\n";
    if ( $makeQualFile ) {
        print QOFILE ">$_\n";
        print QOFILE formatSeq($attrib->{quality},50),"\n";
    }
}

close FOFILE;
close QOFILE if $makeQualFile;

print "Created $fastaFile.\n" if -e $fastaFile;
print "Created $qualFile.\n" if ($makeQualFile && -e $qualFile);

#***********************************************************************#
sub printHelp {
        print <<EOM;
Parse ace file and creates a fasta file of the contig sequences.
Use the -q flag to create a qual file as well.

Usage: ace2contigs [-hq] <acefile> <outputfilename>

Options:
-q      Create qual file.
-h      This message.

If a .fasta extension is present in the outputfilename, it will be
stripped.  The fasta file will be named outputfilename + .fasta
and the qual file will be named outputfilename + .qual.
EOM
}