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

View of /FigKernelScripts/find_gaps.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Thu Jun 8 17:43:12 2006 UTC (14 years ago) by overbeek
Branch: MAIN
Changes since 1.1: +5 -2 lines
Fixed error in 'usage' msg. -- /gdp

# -*- perl -*-
#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
# 
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License. 
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#


use FIG;
$fig = new FIG;

use constant LEFT   =>  0;
use constant RIGHT  =>  1;
use constant FID    =>  2;

use constant TRAN   =>  0;
use constant OTHERS =>  1;

$0 =~ m/([^\/]+)$/;   $this_tool = $1;
$usage = "$this_tool  org_id  contigs  tbl_1 tbl_2 ... > gap_tbl";

if ((not @ARGV) || ($ARGV[0] =~ m/-h(elp)?/)) {
    die "\n   usage: $usage\n\n";
}

$trouble = 0;
($org_id = shift @ARGV) || (($trouble = 1) && (warn "No Org-ID given"));

for ($i=0; $i < @ARGV; ++$i)
{
    if (!-e $ARGV[$i])
    {
	$trouble = 1;
	print STDERR "ERROR: File $ARGV[$i] does not exist\n";
    }
}
die "\n\nAborting due to invalid args\n\nusage: $usage\n\n" if $trouble;

(($contigs_file = shift @ARGV) && (-s $contigs_file)) 
    || die "Contigs file $contigs_file has zero size\n\n\tusage: $usage\n\n";

((@tbls = @ARGV) > 0) || die "\n\tusage: $usage\n\n";

$len_of  = &load_contig_lens($contigs_file);
$regions = &load_regions(@tbls);
# die Dumper($features);

foreach $contig (keys %$regions)
{
    $x = $regions->{$contig};
    
    if ($x->[0]  > 1)                  { unshift @$x, [0, 0]; }
    if ($x->[-1] > $len_of->{$contig}) { push    @$x, [1+$len_of->{$contig}, 1+$len_of->{$contig}]; }
    
    for ($i=1; $i < @$x; ++$i)
    {
	$gap_beg = $x->[$i-1]->[RIGHT] + 1;
	$gap_end = $x->[$i]->[LEFT] - 1;
	$gap_len = $gap_end - $gap_beg + 1;
	$gap_loc = "$contig\_$gap_beg\_$gap_end";
	
	print "fig|$org_id.gap.".($i+1) . "\t$gap_loc\n";
    }
}
print STDERR "$this_tool done\n\n";
exit(0);


sub load_contig_lens
{
    my ($contigs_file) = @_;
    
    my $len_of = {};
    
    open (CONTIGS, "<$contigs_file") or die "could not open $contigs_file to read";
    while (($id, $seqP) = &FIG::read_fasta_record(\*CONTIGS))
    {
	$len_of->{$id} = length($$seqP);
    }
    close(CONTIGS) or die "could not close $contigs_file";
    
    return $len_of;
}

sub load_regions
{
    my (@tbl_files) = @_;
    my ($fid, $org, $locus, $contig, $beg, $end, $len);
    
    my $regions  = {};
    foreach my $tbl_file (@tbl_files)
    {
	my $num_fids = 0;
	open(TBL,"<$tbl_file") || die "could not open $tbl_file";
	print STDERR "Loading $tbl_file ...\n" if $ENV{VERBOSE};
	
	while (defined($entry = <TBL>))
	{
	    ++$num_fids;
	    chomp $entry;
	    if ($entry =~ /^(\S+)\s+(\S+)/)
	    {
		($fid, $locus) = ($1, $2);
		if ($fid !~ m/^fig\|(\d+\.\d+)\.(rna|peg|orf)\.(\d+)$/) 
		{ 
		    die "Invalid FID $fid"; 
		}
		
		($contig, $beg, $end) = $fig->boundaries_of($locus);
		$len = 1 + abs($end-$beg);
		
		unless (defined($regions->{$contig})) { $regions->{$contig} = []; }
		push(@ { $regions->{$contig} }, [ &FIG::min($beg, $end), &FIG::max($beg, $end), $fid ] );
	    }
	    else
	    {
		print STDERR "Skipping invalid entry $entry\n";
	    }
	}
	print STDERR "Loaded $num_fids features from $tbl_file\n\n" if $ENV{VERBOSE};
	close(TBL);
    }
    
    foreach my $contig (keys(%$regions))
    {
	my $x = [ sort { $a->[LEFT] <=> $b->[LEFT] }  @ { $regions->{$contig} } ];
	for (my $i=1; $i < @$x; ++$i)
	{
	    while (($i < @$x) && ($x->[$i-1]->[RIGHT] >= $x->[$i]->[LEFT]))
	    {
		print STDERR "Merging $i:\t$x->[$i-1]->[FID]\t$x->[$i]->[FID]\n"
		    if ($ENV{VERBOSE} && ($ENV{VERBOSE} > 1));
		$x->[$i-1]->[RIGHT] = &FIG::max( $x->[$i-1]->[RIGHT], $x->[$i]->[RIGHT] );
		splice @$x, $i, 1;
	    }
	}
	
	$regions->{$contig} = $x;
	print STDERR "After merger, $contig has ", (scalar @$x), " regions\n\n" if $ENV{VERBOSE}; 
    }

    return $regions;
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3