[Bio] / FigKernelScripts / check_sims_basic.pl Repository:
ViewVC logotype

View of /FigKernelScripts/check_sims_basic.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (download) (as text) (annotate)
Fri Mar 11 20:52:34 2005 UTC (14 years, 9 months ago) by overbeek
Branch: MAIN
CVS Tags: efrank-preMakefileToStandardMk
Changes since 1.3: +54 -52 lines
Fixed bugs and warts in mod to make work as either file or filter. -- /gdp

# -*- perl -*-

$SIG{HUP} = 'ignore';

use File::Path;
use File::Basename;

$usage = "usage: check_sims_basic [-delint_dir=Dir] [-logfile=log] NR [ < sims | - | SimsDir | Sims1 Sims2 Sims3 ...] > checked.sims [2> errors (recommended if a logfile isn't specified)]";

$outdir  = "";
$logfh   = \*STDERR;
$trouble = 0;
for ($i=0; $i < @ARGV; )
{
    if ($ARGV[$i] =~ m/-delint_dir=(\S+)/)
    {
	$outdir = $1;
	splice @ARGV, $i, 1;
	if (-d $outdir)
	{
	    $trouble = 1;
	    warn "$outdir exists";
	} else {
	    mkpath($outdir, 0, 0777) || die "Could not create $outdir";
	}
    }
    elsif ($ARGV[$i] =~ m/-logfile=(\S+)/)
    {
	$logfile = $1;
	splice @ARGV, $i, 1;
	open(LOG, ">$logfile") || die "Could not open $logfile";
	$logfh = \*LOG;
    }
    elsif (-s $ARGV[$i]) { 
	++$i; 
    }
    else {
	$trouble = 1;
	print STDERR "Invalid arg $ARGV[$i]\n";
	++$i;
    }
}
die "aborting due to invalid args" if ($trouble);

(($nr = shift @ARGV) && (-s $nr))
    || die $usage;

if (@ARGV == 0) 
{
    if (-t STDIN)
    {
	push @ARGV, '-';
    }
    else
    {
	print STDERR "No arguments given --- checking $FIG_Config::data/Sims by default\n";
	push @ARGV, "$FIG_Config::data/Sims"; 
    }
}

if ((@ARGV == 1) && (-d $ARGV[0]))
{
    $sims_dir = shift @ARGV;
    opendir(SIMS, $sims_dir) || die "Could not open $sims_dir";
    @ARGV = grep !/^\./, readdir(SIMS);
    @ARGV = map  { $_ = "$sims_dir/$_" } @ARGV;
    closedir(SIMS) || die "Could not close $sims_dir";
}

$trouble = 0;
foreach $file (@ARGV)
{
    next if ($file eq '-');
    if (!-e $file) { print STDERR "Simfile $file does not exist"; $trouble = 1; }
}
die "There were nonexistent input files" if $trouble;



$/ = "\n>";
open(NR,"<$nr") || die $usage;
print STDERR "Loading lengths from $nr ...\n" if $ENV{FIG_VERBOSE};
while (defined($entry = <NR>))
{
    chomp $entry;
    if ($entry =~ /^>?(\S+)[^\n]*\n(.*)/s)
    {
	$id  =  $1;
	$seq =  $2;
	$seq =~ s/\s//gs;
	$ln{$id} = length($seq);
    }
}
$/ = "\n";
close(NR);

if ($outdir)
{
    $file  = "$outdir/" . basename($ARGV[0]);
    open(OUTPUT, ">$file") || die "could not write-open $file";
    print STDERR "Opening $file" if $ENV{FIG_VERBOSE};
    $outfh = \*OUTPUT;
}
else
{
    $outfh = \*STDOUT;
}

foreach $simfile (@ARGV)
{
    print STDERR "Processing $simfile\n" if $ENV{FIG_VERBOSE};
    
    open(SIMFILE, "<$simfile") || die "Could not open $simfile";
    while (defined($sim = <SIMFILE>))
    {
	chomp $sim;
	
	if ($sim =~ m/^\S+\t\S+\t(\d+|\d+\.\d+)(\t\d+){7}\t(\d+(\.\d*)?e[-+]?\d+|\d+\.\d+)\t(\d\.\d*e[-+]?\d+|\d+\.\d+|\d+)/)
	{
	    # print STDERR "$1\t$2\t$3\t$4\t$5\t$6\n";
	    ($id1,$id2,$iden,$ali_ln,$mis,$gaps,$b1,$e1,$b2,$e2,$psc,$bsc,$ln1,$ln2) =
		split(/\t/,$sim);
	    
	    if ($ln{$id1} && $ln{$id2} && ($ln{$id1} == $ln1) && ($ln{$id2} == $ln2))
	    {
		print $outfh "$sim\n";   #...print valid sims to OUTPUT
	    }
	    else
	    {
		if ($ln{$id1})
		{
		    if ($ln{$id1} != $ln1) { print $logfh "badlen1\t$ARGV, $.:\t$id1\t$ln{$id1}\t$ln1\t$sim\n"; }
		}
		else
		{
		    print $logfh "undef1\t$ARGV, $.:\t$id1\t\t\t$sim\n";
		}
		
		if ($ln{$id2})
		{
		    if ($ln{$id2} != $ln2) { print $logfh "badlen2\t$ARGV, $.:\t$id2\t$ln{$id2}\t$ln2\t$sim\n"; }
		}
		else
		{
		    print $logfh "undef2\t$ARGV, $.:\t$id2\t\t\t$sim\n";
		}
	    }
	}
	else
	{
	    print $logfh "INVALID FORMAT\t$ARGV, $.:\t$sim\n";
	}
    }
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3