seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / Genomes.pm
blob416f5c80c1c9fadd8a3f1f786d1ad3edd553917c
1 package SGN::Controller::Genomes;
3 =head1 NAME
5 SGN::Controller::Genomes - controller for genome portal pages
7 =cut
9 use Moose;
10 use namespace::autoclean;
12 use List::MoreUtils 'uniq';
14 BEGIN{ extends 'Catalyst::Controller' }
16 with 'Catalyst::Component::ApplicationAttribute';
18 =head1 PUBLIC ACTIONS
20 =head1 list_genomes
22 Public path: /genomes
24 =cut
26 sub list_genomes : Path( '/genomes' ) Args(0) {
27 my ( $self, $c ) = @_;
29 my $schema = $c->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado' );
31 $c->stash(
33 genome_organisms => [
34 $schema->resultset('Organism::Organismprop')
35 ->search({ 'type.name' => 'genome_page' }, { join => 'type' })
36 ->search_related('organism')
37 ->all
40 template => '/genomes/index.mas',
44 =head2 view_genome_data
46 Public path: /organism/<organism id or name>/genome
48 Queries for Biosource bs_sample records of type 'sequence_collection'
49 or 'genome_annotation_set', and displays them with file download
50 links.
52 =cut
54 sub view_genome_data : Chained('/organism/find_organism') PathPart('genome') {
55 my ( $self, $c ) = @_;
57 my $organism = $c->stash->{organism};
58 $c->throw_404 unless $organism && $organism->search_related('organismprops',
59 { 'type.name' => 'genome_page', 'me.value' => 1 },
60 { join => 'type' },
61 )->count;
63 (my $template_name = '/genomes/'.$organism->species.'.mas') =~ s/ /_/g;
65 # find assemblies for this organism
66 $c->stash->{assembly_list} = my $assembly_list = [
67 map {
68 my $s = $_;
69 my $h = $self->_bs_sample_to_display_hashref( $c, $s );
70 # also add the associated annotation_sets to it
71 $h->{annotation_sets} = [ $self->assembly_annotations( $s ) ];
73 } $self->assemblies_for_organism( $organism )->all
76 # find annotation sets for this organism
77 $c->stash->{annotation_list} = [
78 map {
79 $self->_bs_sample_to_display_hashref( $c, $_ );
81 # annotation sets are the ones that were found related to the
82 # assemblies, plus ones queried from the db by organism, made
83 # unique, and sorted
84 sort { $a->metadata && $a->metadata->create_date->epoch <=> $b->metadata && $b->metadata->create_date->epoch
85 || $a->sample_name cmp $b->sample_name
87 $self->uniq_bs_samples(
88 ( map { @{ $_->{annotation_sets} || [] } } @$assembly_list ),
89 $self->annotation_sets_for_organism( $organism )->all
93 # choose which template we will use
94 $c->stash->{template} =
95 $c->view('Mason')->component_exists( $template_name )
96 ? $template_name : '/genomes/default.mas';
99 =head2 view_inbred_genomes
101 Public path: /organism/Solanum_lycopersicum/inbred_genomes
104 =cut
106 sub view_inbred_genome : Path('/organism/Solanum_lycopersicum/inbred_genomes') {
107 my ( $self, $c ) = @_;
109 $c->stash->{template} = '/genomes/Solanum_lycopersicum/inbreds.mas';
112 =head2 view_tomato_150
114 Public path: /organism/Solanum_lycopersicum/tomato_150
117 =cut
119 sub view_tomato_150 : Path('/organism/Solanum_lycopersicum/tomato_150') {
120 my ( $self, $c ) = @_;
122 $c->stash->{template} = '/genomes/Solanum_lycopersicum/tomato150.mas';
125 =head2 view_tomato_360
127 Public path: /organism/Solanum_lycopersicum/tomato_360
130 =cut
132 sub view_tomato_360 : Path('/organism/Solanum_lycopersicum/tomato_360') {
133 my ( $self, $c ) = @_;
135 $c->stash->{template} = '/genomes/Solanum_lycopersicum/tomato360.mas';
138 ####### helper methods ##########
140 sub uniq_bs_samples {
141 my $self = shift;
142 my %seen;
143 return grep !$seen{$_->sample_id}++, @_
146 sub _bs_sample_to_display_hashref {
147 my ( $self, $c, $bs_sample ) = @_;
149 return {
150 name => $c->view('BareMason')->render( $c, '/biosource/sample_link.mas', { sample => $bs_sample } ),
151 date => $bs_sample->metadata ? $bs_sample->metadata->create_date : undef,
152 description => $bs_sample->description,
153 files => [
154 map +{ text => $_->basename,
155 url => '/metadata/file/'.$_->file_id.'/download',
156 description => $_->comment,
158 $self->assembly_files( $bs_sample )
163 sub _annotates_cvterms_rs {
164 my ( $self, $row ) = @_;
165 my $schema = $row ? $row->result_source->schema : $self->_app->dbic_schema('CXGN::Biosource::Schema');
167 return $schema->resultset('Cv::Cvterm')
168 ->search_rs({ name => 'annotates' });
171 sub assembly_annotations {
172 my ( $self, $sample_row ) = @_;
173 return
174 $sample_row->search_related('bs_sample_relationship_objects',
175 { 'me.type_id' =>
176 { -in => $self->_annotates_cvterms_rs( $sample_row )
177 ->get_column('cvterm_id')
178 ->as_query,
182 ->search_related('subject')
183 ->all
187 sub assembly_files {
188 my ( $self, $sample_row ) = @_;
190 return
191 $sample_row->search_related('bs_sample_files')
192 ->search_related('file')
193 ->all
197 sub assemblies_for_organism {
198 my ( $self, $organism ) = @_;
200 my $schema = $organism->result_source->schema;
202 $schema->resultset('BsSample')
203 ->search({
204 type_id => { -in => [ uniq map $_->cvterm_id, @{$self->_assembly_cvterms( $schema )}] },
205 organism_id => $organism->organism_id,
209 sub annotation_sets_for_organism {
210 my ( $self, $organism ) = @_;
211 my $schema = $organism->result_source->schema;
213 $schema->resultset('BsSample')
214 ->search({
215 type_id => { -in => [ uniq map $_->cvterm_id, @{$self->_annotation_cvterms( $schema )}] },
216 organism_id => $organism->organism_id,
220 # get the RS of types (i.e. cvterms) that would indicate that we
221 # should list a certain biosource
222 # currently defined as 'all SO children '
223 sub _assembly_cvterms {
224 my ( $self, $schema ) = @_;
225 my $sc = $schema->resultset('Cv::Cv')
226 ->search({ 'me.name' => ['sequence','SO'] })
227 ->search_related('cvterms', {
228 'cvterms.name' => 'sequence_collection',
231 my @terms = $sc->all;
232 return [ @terms, map $_->recursive_children, @terms ];
235 sub _annotation_cvterms {
236 my ( $self, $schema ) = @_;
237 my $t = $schema->resultset('Cv::Cvterm')
238 ->search({ 'name' => 'genome_annotation_set' });
240 my @terms = $t->all;
241 return [ @terms, map $_->recursive_children, @terms ];