fixed recursive_children cvterm function, and added tests for parents and children
[cxgn-corelibs.git] / lib / CXGN / Metadata.pm
blob618e7f3814110f7ad132389cfe8137b5cd2c6f94
1 use strict;
2 use CXGN::DB::Connection;
3 use CXGN::People;
5 package CXGN::Metadata;
6 use base('CXGN::DB::Connection');
8 sub new {
9 my $class = shift;
10 my $db_name = shift;
11 my $table_name = shift;
12 my $id = shift;
14 my $self = $class->SUPER::new();
16 $self->set_db_name($db_name);
17 $self->set_table_name($table_name);
18 $self->set_row_id($id);
20 return $self;
23 =head2 function get_db_name
25 Synopsis:
26 Arguments:
27 Returns:
28 Side effects:
29 Description:
31 =cut
33 sub get_db_name {
34 my $self = shift;
35 return $self->{db_name};
38 =head2 function set_db_name
40 Synopsis:
41 Arguments:
42 Returns:
43 Side effects:
44 Description:
46 =cut
48 sub set_db_name {
49 my $self = shift;
50 $self->{db_name} = shift;
53 =head2 function get_table_name
55 Synopsis:
56 Arguments:
57 Returns:
58 Side effects:
59 Description:
61 =cut
63 sub get_table_name {
64 my $self = shift;
65 return $self->{table_name};
68 =head2 function set_table_name
70 Synopsis:
71 Arguments:
72 Returns:
73 Side effects:
74 Description:
76 =cut
78 sub set_table_name {
79 my $self = shift;
80 $self->{table_name} = shift;
83 =head2 function get_id
85 Synopsis:
86 Arguments:
87 Returns:
88 Side effects:
89 Description:
91 =cut
93 sub get_row_id {
94 my $self = shift;
95 return $self->{row_id};
98 =head2 function set_row_id
100 Synopsis:
101 Arguments:
102 Returns:
103 Side effects:
104 Description:
106 =cut
108 sub set_row_id {
109 my $self = shift;
110 $self->{row_id} = shift;
113 sub comments {
114 my $self = shift;
115 my $comments_query = $self->prepare(
116 'select comment_text from comments inner join attribution using (attribution_id) where database_name=? and table_name=? and row_id=?'
118 $comments_query->execute( $self->{db_name}, $self->{table_name},
119 $self->{row_id} );
120 my @comments;
121 while ( my ($comment) = $comments_query->fetchrow_array() ) {
122 push( @comments, $comment );
124 if (@comments) { return \@comments; }
125 else { return undef; }
128 sub attribute_bac_to_chromosome {
129 my $self = shift;
130 my ( $bac_id, $chromosome ) = @_;
131 if ( defined($chromosome) ) {
132 if ( my $proj = $self->get_project_associated_with_bac($bac_id) ) {
134 #remove the existing attribution
135 warn
136 "WARNING: moving BAC $bac_id from chromosome $proj to chromosome $chromosome";
137 $self->attribute_bac_to_chromosome( $bac_id, undef );
140 #find chromosome project id
141 my $project_object = CXGN::People::Project->new($self);
142 my $projects = $project_object->get_projects_with_name_like(
143 "Tomato Chromosome $chromosome Sequencing Project");
144 my $project_id = $projects->[0][0];
145 if ( defined($project_id) ) {
147 #this insert says, "there is a bac that is going to be attributed to some person, project, or organization"
148 my $attribution_insert = $self->prepare(
149 "insert into attribution (database_name,table_name,primary_key_column_name,row_id) values ('genomic','clone','clone_id',?)"
151 $attribution_insert->execute($bac_id);
152 my $id_query = $self->prepare(
153 "select currval('attribution_attribution_id_seq')");
154 $id_query->execute();
155 my @id = $id_query->fetchrow_array();
156 $id_query->finish();
157 my $attribution_id = $id[0];
159 #print STDERR "\n\nATTRIBUTION ID: $attribution_id\n\n";
160 #this insert says, "attribute this bac to the person/project/organization/role"
161 my $attribution_to_insert = $self->prepare(
162 "insert into attribution_to (attribution_id,person_id,organization_id,project_id,role_id) values (?,?,?,?,?)"
164 $attribution_to_insert->execute( $attribution_id, undef, undef,
165 $project_id, undef );
167 # warn"$bac_id attributed with attribution id $attribution_id.\n";
168 return $attribution_id;
171 else {
173 #remove the attribution of a bac to a chromosome
174 $self->do( <<EOQ, undef, 'genomic', 'clone', 'clone_id', $bac_id );
175 delete from attribution where database_name = ? and table_name = ? and primary_key_column_name = ? and row_id = ?
178 #the attribution_to table will be taken care of by the
179 #ON DELETE CASCADE of its attribution_id foreign key column
180 return;
183 #warn"Project not found.\n";
184 return;
187 sub attribute_bac_to_project {
188 my ( $self, $bac_id, $project_id ) = @_;
190 if ( defined($project_id) ) {
191 if ( my $proj = $self->get_project_associated_with_bac($bac_id) ) {
193 #remove the existing attribution
194 $self->attribute_bac_to_project( $bac_id, undef );
197 #this insert says, "there is a bac that is going to be attributed to some person, project, or organization"
198 my $attribution_insert = $self->prepare(
199 "insert into attribution (database_name,table_name,primary_key_column_name,row_id) values ('genomic','clone','clone_id',?)"
201 $attribution_insert->execute($bac_id);
202 my $id_query =
203 $self->prepare("select currval('attribution_attribution_id_seq')");
204 $id_query->execute();
205 my @id = $id_query->fetchrow_array();
206 $id_query->finish();
207 my $attribution_id = $id[0];
209 #print STDERR "\n\nATTRIBUTION ID: $attribution_id\n\n";
210 #this insert says, "attribute this bac to the person/project/organization/role"
211 my $attribution_to_insert = $self->prepare(
212 "insert into attribution_to (attribution_id,person_id,organization_id,project_id,role_id) values (?,?,?,?,?)"
214 $attribution_to_insert->execute( $attribution_id, undef, undef,
215 $project_id, undef );
217 # warn"$bac_id attributed with attribution id $attribution_id.\n";
218 return $attribution_id;
220 else {
222 #remove the attribution of a bac to a chromosome
223 $self->do( <<EOQ, undef, 'genomic', 'clone', 'clone_id', $bac_id );
224 delete from attribution where database_name = ? and table_name = ? and primary_key_column_name = ? and row_id = ?
227 #the attribution_to table will be taken care of by the
228 #ON DELETE CASCADE of its attribution_id foreign key column
229 return;
233 sub get_project_associated_with_bac {
234 my $self = shift;
235 my ($bac_id) = @_;
236 my $project_query = $self->prepare(
237 "select project_id from attribution inner join attribution_to on attribution.attribution_id=attribution_to.attribution_id where database_name='genomic' and table_name='clone' and row_id=?"
239 $project_query->execute($bac_id);
240 my ($proj_id) = $project_query->fetchrow_array();
242 #warn"\n\nASSOCIATED WITH PROJECT: '$proj_id'\n\n";
243 return $proj_id;
248 package CXGN::Metadata::Attribution;
250 use base('CXGN::Metadata');
252 sub new {
253 my $class = shift;
254 my $self = $class->SUPER::new(@_);
255 @{ $self->{attributions} } = ();
257 $self->_fetch_attribution_data();
259 return $self;
262 sub _fetch_attribution_data {
263 my $self = shift;
265 #print "ROW-ID: ".$self->get_row_id()."\n";
266 my $q = "
267 SELECT
268 person_id,
269 sp_organization.name,
270 sp_project.name,
271 role_name,
272 attribution.attribution_id
273 FROM
274 attribution
275 inner join attribution_to using(attribution_id)
276 left join roles using (role_id)
277 left join sgn_people.sp_organization on (organization_id=sp_organization_id)
278 left join sgn_people.sp_project on (project_id=sp_project_id)
279 WHERE
280 attribution.database_name=?
281 and attribution.table_name=?
282 and attribution.row_id=?
285 my $h = $self->prepare($q);
286 $h->execute( $self->get_db_name(), $self->get_table_name(),
287 $self->get_row_id() );
288 my %attribution;
289 while (
290 my ( $person_id, $organization, $project, $role_name, $attribution_id )
291 = $h->fetchrow_array() )
294 #print "PERSON-ID: $person_id, RPOJECT_ID: $project_id etc...\n";
295 $attribution{person} = CXGN::People::Person->new( $self, $person_id );
296 $attribution{organization} = $organization;
297 $attribution{project} = $project;
299 #print "project name: ".($attribution{project}->get_name())."\n";
300 $attribution{role} = $role_name;
302 #print "Attribution_id: $attribution_id\n";
303 push @{ $self->{attributions} }, \%attribution;
308 =head2 function get_attributions
310 Synopsis:
311 Arguments:
312 Returns: a list of hashes containing objects, hash keys are person, project, organization and a string, role.
313 Side effects:
314 Description:
316 =cut
318 sub get_attributions {
319 my $self = shift;
320 return @{ $self->{attributions} };