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

View of /FigKernelScripts/make_sp_func_syn_dicts.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Mon Dec 5 18:56:37 2005 UTC (13 years, 11 months ago) by olson
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, rast_rel_2008_06_18, myrast_rel40, rast_rel_2008_06_16, mgrast_dev_05262011, rast_rel_2008_12_18, mgrast_dev_04082011, rast_rel_2008_07_21, rast_rel_2010_0928, rast_2008_0924, mgrast_version_3_2, mgrast_dev_12152011, rast_rel_2008_04_23, mgrast_dev_06072011, rast_rel_2008_09_30, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, caBIG-05Apr06-00, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2010_0118, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, rast_rel_2011_0119, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, rast_rel_2008_10_09, mgrast_dev_04012011, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, rast_rel_2009_07_09, rast_rel_2010_0827, mgrast_rel_2008_1110, myrast_33, rast_rel_2011_0928, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, mgrast_dev_04052011, mgrast_dev_02222011, caBIG-13Feb06-00, rast_rel_2009_03_26, mgrast_dev_10262011, rast_rel_2008_11_24, rast_rel_2008_08_07, HEAD
Changes since 1.1: +17 -0 lines
Add license words.

# -*- 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 Carp;
use Cwd;
use Data::Dumper;

$usage = "make_sp_func_syn_dicts [OutDir] < sp.desc";

# This script is used to generate the sp_func.table, as well
# as the synonym dictionary.  It takes as input the sp.desc
# table, which is made with make_sp_desc_with_org (I think).

if ($ARGV[0] && (-d $ARGV[0]))  { 
    $outdir = shift @ARGV; 
} else {
    $outdir = getcwd();
}
print STDERR "outdir = $outdir\n" if $ENV{FIG_VERBOSE};

$syn_dict   = "$outdir/syn.dict";
$sp_func    = "$outdir/assigned_functions";

$errors = "$outdir/bad_sp_func";

if (open(ERRS, "<$outdir/$errors"))
{
    while (defined($line = <ERRS>))
    {
	chomp $line;
	($good,$bad) = split(/\t/, $line);
	$bad{$bad} = $good;
    }
    close(ERRS);
}

