ensembl-hive  2.7.0
DirectParser.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::DirectParser;
21 
22 use strict;
23 use warnings;
24 use Carp;
25 
26 use base qw( XrefParser::BaseParser );
27 
28 # This parser will read Direct Xrefs from a simple tab-delimited file.
29 # The columns of the file should be the following:
30 #
31 # 1) Accession ID
32 # 2) Ensembl ID
33 # 3) Type (one of 'transcript', 'gene', or 'translation',
34 # will default to 'gene')
35 # 4) Display label (will default to the Accession ID in column 1)
36 # 5) Description (will default to the empty string)
37 # 6) Version (will default to '1')
38 #
39 # Columns 1 and 2 are obligatory.
40 
41 sub run {
42  my ($self, $ref_arg) = @_;
43  my $source_id = $ref_arg->{source_id};
44  my $species_id = $ref_arg->{species_id};
45  my $filename = $ref_arg->{file};
46  my $verbose = $ref_arg->{verbose};
47 
48  if((!defined $source_id) or (!defined $species_id) or (!defined $filename) ){
49  croak "Need to pass source_id, species_id and file as pairs";
50  }
51  $verbose |=0;
52 
53 
54  my $file_io = $self->get_filehandle($filename);
55  if ( !defined($file_io) ) {
56  return 1;
57  }
58 
59  my $parsed_count = 0;
60 
61  printf( STDERR "source = %d\t species = %d\n",
62  $source_id, $species_id );
63 
64  while ( defined( my $line = $file_io->getline() ) ) {
65  chomp $line;
66 
67  my ( $accession, $ensembl_id, $type, $label, $description, $version )
68  = split( /\t/, $line );
69 
70  if ( !defined($accession) || !defined($ensembl_id) ) {
71  print {*STDERR} "Line $parsed_count contains has less than two columns.\n";
72  print {*STDERR} ("The parsing failed\n");
73  return 1;
74  }
75 
76  $type ||= 'gene';
77  $label ||= $accession;
78  $description ||= '';
79  $version ||= '1';
80 
81  ++$parsed_count;
82 
83  my $xref_id =
84  $self->get_xref( $accession, $source_id, $species_id );
85 
86  if ( !defined($xref_id) || $xref_id eq '' ) {
87  $xref_id =
88  $self->add_xref({ acc => $accession,
89  version => $version,
90  label => $label,
91  desc => $description,
92  source_id => $source_id,
93  species_id => $species_id,
94  info_type => "DIRECT"} );
95  }
96  $self->add_direct_xref( $xref_id, $ensembl_id,
97  $type, $accession );
98  } ## end while ( defined( my $line...
99 
100  printf( "%d direct xrefs succesfully parsed\n", $parsed_count ) if($verbose);
101 
102  $file_io->close();
103 
104  print "Done\n" if($verbose);;
105 
106  return 0;
107 } ## end sub run
108 
109 1;
XrefParser::BaseParser
Definition: BaseParser.pm:8
run
public run()