Debian Codegraph @

examples/viewpfm.cgi

raw
1 #!/usr/bin/perl -w
2
3 # viewpfm.cgi
4 # by Boris Lenhard
5 #
6 # See POD documentation for this CGI script at the end of the file
7 #
8
9
10
11 use strict;
12 use CGI ();
13 use TFBS::Matrix::PFM;
14 use TFBS::DB::FlatFileDir;
15
16
17 use constant DATABASE_DIR => "examples/SAMPLE_FlatFileDir";
18
19 # The directory to store created logo image: we need an absolute
20 # path for access by the script, and a relative path for
21 # access by the web browser
22 use constant ABSOLUTE_IMAGE_DIR => "/var/www/html/TEMP";
23 use constant RELATIVE_IMAGE_DIR => "/TEMP";
24 use constant THIS_SCRIPT => "/cgi-bin/viewpfm.cgi";
25
26 # IMPORTANT NOTE: this script does not delete image files it creates
27 # page
28
29 # connect to FlatFileDir matrix database
30 # (there is a sample FlatFileDir matrix database directory
31 # examples/SAMPLE_FlatFileDir in the TFBS distribution package)
32 # Change this line if you want to use a different type of database
33 # (e.g. TFBS::DB::JASPAR2)
34
35 my $db = TFBS::DB::FlatFileDir->connect(DATABASE_DIR);
36
37 if (CGI::param("matrix_id")) { # matrix entry
38 matrix_info($db, CGI::param("matrix_id"));
39 }
40 else { # draw logo
41 matrix_list_page($db);
42 }
43
44
45 sub matrix_list_page {
46 my ($db) = @_;
47
48 my $q = CGI->new;
49
50 # get all matrices (TFBS::Matrix::PWM objects) into a TFBS::MatrixSet object
51
52 my $matrixset = $db->get_MatrixSet(-matrixtype=>"PFM");
53
54 print $q->header, $q->start_html;
55 print $q->h1("Matrices in the database");
56
57 my $matrix_iterator = $matrixset->Iterator(-sort_by=>"ID");
58 my @table_rows = ($q->Tr($q->th([ 'MatrixID', 'Name',
59 'Class','Length', 'Total IC'])));
60
61 while (my $pfm = $matrix_iterator->next) {
62 push @table_rows,
63 $q->Tr($q->td([$q->a({-href=>THIS_SCRIPT."?matrix_id=".$pfm->ID},
64 $pfm->ID),
65 $pfm->name, $pfm->class,
66 $pfm->length, $pfm->to_ICM->total_ic]));
67
68 }
69
70 print $q->table({-border=>1}, @table_rows);
71 print $q->end_html;
72 }
73
74
75 sub matrix_info {
76 my ($db, $matrix_id) = @_;
77 my $q = CGI->new;
78 my $pfm = $db->get_matrix_by_ID($matrix_id);
79
80 unless(defined $pfm) {
81 # first we draw a sequence logo and store it in a .png file
82
83 my $logofile = $pfm->ID.".png";
84 # we want image size to vary with motif length:
85 my $xsize = 60+20*$pfm->length();
86 # ...but it should not be too narrow for short motifs:
87 $xsize=278 if($pfm->length()<10);
88
89 $pfm->draw_logo(-file =>ABSOLUTE_IMAGE_DIR."/$logofile",
90 -full_scale =>2.25,
91 -xsize =>$xsize,
92 -ysize =>190,
93 -graph_title=> $pfm->name,
94 -x_title=>"Nucleotide position",
95 -y_title=>"ic [bits]");
96
97 # then we output the page
98 print $q->header, $q->start_html;
99 print $q->div("Matrix ID : ".$pfm->ID);
100 print $q->div("Transctiption factor name : ", $pfm->name);
101 print $q->div("Structural class : ", $pfm->class);
102 print $q->div("Total information content : ",
103 sprintf("%2.2f",$pfm->to_ICM->total_ic));
104 print $q->div("Matrix:");
105 print $q->div($q->pre($pfm->prettyprint));
106 print $q->div("Sequence logo:");
107 print $q->img({-src=>RELATIVE_IMAGE_DIR."/$logofile"});
108 print $q->div($q->a({-href=>THIS_SCRIPT}, "Back to matrix list"));
109 print $q->end_html;
110 }
111 else { # matrix not found
112 print $q->header, $q->start_html;
113 print $q->h2("Matrix $matrix_id not found in the database");
114 print $q->a({-href=>THIS_SCRIPT}, "Back to matrix list");
115 print $q->end_html;
116 }
117
118 }