<commit_before># Parse RefSeq GPFF files to create xrefs.

package XrefParser::RefSeqGPFFParser;

use strict;

use File::Basename;

use base qw( XrefParser::BaseParser );

# --------------------------------------------------------------------------------
# Parse command line and run if being run directly

if (!defined(caller())) {

  if (scalar(@ARGV) != 1) {
    print "\nUsage: RefSeqGPFFParser.pm file.SPC <source_id>\n\n";
    exit(1);
  }

  run($ARGV[0], -1);

}

# --------------------------------------------------------------------------------

sub run {

  my $self = shift if (defined(caller(1)));

  my $source_id = shift;
  my $species_id = shift;
  my $file = shift;

  if ($source_id < 1) {
    $source_id =  XrefParser::BaseParser->get_source_id_for_filename(basename($file));
  }
  if(!defined($species_id)){
    $species_id = XrefParser::BaseParser->get_species_id_for_filename($file);
  }

  my $peptide_source_id = XrefParser::BaseParser->get_source_id_for_source_name('RefSeq_peptide');
  my $dna_source_id = XrefParser::BaseParser->get_source_id_for_source_name('RefSeq_dna');
  print "RefSeq_peptide source ID = $peptide_source_id; RefSeq_dna source ID = $dna_source_id\n";

  my $pred_peptide_source_id = XrefParser::BaseParser->get_source_id_for_source_name('RefSeq_peptide_predicted');
  my $pred_dna_source_id = XrefParser::BaseParser->get_source_id_for_source_name('RefSeq_dna_predicted');
  print "RefSeq_peptide_predicted source ID = $pred_peptide_source_id; RefSeq_dna_predicted source ID = $pred_dna_source_id\n";

  my $xrefs =
    $self->create_xrefs( $peptide_source_id, $dna_source_id,
      $pred_peptide_source_id, $pred_dna_source_id, $file, $species_id );

  if(!defined($xrefs)){
    return 1; #error
  }
  if(!defined(XrefParser::BaseParser->upload_xref_object_graphs($xrefs))){
    return 1; # error
  }
  return 0; # successful
}

# --------------------------------------------------------------------------------
# Parse file into array of xref objects
# There are 2 types of RefSeq files that we are interested in:
# - protein sequence files *.protein.faa
# - mRNA sequence files *.rna.fna
# Slightly different formats

