change rules for cluster accessible dirs.
[cxgn-corelibs.git] / lib / CXGN / Accession.pm
blob35afeb7dde6ec484a74c0a1827eea10619a92c1c
1 use strict;
2 use CXGN::DB::Connection;
3 use CXGN::Tools::Text;
4 use CXGN::DB::SQLWrappers;
5 package CXGN::Accession;
7 =head1 NAME
9 CXGN::Accession
11 =head1 DESCRIPTION
13 Gets accession data from the sgn.accession and sgn.accession_names tables.
15 Note: will be soon deprecated in favor of the chado stock table.
17 =head1 AUTHOR
19 Takes an accession id or an accession name and returns a new accession object.
20 With no accession id or name, it creates an empty accession object.
22 john binns - John Binns <zombieite@gmail.com>
25 Code and POD style reformatting by Lukas.
28 =head1 OBJECT METHODS
30 This class implements the following methods:
32 =cut
35 =head2 new
37 Takes an accession id or an accession name and returns a new accession object. With no accession id or name, it creates an empty accession object.
39 use CXGN::Accession;
40 my $accession=CXGN::Accession->new($dbh,$accession_name);
42 =cut
44 sub new {
45 my $class=shift;
46 my($dbh,$accession)=@_;
47 unless(CXGN::DB::Connection::is_valid_dbh($dbh)){die"Invalid DBH.";}
48 my $self=bless({},$class);
49 $self->{dbh}=$dbh;
50 if($accession) {
51 my $id_query;
52 if($accession=~/^\d+$/) {
53 $id_query=$self->{dbh}->prepare('SELECT accession_id FROM accession WHERE accession_id=?');
55 else {
56 $accession = '%'.$accession.'%';
57 $id_query=$self->{dbh}->prepare('SELECT accession_id FROM accession_names WHERE accession_name ilike ?');
59 $id_query->execute($accession);
60 ($self->{accession_id})=$id_query->fetchrow_array();
61 if($self->{accession_id}) {
62 my $accession_query=$self->{dbh}->prepare('SELECT organism.organism_id,organism_name,common_name.common_name_id,
63 common_name.common_name,accession.common_name, accession.accession_name_id,
64 accession_names.accession_name, accession.chado_organism_id
65 FROM accession
66 LEFT JOIN accession_names USING (accession_name_id)
67 LEFT JOIN organism ON (accession.organism_id=organism.organism_id)
68 LEFT JOIN common_name USING (common_name_id)
69 WHERE accession.accession_id=?'
72 $accession_query->execute($self->{accession_id});
73 ($self->{organism_id},
74 $self->{organism_name},
75 $self->{organism_common_name_id},
76 $self->{organism_common_name},
77 $self->{accession_common_name},
78 $self->{preferred_name_id},
79 $self->{preferred_name},
80 $self->{chado_organism_id})=$accession_query->fetchrow_array();
81 my @aliases;
82 my $aliases_query=$self->{dbh}->prepare('SELECT accession_name
83 FROM accession_names
84 WHERE accession_id=? AND accession_name!=?'
86 $aliases_query->execute($self->{accession_id},$self->{preferred_name});
87 while(my($alias)=$aliases_query->fetchrow_array)
89 push(@aliases,$alias);
91 $self->{aliases}=\@aliases;
93 else {
94 return undef;
97 return $self;
100 =head2 all_accessions
102 Usage: my %hash = CXGN::Accession->all_accessions($dbh);
103 Desc: class method that returns all the accessions
104 as a hash of class_ids and accession names.
105 Author: added by Lukas 10/2009
107 =cut
109 sub all_accessions {
110 my $class = shift;
111 my $dbh = shift;
113 my $q = "SELECT accession_id, organism_name, accession_name FROM sgn.accession join sgn.accession_names using(accession_id) join sgn.organism using(organism_id)";
114 my $h = $dbh->prepare($q);
115 $h->execute();
116 my %hash = ();
117 while (my ($accession_id, $organism_name, $accession_name) = $h->fetchrow_array()) {
118 $hash{$accession_id} = "$organism_name ($accession_name)";
121 return %hash;
125 =head2 accession_id
127 my $id=$accession->accession_id();
129 =cut
131 sub accession_id {
132 my $self=shift;
133 return $self->{accession_id};
136 =head2 preferred_name
138 my $accession_name=$accession->preferred_name();
140 =cut
142 sub preferred_name {
143 my $self=shift;
144 if(@_)
146 ($self->{preferred_name})=@_;
148 return $self->{preferred_name};
151 =head2 other_names
153 my @aliases=@{$accession->other_names()};
155 =cut
157 sub other_names {
158 my $self=shift;
159 return @{$self->{aliases}};
162 =head2 accession_common_name
164 my $acn=$accession->accession_common_name();
166 =cut
168 sub accession_common_name {
169 my $self=shift;
170 if(@_) {
171 ($self->{accession_common_name})=@_;
173 return $self->{accession_common_name};
176 =head2 chado_organism_id
178 my $acn=$accession->chado_organism_id();
180 =cut
182 sub chado_organism_id {
183 my $self=shift;
184 if(@_) {
185 ($self->{chado_organism_id})=@_;
187 return $self->{chado_organism_id};
190 =head2 organism_name
192 my $org_name=$accession->organism_name();
194 =cut
196 sub organism_name {
197 my $self=shift;
198 if(@_)
200 ($self->{organism_name})=@_;
202 return $self->{organism_name};
205 =head2 organism_common_name
207 my $org_cn=$accession->organism_common_name();
209 =cut
211 sub organism_common_name {
212 my $self=shift;
213 if(@_)
215 ($self->{organism_common_name})=@_;
217 return $self->{organism_common_name};
220 =head2 verbose_name
222 my $verbose_name = $accession->verbose_name();
224 =cut
226 sub verbose_name {
227 my $self=shift;
228 my $verbose_name=$self->{organism_name}." ".$self->{preferred_name};
229 $verbose_name=CXGN::Tools::Text::abbr_latin($verbose_name);
230 $verbose_name=~s/ \(.*\)//;
231 return $verbose_name;
234 =head2 extra_verbose_name
236 my $xvn=$accession->extra_verbose_name();
238 =cut
240 sub extra_verbose_name {
241 my $self=shift;
242 my @extra_verbose_name;
243 if($self->{organism_common_name}) {
244 push(@extra_verbose_name,"<b>".$self->{organism_common_name}."</b>");
246 if($self->{organism_name}) {
247 push(@extra_verbose_name,$self->{organism_name});
249 # if($self->{accession_common_name})
251 # push(@extra_verbose_name,$self->{accession_common_name});
253 if($self->{preferred_name}) {
254 push(@extra_verbose_name,$self->{preferred_name});
256 if(@{$self->{aliases}}[0]) {
257 push(@extra_verbose_name,'('.join(', ',@{$self->{aliases}}).')');
259 my $extra_verbose_name=join(' ',@extra_verbose_name);
260 $extra_verbose_name=CXGN::Tools::Text::abbr_latin($extra_verbose_name);
261 return $extra_verbose_name;