ensembl-hive  2.7.0
WormbaseDirectParser.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 package XrefParser::WormbaseDirectParser;
21 
22 use strict;
23 use warnings;
24 use Carp;
25 use File::Basename;
26 
28 
29 use base qw( XrefParser::BaseParser );
30 
31 sub run {
32 
33  my ($self, $ref_arg) = @_;
34  my $source_id = $ref_arg->{source_id};
35  my $species_id = $ref_arg->{species_id};
36  my $files = $ref_arg->{files};
37 
38  if((!defined $source_id) or (!defined $species_id) or (!defined $files)){
39  croak "Need to pass source_id, species_id and files as pairs";
40  }
41 
42  my $file = @{$files}[0];
43  my @fields = qw/wormbase_gene wormbase_gseqname wormbase_locus wormbase_transcript wormbase_cds wormpep_id protein_id/;
44  my %src_ids;
45  my $sth = $self->dbi()->prepare("SELECT xref_id FROM xref WHERE accession=? AND source_id=? AND species_id=$species_id");
46  for my $field (@fields){
47  $src_ids{$field} = $self->get_source_id_for_source_name($field);
48  }
49  my $data = $self->get_data(@$files);
50  for my $gene_id (keys %$data){
51  $self->add_xref_and_direct_xref(
52  $sth, $species_id, "gene", $src_ids{wormbase_gene},
53  $gene_id, $gene_id
54  );
55  $self->add_xref_and_direct_xref(
56  $sth, $species_id, "gene", $src_ids{wormbase_gseqname},
57  $gene_id, $data->{$gene_id}->{wormbase_gseqname}
58  );
59  $self->add_xref_and_direct_xref(
60  $sth, $species_id, "gene", $src_ids{wormbase_locus},
61  $gene_id, $data->{$gene_id}->{wormbase_locus}
62  );
63  for my $transcript (@{$data->{$gene_id}->{transcripts}}){
64  $self->add_xref_and_direct_xref(
65  $sth, $species_id, "transcript", $src_ids{wormbase_transcript},
66  $transcript->{transcript_id}, $transcript->{transcript_id}
67  );
68  $self->add_xref_and_direct_xref(
69  $sth, $species_id, "transcript", $src_ids{wormbase_cds},
70  $transcript->{wormbase_cds}, $transcript->{wormbase_cds}, $transcript->{transcript_id}
71  );
72  $self->add_xref_and_direct_xref(
73  $sth, $species_id, "translation", $src_ids{wormpep_id},
74  $transcript->{wormpep_id}, $transcript->{wormpep_id}, $transcript->{transcript_id}
75  );
76  $self->add_xref_and_direct_xref(
77  $sth, $species_id, "translation", $src_ids{protein_id},
78  $transcript->{protein_id}, $transcript->{protein_id}, $transcript->{transcript_id}
79  );
80  }
81  }
82 }
83 sub get_data {
84  my ($self, $file) = @_;
85  my $pep_io = $self->get_filehandle($file) or croak "Could not open: $file";
86 
87  my $data = {};
88 
89  while ( $_ = $pep_io->getline() ) {
90  next if /^\/\//;
91  my ($gseqid, $wbgeneid, $locus, $wbtranscript, $wormpep, $insdc_parent, $insdc_locus_tag, $protein_id, $uniprot_id) = split(/\t/, $_);
92 
93  $data->{$wbgeneid}->{transcripts} //=[];
94  push @{$data->{$wbgeneid}->{transcripts}}, {
95  transcript_id => $wbtranscript,
96  ($wormpep ne '.' && $wbtranscript =~ /^(.*?)(\.\d+)?$/ ? (wormbase_cds => $1 ) : ()),
97  ($wormpep ne '.' ? (wormpep_id => $wormpep) : ()),
98  ($protein_id ne '.' ? (protein_id => $protein_id) : ()),
99  };
100  $data->{$wbgeneid}->{wormbase_gseqname} = $gseqid;
101  $data->{$wbgeneid}->{wormbase_locus} = $locus if $locus ne '.';
102  }
103  $pep_io->close();
104  return $data;
105 }
106 
107 sub add_xref_and_direct_xref {
108  my ($self, $sth, $species_id, $object_type, $source_id, $object_id, $label, $primary_id) = @_;
109  $primary_id //= $object_id;
110  return unless $label;
111  $sth->execute($primary_id, $source_id);
112  $self->add_direct_xref(
113  ($sth->fetchrow_array())[0]
114  || $self->add_xref({
115  acc => $object_id,
116  label => $label,
117  source_id => $source_id,
118  species_id => $species_id,
119  info_type => "DIRECT"
120  })
121  , $primary_id, $object_type, "");
122 }
123 1;
XrefParser::BaseParser
Definition: BaseParser.pm:8
run
public run()