ensembl-hive  2.7.0
WormBaseCuratedSynonymsParser.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 =head1 NAME
21 
23 
24 =head1 DESCRIPTION
25 
26 This parser will read and create dependent xrefs from a simple
27 tab delimited file.
28 
29 =head1 SYNOPSIS
30 
31  my $parser = XrefParser::WormBaseCuratedSynonymsParser->new($db->dbh);
32  $parser->run({
33  source_id => 11,
34  species_id => 9606,
35  files => [ "/nfs/production/flicek/wormbase/parasite/data/synonyms/strongyloides_stercoralis_prjeb528/curated_synonyms.tsv" ],
36  });
37 
38 =cut
39 
40 package XrefParser::WormBaseCuratedSynonymsParser;
41 
42 use strict;
43 use warnings;
44 use Carp;
45 use File::Basename;
46 
48 
49 use base qw( XrefParser::BaseParser );
50 
51 sub run {
52 
53  my ($self, $ref_arg) = @_;
54  my $source_id = $ref_arg->{source_id};
55  my $species_id = $ref_arg->{species_id};
56  my $files = $ref_arg->{files};
57 
58  if((!defined $source_id) or (!defined $species_id) or (!defined $files)){
59  croak "Need to pass source_id, species_id and files as pairs";
60  }
61 
62  my %src_ids;
63  my $sth = $self->dbi()->prepare("SELECT xref_id FROM xref WHERE accession=? AND source_id=? AND species_id=$species_id");
64  $src_ids{curated_gene_synonyms} = $self->get_source_id_for_source_name("curated_gene_synonyms");
65  my $data = $self->get_data(@$files);
66  for my $gene_id (keys %$data){
67  for my $synonym (@{$data->{$gene_id}->{synonyms}}) {
68  $self->add_xref_and_direct_xref(
69  $sth, $species_id, "gene", $src_ids{curated_gene_synonyms},
70  $gene_id, $synonym->{synonym_id}
71  );
72  }
73  }
74 }
75 
76 sub get_data {
77  my ($self, $file) = @_;
78  my $pep_io = $self->get_filehandle($file) or croak "Could not open: $file";
79 
80  my $data = {};
81 
82  while ( $_ = $pep_io->getline() ) {
83  chomp;
84  next if /^\/\//;
85  my ($stabid, $synonym) = split(/\t/, $_);
86  chomp($synonym);
87  chomp($stabid);
88  $data->{$stabid}->{synonyms} //=[];
89  push @{$data->{$stabid}->{synonyms}}, {
90  synonym_id => $synonym
91  }
92  }
93  $pep_io->close();
94  return $data;
95 }
96 
97 sub add_xref_and_direct_xref {
98  my ($self, $sth, $species_id, $object_type, $source_id, $object_id, $label, $primary_id) = @_;
99  $primary_id //= $object_id;
100  return unless $label;
101  $sth->execute($primary_id, $source_id);
102  $self->add_direct_xref(
103  ($sth->fetchrow_array())[0]
104  || $self->add_xref({
105  acc => $object_id,
106  label => $label,
107  source_id => $source_id,
108  species_id => $species_id,
109  info_type => "DIRECT"
110  })
111  , $primary_id, $object_type, "");
112 }
113 1;
XrefParser::BaseParser
Definition: BaseParser.pm:8
XrefParser::WormBaseCuratedSynonymsParser
Definition: WormBaseCuratedSynonymsParser.pm:22
XrefParser::WormBaseCuratedSynonymsParser::run
public run()
run
public run()