<commit_before>#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use POSIX; # for strftime

use GO::Parser;
use Bio::DB::Taxonomy;
use Env;

my %uncompress = ('bz2' => 'bzcat',
		  'gz'  => 'zcat',
		  );

my $taxdb = Bio::DB::Taxonomy->new(-source => 'entrez');    
my $srcPrefix = 'UCR';
my $centerPrefix = 'StajichLab';
my $taxon;
my $species;
my $interprotab;
my $outputfile;
my $use_stdout = 0;
my $strip = 1;
my $debug = 0;
my $gofile;
GetOptions('ctr:s'       => \$centerPrefix,
	   'src:s'       => \$srcPrefix,
	   't|taxonid:s' => \$taxon,
	   's|species:s' => \$species,
	   'i|input:s'   => \$interprotab,
	   'o|output:s'  => \$outputfile,
	   'g|go:s'      => \$gofile,
	   'stdout!'     => \$use_stdout,
	   'strip!'      => \$strip,
	   'v|debug'     => \$debug,
	   );
if( $species ) {
    $taxon = $taxdb->get_taxonid($species);
}
if( ! $taxon ) {
    warn("need a taxonid, either provide it with --taxonid or give a --species='genus species' [strain] (add strain if specified in genbank)");
    exit;
}
unless($interprotab ) {
    $interprotab = shift @ARGV if @ARGV;
}

unless($interprotab && -f $interprotab ) {
    warn("must specify an interpro tab file either on the cmdline or with the -i option\n");
    exit;
}

unless( $gofile ) {
    warn("Need a GO file (gene_ontology.obo)\n");
    exit;
}
my $parser = GO::Parser->new({handler => 'obj',use_cache=>1});
#$parser->handler->file("output.xml");
$parser->parse($gofile);
my $go_graph = $parser->handler->graph;
warn("done parsing\n");


my ($stem,$fh);
if( $interprotab =~ /(\S+)\.(gz|bz2)$/) {
    $stem = ($1);
    open($fh, "$uncompress{$2} $interprotab |") || die "$uncompress{$2} $interprotab: $!";
} else {
    open($fh, "< $interprotab") || die "$interprotab: $!";
    $stem = $interprotab;
}

my $ofh;
if($use_stdout ) {
    $ofh = \*STDOUT;
} else {
    if( $strip ) {
	$stem =~ s/\.tab$//;
	$stem =~ s/\.InterPro$//i;
    }
    open($ofh,">$stem.InterPro.association");
}

my %seen;
my $date = strftime("%d-%b-%Y", localtime);
while(<$fh>) {
    # columns from IPRscan are
    chomp;
    my ($gene,$crc64,$length,$analyis_method,
	$hit_dbid,$hitdesc,$qstart,$qend,$evalue,$hit_status,
	$date_run,$iprdomain,$iprdesc,
	$go_info) = split(/\t/,$_);
    $gene =~ s/\.T\d+$//;
    if($go_info) {
	for my $goid ( split(/\|/,$go_info) ) {
	    unless( $goid =~ /(GO:\d+)/ ) {
		warn("No GOID in $goid\n");
		next;
	    }
	    next if $seen{$gene}->{$goid}++;
	    my $term = $go_graph->get_term($goid);
	    my $code = $term->get_code_from_namespace;
	    print $ofh join("\t", 
			    $srcPrefix,
			    $gene, $gene, '',
			    $goid, $analyis_method, 'ISS', '', $code,
			    '','', 'protein', "taxon:$taxon",
			    $date,
			    $centerPrefix),"\n";
	}

	last if $debug;
    }
}
close($fh);
<commit_msg>don't bonk when GO file doesn't have a GO term<commit_after>#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use POSIX; # for strftime

use GO::Parser;
use Bio::DB::Taxonomy;
use Env;

my %uncompress = ('bz2' => 'bzcat',
		  'gz'  => 'zcat',
		  );

my $taxdb = Bio::DB::Taxonomy->new(-source => 'entrez');    
my $srcPrefix = 'UCR';
my $centerPrefix = 'StajichLab';
my $taxon;
my $species;
my $interprotab;
my $outputfile;
my $use_stdout = 0;
my $strip = 1;
my $debug = 0;
my $gofile;
GetOptions('ctr:s'       => \$centerPrefix,
	   'src:s'       => \$srcPrefix,
	   't|taxonid:s' => \$taxon,
	   's|species:s' => \$species,
	   'i|input:s'   => \$interprotab,
	   'o|output:s'  => \$outputfile,
	   'g|go:s'      => \$gofile,
	   'stdout!'     => \$use_stdout,
	   'strip!'      => \$strip,
	   'v|debug'     => \$debug,
	   );
if( $species ) {
    $taxon = $taxdb->get_taxonid($species);
}
if( ! $taxon ) {
    warn("need a taxonid, either provide it with --taxonid or give a --species='genus species' [strain] (add strain if specified in genbank)");
    exit;
}
unless($interprotab ) {
    $interprotab = shift @ARGV if @ARGV;
}

unless($interprotab && -f $interprotab ) {
    warn("must specify an interpro tab file either on the cmdline or with the -i option\n");
    exit;
}

unless( $gofile ) {
    warn("Need a GO file (gene_ontology.obo)\n");
    exit;
}
my $parser = GO::Parser->new({handler => 'obj',use_cache=>1});
#$parser->handler->file("output.xml");
$parser->parse($gofile);
my $go_graph = $parser->handler->graph;
warn("done parsing\n");


my ($stem,$fh);
if( $interprotab =~ /(\S+)\.(gz|bz2)$/) {
    $stem = ($1);
    open($fh, "$uncompress{$2} $interprotab |") || die "$uncompress{$2} $interprotab: $!";
} else {
    open($fh, "< $interprotab") || die "$interprotab: $!";
    $stem = $interprotab;
}

my $ofh;
if($use_stdout ) {
    $ofh = \*STDOUT;
} else {
    if( $strip ) {
	$stem =~ s/\.tab$//;
	$stem =~ s/\.InterPro$//i;
    }
    open($ofh,">$stem.InterPro.association");
}

my %seen;
my $date = strftime("%d-%b-%Y", localtime);
while(<$fh>) {
    # columns from IPRscan are
    chomp;
    my ($gene,$crc64,$length,$analyis_method,
	$hit_dbid,$hitdesc,$qstart,$qend,$evalue,$hit_status,
	$date_run,$iprdomain,$iprdesc,
	$go_info) = split(/\t/,$_);
    $gene =~ s/\.T\d+$//;
    if($go_info) {
	for my $goid ( split(/\|/,$go_info) ) {
	    unless( $goid =~ /(GO:\d+)/ ) {
		warn("No GOID in $goid\n");
		next;
	    }
	    next if $seen{$gene}->{$goid}++;
	    my $term = $go_graph->get_term($goid);
	    my $code;
	    if( ! $term ) {
	     $code = 'U';
            } else {
	     $code = $term->get_code_from_namespace;
	    }
	    print $ofh join("\t", 
			    $srcPrefix,
			    $gene, $gene, '',
			    $goid, $analyis_method, 'ISS', '', $code,
			    '','', 'protein', "taxon:$taxon",
			    $date,
			    $centerPrefix),"\n";
	}

	last if $debug;
    }
}
close($fh);

{'fraction_non_alphanumeric': 0.22556258790436007, 'fraction_numerical': 0.003867791842475387, 'mean_word_length': 3.078136200716846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '820784', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2284, 'n_words': 485}