sub create_xrefs {
  my $self = shift;

  my ( $peptide_source_id, $dna_source_id, $pred_peptide_source_id,
      $pred_dna_source_id, $file, $species_id ) = @_;

  my %name2species_id =  XrefParser::BaseParser->name2species_id();

  my %dependent_sources =  XrefParser::BaseParser->get_dependent_xref_sources();

#  my (%genemap) = %{XrefParser::BaseParser->get_valid_codes("mim_gene",$species_id)};
#  my (%morbidmap) = %{XrefParser::BaseParser->get_valid_codes("mim_morbid",$species_id)};

  my $refseq_io = $self->get_filehandle($file);

  if ( !defined $refseq_io ) {
    print "ERROR: Can't open RefSeqGPFF file $file\n";
    return undef;
  }

  my @xrefs;

  local $/ = "\/\/\n";

  my $type;
  if ($file =~ /protein/) {

    $type = 'peptide';

  } elsif ($file =~ /rna/) {

    $type = 'dna';

  } elsif($file =~ /RefSeq_dna/){<commit_msg>Do RefSeq release info thing in RefSeqGPFFParser as well...<commit_after># Parse RefSeq GPFF files to create xrefs.

package XrefParser::RefSeqGPFFParser;

use strict;

use File::Basename;

use base qw( XrefParser::BaseParser );

# --------------------------------------------------------------------------------
# Parse command line and run if being run directly

if (!defined(caller())) {

  if (scalar(@ARGV) != 1) {
    print "\nUsage: RefSeqGPFFParser.pm file.SPC <source_id>\n\n";
    exit(1);
  }

  run($ARGV[0], -1);

}

# --------------------------------------------------------------------------------

sub run {

  my $self = shift if (defined(caller(1)));

  my $source_id = shift;
  my $species_id = shift;
  my $file = shift;
  my $release_file = shift;

  if ($source_id < 1) {
    $source_id =  $self->get_source_id_for_filename(basename($file));
  }
  if(!defined($species_id)){
    $species_id = $self->get_species_id_for_filename($file);
  }

    my $peptide_source_id =
      $self->get_source_id_for_source_name('RefSeq_peptide');
    my $dna_source_id =
      $self->get_source_id_for_source_name('RefSeq_dna');

    print "RefSeq_peptide source ID = $peptide_source_id\n";
    print "RefSeq_dna source ID = $dna_source_id\n";

    my $pred_peptide_source_id =
      $self->get_source_id_for_source_name('RefSeq_peptide_predicted');
    my $pred_dna_source_id =
      $self->get_source_id_for_source_name('RefSeq_dna_predicted');

    print "RefSeq_peptide_predicted source ID = "
      . "$pred_peptide_source_id\n";
    print "RefSeq_dna_predicted source ID = $pred_dna_source_id\n";

    my $xrefs =
      $self->create_xrefs( $peptide_source_id, $dna_source_id,
        $pred_peptide_source_id, $pred_dna_source_id, $file,
        $species_id );

    if ( !defined($xrefs) ) {
        return 1;    #error
    }
    if ( !defined( $self->upload_xref_object_graphs($xrefs) ) ) {
        return 1;    # error
    }

    if ( defined $release_file ) {
        # Parse and set release info.
        my $release_io = $self->get_filehandle($release_file);
        local $/ = "\n*";
        my $release = $release_io->getline();
        $release_io->close();

        $release =~ s/\s{2,}/ /g;
        $release =~ s/.*(NCBI Reference Sequence.*) Distribution.*/$1/s;
        # Put a comma after the release number to make it more readable.
        $release =~ s/Release (\d+)/Release $1,/;

        print "RefSeq release: '$release'\n";

        $self->set_release( $source_id,              $release );
        $self->set_release( $peptide_source_id,      $release );
        $self->set_release( $dna_source_id,          $release );
        $self->set_release( $pred_peptide_source_id, $release );
        $self->set_release( $pred_dna_source_id,     $release );
    }

  return 0; # successful
}

# --------------------------------------------------------------------------------
# Parse file into array of xref objects
# There are 2 types of RefSeq files that we are interested in:
# - protein sequence files *.protein.faa
# - mRNA sequence files *.rna.fna
# Slightly different formats

sub create_xrefs {
  my $self = shift;

  my ( $peptide_source_id, $dna_source_id, $pred_peptide_source_id,
      $pred_dna_source_id, $file, $species_id ) = @_;

  my %name2species_id =  $self->name2species_id();

  my %dependent_sources =  $self->get_dependent_xref_sources();

#  my (%genemap) = %{$self->get_valid_codes("mim_gene",$species_id)};
#  my (%morbidmap) = %{$self->get_valid_codes("mim_morbid",$species_id)};

  my $refseq_io = $self->get_filehandle($file);

  if ( !defined $refseq_io ) {
    print "ERROR: Can't open RefSeqGPFF file $file\n";
    return undef;
  }

  my @xrefs;

  local $/ = "\/\/\n";

  my $type;
  if ($file =~ /protein/) {

    $type = 'peptide';

  } elsif ($file =~ /rna/) {

    $type = 'dna';

  } elsif($file =~ /RefSeq_dna/){
{'fraction_non_alphanumeric': 0.20263997679141282, 'fraction_numerical': 0.0039164490861618795, 'mean_word_length': 2.7595419847328246, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4936553', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2345, 'n_words': 477}