sub remove_contains {
    my($line) = @_;
    my($i,$start,$end);    

    if ($line =~ /^(^.*)\(CONTAINS:(.*)$/)
    {
	$start = $1;
	$end   = $2;
	
	$ln = length($end);
	for ($n=1,$i=0; ($i < $ln) && ($n > 0); $i++)
	{
	    $c = substr($end,$i,1);
	    if     ($c eq "(") { $n++; }
	    elsif  ($c eq ")") { $n--; }
	}

	if ($i < $ln)
	{
	    substr($end,0,$i) = "";
	    $end =~ s/^\s+\.*\s*//;
	    return $start . $end;
	}
	else
	{
	    return $line;
	}
    }
    else
    {
	return $line;
    }
}

sub split_at_parens {
    my($line) = @_;
    my($p, $ln, $i, $j, $k, $str, $c);
    
    $line =~ s/\.\s*$//;
#   print STDERR "line=$line\n";
    $p = [];
    $ln = length($line);

    $i=0;
    while (($i < $ln) && (($j = index($line," (",$i)) >= 0))
    {
	if (($j-$i) > 0)
	{
	    $str = substr($line,$i,($j-$i));
	    $str =~ s/^\s+//;
	    $str =~ s/\s+$//;
	    if ($str)
	    {
		push(@$p,$str);
	    }
	}
        else
	{
	    $str = substr($line,$i);
#	    print STDERR "matched at start: $str\n";
	}

	for ($n=1,$k=$j+2; ($k < $ln) && ($n > 0); $k++)
	{
	    $c = substr($line,$k,1);
	    if     ($c eq "(") { $n++; }
	    elsif  ($c eq ")") { $n--; }
	}
	
	if ($k <= $ln)
	{
	    $str = substr($line,$j+1,($k-($j+1)));
	    $str =~ s/^\s+//;
	    $str =~ s/\s+$//;
	    if ($str)
	    {
		push(@$p,$str);
	    }
	    $i = $k;
#	    if (($k = index($line," (",$k)) >= 0)
#	    {
#		$i = $k;
#	    }
#	    else
#	    {
#		$i = $ln;
#	    }
	}
	else
	{
	    $str = substr($line,$j+1);
	    $str =~ s/^\s+//;
	    $str =~ s/\s+$//;
	    if ($str)
	    {
		push(@$p,$str);
		$i = $ln;
	    }
	}
    }

    if ($i < $ln)
    {
	$str = substr($line,$i);
	$str =~ s/^\s+//;
	$str =~ s/\s+$//;
	if ($str)
	{
	    push(@$p,$str);
	}
    }
    for ($i=0; ($i <= $#{$p}); $i++)
    {
#	print STDERR "$i: $p->[$i]\n";
    }
#    print STDERR "=====\n";
    return $p;
}

sub parse_unit {
    my($in) = @_;
    my($desc,$syns,@ecs,$piecesP,$piece,@pieces,$i,$j,$altered,$glued);

    $syns = [];
    @ecs  = ();

    $piecesP = &split_at_parens($in);
    @pieces = @$piecesP;
    foreach $piece (@pieces)
    {
#	print STDERR "piece: $piece\n";
    }
	    
    for ($i=0; ($i <= $#pieces); $i++)
    {
	$pieces[$i] =~ s/\.\s*$//;
    }

    $altered = 1;
    while ($altered)
    {
	$altered = 0;
	for ($i=0; (! $altered) && ($i < $#pieces); $i++)
	{
	    if (($pieces[$i+1] !~ /\)$/) || (($i < ($#pieces-1)) && ($pieces[$i+2] !~ /^\(/)))
	    {
		$altered = 1;
		$glued   = $pieces[$i] . " " . $pieces[$i+1];
#		print STDERR "gluing $pieces[$i] and $pieces[$i+1]\n";
		splice(@pieces,$i,2,$glued);
	    }
	}
    }

    for ($i=0; ($i <= $#pieces) && ($pieces[$i] !~ /^\(EC/); $i++) {}
    if ($i <= $#pieces)
    {
	$desc = $pieces[0];
	for ($j=1; ($j < $i); $j++)
	{
	    $desc .= " " . $pieces[$j];
	}
	splice(@pieces,0,$j,$desc);
    }

    $desc = $pieces[0];
#   print STDERR "desc before EC: $desc\n";
    for ($i=1; ($i <= $#pieces) && ($pieces[$i] =~ /^\(EC /); $i++)
    {
	push(@ecs,$pieces[$i]);
    }

    while ($i <= $#pieces)
    {
	if ($pieces[$i] !~ /FRAGMENT/)
	{
	    push(@$syns,$pieces[$i]);
	}
	$i++;
    }

    if (@ecs > 0)
    {
	$ecs    = join(" ",@ecs);
	$desc  .= " $ecs";
    }
    if (! $desc) { return ""; }

    $desc =~ s/^\'(.*)\'$/$1/;
    $desc =~ s/^\'([^\']*)$/$1/;
    return [$desc,$syns];
}


@ecs = ();

sub pcount {
    my($str) = @_;
    my($n,$c,$ln,$i);

    for ($i=0, $n=0, $ln = length($str); ($i < $ln); $i++)
    {
	$c = substr($str,$i,1);
	if ($c eq "(") { $n++; }
	if ($c eq ")") { $n--; }
    }
    return $n;
}

$cnt = $tick = $tock = 0;
while (defined($line = <STDIN>))
{
    chomp $line;
    if ($tick >= 10000) { print STDERR ".";  $tick = 0; $tock++; } else { $tick++; }
    if ($tock >= 50)    { print STDERR "\n"; $tock = 0; }

    ($sp,$desc) = split(/\t/,$line);
    $desc =~ s/^.*\[Includes:\s+([^\]]+)\]\.?\s*/$1/;
    while ($desc =~ s/\[[^\]]+\]\.*\s*$//) {}
    $desc =~ s/ ?\(FRAGMENT\)//;

#   print STDERR "desc=$desc\n";
    @functions = ();
    $last = 0; 
    while (($x = index($desc," / ",$last)) > 0)
    {
#	print STDERR "hit: $x\n";
	$piece = substr($desc,0,$x);
	if (($cnt = &pcount($piece)) == 0)
	{
	    $piece = substr($desc,0,($x-$last));
#	    print STDERR "pushing $piece\n";
	    push(@functions,$piece);
	    substr($desc,0,$x+3) = "";
	    $last = 0;
	}
	else
	{
	    $last = $x+1;
	}
    }
#   print STDERR "pushing $desc\n";
    $desc =~ s/ ?\(FRAGMENT\)//;
    if ($desc)
    {
	push(@functions,$desc);
    }
    
    @parsed    = ();
    
#   if ($#functions > 0) { print STDERR "processing multifunctional: $line\n"; }
    foreach $function (@functions)
    {
	$function = &remove_contains($function);
        next if (! $function);    
	if ($parsed = &parse_unit($function))
	{
	    $desc   = $parsed->[0];
	    $syns   = $parsed->[1];
	    push(@parsed,$desc);
	}
    }

    if ($desc = join(" / ",@parsed))
    {
#       if ($#functions > 0) { print STDERR "    producing: $desc\n"; }
  
	if (!($entries = $sp{$desc}) )
	{	  
	    $sp{$desc} = [];
	    $entries   = $sp{$desc};
	}
	push(@$entries,$sp);
#       print STDERR ">>>added $sp to $desc\n";

	if ((@parsed == 1) && ($#{$syns} > 0))
	{
	    if (! ($sofar = $synonyms{$desc}))
	    {
		$synonyms{$desc} = [];
		$sofar = $synonyms{$desc};
	    }
	    
	    foreach $syn (@$syns)
	    {
		for ($i=0; ($i <= $#{$sofar}) && ($sofar->[$i] ne $syn); $i++) {}
		if ($i > $#{$sofar})
		{
		    push(@$sofar,$syn);
#		    print STDERR "    added syn $syn for $desc\n";
		}
	    }
	}
    }
}
print STDERR "\n";



sub by_ec {
    my($x,$y);

    if ($a =~ /EC ([0-9\-]\.[0-9\-]+\.[0-9\-]+\.[0-9\-]+)/)
    {
	$x = "$1\t$a";
    }
    else
    {
	$x = $a;
    }

    if ($b =~ /EC ([0-9\-]\.[0-9\-]+\.[0-9\-]+\.[0-9\-]+)/)
    {
	$y = "$1\t$b";
    }
    else
    {
	$y = $b;
    }

    return ($x cmp $y);
}

open(SYN, ">$syn_dict") || die "could not write-open $syn_dict";
foreach $desc (sort keys(%synonyms))
{
    $x = $synonyms{$desc};
    @syns = sort(@$x);
    foreach $syn (@syns)
    {
	print SYN "$desc\t$syn\n";
    }
}
close(SYN);

sub bad {
    my($desc) = @_;

    if ($desc =~ /^!!!! ALU/) { return 1; }
    if ($desc =~ /^[0-9\.]+\s+KD\s+PROTEIN\s*$/) { return 1; }
    if (&hypo($desc)) { return 1; }
    if (! $desc) { return 1; }
    if (($desc =~ /Zm[RG]/) ||
	($desc =~ /Zwh\d/)  ||
	($desc =~ /Zhb/)  ||
	($desc =~ /ZK\d/)  ||
	($desc =~ /ORF-/))
    {
	return 1;
    }
    return 0;
}

open(SPFUNC, ">$sp_func") || die "could not write-open $sp_func";
foreach $desc (sort keys(%sp))
{
    if (! &bad($desc))
    {
	$x   = $sp{$desc};
	@sps = sort(@$x);
	foreach $sp (@sps)
	{
	    if ($good = $bad{$desc})
	    {
		$desc = $good;
	    }
	    defined($sp) || warn "BAD $sp\n";
	    $reduced = $sp; $reduced =~ s/^([^\|]+\|[^\|]+).*$/$1/;
	    $desc =~ s/ \; / \/ /g;
	    print SPFUNC "$reduced\t$desc\n";
	}
    }
}
close(SPFUNC);

sub hypo {
    my $x = (@_ == 1) ? $_[0] : $_[1];

    return ((! $x) ||
	    ($x =~ /hypoth/i) || 
	    ($x =~ /,.*genes/i) ||
	    ($x =~ /gene \d/i) ||
	    ($x =~ /\d{3}.pep/i) ||
	    ($x =~ /\bFROM\b/i) ||
	    ($x =~ /\bA\.L/i) ||
	    ($x =~ /\bA\d\d/i) ||
	    ($x =~ /^C$/i) ||
	    ($x =~ /^\([A-Z]+\d+\)$/) || 
	    ($x =~ /putative/i) || 
	    ($x =~ /tentative/i) || 
	    ($x =~ /predicted/i) || 
	    # ($x =~ /homolog/i) || 
	    ($x =~ /dna fragment/i) || 
	    ($x =~ /conserved protein\b/) || 
	    ($x =~ /^[XY]\d\S+/i) || 
	    ($x =~ /^[Yy][a-z]{2}[A-Z]/) || 
	    ($x =~ /^[Yy][A-Z]{3}\b/) || 
	    ($x =~ /weak similarity/i) || 
	    ($x =~ /similar to/i) || 
	    ($x =~ /gene product/i) || 
	    ($x =~ /ORF_/) || 
	    ($x =~ /NO SWISS-PROT/) || 
	    ($x =~ /predicted coding/i) || 
	    ($x =~ /predicted by/i) || 
	    ($x =~ /pct identical/i) || 
	    ($x =~ /\borf\d+/i) || 
	    ($x =~ /\bcosmid\d+\b/i) || 
	    ($x =~ /^[a-zA-Z0-9]+\d+[a-z]?$/i) || 
	    ($x =~ /^[a-zA-Z0-9]+[\.-]\d+[a-z]?$/i) || 
	    ($x =~ /^[a-zA-Z0-9]+[\.-]\d+[a-z]?\s+PROTEIN$/i) || 
	    ($x =~ /^cosmid\s+\S+$/i) || 
	    ($x =~ /^\([A-Z0-9]+\) [A-Z][a-z]{2}[a-zA-Z] \[\S+ \S+\]\s*$/) ||
	    ($x =~ /region orf/i) ||
	    ($x =~ /UNNAMED PROTEIN PRODUCT/) ||
	    ($x =~ /HYDROPHOBIC PROTEIN/) ||
	    ($x =~ /\bORF\b/i) ||
	    ($x =~ /protein similarity/) ||
	    ($x =~ /Uncharacterized/) ||
	    ($x =~ /UNIDENTIFIED/) ||
	    ($x =~ /belongs to the family/) ||
	    ($x =~ /predicted protein/) ||
	    ($x =~ /1-EVIDENCE=PREDICTED BY MATCH/) ||
	    ($x =~ /INTERGENIC REGION/) ||
	    ($x =~ /NO SWISS-PROT SIMILARITIES/) ||
	    ($x =~ /no known similarities/) ||
	    ($x =~ /alternate gene name/) ||
	    ($x =~ /alternate open reading frame/) ||
	    ($x =~ /similar to GenBank Accession Number/) ||
	    ($x =~ /family with/) ||
	    ($x =~ /No definition/) ||
	    ($x =~ /id:/i) ||
	    ($x =~ /cDNA/) ||
	    ($x =~ /SP:/) ||
	    ($x =~ /COMPLETE CDS/) ||
	    ($x =~ /GENE CLUSTER/) ||
	    ($x =~ /\dp,Lp/) ||
	    ($x =~ /3\' END/) ||
	    ($x =~ /START CODON/) ||
	    ($x =~ /_\S+_/) ||
	    ($x =~ /GTG START/i) ||
	    ($x =~ /TTG START/i) ||
	    ($x =~ /chain length determinant/i) ||
	    ($x =~ /f135/i) ||
	    ($x =~ /KDA PROTEIN/i) ||
	    ($x =~ /yole/i) ||
	    ($x =~ /\bMAP\b/) ||
	    ($x =~ /\(\d+-\d+\)/i) ||
	    ($x =~ /D9719.36p/i) ||
	    ($x =~ /THYMOCYTE PROTEIN CTHY28KD/i) ||
	    ($x =~ /PHAC1, PHAC2 AND PHAD GENES/i) ||
	    ($x =~ /OR23peptide/i) ||
	    ($x =~ /\(AE/i) ||
	    ($x =~ /Bem3p,Lph12p/i) ||
	    ($x =~ /Rlm1p,Lpg19p/i) ||
	    ($x =~ /unknown/i));
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3