<commit_before>package XrefParser::InterproParser;
  
use strict;
use POSIX qw(strftime);
use File::Basename;
  
use XrefParser::BaseParser;
  
use vars qw(@ISA);
@ISA = qw(XrefParser::BaseParser);
 
my $xref_sth ;
my $dep_sth;
  
 
  
# --------------------------------------------------------------------------------
# Parse command line and run if being run directly
  
if (!defined(caller())) {
  
  if (scalar(@ARGV) != 1) {
    print "\nUsage: InterproParser.pm file\n\n";
    exit(1);
  }
  
  run(@ARGV);
}
  
 
sub run {
  my $self = shift if (defined(caller(1)));
  my $file = shift;
 
  my $source_id = shift;
  my $species_id = shift;

  print STDERR "source = $source_id\tspecies = $species_id\n";
  if(!defined($source_id)){
    $source_id = XrefParser::BaseParser->get_source_id_for_filename($file);
    print "source id is $source_id \n";
  }
  if(!defined($species_id)){
    $species_id = XrefParser::BaseParser->get_species_id_for_filename($file);
    print "species id is $species_id \n";
  }

  my $dir = dirname($file);
 
  my %short_name;
  my %description;
  my %pfam;
    
  open (SHORT, $dir."/short_name.dat") || die "Can't open hugo interpro file $dir/short_name.dat\n";
#IPR000001       Kringle
#IPR000002       Fizzy
#IPR000003       RtnoidX_receptor

  while(<SHORT>){
    chomp;
    my ($interpro, $name) = split(/\t/,$_);
    $short_name{$interpro} = $name;
  }
  close SHORT;

  
  my $count = 0;

  open (LONG, $dir."/protein2interpro.dat") || 
    die "Can't open interpro file  $dir/protein2interpro.dat\n";
  #O00050  PF03184 166     377     IPR004875       CENP-B protein
  #O00050  PF05225 15      60      IPR007889       Helix-turn-helix, Psq
  #O00050  PF06465 448     500     IPR009463       Protein of unknown function DUF10
  #  0        1     2       3        4               5

  while (<LONG>) {
    chomp;
    my @array = split(/\t/,$_);
    my $hgnc = $array[0];
    $description{$array[4]} = $array[5];
    $pfam{$array[4]} = $array[1];
  }
  close (LONG);

  my $add_interpro_sth =  XrefParser::BaseParser->dbi->prepare
    ("INSERT INTO interpro (interpro, pfam) VALUES(?,?)");

  foreach my $interpro (keys %short_name){
    $count++;
#    print $short_name{$interpro}."\t".$interpro."\t".$description{$interpro}.
#      "\t".$pfam{$interpro}."\n";
    XrefParser::BaseParser->add_xref($interpro,'',$short_name{$interpro},
				     $description{$interpro},$source_id,$species_id);
    if(defined($pfam{$interpro})){
      $add_interpro_sth->execute($interpro,$pfam{$interpro});
    }
    else{
      print "No pfam for $interpro\n";
    }
       
  }
  print "$count xref successfully loaded.\n";
#  die "not ready yet\n";
  
  
}

