enable the BLAST button for sequences on show_match_seq.pl
[sgn.git] / cgi-bin / tools / blast / show_match_seq.pl
blobcbd70bc557c70468e5da3d8acd9e2c4a186ce971
1 use CatalystX::GlobalContext qw( $c );
3 =head1 NAME
5 show_match_seq.pl - a simple script to show the entire sequence of a
6 match in a blast database with optional highlighting of the matched
7 region
9 =head1 DESCRIPTION
11 This script shows a webpage with the sequence from a blast database
12 for a specific id, using CXGN::BlastDB. This is the script that should
13 be called if the CXGN::Tools::Identifiers cannot determine an
14 appropriate link, so that the users still can get at the matched
15 sequence, instead of uselessly defaulting to Genbank.
17 Page arguments:
19 =over 10
21 =item id
23 The id of the sequence [string], as it appears in the database (best
24 retrieved from a blast report).
26 =item blast_db_id
28 The id [int] of the blast database file for CXGN::BlastDB.
30 =item hilite_coords
32 a list of start and end coordinates, of the form:
34 start1-end1,start2-end2,start3-end3
36 =item format
38 either "text" or "html" to output fasta text or a nicely ;-) formatted
39 html page.
41 =back
43 =head1 AUTHOR
45 Lukas Mueller <lam87@cornell.edu>
47 Robert Buels <rmb32@cornelle.edu>
49 =cut
51 use strict;
52 use warnings;
54 use CGI ();
56 use CXGN::BlastDB;
57 use CXGN::Tools::Text qw/ sanitize_string /;
58 use CXGN::MasonFactory;
60 use CXGN::Page::FormattingHelpers qw | info_section_html html_break_string html_string_linebreak_and_highlight | ;
62 my $cgi = CGI->new;
64 #get params
65 my ( $id ) = $cgi->param( 'id' );
66 my ( $blast_db_id ) = $cgi->param( 'blast_db_id' );
67 my ( $format ) = $cgi->param( 'format' );
68 my ( $hilite_coords ) = $cgi->param( 'hilite_coords' );
70 #sanitize params
71 $format ||= 'html';
72 $blast_db_id += 0;
73 $id = sanitize_string( $id );
75 $c->forward_to_mason_view('/blast/show_seq/input.mas') unless $blast_db_id && defined $id;
77 # parse the coords param
78 my @coords =
79 map {
80 my ($s, $e) = split "-", $_;
81 defined $_ or die 'parse error' for $s, $e;
82 [ $s, $e ]
84 grep length,
85 split ',',
86 ( $hilite_coords || '' );
88 #die("hilite_coords $hilite_coords. ".(join ",", @coords)."\n");
90 # look up our blastdb
91 my $bdbo = CXGN::BlastDB->from_id( $blast_db_id )
92 or $c->throw( is_error => 0,
93 message => "The blast database with id $blast_db_id could not be found (please set the blast_db_id parameter).");
95 my $seq = $bdbo->get_sequence( $id ) # returns a Bio::Seq object.
96 or $c->throw( is_error => 0,
97 message => "The sequence could not be found in the blast database with id $blast_db_id.");
99 # dispatch to the proper view
100 if ( $format eq 'html' ) {
101 my $view_link = do { $cgi->param( format => 'fasta_text'); '?'.$cgi->query_string };
102 my $download_link = do { $cgi->param( format => 'fasta_file'); '?'.$cgi->query_string };
104 $c->forward_to_mason_view(
105 '/blast/show_seq/html.mas',
106 seq => $seq,
107 highlight_coords => \@coords,
108 source => '"'.$bdbo->title.'" BLAST dataset ',
109 format_links => [
110 ( $seq->length > 500_000 ? () : [ 'View as FASTA' => $view_link ] ),
111 [ 'Download as FASTA' => $download_link ],
113 blast_url => '/tools/blast/index.pl',
116 } elsif( $format eq 'fasta_file' || $format eq 'fasta_text' ) {
118 require Bio::SeqIO;
120 print "Content-Type: text/plain\n";
121 my $attachment = $format eq 'fasta_file' ? 'attachment;' : '';
122 print "Content-Disposition: $attachment filename=$id.fa\n";
123 print "\n";
124 Bio::SeqIO->new( -fh => \*STDOUT, -format => 'fasta' )
125 ->write_seq( $seq )