2 use CXGN
::DB
::Connection
;
5 package CXGN
::Metadata
;
6 use base
('CXGN::DB::Connection');
11 my $table_name = 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);
23 =head2 function get_db_name
35 return $self->{db_name
};
38 =head2 function set_db_name
50 $self->{db_name
} = shift;
53 =head2 function get_table_name
65 return $self->{table_name
};
68 =head2 function set_table_name
80 $self->{table_name
} = shift;
83 =head2 function get_id
95 return $self->{row_id
};
98 =head2 function set_row_id
110 $self->{row_id
} = 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
},
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
{
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
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();
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;
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
183 #warn"Project not found.\n";
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);
203 $self->prepare("select currval('attribution_attribution_id_seq')");
204 $id_query->execute();
205 my @id = $id_query->fetchrow_array();
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;
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
233 sub get_project_associated_with_bac
{
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";
248 package CXGN
::Metadata
::Attribution
;
250 use base
('CXGN::Metadata');
254 my $self = $class->SUPER::new
(@_);
255 @
{ $self->{attributions
} } = ();
257 $self->_fetch_attribution_data();
262 sub _fetch_attribution_data
{
265 #print "ROW-ID: ".$self->get_row_id()."\n";
269 sp_organization.name,
272 attribution.attribution_id
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)
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() );
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
312 Returns: a list of hashes containing objects, hash keys are person, project, organization and a string, role.
318 sub get_attributions
{
320 return @
{ $self->{attributions
} };