sub new {

  my $self = {};
  bless $self, "XrefParser::InterproParser";<commit_msg>InterPro now uses completely different fils and parsing. OLD way too long and complicated.<commit_after>package XrefParser::InterproParser;
  
use strict;
use POSIX qw(strftime);
use File::Basename;
  
use XrefParser::BaseParser;
  
use vars qw(@ISA);
@ISA = qw(XrefParser::BaseParser);
 
my $xref_sth ;
my $dep_sth;
  
 
  
# --------------------------------------------------------------------------------
# Parse command line and run if being run directly
  
if (!defined(caller())) {
  
  if (scalar(@ARGV) != 1) {
    print "\nUsage: InterproParser.pm file\n\n";
    exit(1);
  }
  
  run(@ARGV);
}
  
 
sub run {
  my $self = shift if (defined(caller(1)));
  my $file = shift;
 
  my $source_id = shift;
  my $species_id = shift;

  print STDERR "source = $source_id\tspecies = $species_id\n";
  if(!defined($source_id)){
    $source_id = XrefParser::BaseParser->get_source_id_for_filename($file);
    print "source id is $source_id \n";
  }
  if(!defined($species_id)){
    $species_id = XrefParser::BaseParser->get_species_id_for_filename($file);
    print "species id is $species_id \n";
  }

  my $add_interpro_sth =  XrefParser::BaseParser->dbi->prepare
    ("INSERT INTO interpro (interpro, pfam) VALUES(?,?)");

  my $get_interpro_sth =  XrefParser::BaseParser->dbi->prepare
    ("SELECT interpro from interpro where interpro = ? and pfam = ?");
 
  my $add_xref_sth = XrefParser::BaseParser->dbi->prepare
    ("INSERT INTO xref (accession,version,label,description,source_id,species_id) VALUES(?,?,?,?,?,?)");
  
  my $get_xref_sth = XrefParser::BaseParser->dbi->prepare
    ("select xref_id from xref where accession = ? and source_id = ?");


  my $dir = dirname($file);
                                                                                                                         
  my %short_name;
  my %description;
  my %pfam;
     
  open (XML, $dir."/interpro.xml") || die "Can't open hugo interpro file $dir/interpro.xml\n";
  #<interpro id="IPR001023" type="Family" short_name="Hsp70" protein_count="1556">
  #    <name>Heat shock protein Hsp70</name>
  #     <db_xref protein_count="18" db="PFAM" dbkey="PF01278" name="Omptin" />
  #      <db_xref protein_count="344" db="TIGRFAMs" dbkey="TIGR00099" name="Cof-subfamily" />
  
  my $count  = 0;
  my $count2 = 0;
  my $count3 = 0;
  local $/ = "</interpro>";
 

  my $last = "";
  my $i =0;
  while (<XML>) {

    my $interpro;
    my $short_name;
    my $pfam;
    my $tigr;
    my $name;
    
    ($interpro) = $_ =~ /interpro id\=\"(\S+)\"/;
    ($short_name) = $_ =~ /short_name\=\"(\S+)\"/;
    ($name) = $_ =~ /\<name\>(.*)\<\/name\>/;
    ($pfam) = $_ =~ /db\=\"PFAM\".*dbkey\=\"(\S+)\"/;
    ($tigr) = $_ =~ /db\=\"TIGRFAMs\".*dbkey\=\"(\S+)\"/;
    

#    print "#########################################################\n$interpro\n$name\n$short_name\n";
#    $i++;
#    if($i > 10){
#      die "first ten done";
#    }
    if($interpro){
      if(!get_xref($get_xref_sth, $interpro, $source_id)){
	$count++;
	$add_xref_sth->execute($interpro,'',$short_name, $name,$source_id,$species_id)
	  || die "Problem adding ".$interpro."\n";
      }
      if($pfam){
	#      print "PFAM $pfam\n";
	if(!get_xref($get_interpro_sth, $interpro,$pfam)){
	  $add_interpro_sth->execute($interpro,$pfam);
	  $count2++;
	}
      }  
      if($tigr){
	#     print "TIGR $tigr\n";
	if(!get_xref($get_interpro_sth, $interpro,$tigr)){
	  $add_interpro_sth->execute($interpro,$tigr);
	  $count3++;
	}
      }  
    }
  }
  close (LONG);
       
  print "$count xref successfully loaded.\n";
  print "$count2 interpro/pfam relationships added\n";
  print "$count3 interpro/tigr relationships added\n";
#  die "not ready yet\n";
  
  
}

sub get_xref{
  my ($get_xref_sth, $acc, $source) = @_;

  $get_xref_sth->execute($acc, $source) || die "FAILED $acc  $source\n";
  if(my @row = $get_xref_sth->fetchrow_array()) {
#    print "FOUND $acc\n";
    return $row[0];
  }   
#  print "UNKOWN $acc";
  return 0;
}

sub new {

  my $self = {};
  bless $self, "XrefParser::InterproParser";
{'fraction_non_alphanumeric': 0.19991206214275245, 'fraction_numerical': 0.02154477502564854, 'mean_word_length': 2.743280307185957, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055244', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2528, 'n_words': 511}