Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / lib / CXGN / Tools / Organism.pm
blob9b16bbf9a55b0b744ada5aaa3a4ee5eda126c7ec
4 =head1 NAME
6 Functions for accessing ogranism names and their identifiers
8 =head1 SYNOPSIS
11 =head1 DESCRIPTION
14 =cut
17 =head2 get_all_organisms
19 Usage: my ($names_ref, $ids_ref) = CXGN::Tools::Organism::get_all_organisms($dbh, %return_hash_flag);
20 Desc: This is a static function. Retrieves distinct organism names and IDs from sgn.common_name
21 Ret: Returns two arrayrefs. One array contains all the
22 organism names, and the other all the organism ids
23 with corresponding array indices
24 or a hash {common_name}=common_name_id if 2nd argument is provided
26 Args: a database handle, a boolean (optional)
27 Side Effects:
28 Example:
30 =cut
32 package CXGN::Tools::Organism;
34 use strict;
35 use warnings;
40 sub get_all_organisms {
41 my $dbh = shift;
42 my $return_hash = shift;
43 #this query should be changed to work with chado's organism table after sgn.common_name is replaced with it.
44 my $query = "SELECT common_name, common_name_id
45 FROM sgn.common_name ORDER BY upper(common_name) desc";
46 my $sth = $dbh->prepare($query);
47 $sth->execute();
48 my @names = ();
49 my @ids = ();
50 my %common_names=();
51 while (my($common_name, $common_name_id) = $sth->fetchrow_array()) {
52 push @names, $common_name;
53 push @ids, $common_name_id;
54 $common_names{$common_name}= $common_name_id;
56 return %common_names if $return_hash;
57 return (\@names, \@ids);
61 =head2 get_existing_organisms
63 Usage: my ($names_ref, $ids_ref) = CXGN::Tools::Organism::get_existing_organisms($dbh , return_hash_flag);
64 Desc: This is a static function. Selects the distinct organism names and their IDs from phenome.locus.
65 Useful for populating a unique drop-down menu with only the organism names that exist in the table.
66 Ret: Returns two arrayrefs. One array contains all the
67 organism names, and the other all the organism ids with corresponding array indices.
68 or a hash {common_name}=common_name_id if 2nd argument is provided
69 Args: a database handle, and a boolean (optional)
70 Side Effects:
71 Example:
73 =cut
75 sub get_existing_organisms {
76 my $dbh= shift;
77 my $return_hash = shift;
78 my $query = "SELECT distinct(common_name), common_name_id FROM phenome.locus
79 JOIN sgn.common_name using(common_name_id)
80 WHERE obsolete = 'f'";
81 my $sth = $dbh->prepare($query);
82 $sth->execute();
83 my @names = ();
84 my @ids = ();
85 my %common_names=();
86 while (my($common_name, $common_name_id) = $sth->fetchrow_array()) {
87 push @names, $common_name;
88 push @ids, $common_name_id;
89 $common_names{$common_name}= $common_name_id;
91 return %common_names if $return_hash;
92 return (\@names, \@ids);
95 =head2 get_all_populations
97 Usage: my ($names_ref, $ids_ref) = CXGN::Tools::Organism::get_all_populations($dbh);
98 Desc: This is a static function. Retrieves distinct population names and IDs from phenome.population
99 Ret: Returns two arrayrefs. One array contains all the
100 population names, and the other all the population ids
101 with corresponding array indices.
102 Args: a database handle
103 Side Effects:
104 Example:
106 =cut
108 sub get_all_populations {
109 my $dbh = shift;
111 my $query = "SELECT name, population_id
112 FROM phenome.population";
113 my $sth = $dbh->prepare($query);
114 $sth->execute();
115 my @names = ();
116 my @ids = ();
117 while (my($name, $population_id) = $sth->fetchrow_array()) {
118 push @names, $name;
119 push @ids, $population_id;
121 return (\@names, \@ids);
125 =head2 organism_id_specie
127 Usage: $id = CXGN::Tools::Organism::organism_id_specie($dbh, $specie);
128 Desc: retrieves the organism id from sgn.organism
129 Ret: organism id
130 Args: db handle, specie name (eg. solanum lycopersicum)
131 Side Effects: access db
132 Example:
134 =cut
136 sub organism_id_specie {
137 my $dbh = shift;
138 my $specie = shift;
140 print STDERR "specie_tax: $specie\n";
142 my $sth = $dbh->prepare("SELECT organism_id
143 FROM sgn.organism
144 WHERE specie_tax ILIKE ?"
146 $sth->execute($specie);
147 my $id;
148 while (my $id_1 = $sth->fetchrow_array()) {
149 $id = $id_1;
151 return $id;
158 return 1;