Merge pull request #3124 from solgenomics/topic/drone_imagery_analysis
[sgn.git] / mason / feature / region_sequence.mas
blobf89ee3fa842352ba2fe512e12507cb22ce9b6d0f
1 <%args>
2     $feature
3     $blast_url        => undef
4     $max_display_size => 50_000
5 </%args>
6 <%perl>
8   my $seq;
10   #my @locs = $feature->featureloc_features(undef,{ prefetch => 'srcfeature' });
11   my @locs = $feature->featureloc_features({locgroup => 0,},{ prefetch => 'srcfeature' });
12   for my $loc ( @locs ) {
13     if( my $src = $loc->srcfeature ) {
15         my $has_seq = $src->subseq(1,1);
17         if( $has_seq ) {
18 </%perl>
19             <div style="margin: 1em 3em">
20 <%perl>
21             # if the sequence is small enough to display
22             if( $loc->fmax - $loc->fmin <= $max_display_size ) {
24                 my $seq = $src->trunc( $loc->fmin+1, $loc->fmax );
25                 $seq->desc( location_string( $loc ) );
26                 if( $loc->strand && $loc->strand == -1 ) {
27                     $seq = $seq->revcom;
28                     $seq->desc( $seq->desc.' (sequence from reverse strand)' );
29                 }
31                 $seq->id( $feature->name );
33 </%perl>
35                 <& /sequence/with_markup.mas,
36                    seq       => $seq,
37                    width     => 80,
38                    subdiv    => 10,
39                    blast_url => $blast_url,
40                  &>
42 %            } else { # otherwise, if the sequence is big
43                 <p class="warning">Sequence region (<% location_string_html($loc) %>) too large to display.</p>
44 %            }
45 %            my ( $start, $end ) = $loc->strand == -1 ? ( $loc->fmax, $loc->fmin+1 ) : ( $loc->fmin+1, $loc->fmax );
46              <% info_table_html(
47                  __tableattrs => 'width="100%"',
48                  __multicol   => 2,
49                  'Download sequence region' => $m->scomp(
50                      '/feature/sequence_download.mas',
51                      feature      => $feature,
52                      length       => $loc->fmax - $loc->fmin,
53                      download_url => '/api/v1/sequence/download/single/'.$src->feature_id.".fasta?$start..$end",
54                      ),
55                  'Get flanking sequences on '.($src->name || 'feature_'.$src->feature_id) =>
56                     $m->scomp('.flanking_download', loc => $loc ),
57                 )
58               %>
59               </div>
60 %      }
61 %   }
62 % }
64 <%once>
65    use SGN::View::Feature qw/ location_string location_string_html /;
66    use List::Util qw/ min max /;
67    use List::MoreUtils qw/ pairwise /;
68    use CXGN::Page::FormattingHelpers 'info_table_html';
70    sub upstream_downstream_options {
71        my ( $loc ) = @_;
73        my $id         = $loc->srcfeature->feature_id;
74        my $start      = $loc->fmin+1;
75        my $end        = $loc->fmax;
76        my $strand     = $loc->strand;
77        my $src_length = $loc->srcfeature->length || 1e20;
79        my @increments = ( 1000, 3000, 5000 );
81        # calculate location strings for upstream and downstream regions
82        my $upstream = [
83            map location_string(
84                $id,
85                max( 1, $start - $_ ),
86                max( 1, $start - 1  ),
87                $strand
88              ), @increments
89          ];
91        my $downstream = [
92            map location_string(
93                $id,
94                min( $end + 1,  $src_length ),
95                min( $end + $_, $src_length ),
96                $strand
97              ), @increments
98          ];
100        # upstream/downstream sense is reversed if feature is on the
101        # reverse strand
102        if( $strand == -1 ) {
103            ( $upstream, $downstream ) = ( $downstream, $upstream );
104        }
106        # decorate the upstream/downstream loc strings with text
107        # descriptions for the dropdown
108        my @upstream_desc   = map "$_ bp upstream",   @increments;
109        my @downstream_desc = map "$_ bp downstream", @increments;
110        $upstream   = [ pairwise { [$a, $b] } @$upstream,   @upstream_desc   ];
111        $downstream = [ pairwise { [$a, $b] } @$downstream, @downstream_desc ];
113        return ( reverse(@$upstream), @$downstream );
114    }
116 </%once>
118 <%def .flanking_download>
119 <%args>
120   $loc
121 </%args>
122     <form name="get_flanking_sequence" action="/api/v1/sequence/download/multi" method="GET">
123       <input type="hidden" name="format" value="fasta" />
124       <select name="s">
125 %       for ( upstream_downstream_options( $loc ) ) {
126 %          my ( $locstring, $description ) = @$_;
127            <option value="<% $locstring %>"><% $description %></option>
128 %       }
129       </select>
130       <input value="Download" type="submit" />
131     </form>
133 </%def>