1 package CXGN
::Stock
::StockLookup
;
5 CXGN::Stock::StockLookup - a module to lookup stock names by unique name or synonym.
9 my $stock_lookup = CXGN::Stock::StockLookup->new({ schema => $schema} );
14 Looks up stocks ("Stock::Stock") that have a match with the unique name or synonym to the searched name. Provides a count of matching stocks when more than one stock is found. Provides the Stock::Stock object when only a single stock matches.
18 Jeremy D. Edwards (jde22@cornell.edu)
23 use MooseX
::FollowPBP
;
24 use Moose
::Util
::TypeConstraints
;
26 use SGN
::Model
::Cvterm
;
30 isa
=> 'DBIx::Class::Schema',
34 =head2 predicate has_stock_name(), clearer clear_stock_name(), accessors stock_name()
36 functions to test, clear, set or get the stock name.
40 has
'stock_name' => (isa
=> 'Str', is
=> 'rw', predicate
=> 'has_stock_name', clearer
=> 'clear_stock_name');
44 Usage: $self-> get_stock($stock_type_id, $stock_organism_id)
45 Desc: check if the uniquename exists in the stock table
47 Args: optional: stock_type_id (cvterm_id) , $stock_organism_id (organism_id)
48 Side Effects: calls _get_stock_resultset, returns only one object row even if multiple stocks are found (would happen only if there are multiple stocks with the same uniquename of different letter case or different type_id or different organism_id)
49 Example: $self->get_stock(undef, $manihot_esculenta_organism_id)
55 my $stock_type_id = shift;
56 my $stock_organism_id = shift;
57 my $stock_rs = $self->_get_stock_resultset($stock_type_id, $stock_organism_id);
59 if ($stock_rs->count > 0) {
60 $stock = $stock_rs->first;
67 =head2 function get_stock_exact()
69 retrieves the stock row with an exact match to the stock name or synonym
75 my $stock_rs = $self->_get_stock_resultset_exact();
77 if ($stock_rs->count == 1) {
78 $stock = $stock_rs->first;
85 =head2 function get_matching_stock_count()
87 retrieves the number of stocks that match the name (or synonym)
91 sub get_matching_stock_count
{
93 my $stock_name = $self->get_stock_name();
94 my $stock_rs = $self->_get_stock_resultset();
98 my $stock_match_count = $stock_rs->count;
99 if (!$stock_match_count) {
102 if ($stock_match_count == 0) {
105 return $stock_match_count;
108 sub get_synonym_hash_lookup
{
110 print STDERR
"Synonym Start:".localtime."\n";
111 my $schema = $self->get_schema();
112 my $synonym_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'stock_synonym', 'stock_property')->cvterm_id();
113 my $accession_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'accession', 'stock_type')->cvterm_id();
114 my $q = "SELECT stock.uniquename, stockprop.value FROM stock JOIN stockprop USING(stock_id) WHERE stock.type_id=$accession_type_id AND stockprop.type_id=$synonym_type_id ORDER BY stockprop.value;";
115 my $h = $schema->storage->dbh()->prepare($q);
118 while (my ($uniquename, $synonym) = $h->fetchrow_array()) {
119 push @
{$result{$uniquename}}, $synonym;
121 print STDERR
"Synonym End:".localtime."\n";
125 sub get_owner_hash_lookup
{
128 my $where_clause = '';
129 print STDERR
"StockOwner Start:".localtime."\n";
131 my $stock_id_sql = join ',', @
$stock_ids;
132 $where_clause = "WHERE stock_id IN ($stock_id_sql)";
134 my $schema = $self->get_schema();
135 my $q = "SELECT stock_id, sp_person_id, username, first_name, last_name FROM sgn_people.sp_person JOIN phenome.stock_owner USING(sp_person_id) $where_clause GROUP BY (stock_id, sp_person_id, username, first_name, last_name) ORDER BY sp_person_id;";
136 my $h = $schema->storage->dbh()->prepare($q);
139 while (my ($stock_id, $sp_person_id, $username, $first_name, $last_name) = $h->fetchrow_array()) {
140 push @
{$result{$stock_id}}, [$sp_person_id, $username, $first_name, $last_name];
142 print STDERR
"StockOwner End:".localtime."\n";
146 sub get_organization_hash_lookup
{
148 print STDERR
"StockOrg Start:".localtime."\n";
149 my $schema = $self->get_schema();
150 my $organization_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'organization', 'stock_property')->cvterm_id();
151 my $q = "SELECT stock_id, value FROM stockprop WHERE type_id=$organization_type_id ORDER BY value;";
152 my $h = $schema->storage->dbh()->prepare($q);
155 while (my ($stock_id, $organization) = $h->fetchrow_array()) {
156 push @
{$result{$stock_id}}, $organization;
158 print STDERR
"StockOrg End:".localtime."\n";
162 sub _get_stock_resultset
{
164 my $stock_type_id = shift;
165 my $stock_organism_id = shift;
166 my $schema = $self->get_schema();
167 my $stock_name = $self->get_stock_name();
169 'me.is_obsolete' => { '!=' => 't' },
171 'lower(me.uniquename)' => { like
=> lc($stock_name) },
173 'lower(type.name)' => { like
=> '%synonym%' },
174 'lower(stockprops.value)' => { like
=> lc($stock_name) },
179 $search_hash->{'me.type_id'} = $stock_type_id;
181 if ($stock_organism_id) {
182 $search_hash->{'me.organism_id'} = $stock_organism_id;
184 my $stock_rs = $schema->resultset("Stock::Stock")
185 ->search($search_hash,
187 join => { 'stockprops' => 'type'} ,
194 sub _get_stock_resultset_exact
{
196 my $schema = $self->get_schema();
197 my $stock_name = $self->get_stock_name();
198 my $stock_rs = $schema->resultset("Stock::Stock")
199 ->search({ 'me.is_obsolete' => { '!=' => 't' },
200 'uniquename' => $stock_name,
203 join => { 'stockprops' => 'type'} ,
210 sub get_stock_synonyms
{
212 my $search_type = shift; # 'stock_id' | 'uniquename' | 'any_name'
213 my $stock_type = shift; # type of stock ex 'accession'
214 my $to_get = shift; # array ref
216 my $schema = $self->get_schema();
218 my $stock_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema,$stock_type,'stock_type')->cvterm_id;
221 join => { 'stockprops' => 'type'},
222 '+select' => ['stockprops.value','type.name'],
223 '+as' => ['stockprop_value','cvterm_name']
224 # join_type => 'FULL_OUTER'
227 'me.is_obsolete' => { '!=' => 't' },
228 'me.type_id' => {'=' => $stock_type_id}
230 if ($search_type eq 'stock_id'){
231 $query->{'me.stock_id'} = {-in=>$to_get};
232 } elsif ($search_type eq 'uniquename'){
233 $query->{'me.uniquename'} = {-in=>$to_get};
234 } elsif ($search_type eq 'any_name'){
236 'me.uniquename' => {-in=>$to_get},
238 'type.name' => 'stock_synonym',
239 'stockprops.value' => {-in=>$to_get}
245 my $stock_rs = $schema->resultset("Stock::Stock")
246 ->search($query,$table_joins);
247 my $synonym_hash = {};
248 while( my $row = $stock_rs->next) {
249 my $uname = $row->uniquename;
250 if (not defined $synonym_hash->{$uname}){
251 $synonym_hash->{$uname} = [];
253 my $cvname = $row->get_column('cvterm_name');
254 if ($cvname && $cvname eq 'stock_synonym'){
255 push @
{$synonym_hash->{$uname}}, $row->get_column('stockprop_value');
258 return $synonym_hash;
261 =head2 function get_cross_exact()
263 retrieves the stock row with cross stock type and with an exact match to the stock name
267 sub get_cross_exact
{
269 my $schema = $self->get_schema();
270 my $stock_name = $self->get_stock_name();
271 my $cross_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema,'cross','stock_type')->cvterm_id;
273 my $stock_rs = $schema->resultset("Stock::Stock")->search({ 'me.is_obsolete' => { '!=' => 't' }, 'uniquename' => $stock_name, 'type_id' => $cross_type_id });
275 if ($stock_rs->count == 1) {
276 $stock = $stock_rs->first;
284 =head2 function get_accession_exact()
286 retrieves the stock row with accession stock type and with an exact match to the stock name
290 sub get_accession_exact
{
292 my $schema = $self->get_schema();
293 my $stock_name = $self->get_stock_name();
294 my $accession_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema,'accession','stock_type')->cvterm_id;
296 my $stock_rs = $schema->resultset("Stock::Stock")->search({ 'me.is_obsolete' => { '!=' => 't' }, 'uniquename' => $stock_name, 'type_id' => $accession_type_id });
298 if ($stock_rs->count == 1) {
299 $stock = $stock_rs->first;