Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / SGN / Controller / Clone / Genomic.pm
blob5f9c2c248e6a7c464f7dd0b764250519a586c4f5
1 =head1 NAME
3 SGN::Controller::Clone::Genomic - Catalyst controller for dealing with
4 genomic clone data
6 =cut
8 package SGN::Controller::Clone::Genomic;
9 use namespace::autoclean;
10 use Moose;
11 use Carp;
13 use Memoize;
14 use File::Basename;
16 use CXGN::DB::DBICFactory;
17 use CXGN::Genomic::Clone;
19 use CXGN::PotatoGenome::Config;
20 use CXGN::PotatoGenome::FileRepository;
21 use CXGN::TomatoGenome::Config;
23 BEGIN { extends 'SGN::Controller::Clone' }
25 with 'Catalyst::Component::ApplicationAttribute';
27 =head1 ACTIONS
29 =head2 clone_annot_download
31 Public Path: /genomic/clone/<clone_id>/annotation/download?set=<set_name>&format=<format>
33 Download an annotation set for a particular clone.
35 =cut
37 sub clone_annot_download :Chained('get_clone') :PathPart('annotation/download') :Args(0) {
38 my ( $self, $c, $annot ) = @_;
40 my ( $set, $format ) = @{ $c->req->query_parameters }{'set','format'};
41 $set =~ s![\\/]!!g;
42 $format =~ s/\W//g;
44 my $clone = $c->stash->{clone};
46 my %files =
47 $self->_is_tomato($clone) ? CXGN::TomatoGenome::BACPublish::sequencing_files( $clone, $c->config->{'ftpsite_root'} ) :
48 $self->_is_potato($clone) ? $self->_potato_seq_files( $c, $clone ) :
49 $c->throw_404('No annotation sets found for that clone.');
51 %files or $c->throw_404('No annotation sets found for that clone.');
53 if( my $file = $files{$set eq 'all' ? $format : $set.'_'.$format} ) {
54 $c->stash->{download_filename} = $file;
55 $c->forward('Controller::Download','download');
56 } else {
57 $c->throw_404('Annotation set not found.');
61 =head2 get_clone
63 Public path: /genomic/clone/<clone_id>
65 Chaining base for fetching a CXGN::Genomic::Clone, stashes the clone
66 object in $c->stash->{clone}
68 =cut
70 sub get_clone :Chained('/') PathPart('genomic/clone') :CaptureArgs(1) {
71 my ( $self, $c, $clone_id ) = @_;
73 $c->stash->{clone} =
74 CXGN::Genomic::Clone->retrieve( $clone_id )
75 or $c->throw_404('Clone not found.');
79 # find the chado organism for a clone
80 sub _clone_organism {
81 my ( $self, $clone ) = @_;
82 $self->_app->dbic_schema('Bio::Chado::Schema','sgn_chado')->resultset('Organism::Organism')->find( $clone->library_object->organism_id );
85 sub _is_tomato {
86 my ( $self, $clone ) = @_;
87 return lc $self->_clone_organism($clone)->species eq 'solanum lycopersicum';
89 sub _is_potato {
90 my ( $self, $clone ) = @_;
91 return lc $self->_clone_organism($clone)->species eq 'solanum tuberosum';
94 sub _clone_seq_project_name {
95 my ( $self, $clone ) = @_;
96 if( $self->_is_tomato( $clone ) ) {
97 my $chr = $clone->chromosome_num;
98 return "Chromosome $chr" if defined $chr;
99 return 'none';
100 } elsif( $self->_is_potato( $clone ) ) {
101 return $clone->seqprops->{project_country} || 'unknown';
102 } else {
103 return 'none';
107 sub _potato_seq_files {
108 my ( $self, $c, $clone, $repos_path ) = @_;
110 return unless $clone->latest_sequence_name;
111 return unless $clone->seqprops->{project_country};
113 $repos_path ||= CXGN::PotatoGenome::Config->load_locked->{repository_path};
115 return unless -d $repos_path;
117 my $repos = CXGN::PotatoGenome::FileRepository->new( $repos_path );
119 my $seq = $repos->get_file( class => 'SingleCloneSequence',
120 sequence_name => $clone->latest_sequence_name,
121 project => $clone->seqprops->{project_country},
122 format => 'fasta',
124 #warn $clone->clone_name." -> ".$seq;
125 return ( seq => $seq );
128 #make an ftp site link
129 sub _ftp_seq_repos_link {
130 my ( $self, $c, $clone ) = @_;
132 my $ftp_base = $c->config->{ftpsite_url};
134 if( $self->_is_tomato( $clone ) ) {
135 my $chr = $clone->chromosome_num;
136 my $chrnum = $chr;
137 $chrnum = 0 if $chr eq 'unmapped';
138 my $ftp_subdir = $chr ? sprintf("chr%02d/",$chrnum) : '';
139 my $project_name = $chr ? $chr eq 'unmapped' ? 'Unmapped Clones '
140 : "Chromosome $chrnum "
141 : '';
142 my $bac_dir = CXGN::TomatoGenome::Config->load_locked->{'bac_publish_subdir'};
143 return qq|<a href="$ftp_base/$bac_dir/$ftp_subdir">${project_name}Sequencing repository (FTP)</a>|;
145 elsif( $self->_is_potato( $clone ) ) {
146 my $country = $clone->seqprops->{project_country} || '';
147 my $bac_dir = CXGN::PotatoGenome::Config->load_locked->{'bac_publish_subdir'};
148 my $subdir = $country ? "$country/" : '';
149 return qq|<a href="$ftp_base/$bac_dir/$subdir">$country Sequencing repository (FTP)</a>|;
152 return '<span class="ghosted">not available</span>';
156 __PACKAGE__->meta->make_immutable;