fix test with new description field.
[sgn.git] / lib / CXGN / DbStats.pm
blob9a440f300104809b1cd01c9369eb754e0077110c
2 package CXGN::DbStats;
4 use Moose;
6 has 'dbh' => (isa => 'Ref', is => 'rw');
7 has 'start_date' => ( is => 'rw', isa => 'Str', default => '1900-01-01' );
8 has 'end_date' => ( is => 'rw', isa => 'Str', default => '2100-12-31' );
9 has 'include_dateless_items' => (is => 'rw', isa => 'Bool', default => 1 );
11 # retrieve all trials grouped by trial type
13 sub trial_types {
14 my $self = shift;
15 my $start_date = shift || $self->start_date();
16 my $end_date = shift || $self->end_date();
17 my $include_dateless_items = shift;
19 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
20 my $datelessq = "";
22 if ($include_dateless_items == 1) {
23 $datelessq = " project.create_date IS NULL OR ";
26 my $q = "SELECT cvterm.name, count(*) from project join projectprop using(project_id) join cvterm on(projectprop.type_id=cvterm_id) JOIN cv USING (cv_id) WHERE ( $datelessq (project.create_date > ? and project.create_date < ?) ) and cv_id=(SELECT cv_id FROM cv WHERE name='project_type') GROUP BY cvterm.name ORDER BY count(*) desc";
27 my $h = $self->dbh->prepare($q);
28 $h->execute($start_date, $end_date);
29 return $h->fetchall_arrayref();
32 # retrieve all trials grouped by breeding programs
34 sub trial_count_by_breeding_program {
35 my $self = shift;
36 my $start_date = shift || $self->start_date();
37 my $end_date = shift || $self->end_date();
38 my $include_dateless_items = shift;
40 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
42 my $datelessq = "";
43 if ($include_dateless_items == 1) {
44 $datelessq = " trial.create_date IS NULL OR ";
46 my $q = "select project.name, count(*) from project join project_relationship on (project.project_id=project_relationship.object_project_id) join project as trial on(subject_project_id=trial.project_id) join projectprop on(project.project_id = projectprop.project_id) join cvterm on (projectprop.type_id=cvterm.cvterm_id) join projectprop as trialprop on(trial.project_id = trialprop.project_id) join cvterm as trialcvterm on(trialprop.type_id=trialcvterm.cvterm_id) where ( $datelessq ( trial.create_date > ? and trial.create_date < ?)) and cvterm.name='breeding_program' and trialcvterm.name in (SELECT cvterm.name FROM cvterm join cv using(cv_id) WHERE cv.name='project_type') group by project.name order by count(*) desc";
47 my $h = $self->dbh->prepare($q);
48 $h->execute($start_date, $end_date);
49 return $h->fetchall_arrayref();
53 sub phenotype_count_by_breeding_program {
54 my $self = shift;
55 my $start_date = shift || $self->start_date();
56 my $end_date = shift || $self->end_date();
57 my $include_dateless_items = shift;
59 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
61 my $datelessq = "";
62 if ($include_dateless_items == 1) {
63 $datelessq = " trial.create_date IS NULL OR ";
66 my $q = "select project.name, count(*) from project join project_relationship on (project.project_id=project_relationship.object_project_id) join project as trial on(subject_project_id=trial.project_id) join projectprop on(project.project_id = projectprop.project_id) join cvterm on (projectprop.type_id=cvterm.cvterm_id) join projectprop as trialprop on(trial.project_id = trialprop.project_id) join cvterm as trialcvterm on(trialprop.type_id=trialcvterm.cvterm_id) join nd_experiment_project on(trial.project_id=nd_experiment_project.project_id) join nd_experiment_phenotype using(nd_experiment_id) where ( $datelessq ( trial.create_date > ? and trial.create_date < ?)) and cvterm.name='breeding_program' and trialcvterm.name in (SELECT cvterm.name FROM cvterm join cv using(cv_id) WHERE cv.name='project_type') group by project.name order by count(*) desc";
67 my $h = $self->dbh->prepare($q);
68 $h->execute($start_date, $end_date);
69 return $h->fetchall_arrayref();
73 # retrieve all the traits measured with counts
75 sub traits {
76 my $self = shift;
77 my $start_date = shift || $self->start_date();
78 my $end_date = shift || $self->end_date();
79 my $include_dateless_items = shift;
81 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
83 my $datelessq;
84 if ($include_dateless_items == 1) {
85 $datelessq = " create_date IS NULL OR ";
88 my $q = "select cvterm.name, count(*) from phenotype join cvterm on (observable_id=cvterm_id) where ( $datelessq ( create_date > ? and create_date < ? )) group by cvterm.name order by count(*) desc";
89 my $h = $self->dbh->prepare($q);
90 $h->execute($start_date, $end_date);
91 return $h->fetchall_arrayref();
94 sub stocks {
95 my $self = shift;
96 my $start_date = shift || $self->start_date();
97 my $end_date = shift || $self->end_date();
98 my $include_dateless_items = shift;
100 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
102 my $datelessq;
103 if ($include_dateless_items == 1) {
104 $datelessq = " create_date IS NULL OR ";
107 my $q = "SELECT cvterm.name, count(*) FROM stock join cvterm on(type_id=cvterm_id) WHERE ( $datelessq ( create_date > ? and create_date < ? )) GROUP BY cvterm.name ORDER BY count(*) desc";
108 my $h = $self->dbh->prepare($q);
109 $h->execute($start_date, $end_date);
110 return $h->fetchall_arrayref();
113 sub projects {
114 my $self = shift;
116 my $start_date = shift || $self->start_date();
117 my $end_date = shift || $self->end_date();
118 my $include_dateless_items = shift;
120 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
122 my $datelessq;
123 if ($include_dateless_items == 1) {
124 $datelessq = " create_date IS NULL OR ";
127 my $q = "SELECT project.project_id, project.name FROM project join projectprop using(project_id) where ( $datelessq ( project.create_date > ? and project.create_date < ? )) group by project.project_id, project.name";
129 my $h = $self->dbh->prepare($q);
131 $h->execute($start_date, $end_date);
137 sub activity {
138 my $self = shift;
140 my @counts;
141 my @weeks;
142 foreach my $week (0..51) {
143 my $days = $week * 7;
144 my $previous_days = ($week + 1) * 7;
145 my $q = "SELECT count(*) FROM nd_experiment WHERE create_date > (now() - INTERVAL '$previous_days DAYS') and create_date < (now() - INTERVAL '$days DAYS')";
146 my $h = $self->dbh()->prepare($q);
147 $h->execute();
148 my ($count) = $h->fetchrow_array();
150 print STDERR "Activity in week $week = $count\n";
152 push @counts, { letter => $week, frequency => $count };
153 #push @weeks, $week;
155 return \@counts;
160 sub stock_stats {
161 my $self = shift;
162 my $start_date = shift || $self->start_date();
163 my $end_date = shift || $self->end_date();
164 my $include_dateless_items = shift;
166 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
168 my $datelessq;
169 if ($include_dateless_items == 1) {
170 $datelessq = " create_date IS NULL OR ";
173 my $dbh = $self->dbh();
175 my $query = "SELECT distinct(cvterm.name), count(*) FROM stock join cvterm on(type_id=cvterm_id) WHERE ( $datelessq ( create_date > ? and create_date < ? )) group by cvterm.name";
177 my $h = $dbh->prepare($query);
178 $h->execute($start_date, $end_date);
180 my @data;
181 while (my ($stock_type, $count) = $h->fetchrow_array()) {
182 push @data, [ $stock_type, $count ];
185 return \@data;
189 sub accession_count_by_breeding_program {
190 my $self = shift;
191 my $start_date = shift || $self->start_date();
192 my $end_date = shift || $self->end_date();
193 my $include_dateless_items = shift;
195 my $dbh = $self->dbh();
197 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
199 my $datelessq;
200 if ($include_dateless_items == 1 ) {
201 $datelessq = " accession.create_date IS NULL OR ";
203 my $query = "select breeding_program.name, count(*) from project as breeding_program join project_relationship on(breeding_program.project_id=project_relationship.object_project_id) join project as trial on(project_relationship.subject_project_id=trial.project_id) join projectprop on (breeding_program.project_id=projectprop.project_id) join nd_experiment_project on (trial.project_id=nd_experiment_project.project_id) join nd_experiment_stock using(nd_experiment_id) join stock using(stock_id) join stock_relationship on (stock.stock_id=stock_relationship.object_id) join stock as accession on (accession.stock_id=stock_relationship.subject_id) where ( $datelessq ( accession.create_date > ? and accession.create_date < ? ) ) and accession.type_id = (select cvterm_id from cvterm where name='accession') and projectprop.type_id = (select cvterm_id from cvterm where name='breeding_program') group by breeding_program.name order by count(*) desc";
205 my $h = $dbh->prepare($query);
206 $h->execute($start_date, $end_date);
208 my @data;
209 while (my ($bp, $count) = $h-> fetchrow_array()) {
210 push @data, [ $bp, $count ];
212 return \@data;
215 sub accession_count_by_breeding_program_without_trials {
216 my $self = shift;
217 my $start_date = shift || $self->start_date();
218 my $end_date = shift || $self->end_date();
219 my $include_dateless_items = shift;
221 my $dbh = $self->dbh();
223 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
225 my $datelessq;
226 if ($include_dateless_items == 1 ) {
227 $datelessq = " stock.create_date IS NULL OR ";
229 my $query = "select distinct(value), count(*) from stock join stockprop using(stock_id) join cvterm on (stockprop.type_id=cvterm.cvterm_id) where cvterm.name = 'organization' and ( $datelessq ( stock.create_date > ? and stock.create_date < ? )) group by value;";
231 my $h = $dbh->prepare($query);
232 $h->execute($start_date, $end_date);
234 my @data;
235 while (my ($bp, $count) = $h-> fetchrow_array()) {
236 push @data, [ $bp, $count ];
238 return \@data;
241 sub plot_count_by_breeding_program {
242 my $self = shift;
243 my $start_date = shift || $self->start_date();
244 my $end_date = shift || $self->end_date();
245 my $include_dateless_items = shift;
247 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
249 my $dbh = $self->dbh();
251 my $datelessq;
252 if ($include_dateless_items == 1) {
253 $datelessq = " stock.create_date IS NULL OR ";
255 my $query = "select breeding_program.name, count(*) from project as breeding_program join project_relationship on(breeding_program.project_id=project_relationship.object_project_id) join project as trial on(project_relationship.subject_project_id=trial.project_id) join projectprop on (breeding_program.project_id=projectprop.project_id) join nd_experiment_project on (trial.project_id=nd_experiment_project.project_id) join nd_experiment_stock using(nd_experiment_id) join stock using(stock_id) where ( $datelessq ( stock.create_date > ? and stock.create_date < ? ) ) and stock.type_id = (select cvterm_id from cvterm where name='plot') and projectprop.type_id = (select cvterm_id from cvterm where name='breeding_program') group by breeding_program.name order by count(*) desc";
257 my $h = $dbh->prepare($query);
258 $h->execute($start_date, $end_date);
260 my @data;
261 while (my ($bp, $count) = $h-> fetchrow_array()) {
262 push @data, [ $bp, $count ];
264 return \@data;
267 sub germplasm_count_with_pedigree {
268 my $self = shift;
270 my $start_date = shift || $self->start_date();
271 my $end_date = shift || $self->end_date();
272 my $include_dateless_items = shift;
274 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
276 my $dbh = $self->dbh();
278 my $datelessq;
279 if ($include_dateless_items == 1) {
280 $datelessq = " stock.create_date IS NULL OR ";
284 my $query = "SELECT count(*) FROM stock join cvterm on(type_id=cvterm_id) join stock_relationship on(stock_id=object_id) WHERE stock_relationship.type_id in (select cvterm_id FROM cvterm where name='female_parent' or name='male_parent') and cvterm.name='accession' and ( $datelessq ( stock.create_date > ? and stock.create_date < ? )) group by cvterm.name";
286 my $h = $dbh->prepare($query);
287 $h->execute($start_date, $end_date);
289 my @data;
290 while (my ($stock_type, $count) = $h->fetchrow_array()) {
291 push @data, [ 'accessions with pedigree', $count ];
294 return \@data;
297 sub germplasm_count_with_phenotypes {
298 my $self = shift;
300 my $start_date = shift || $self->start_date();
301 my $end_date = shift || $self->end_date();
302 my $include_dateless_items = shift;
304 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
306 my $dbh = $self->dbh();
308 my $datelessq;
309 if ($include_dateless_items == 1) {
310 $datelessq = " stock.create_date IS NULL OR ";
313 my $query = "SELECT count(*) FROM stock join cvterm on(type_id=cvterm_id) join stock_relationship on (stock.stock_id=object_id) join stock as plot on (stock_relationship.subject_id=plot.stock_id) join nd_experiment_stock on(plot.stock_id=nd_experiment_stock.stock_id) join nd_experiment_phenotype on(nd_experiment_stock.nd_experiment_id=nd_experiment_phenotype.nd_experiment_id) WHERE cvterm.name='accession' and ( $datelessq ( stock.create_date > ? and stock.create_date < ? )) group by cvterm.name";
315 my $h = $dbh->prepare($query);
316 $h->execute($start_date, $end_date);
318 my @data;
319 while (my ($stock_type, $count) = $h->fetchrow_array()) {
320 push @data, [ 'accessions with pedigree', $count ];
323 return \@data;
326 sub germplasm_count_with_genotypes {
327 my $self = shift;
329 my $start_date = shift || $self->start_date();
330 my $end_date = shift || $self->end_date();
331 my $include_dateless_items = shift;
333 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
335 my $dbh = $self->dbh();
337 my $datelessq;
338 if ($include_dateless_items == 1) {
339 $datelessq = " stock.create_date IS NULL OR ";
342 # genotypes associated with accessions
344 my $query = "SELECT count(*) FROM stock join cvterm on(type_id=cvterm_id) join nd_experiment_stock on(stock.stock_id=nd_experiment_stock.stock_id) join nd_experiment_genotype on(nd_experiment_stock.nd_experiment_id=nd_experiment_genotype.nd_experiment_id) WHERE cvterm.name='accession' and ( $datelessq (stock.create_date > ? and stock.create_date < ?)) group by cvterm.name";
347 my $h = $dbh->prepare($query);
348 $h->execute($start_date, $end_date);
351 my @data;
352 while (my ($stock_type, $count) = $h->fetchrow_array()) {
353 push @data, [ 'accessions with pedigree', $count ];
356 # genotypes associated with plants
358 # gentoypes associated with plots, etc.??? need to do this separately?
360 return \@data;
364 sub phenotype_count_per_trial {
365 my $self = shift;
367 my $start_date = shift || $self->start_date();
368 my $end_date = shift || $self->end_date();
369 my $include_dateless_items = shift;
371 if (!defined($include_dateless_items)) { $include_dateless_items = $self->include_dateless_items(); }
373 my $dbh = $self->dbh();
375 my $datelessq;
376 if ($include_dateless_items == 1) {
377 $datelessq = " phenotype.create_date IS NULL OR ";
380 my $q = "select project.project_id, project.name, cvterm.name, count(cvterm_id) from project join nd_experiment_project using(project_id) join nd_experiment_phenotype using(nd_experiment_id) join phenotype using(phenotype_id) join cvterm on(cvalue_id=cvterm_id) where ( $datelessq ( phenotype.create_date > ? and phenotype.create_date < ? )) group by project.project_id, project.name, cvterm.name";
382 my $h = $dbh->prepare($q);
383 $h->execute( $start_date, $end_date);
385 return $h->fetch