modified autogenerated name method
[sgn.git] / cgi-bin / search / trace_download.pl
blobe01e097ddfe8636e02e93ff5c520e2b15f472aa9
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use CXGN::Page;
5 use CXGN::Genomic::Chromat;
6 use CXGN::DB::Connection;
7 use CatalystX::GlobalContext '$c';
9 # KONI 2003 August 4
11 # New download script for chromatogram access.
12 our $trace_basepath;
13 our $page = CXGN::Page->new( "Trace Download", "Koni");
15 my $dbh = CXGN::DB::Connection->new();
17 #read_id is for a chromat from sgn.seqread
18 #chromat_id is for a chromat from genomic.chromat
19 my ($read_id,$chromat_id) = $page->get_arguments(qw/read_id chrid/);
21 unless ((defined($read_id) && $read_id =~ m/^[0-9]+$/)
22 || (defined($chromat_id) && $chromat_id =~ m/^[0-9]+$/)) {
23 $page->message_page("Invalid: '$chromat_id', '$read_id'.\n");
24 # invalid_search($page);
27 my ($tablename,$idcol);
28 my ($path, $name);
29 if($read_id) {
30 my $schema = $dbh->qualify_schema('sgn');
31 $tablename = "$schema.seqread";
32 $idcol = 'read_id';
33 ($path, $name) = $dbh->selectrow_array("SELECT trace_location,trace_name FROM $tablename WHERE $idcol=?",undef,$read_id||$chromat_id);
34 } elsif($chromat_id) {
35 my $chromat = CXGN::Genomic::Chromat->retrieve($chromat_id);
36 $path = $chromat->subpath;
37 $name = $chromat->filename;
38 } else {
39 $page->error_page('Invalid GET parameters');
42 ($path && $name) || not_found($page, $read_id);
44 my $basename = $c->config->{'trace_path'}."/$path/$name";
45 my $full_pathname = '';
47 # The actual extension used should have been stored in the database, but it
48 # was not. This is because most chromatograms we had at the start did not have
49 # an extension. Overtime, it became clear that preserving the facility's
50 # specified filename is necessary, thus conventions for file extenstions are
51 # not standardized. This nested loop below wastes some time, but is arguably
52 # robust and easily extended to new types.
54 # Add the extenstion used to seqread in the database to solve the problem
55 # cleanly, do NOT standardize the extensions used on the file system.
56 my $type_ext;
57 my $comp_ext;
58 foreach $type_ext ( '',qw/.ab1 .abi .scf .esd .SCF/) {
59 foreach $comp_ext ( '',qw/.gz .Z .bz2/) {
60 if ( -f "$basename$type_ext$comp_ext" ) {
61 $full_pathname = $basename . $type_ext . $comp_ext;
62 $name .= $type_ext . $comp_ext;
63 last;
68 if (!$full_pathname) {
69 print STDERR "Can't find chromatogram file at $basename";
70 $page->message_page("This sequence does not seem to have an associated chromatogram.");
73 open F, "<$full_pathname"
74 or $page->error_page("Failed to open chromatogram file for SGN-T$read_id ($!)");
76 print "Pragma: \"no-cache\"\nContent-Disposition: filename=$name\n";
77 print "Content-type: application/data\n\n";
80 while(read(F,$_,4096)) {
81 print;
84 close F;
86 sub invalid_search {
87 my ($page) = @_;
89 $page->header();
91 print <<EOF;
93 Requested search is invalid.
94 EOF
96 $page->footer();
97 exit 0;
100 sub not_found {
101 my ($page, $id) = @_;
103 $page->header();
105 print <<EOF;
107 No chromatogram was found for SGN-T$id.
111 $page->footer();
112 exit 0;