start fixing test for multi cat phenotype upload.
[sgn.git] / lib / CXGN / Cvterm.pm
blob83b55e879d8847a3e526b9118ab6f6196a4d283c
1 =head1 NAME
3 CXGN::Cvterm - a second-level object for Cvterm
5 Version: 1.2
7 =head1 DESCRIPTION
9 This object was re-factored from CXGN::Chado::Cvterm and moosified.
10 Use CXGN::Cvterm for new code. CXGN::Chado::Cvterm is deprecated
13 =head1 AUTHOR
15 Naama Menda <nm249@cornell.edu>
16 Lukas Mueller <lam87@cornell.edu>
18 =cut
20 package CXGN::Cvterm ;
22 use Moose;
24 use Carp;
25 use Data::Dumper;
26 use Bio::Chado::Schema;
27 use CXGN::Metadata::Schema;
28 use SGN::Model::Cvterm;
30 use base qw / CXGN::DB::Object / ;
32 use Try::Tiny;
34 =head2 accessor schema
36 =cut
38 has 'schema' => (
39 isa => 'Bio::Chado::Schema',
40 is => 'rw',
41 required => 1
44 =head2 accessor cvterm
46 Returns: Cv::Cvterm DBIx::Class object
48 =cut
50 has 'cvterm' => (
51 isa => 'Bio::Chado::Schema::Result::Cv::Cvterm',
52 is => 'rw',
55 =head2 accessor cvterm_id
57 =cut
59 has 'cvterm_id' => (
60 isa => 'Int',
61 is => 'rw',
64 =head2 accessor cv
66 =cut
68 has 'cv' => (
69 isa => 'Bio::Chado::Schema::Result::Cv::Cv',
70 is => 'rw',
73 =head2 accessor cv_id
75 =cut
77 has 'cv_id' => (
78 isa => 'Int',
79 is => 'rw',
82 =head2 accessor dbxref
84 =cut
86 has 'dbxref' => (
87 isa => 'Bio::Chado::Schema::Result::General::Dbxref',
88 is => 'rw',
91 =head2 accessor db
93 =cut
95 has 'db' => (
96 isa => 'Bio::Chado::Schema::Result::General::Db',
97 is => 'rw',
100 =head2 accessor name
102 =cut
104 has 'name' => (
105 isa => 'Str',
106 is => 'rw',
109 =head2 accessor definition
111 =cut
113 has 'definition' => (
114 isa => 'Str',
115 is => 'rw',
118 =head2 accessor is_obsolete
120 =cut
123 has 'is_obsolete' => (
124 isa => 'Bool',
125 is => 'rw',
126 default => 0,
130 =head2 accessor accession
132 refers to dbxref.accession column
134 =cut
136 has 'accession' => (
137 isa => 'Maybe[Str]',
138 is => 'rw',
143 #########################################
146 sub BUILD {
147 my $self = shift;
149 my $cvterm;
150 if ($self->cvterm_id){
151 $cvterm = $self->schema()->resultset("Cv::Cvterm")->find({ cvterm_id => $self->cvterm_id() });
152 } elsif ($self->accession ) {
153 my ($db_name, $dbxref_accession) = split "\:", $self->accession;
155 #InterPro accessions have a namespace (db.name) that is different from the accession prefic
156 if ($self->accession =~ m/^IPR*/ ) {
157 $db_name = 'InterPro';
158 $dbxref_accession= $self->accession;
160 my $dbxref = $self->schema()->resultset("General::Dbxref")->find(
162 'db.name' => $db_name,
163 'me.accession' => $dbxref_accession,
165 { join => 'db'}
168 if ($dbxref) { $cvterm = $dbxref->cvterm ; }
171 if (defined $cvterm) {
172 $self->cvterm($cvterm);
173 $self->cvterm_id($cvterm->cvterm_id);
174 $self->name($cvterm->name);
175 $self->definition($cvterm->definition || '' );
176 $self->is_obsolete($cvterm->is_obsolete);
178 $self->dbxref( $self->schema()->resultset("General::Dbxref")->find({ dbxref_id=>$cvterm->dbxref_id() }) );
179 $self->cv_id( $cvterm->cv_id);
180 $self->cv( $self->schema()->resultset("Cv::Cv")->find( { cv_id => $cvterm->cv_id() }) );
181 $self->db( $self->dbxref->db );
182 $self->accession( $self->db->name . ':' . $self->dbxref->accession );
185 return $self;
194 =head2 function get_image_ids
196 Synopsis: my @images = $self->get_image_ids()
197 Arguments: none
198 Returns: a list of md_image_ids
199 Side effects: none
200 Description: a method for fetching all images associated with a cvterm
202 =cut
204 sub get_image_ids {
205 my $self = shift;
206 my @ids;
207 my $q = "SELECT image_id FROM metadata.md_image_cvterm WHERE cvterm_id=? AND obsolete = 'f' ";
208 my $h = $self->schema->storage->dbh()->prepare($q);
209 $h->execute($self->cvterm_id);
210 while (my ($image_id) = $h->fetchrow_array()){
211 push @ids, [$image_id, 'cvterm'];
213 return @ids;
218 =head2 function get_is_relationshiptype
220 Usage: my $is_relationshiptype = $self->get_is_relationship_type
221 Desc: find the database value of teh cvterm column is_relationship_type (integer 0,1)
222 Property
223 Side Effects:
224 Example:
226 =cut
228 sub get_is_relationshiptype {
229 my $self = shift;
230 return $self->cvterm->is_relationship_type;
236 =head2 synonyms
238 Usage: my @synonyms = $self->synonyms()
239 Desc: Fetch all synonym names of a cvterm. use BCS cvterm->add_synonym and $cvterm->delete_synonym to manipulate cvtermsynonyms
240 Ret: an array of synonym strings
241 Args: none
242 Side Effects: none
243 Example:
245 =cut
247 sub synonyms {
248 my $self = shift;
249 my $cvterm = $self->cvterm;
250 my $synonym_rs = $cvterm->cvtermsynonyms;
252 my @synonyms =() ;
253 while ( my $s = $synonym_rs->next ) {
254 push (@synonyms, $s->synonym) ;
256 return @synonyms;
259 =head2 get_single_synonym
261 Usage: my $single_synonym = $self->get_single_synonym;
262 Desc: a method for fetching a single synonym, based on the synonym structure. This is ugly and it would be better if we used types and type ids
263 Ret: a single synonym
264 Args: none
265 Side Effects:
266 Example:
268 =cut
270 sub get_single_synonym {
271 my $self=shift;
272 my $cvterm_id= $self->cvterm_id();
274 my $query= "SELECT synonym FROM cvtermsynonym WHERE cvterm_id= ? AND synonym NOT LIKE '% %' AND synonym NOT LIKE '%\\_%' LIMIT 1";
275 my $synonym_sth = $self->schema->storage->dbh->prepare($query);
276 $synonym_sth->execute($cvterm_id);
278 my $single_synonym = $synonym_sth->fetchrow_array();
280 return $single_synonym;
284 =head2 secondary_dbxrefs
286 Usage: $self->secondary_dbxrefs
287 Desc: find all secondary accessions associated with the cvterm
288 These are stored in cvterm_dbxref table
289 Ret: a list of full accession strings (PO:0001234)
290 Args: none
291 Side Effects: none
292 Example:
294 =cut
296 sub secondary_dbxrefs {
297 my $self=shift;
298 my $rs = $self->cvterm->search_related('cvterm_dbxrefs' , { is_for_definition => 0} );
299 my @list;
300 while (my $r = $rs->next) {
301 push @list , $r->dbxref;
303 return @list;
309 =head2 def_dbxrefs
311 Usage: $self->def_dbxrefs
312 Desc: find all definition dbxrefs of the cvterm
313 These are stored in cvterm_dbxref table
314 Ret: an array of dbxref objects
315 Args: none
316 Side Effects: none
317 Example:
319 =cut
321 sub def_dbxrefs {
322 my $self=shift;
323 my $cvterm = $self->cvterm;
324 my @defs = $cvterm->search_related('cvterm_dbxrefs' , { is_for_definition => 1} );
326 return @defs || undef ;
330 =head2 cvtermprops
332 Usage: $self->cvtermprops
333 Desc: find all cvtermprops (names and values) of the cvterm
334 These are stored in cvtermprop table
335 Ret: hashref of arrays - key = cvtermprop type name, value = list of cvtermprop values of that type
336 Args: none
337 Side Effects: none
338 Example:
340 =cut
342 sub cvtermprops {
343 my $self = shift;
344 my $properties;
345 my $cvtermprops = $self->cvterm->cvtermprops;
347 while ( my $prop = $cvtermprops->next ) {
348 push @{ $properties->{$prop->type->name } } , $prop->value ;
350 return $properties;
354 ########################################
355 sub _retrieve_cvtermprop {
356 my $self = shift;
357 my $type = shift;
358 my @results;
360 try {
361 my $cvtermprop_type_id = SGN::Model::Cvterm->get_cvterm_row($self->schema, $type, 'trait_property')->cvterm_id();
362 my $rs = $self->schema()->resultset("Cv::Cvtermprop")->search({ cvterm_id => $self->cvterm_id(), type_id => $cvtermprop_type_id }, { order_by => {-asc => 'cvtermprop_id'} });
364 while (my $r = $rs->next()){
365 push @results, $r->value;
367 } catch {
368 print STDERR "Cvterm $type does not exist in this database\n";
371 my $res = join ',', @results;
372 return $res;
375 sub _remove_cvtermprop {
376 my $self = shift;
377 my $type = shift;
378 my $value = shift;
379 my $type_id = SGN::Model::Cvterm->get_cvterm_row($self->schema, $type, 'trait_property')->cvterm_id();
380 my $rs = $self->schema()->resultset("Cv::Cvtermprop")->search( { type_id=>$type_id, cvterm_id => $self->cvterm_id(), value=>$value } );
382 if ($rs->count() == 1) {
383 $rs->first->delete();
384 return 1;
386 elsif ($rs->count() == 0) {
387 return 0;
389 else {
390 print STDERR "Error removing cvtermprop from cvterm ".$self->cvterm_id().". Please check this manually.\n";
391 return 0;
396 sub _store_cvtermprop {
397 my $self = shift;
398 my $type = shift;
399 my $value = shift;
400 my $cvtermprop = SGN::Model::Cvterm->get_cvterm_row($self->schema, $type, 'trait_property')->name();
401 my $stored_cvtermprop = $self->cvterm->create_cvtermprops({ $cvtermprop => $value});
407 __PACKAGE__->meta->make_immutable;
409 ##########
410 1;########
411 ##########