<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package FASTA::Util;
BEGIN{ push(@INC, $ENV{LOCAL_PERL_MODULES}); }
use Carp;
use warnings;
use strict;
use FASTA::Sequence;

my $validFastaExtensions = ["mfa", "fa", "fasta"];

###################################################
sub getFastaFiles {
    my $dir = shift or croak "Directory name is not defined.";
    $dir   .= '/' unless $dir =~ /\/$/;
    my $fastaFiles = [];
    opendir DIR, $dir or die "Can't open $dir as a directory.";
    while( defined( my $file = readdir(DIR) ) ){
	if( isValidFastaFile($file) ){
	    push(@$fastaFiles, $dir.$file);
	}
    }
    closedir DIR;
    return $fastaFiles;
}


###################################################
sub isValidFastaFile {
    my $filename  = shift;
    my $validFlag = 0;
    foreach my $ext (@$validFastaExtensions){
	if( File::Util::isValidExtension( $filename, $ext ) ){
	    $validFlag = 1;
	}
    }
    return $validFlag;
}

###################################################
sub indexing {
    my $seqs = shift;
    my $seqHash = {};
    foreach my $seq (@$seqs){
	if( $seqHash-&gt;{$seq-&gt;getTitle()} ){
	    print STDERR "Duplicated title: " . $seq-&gt;getTitle() . "\n";
	}
	$seqHash-&gt;{$seq-&gt;getTitle()} = $seq-&gt;getSequence();
    }
    return $seqHash;
}

###################################################
sub indexByShortTitle {
    my $seqs = shift;
    my $seqHash = {};
    foreach my $seq (@$seqs){
	if( $seqHash-&gt;{$seq-&gt;getShortTitle()} ){
	    print STDERR "Duplicated title: " . $seq-&gt;getShortTitle() . "\n";
	}
	$seqHash-&gt;{$seq-&gt;getShortTitle()} = $seq;
    }
    return $seqHash;
}

###################################################
sub scaffolds2contigs {
    my $scafSeqs = shift;
    my $contigSeqs = [];
    foreach my $scafSeq (@$scafSeqs){
	my $curContigSeqs = scaffold2contigs( $scafSeq );
	push( @$contigSeqs, @$curContigSeqs );
    }
    return $contigSeqs;
}

###################################################
sub scaffold2contigs {
    my $scaf = shift;
    my $scafSeq = $scaf-&gt;getSequence();
    my @contSeqs = split /N+/i, $scafSeq;
    my $cnt = 0;
    my $conts = [];
    foreach my $contSeq (@contSeqs) {
	if( $contSeq ){
	    ++$cnt;
	    my $cont = FASTA::Sequence-&gt;new();
	    $cont-&gt;setTitle( $scaf-&gt;getTitle()."-".$cnt );
	    $cont-&gt;setSequence( $contSeq );
	    push( @$conts, $cont );
	} else {
	    print $scaf-&gt;getTitle()."\n";
	}
    }
    return $conts;
}

###################################################
sub calcTotalLength {
    my $seqs = shift;
    my $totalLength = 0;
    foreach my $seq (@$seqs){
	$totalLength += $seq-&gt;getLength();
    }
    return $totalLength;
}

###################################################
sub calcMaxLength {
    my $seqs = shift;
    my $maxLength = 0;
    foreach my $seq (@$seqs){
	$maxLength = $seq-&gt;getLength() if $seq-&gt;getLength() &gt; $maxLength;
    }
    return $maxLength;
}

###################################################
sub calcNStats {
    my $seqs = shift;
    my $percent = shift;
    my $totalLength = calcTotalLength( $seqs );
    my $nStats = 0;
    my $curTotalLength = 0;
    foreach my $seq (reverse sort{$a-&gt;getLength &lt;=&gt; $b-&gt;getLength()} @$seqs){
	$curTotalLength += $seq-&gt;getLength();
	if( $curTotalLength &gt;= ($totalLength)*$percent/100 ){
	    $nStats = $seq-&gt;getLength();
	    last;
	}
    }
    return $nStats;
}

###################################################
sub extractLongSequences {
    my $seqs = shift;
    my $minLength = shift;
    my $longSeqs = [];
    foreach my $seq (@$seqs){
	if( $seq-&gt;getLength() &gt;= $minLength ){
	    push( @$longSeqs, $seq );
	}
    }
    return $longSeqs;
}

1;
</pre></body></html>