]> git.donarmstrong.com Git - function2gene.git/blob - bin/parse_uniprot_results
update function2gene to allow uniprot
[function2gene.git] / bin / parse_uniprot_results
1 #! /usr/bin/perl
2
3 # parse_uniprot_results retreives files of search results from ncbi,
4 # and is released under the terms of the GPL version 2, or any later
5 # version, at your option. See the file README and COPYING for more
6 # information.
7
8 # Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
9
10 # $Id: ss,v 1.1 2004/06/29 05:26:35 don Exp $
11
12
13 use warnings;
14 use strict;
15
16
17 use Getopt::Long;
18 use Pod::Usage;
19
20 =head1 NAME
21
22   parse_uniprot_results [options]
23
24 =head1 SYNOPSIS
25
26
27  Options:
28   --dir, -D directory to stick results into [default .]
29   --name, -n file naming scheme [default ${search}_results.$format]
30   --terms, -t file of search terms [default -]
31   --debug, -d debugging level [default 0]
32   --help, -h display this help
33   --man, -m display manual
34
35 =head1 OPTIONS
36
37 =over
38
39 =item B<--debug, -d>
40
41 Debug verbosity. (Default 0)
42
43 =item B<--help, -h>
44
45 Display brief useage information.
46
47 =item B<--man, -m>
48
49 Display this manual.
50
51 =back
52
53 =head1 EXAMPLES
54
55   parse_uniprot_results -D ./uniprot_results/ -n '${search}_name.html' < search_parameters
56
57 Will pretty much do what you want
58
59 =cut
60
61
62
63 use vars qw($DEBUG $REVISION);
64
65 BEGIN{
66      ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
67      $DEBUG = 0 unless defined $DEBUG;
68 }
69
70 use IO::File;
71 use IO::Dir;
72
73 use HTML::TreeBuilder;
74 use HTML::ElementTable;
75
76 my %options = (debug    => 0,
77                help     => 0,
78                man      => 0,
79                dir      => '.',
80                keyword  => undef,
81                keywords => 0,
82               );
83
84 GetOptions(\%options,'keyword|k=s','dir|D=s','debug|d+','help|h|?','man|m',
85            'keywords',
86           );
87
88
89 pod2usage() if $options{help};
90 pod2usage({verbose=>2}) if $options{man};
91
92 $DEBUG = $options{debug};
93
94 # CSV columns
95 use constant {NAME        => 0,
96               REFSEQ      => 1,
97               LOCATION    => 2,
98               ALIAS       => 3,
99               FUNCTION    => 4,
100               DESCRIPTION => 5,
101               KEYWORD     => 6,
102               DBNAME      => 7,
103               FILENAME    => 8,
104              };
105
106 if ($options{keywords}) {
107      if (@ARGV != 1) {
108           pod2usage("If the --keywords option is used, exactly one argument (the keyword) must be passed");
109      }
110      $options{dir} = "$ARGV[0]_results_uniprot";
111 }
112
113 if (not -d $options{dir}) {
114      die "$options{dir} does not exist or is not a directory";
115 }
116
117 my $dir = new IO::Dir $options{dir} or die "Unable to open dir $options{dir}: $!";
118
119 print join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
120
121 my ($keyword) = $options{keyword} || $options{dir} =~ m#(?:^|/)([^\/]+)_results_uniprot#;
122
123 FILE: while ($_ = $dir->read) {
124      my $file_name = $_;
125      next if $file_name =~ /^\./;
126      next unless -f "$options{dir}/$file_name" and -r "$options{dir}/$file_name";
127
128      my $file = new IO::File "$options{dir}/$file_name", 'r' or die "Unable to open file $file_name";
129
130      my @fields;
131      my $current_field = undef;
132  LINE: while (<$file>) {
133           my ($type,$data) = $_ =~ /^(\w{2})\ {3}(.+)/s;
134           $type ||= 'SQ';
135           next LINE if not defined $data;
136           if (not defined $current_field and $type ne 'ID') {
137                next FILE;
138           }
139           if (not defined $current_field or $current_field->{type} ne $type) {
140                if (defined $current_field) {
141                     push @fields,$current_field;
142                }
143                $current_field = {type => $type,
144                                  data  => '',
145                                 };
146           }
147           $current_field->{data} .= $data;
148      }
149      if (defined $current_field) {
150           push @fields,$current_field;
151      }
152
153      my @results;
154
155      for my $field (@fields) {
156           my $type = $field->{type};
157           if ($type eq 'GN') {
158                # Find gene name
159                ($results[NAME]) = $field->{data} =~ m{Name=(.+?);}xis;
160           }
161           elsif ($type eq 'DR') {
162                # Find REF SEQ number
163                ($results[REFSEQ]) = $field->{data} =~ m{RefSeq;\s*([^;]+)}xis;
164                if (not defined $results[REFSEQ]) {
165                     ($results[REFSEQ]) = $field->{data} =~ m{Ensembl;\s*([^;]+)}xis;
166                }
167           }
168           elsif ($type eq 'DE') {
169                # Find gene aliases; these are odd bits separated by ()
170                # and such.
171                my $alias = $field->{data};
172                $alias =~ s/\n/ /g;
173                my @aliases = split /(?:\)\s+\(|\s+\(|\)(?:\s+|\.\s*$))/, $alias;
174                $results[ALIAS] = join('; ',map {s/(\s)\s+/$1/g; $_;} @aliases);
175           }
176           elsif ($type eq 'CC') {
177                my ($function) = $field->{data} =~ m{-!-\s*FUNCTION:\s*(.+?)(?:-!-|$)}xs;
178                if (defined $function) {
179                     $function =~ s/\n//g;
180                     $function =~ s/(\s)\s+/$1/g;
181                     $results[FUNCTION] = $function;
182                }
183                my $description = $field->{data};
184                $description =~ s/\n/ /g;
185                $description =~ s/-!-//g;
186                $description =~ s/(\s)\s+/$1/g;
187                $description =~ s/-----{5,}.+$//;
188                $results[DESCRIPTION] = $description;
189           }
190      }
191
192      $results[NAME] ||= 'NO NAME';
193      $results[REFSEQ] ||= 'NO REFSEQ';
194
195      $results[LOCATION] ||= 'NO LOCATION';
196      $results[ALIAS] ||= 'NO ALIASES';
197      $results[FUNCTION] ||= 'NO FUNCTION';
198
199      # Figure out the keyword used
200      $results[KEYWORD] ||= $keyword || 'NO KEYWORD';
201
202      # Database searched
203      $results[DBNAME] = 'uniprot';
204      $results[FILENAME] = $file_name;
205
206      print join(',',map {qq("$_")} @results),qq(\n);
207 }
208
209
210
211
212
213
214 __END__