clean
[sgn.git] / lib / SGN / View / Stock.pm
blobb6c22f4185739277afe36cfe0c2f8869830ca6ca
1 package SGN::View::Stock;
2 use base 'Exporter';
3 use strict;
4 use warnings;
6 our @EXPORT_OK = qw/
7 stock_link organism_link cvterm_link
8 stock_table related_stats
9 stock_organisms stock_types breeding_programs
11 our @EXPORT = ();
14 sub stock_link {
15 my ($stock) = @_;
16 my $name = $stock->uniquename;
17 my $id = $stock->stock_id;
18 return qq{<a href="/stock/$id/view">$name</a>};
21 sub organism_link {
22 my ($organism) = @_;
23 my $id = $organism->organism_id;
24 my $species = $organism->species;
25 return <<LINK;
26 <span class="species_binomial">
27 <a href="/chado/organism.pl?organism_id=$id">$species</a>
28 LINK
31 sub cvterm_link {
32 my ($cvterm) = @_;
33 my $name = $cvterm->name;
34 my $id = $cvterm->cvterm_id;
35 return qq{<a href="/cvterm/$id/view">$name</a>};
38 sub stock_table {
39 my ($stocks) = @_;
40 my $data = [];
41 for my $s (@$stocks) {
42 # Add a row for every stock
43 push @$data, [
44 cvterm_link($s->type),
45 stock_link($s),
49 return $data;
53 sub related_stats {
54 my ($stocks) = @_;
55 my $stats = { };
56 my $total = scalar @$stocks;
57 for my $s (@$stocks) {
58 $stats->{cvterm_link($s->type)}++;
60 my $data = [ ];
61 for my $k (sort keys %$stats) {
62 push @$data, [ $stats->{$k}, $k ];
64 if( 1 < scalar keys %$stats ) {
65 push @$data, [ $total, "<b>Total</b>" ];
67 return $data;
70 sub breeding_programs {
71 my ($schema) = @_;
72 return [
73 [ 0, '' ],
74 map [ $_->project_id, $_->name ],
75 $schema
76 ->resultset('Project::Project')->search(
77 { 'type.name' => 'breeding_program',
78 },
80 join => { 'projectprops' => 'type' },
81 select => [qw[ me.project_id me.name ]],
82 distinct => 1,
83 order_by => 'me.name',
89 sub stock_organisms {
90 my ($schema) = @_;
91 return [
92 [ 0, '' ],
93 map [ $_->organism_id, $_->species ],
94 $schema
95 ->resultset('Stock::Stock')
96 ->search_related('organism' , {}, {
97 select => [qw[ organism.organism_id species ]],
98 distinct => 1,
99 order_by => 'species',
104 sub stock_types {
105 my ($schema) = @_;
107 my $ref = [
108 map [$_->cvterm_id,$_->name],
109 $schema
110 ->resultset('Stock::Stock')
111 ->search_related(
112 'type',
114 { select => [qw[ cvterm_id type.name ]],
115 group_by => [qw[ cvterm_id type.name ]],
116 order_by => 'type.name',
120 # add an empty option
121 unshift @$ref , ['0', ''];
122 return $ref;
125 sub stock_dbxrefprops {
126 my $stock_dbxref = shift;
127 my $props = $stock_dbxref->stock_dbxrefprops;
128 while (my $p = $props->next ) {
129 my $value = $p->value ;
130 my $type = $p->type->name;
131 my $accession = $p->type->dbxref->accession;
132 my $db_name = $p->type->dbxref->db->name;
137 ######
139 ######