Repository navigation
Expand file tree
/
Copy pathMetaDataCollection.pm
More file actions
280 lines (225 loc) · 7.09 KB
/
Copy pathMetaDataCollection.pm
File metadata and controls
280 lines (225 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package JobDB::MetaDataCollection;
use strict;
use warnings;
use Data::Dumper;
sub last_id {
my ($self) = @_;
my $dbh = $self->_master()->db_handle();
my $sth = $dbh->prepare("SELECT max(ID + 0) FROM MetaDataCollection");
$sth->execute;
my $result = $sth->fetchrow_arrayref();
my ($id) = $result->[0] =~/^(\d+)/;
return $id || "0" ;
}
=item * B<data> ()
Returns a hash of all MDE keys and values for a collection.
If a key is given, returns only hash of specified key, value pair.
Sets a value if key and value is given (return true or false if works)
=cut
sub data {
my ($self, $tag, $value) = @_;
my $dbh = $self->_master->db_handle;
my $sth;
if (defined($value) and $tag) {
if (ref $value) {
print STDERR "ERROR: invalid value type for $tag ($value) \n";
print STDERR Dumper $value;
return 0;
}
my $jstat = $self->_master->MetaDataEntry->get_objects({
collection => $self,
tag => $tag
});
if (ref $jstat and scalar @$jstat) {
$jstat->[0]->value($value);
} else {
$jstat = $self->_master->MetaDataEntry->create({
collection => $self,
tag => $tag,
value => $value
});
}
return 1;
}
elsif ($tag) {
$sth = $dbh->prepare("SELECT tag, value FROM MetaDataEntry where collection=".$self->_id." and tag='$tag'");
} else {
$sth = $dbh->prepare("SELECT tag, value FROM MetaDataEntry where collection=".$self->_id);
}
$sth->execute;
my $results = $sth->fetchall_arrayref();
my $rhash = {};
foreach my $set (@$results) {
if (defined($set->[1]) && ($set->[1] =~ /\S/)) {
$set->[1] =~ s/(envo|gaz)://i;
$rhash->{$set->[0]} = $set->[1];
}
}
return $rhash;
}
=item * B<value_set> ()
Returns a list of values for given tag
=cut
sub value_set {
my ($self, $tag) = @_;
my $set = [];
my $dbh = $self->_master->db_handle;
my $results = $dbh->selectcol_arrayref("SELECT value FROM MetaDataEntry where collection=".$self->_id." and tag='$tag'");
if ($results && (@$results > 0)) {
foreach my $v (@$results) {
if (defined($v) && ($v =~ /\S/)) { push @$set, $v; }
}
}
return $set;
}
=item * B<project> ()
Returns project that have this collection
=cut
sub project {
my ($self) = @_;
my $proj_coll = $self->_master->ProjectCollection->get_objects({collection => $self});
return (@$proj_coll > 0) ? $proj_coll->[0]->project : undef;
}
=item * B<jobs> ()
Returns array of jobs that have this collection
=cut
sub jobs {
my ($self) = @_;
my $jobs = [];
if ($self->type eq 'sample') {
$jobs = $self->_master->Job->get_objects({sample => $self, viewable => 1});
}
elsif ($self->type eq 'library') {
$jobs = $self->_master->Job->get_objects({library => $self, viewable => 1});
}
elsif (($self->type eq 'ep') && $self->parent && ref($self->parent)) {
$jobs = $self->_master->Job->get_objects({sample => $self->parent, viewable => 1});
}
return $jobs;
}
=item * B<metagenome_ids> ()
Returns array of metagenome_ids that have this collection
=cut
sub metagenome_ids {
my ($self) = @_;
my $mgids = [];
my $dbh = $self->_master->db_handle;
if ($self->type eq 'sample') {
$mgids = $dbh->selectcol_arrayref("SELECT metagenome_id FROM Job where sample=".$self->_id." and viewable=1");
}
elsif ($self->type eq 'library') {
$mgids = $dbh->selectcol_arrayref("SELECT metagenome_id FROM Job where library=".$self->_id." and viewable=1");
}
elsif (($self->type eq 'ep') && $self->parent && ref($self->parent)) {
$mgids = $dbh->selectcol_arrayref("SELECT metagenome_id FROM Job where sample=".$self->parent->_id." and viewable=1");
}
return $mgids;
}
=item * B<children> ()
Returns array of children collections,
if type given only returns children of that type
=cut
sub children {
my ($self, $type) = @_;
my $children = [];
if ($type) {
$children = $self->_master->MetaDataCollection->get_objects({parent => $self, type => $type});
} else {
$children = $self->_master->MetaDataCollection->get_objects({parent => $self});
}
return $children;
}
sub category_type {
my ($self, $type) = @_;
my $ct = '';
if ($type eq 'library') {
$ct = $self->data('investigation_type')->{'investigation_type'};
}
elsif ($type eq 'ep') {
$ct = $self->data('env_package')->{'env_package'} || $self->parent->data('env_package')->{'env_package'};
}
else {
$ct = $self->type;
}
return $ct;
}
sub lib_type {
my ($self) = @_;
return $self->category_type('library');
}
sub ep_type {
my ($self) = @_;
return $self->category_type('ep');
}
sub delete_all {
my ($self) = @_;
$self->delete_children;
$self->delete_entries;
$self->delete_project;
$self->delete_jobentry;
}
sub delete_jobentry {
my ($self) = @_;
foreach my $job ( @{ $self->_master->Job->get_objects({sample => $self}) } ) {
$job->sample(undef);
$job->library(undef);
}
}
sub delete_project {
my ($self) = @_;
foreach my $pc ( @{ $self->_master->ProjectCollection->get_objects({collection => $self}) } ) {
$pc->delete;
}
}
sub delete_entries {
my ($self) = @_;
foreach my $mde ( @{ $self->_master->MetaDataEntry->get_objects({collection => $self}) } ) {
$mde->delete;
}
}
sub delete_children {
my ($self) = @_;
foreach my $child ( @{ $self->children } ) {
$child->delete_all;
$child->delete;
}
}
sub xml {
my ($self) = @_;
my $jobs = [];
push @$jobs , $self->job if ($self->job) ;
push @$jobs , @{ $self->_master->Job->get_objects( {sample => $self, viewable => 1} ) };
my $pjs = {};
map { map {$pjs->{ $_->project->id } = $_->project } @{ $self->_master->ProjectJob->get_objects( { job => $_ } ) } } @$jobs;
my $xml = "<?xml version=\"1.0\" ?>\n" ;
$xml .= "<sample>\n";
$xml .= "<name>". ($self->name || $self->data('sample_name') || "unknown" ) ."</name>\n";
$xml .= "<sample_id namespace='MG-RAST'>". $self->ID ."</sample_id>\n";
foreach my $pid (keys %$pjs){
my $p = $pjs->{$pid};
$xml .="<project>\n";
$xml .="\t<name>".$p->name."</name>\n";
$xml .="\t<project_id namespace='MG-RAST'>".$p->id."</project_id>\n";
$xml .="</project>\n";
}
foreach my $j (@$jobs){
print Dumper $jobs unless (ref $j);
$xml .="<metagenome>\n";
$xml .= "\t<metagenome_id namespace='MG-RAST'>". $j->metagenome_id."</metagenome_id>\n";
$xml .= "\t<name>". $j->name."</name>\n";
$xml .="</metagenome>\n";
}
$xml .= "<submitter>". ( $self->creator ? $self->creator->name : 'ERROR:no submitter') ."</submitter>\n";
$xml .= "<submitted>". $self->entry_date ."</submitted>\n";
$xml .= "<source>". $self->source ."</source>\n" if ($self->source);
$xml .= "<url>". $self->source ."</url>\n" if ($self->url);
my $data = $self->_master->MetaDataEntry->get_objects( { collection => $self } );
foreach my $md (@$data){
$xml .= "<".$md->tag.">".$md->value."</".$md->tag.">\n";
}
$xml .= "</sample>\n";
return $xml ;
}
1;
# this class is a stub, this file will not be automatically regenerated
# all work in this module will be saved