interaction on sommer
[sgn.git] / lib / CXGN / Cvterm.pm
blobe2406ce4b8da48f4df30780786ed51a394852d26
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 has 'schema' => (
35 isa => 'Bio::Chado::Schema',
36 is => 'rw',
37 required => 1
40 has 'cvterm' => (
41 isa => 'Bio::Chado::Schema::Result::Cv::Cvterm',
42 is => 'rw',
45 has 'cvterm_id' => (
46 isa => 'Int',
47 is => 'rw',
50 has 'cv' => (
51 isa => 'Bio::Chado::Schema::Result::Cv::Cv',
52 is => 'rw',
55 has 'cv_id' => (
56 isa => 'Int',
57 is => 'rw',
60 has 'dbxref' => (
61 isa => 'Bio::Chado::Schema::Result::General::Dbxref',
62 is => 'rw',
65 has 'db' => (
66 isa => 'Bio::Chado::Schema::Result::General::Db',
67 is => 'rw',
70 has 'name' => (
71 isa => 'Str',
72 is => 'rw',
75 has 'definition' => (
76 isa => 'Str',
77 is => 'rw',
80 has 'is_obsolete' => (
81 isa => 'Bool',
82 is => 'rw',
83 default => 0,
86 has 'accession' => (
87 isa => 'Maybe[Str]',
88 is => 'rw',
93 #########################################
96 sub BUILD {
97 my $self = shift;
99 my $cvterm;
100 if ($self->cvterm_id){
101 $cvterm = $self->schema()->resultset("Cv::Cvterm")->find({ cvterm_id => $self->cvterm_id() });
102 } elsif ($self->accession ) {
103 my ($db_name, $dbxref_accession) = split "\:", $self->accession;
105 #InterPro accessions have a namespace (db.name) that is different from the accession prefic
106 if ($self->accession =~ m/^IPR*/ ) {
107 $db_name = 'InterPro';
108 $dbxref_accession= $self->accession;
110 my $dbxref = $self->schema()->resultset("General::Dbxref")->find(
112 'db.name' => $db_name,
113 'me.accession' => $dbxref_accession,
115 { join => 'db'}
118 if ($dbxref) { $cvterm = $dbxref->cvterm ; }
121 if (defined $cvterm) {
122 $self->cvterm($cvterm);
123 $self->cvterm_id($cvterm->cvterm_id);
124 $self->name($cvterm->name);
125 $self->definition($cvterm->definition || '' );
126 $self->is_obsolete($cvterm->is_obsolete);
128 $self->dbxref( $self->schema()->resultset("General::Dbxref")->find({ dbxref_id=>$cvterm->dbxref_id() }) );
129 $self->cv_id( $cvterm->cv_id);
130 $self->cv( $self->schema()->resultset("Cv::Cv")->find( { cv_id => $cvterm->cv_id() }) );
131 $self->db( $self->dbxref->db );
132 $self->accession( $self->db->name . ':' . $self->dbxref->accession );
135 return $self;
144 =head2 function get_image_ids
146 Synopsis: my @images = $self->get_image_ids()
147 Arguments: none
148 Returns: a list of md_image_ids
149 Side effects: none
150 Description: a method for fetching all images associated with a cvterm
152 =cut
154 sub get_image_ids {
155 my $self = shift;
156 my @ids;
157 my $q = "SELECT image_id FROM metadata.md_image_cvterm WHERE cvterm_id=? AND obsolete = 'f' ";
158 my $h = $self->schema->storage->dbh()->prepare($q);
159 $h->execute($self->cvterm_id);
160 while (my ($image_id) = $h->fetchrow_array()){
161 push @ids, [$image_id, 'cvterm'];
163 return @ids;
168 =head2 function get_is_relationshiptype
170 Usage: my $is_relationshiptype = $self->get_is_relationship_type
171 Desc: find the database value of teh cvterm column is_relationship_type (integer 0,1)
172 Property
173 Side Effects:
174 Example:
176 =cut
178 sub get_is_relationshiptype {
179 my $self = shift;
180 return $self->cvterm->is_relationship_type;
186 =head2 synonyms
188 Usage: my @synonyms = $self->synonyms()
189 Desc: Fetch all synonym names of a cvterm. use BCS cvterm->add_synonym and $cvterm->delete_synonym to manipulate cvtermsynonyms
190 Ret: an array of synonym strings
191 Args: none
192 Side Effects: none
193 Example:
195 =cut
197 sub synonyms {
198 my $self = shift;
199 my $cvterm = $self->cvterm;
200 my $synonym_rs = $cvterm->cvtermsynonyms;
202 my @synonyms =() ;
203 while ( my $s = $synonym_rs->next ) {
204 push (@synonyms, $s->synonym) ;
206 return @synonyms;
209 =head2 get_single_synonym
211 Usage: my $single_synonym = $self->get_single_synonym;
212 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
213 Ret: a single synonym
214 Args: none
215 Side Effects:
216 Example:
218 =cut
220 sub get_single_synonym {
221 my $self=shift;
222 my $cvterm_id= $self->cvterm_id();
224 my $query= "SELECT synonym FROM cvtermsynonym WHERE cvterm_id= ? AND synonym NOT LIKE '% %' AND synonym NOT LIKE '%\\_%' LIMIT 1";
225 my $synonym_sth = $self->schema->storage->dbh->prepare($query);
226 $synonym_sth->execute($cvterm_id);
228 my $single_synonym = $synonym_sth->fetchrow_array();
230 return $single_synonym;
234 =head2 secondary_dbxrefs
236 Usage: $self->secondary_dbxrefs
237 Desc: find all secondary accessions associated with the cvterm
238 These are stored in cvterm_dbxref table
239 Ret: a list of full accession strings (PO:0001234)
240 Args: none
241 Side Effects: none
242 Example:
244 =cut
246 sub secondary_dbxrefs {
247 my $self=shift;
248 my $rs = $self->cvterm->search_related('cvterm_dbxrefs' , { is_for_definition => 0} );
249 my @list;
250 while (my $r = $rs->next) {
251 push @list , $r->dbxref;
253 return @list;
259 =head2 def_dbxrefs
261 Usage: $self->def_dbxrefs
262 Desc: find all definition dbxrefs of the cvterm
263 These are stored in cvterm_dbxref table
264 Ret: an array of dbxref objects
265 Args: none
266 Side Effects: none
267 Example:
269 =cut
271 sub def_dbxrefs {
272 my $self=shift;
273 my $cvterm = $self->cvterm;
274 my @defs = $cvterm->search_related('cvterm_dbxrefs' , { is_for_definition => 1} );
276 return @defs || undef ;
280 =head2 cvtermprops
282 Usage: $self->cvtermprops
283 Desc: find all cvtermprops (names and values) of the cvterm
284 These are stored in cvtermprop table
285 Ret: hashref of arrays - key = cvtermprop type name, value = list of cvtermprop values of that type
286 Args: none
287 Side Effects: none
288 Example:
290 =cut
292 sub cvtermprops {
293 my $self = shift;
294 my $properties;
295 my $cvtermprops = $self->cvterm->cvtermprops;
297 while ( my $prop = $cvtermprops->next ) {
298 push @{ $properties->{$prop->type->name } } , $prop->value ;
300 return $properties;
304 ########################################
305 sub _retrieve_cvtermprop {
306 my $self = shift;
307 my $type = shift;
308 my @results;
310 try {
311 my $cvtermprop_type_id = SGN::Model::Cvterm->get_cvterm_row($self->schema, $type, 'trait_property')->cvterm_id();
312 my $rs = $self->schema()->resultset("Cv::Cvtermprop")->search({ cvterm_id => $self->cvterm_id(), type_id => $cvtermprop_type_id }, { order_by => {-asc => 'cvtermprop_id'} });
314 while (my $r = $rs->next()){
315 push @results, $r->value;
317 } catch {
318 print STDERR "Cvterm $type does not exist in this database\n";
321 my $res = join ',', @results;
322 return $res;
325 sub _remove_cvtermprop {
326 my $self = shift;
327 my $type = shift;
328 my $value = shift;
329 my $type_id = SGN::Model::Cvterm->get_cvterm_row($self->schema, $type, 'trait_property')->cvterm_id();
330 my $rs = $self->schema()->resultset("Cv::Cvtermprop")->search( { type_id=>$type_id, cvterm_id => $self->cvterm_id(), value=>$value } );
332 if ($rs->count() == 1) {
333 $rs->first->delete();
334 return 1;
336 elsif ($rs->count() == 0) {
337 return 0;
339 else {
340 print STDERR "Error removing cvtermprop from cvterm ".$self->cvterm_id().". Please check this manually.\n";
341 return 0;
346 sub _store_cvtermprop {
347 my $self = shift;
348 my $type = shift;
349 my $value = shift;
350 my $cvtermprop = SGN::Model::Cvterm->get_cvterm_row($self->schema, $type, 'trait_property')->name();
351 my $stored_cvtermprop = $self->cvterm->create_cvtermprops({ $cvtermprop => $value});
357 __PACKAGE__->meta->make_immutable;
359 ##########
360 1;########
361 ##########