]> git.donarmstrong.com Git - biopieces.git/blobdiff - www/index.cgi
fixed encoding bug in read_454
[biopieces.git] / www / index.cgi
index 8a3377f0248fdfcc9b35bb7fde3905e0e2551f84..64314fa6520ff70122f3b54af9e6c473c8ee0665 100755 (executable)
@@ -29,6 +29,7 @@ use CGI;
 use URI::Escape;
 use Data::Dumper;
 use Digest::MD5;
+use Time::HiRes;
 use Maasha::Common;
 use Maasha::Filesys;
 use Maasha::Calc;
@@ -62,12 +63,11 @@ push @html, Maasha::XHTML::body_end;
 push @html, Maasha::XHTML::html_end;
 
 # push @html, Maasha::XHTML::hdump( { $cgi->Vars } );  # DEBUG
-# push @html, Maasha::XHTML::hdump( \%ENV );           # DEBUG
 # push @html, Maasha::XHTML::hdump( $cookie );         # DEBUG
+# push @html, Maasha::XHTML::hdump( \%ENV );           # DEBUG
 
 print "$_\n" foreach @html;
 
-
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
@@ -85,7 +85,7 @@ sub cookie_default
 
     # Returns a hash.
 
-    my ( $cookie );
+    my ( $cookie, $session );
 
     $cookie = {};
 
@@ -104,40 +104,54 @@ sub cookie_default
         }
     }
 
-    $cookie->{ 'SCRIPT' }          = Maasha::Common::get_scriptname();
-    $cookie->{ 'DATA_DIR' }        = "Data";
-    $cookie->{ 'SESSION_DIR' }     = "Sessions";
-    $cookie->{ 'LIST_PAGES' }      = [ qw( clade genome assembly contig browse ) ];
+    $cookie->{ 'SCRIPT' }      = Maasha::Common::get_scriptname();
+    $cookie->{ 'DATA_DIR' }    = "Data";
+    $cookie->{ 'SESSION_DIR' } = "Sessions";
+    $cookie->{ 'LIST_PAGES' }  = [ qw( clade genome assembly contig browse ) ];
+    $cookie->{ 'USER' }        = $cgi->param( 'user' );
+    $cookie->{ 'PASSWORD' }    = $cgi->param( 'password' );
+    $cookie->{ 'SESSION_ID' }  = $cgi->param( 'session_id' );
 
-    $cookie->{ 'USER' }            = $cgi->param( 'user' ) || '';
-    $cookie->{ 'PASSWORD' }        = $cgi->param( 'password' );
-    $cookie->{ 'SESSION_ID' }      = $cgi->param( 'session_id' );
+    $session = session_restore( $cookie );
 
-    cookie_session( $cookie );
     cookie_login( $cookie );
 
-    $cookie->{ 'PAGE' }          ||= $cgi->param( 'page' )     || 'login';
+    # session_id_check( $cookie, $session );
 
-    if ( $cookie->{ 'LOGIN' } eq 'OK' )
+    if ( $cookie->{ 'LOGIN_ERROR' } or $cookie->{ 'SESSION_ERROR' } )
+    {
+        $cookie->{ 'PAGE' } = 'login';
+    }
+    elsif ( $cookie->{ 'LOGIN' } )
     {
         if ( $cgi->param( 'genome' ) ) {
             $cookie->{ 'PAGE' } = 'browse';
         } else {
-            $cookie->{ 'PAGE' } = 'clade';
+            $cookie->{ 'PAGE' } = $session->{ 'PAGE' } || 'clade';
         }
     }
+    elsif ( not defined $cookie->{ 'USER' } )
+    {
+        $cookie->{ 'PAGE' } = 'login';
+    }
+    else
+    {
+         $cookie->{ 'PAGE' } = $cgi->param( 'page' ) || 'login';
+    }
 
-    $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )    || '';
-    $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )   || '';
-    $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' ) || '';
-    $cookie->{ 'CONTIG' }          = $cgi->param( 'contig' )   || '';
-    $cookie->{ 'Q_ID' }            = $cgi->param( 'q_id' );
-    $cookie->{ 'NAV_START' }     ||= $cgi->param( 'nav_start' );
-    $cookie->{ 'NAV_END' }       ||= $cgi->param( 'nav_end' );
+    $cookie->{ 'TRACK_STATUS' }    = $session->{ 'TRACK_STATUS' };
+    $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )       || $session->{ 'CLADE' };
+    $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )      || $session->{ 'GENOME' };
+    $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' )    || $session->{ 'ASSEMBLY' };
+    $cookie->{ 'CONTIG' }          = $cgi->param( 'contig' )      || $session->{ 'CONTIG' };
+    $cookie->{ 'Q_ID' }            = $cgi->param( 'q_id' )        || $session->{ 'Q_ID' };
+    $cookie->{ 'NAV_START' }     ||= defined $cgi->param( 'nav_start' ) ? $cgi->param( 'nav_start' ) : $session->{ 'NAV_START' };
+    $cookie->{ 'NAV_END' }       ||= defined $cgi->param( 'nav_end' )   ? $cgi->param( 'nav_end' )   : $session->{ 'NAV_END' };
     $cookie->{ 'NAV_CENTER' }      = $cgi->param( 'nav_center' );
-    $cookie->{ 'S_BEG' }           = $cgi->param( 's_beg' );
-    $cookie->{ 'S_END' }           = $cgi->param( 's_end' );
-    $cookie->{ 'STRAND' }          = $cgi->param( 'strand' );
+    $cookie->{ 'S_BEG' }           = defined $cgi->param( 's_beg' ) ? $cgi->param( 's_beg' ) : $session->{ 'S_BEG' };
+    $cookie->{ 'S_END' }           = defined $cgi->param( 's_end' ) ? $cgi->param( 's_end' ) : $session->{ 'S_END' };
+    $cookie->{ 'STRAND' }          = $cgi->param( 'strand' )      || $session->{ 'STRAND' };
+    $cookie->{ 'TRACK' }           = $cgi->param( 'track' );
     $cookie->{ 'ZOOM_IN1' }        = $cgi->param( 'zoom_in1' );
     $cookie->{ 'ZOOM_IN2' }        = $cgi->param( 'zoom_in2' );
     $cookie->{ 'ZOOM_IN3' }        = $cgi->param( 'zoom_in3' );
@@ -152,7 +166,7 @@ sub cookie_default
     $cookie->{ 'MOVE_RIGHT3' }     = $cgi->param( 'move_right3' );
 
     $cookie->{ 'IMG_WIDTH' }       = 1200;   # Width of browser image in pixels
-    $cookie->{ 'IMG_HEIGHT' }      = 800;    # Height of browser image in pixels
+    $cookie->{ 'IMG_HEIGHT' }      = 800;    # Height of browser image in pixels  # TODO: Redundant?
     $cookie->{ 'WIGGLE_HEIGHT' }   = 75;     # Height of Wiggle tracks in pixels
     $cookie->{ 'TRACK_OFFSET' }    = 20;
     $cookie->{ 'TRACK_SPACE' }     = 20;
@@ -162,16 +176,28 @@ sub cookie_default
     $cookie->{ 'SEQ_COLOR' }       = [ 0, 0, 0, ];
     $cookie->{ 'FEAT_WIDTH' }      = 5;
     $cookie->{ 'FEAT_COLOR' }      = [ 0, 0, 0 ];
-    $cookie->{ 'FEAT_MAX' }        = 5000;   # TODO: Reduntant?
+#    $cookie->{ 'FEAT_MAX' }        = 5000;   # TODO: Reduntant?
 
-    $cookie->{ 'LIST_USER' }       = Maasha::BGB::Track::list_users();
+#    $cookie->{ 'LIST_USER' }       = Maasha::BGB::Track::list_users();  # TODO: Redundant?
 
-    if ( $cookie->{ 'USER' } )
+    if ( $cookie->{ 'USER' } and not $cookie->{ 'LOGIN_ERROR' } )
     {
         $cookie->{ 'LIST_CLADE' }      = Maasha::BGB::Track::list_clades(     $cookie->{ 'USER' } );
-        $cookie->{ 'LIST_GENOME' }     = Maasha::BGB::Track::list_genomes(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' } );
-        $cookie->{ 'LIST_ASSEMBLY' }   = Maasha::BGB::Track::list_assemblies( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' } );
-        $cookie->{ 'LIST_CONTIG' }     = Maasha::BGB::Track::list_contigs(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' } );
+
+        if ( $cookie->{ 'CLADE' } )
+        {
+            $cookie->{ 'LIST_GENOME' }     = Maasha::BGB::Track::list_genomes(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' } );
+
+            if ( $cookie->{ 'GENOME' } )
+            {
+                $cookie->{ 'LIST_ASSEMBLY' }   = Maasha::BGB::Track::list_assemblies( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' } );
+
+                if ( $cookie->{ 'ASSEMBLY' } )
+                {
+                    $cookie->{ 'LIST_CONTIG' }     = Maasha::BGB::Track::list_contigs(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' } );
+                }
+            }
+        }
 
         if ( $cookie->{ 'CONTIG' } )
         {
@@ -180,58 +206,123 @@ sub cookie_default
             cookie_zoom( $cookie );
             cookie_move( $cookie );
             cookie_center( $cookie );
+            cookie_track_status( $cookie );
         }
     }
 
     $cookie->{ 'STRAND' } = '+' if defined $cookie->{ 'STRAND' } and $cookie->{ 'STRAND' } eq ' ';   # FIXME ugly HTML fix
 
+    session_store( $cookie, $session );
+
     return wantarray ? %{ $cookie } : $cookie;
 }
 
 
-sub cookie_session
+sub session_restore
 {
-    # Martin A. Hansen, December 2009.
+    # Martin A. Hansen, March 2010.
 
-    # Check cookie information against session information.
+    # Restores session and returns this.
 
     my ( $cookie,   # cookie hash
        ) = @_;
 
-    # Returns nothing.
+    # Returns hashref.
 
     my ( $session );
 
-    $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.json" );
-
-    # if ( $cookie->{ 'SESSION_ID' } )
-    # {
-    #     if ( $cookie->{ 'SESSION_ID' } ne $ENV{ 'SSL_SESSION_ID' } ) {
-    #         $cookie->{ 'PAGE' } = 'login';
-    #         print STDERR "HER1\n"; # DEBUG
-    #     }
-    # }
-    # else
-    # {
-    #     $cookie->{ 'PAGE' } = 'login';
-    # 
-    #     print STDERR "HER2\n"; # DEBUG
-    # }
-
-    if ( exists $session->{ $cookie->{ 'USER' } } and $cookie->{ 'SESSION_ID' } )
+    if ( defined $cookie->{ 'USER' } and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" ) {
+        $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json" );
+    }
+
+    return wantarray ? %{ $session } : $session;
+}
+
+
+sub session_store
+{
+    # Martin A. Hansen, March 2010.
+
+    # Store a session updated with cookie information to session file.
+
+    my ( $cookie,    # cookie hash
+         $session,   # session hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $new_session );
+
+    if ( defined $cookie->{ 'USER' } and $cookie->{ 'USER' } ne '' and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" )
     {
-        if ( $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ne $cookie->{ 'SESSION_ID' } ) {
-            $cookie->{ 'PAGE' } = 'login';
-            print STDERR "HER3\n"; # DEBUG
+        $new_session->{ 'PASSWORD' }   = $session->{ 'PASSWORD' };
+        $new_session->{ 'SESSION_ID' } = $session->{ 'SESSION_ID' };
+        $new_session->{ 'TIME' }       = Maasha::Common::time_stamp();
+        $new_session->{ 'PAGE' }       = $cookie->{ 'PAGE' };
+
+        if ( $cookie->{ 'PAGE' } =~ /browse|export_pdf|export_svg/ )
+        {
+            $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
+            $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
+            $new_session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
+            $new_session->{ 'CONTIG' }    = $cookie->{ 'CONTIG' };
+            $new_session->{ 'NAV_START' } = $cookie->{ 'NAV_START' };
+            $new_session->{ 'NAV_END' }   = $cookie->{ 'NAV_END' };
+        }
+        elsif ( $cookie->{ 'PAGE' } eq 'contig' )
+        {
+            $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
+            $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
+            $new_session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
+        }
+        elsif ( $cookie->{ 'PAGE' } eq 'assembly' )
+        {
+            $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
+            $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
         }
+        elsif ( $cookie->{ 'PAGE' } eq 'genome' )
+        {
+            $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
+        }
+        elsif ( $cookie->{ 'PAGE' } =~ /export|dna/ )
+        {
+            $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
+            $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
+            $new_session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
+            $new_session->{ 'CONTIG' }    = $cookie->{ 'CONTIG' };
+            $new_session->{ 'Q_ID' }      = $cookie->{ 'Q_ID' };
+            $new_session->{ 'S_BEG' }     = $cookie->{ 'S_BEG' };
+            $new_session->{ 'S_END' }     = $cookie->{ 'S_END' };
+            $new_session->{ 'STRAND' }    = $cookie->{ 'STRAND' };
+        }
+
+        $new_session->{ 'TRACK_STATUS' } = $cookie->{ 'TRACK_STATUS' };
+
+        Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json", $new_session );
     }
-    else
+}
+
+
+sub session_id_check
+{
+    # Martin A. Hansen, March 2010.
+
+    # Check that the session id in the cookie and session match.
+    # Sets SESSION_ERROR flag in cookie if not matching.
+
+    my ( $cookie,    # cookie hash
+         $session,   # session hash
+       ) = @_;
+
+    # Returns nothing.
+
+    if ( defined $cookie->{ 'SESSION_ID' } and defined $session->{ 'SESSION_ID' } )
     {
-        $cookie->{ 'PAGE' } = 'login';
-        print STDERR "HER4\n";                                                                 # DEBUG
-        print STDERR "NO COOKIE SESSION ID\n" if not $cookie->{ 'SESSION_ID' };                # DEBUG
-        print STDERR "NO COOKIE USER\n"       if not $cookie->{ 'USER' };                      # DEBUG
-        print STDERR "NO SESSION USER\n"      if not exists $session->{ $cookie->{ 'USER' } }; # DEBUG
+        if ( $cookie->{ 'SESSION_ID' } ne $session->{ 'SESSION_ID' } ) {
+            $cookie->{ 'SESSION_ERROR' } = 1;
+        } else {
+            $cookie->{ 'SESSION_ERROR' } = 0;
+        }
     }
 }
 
@@ -250,25 +341,27 @@ sub cookie_login
 
     my ( $session );
 
-    if ( $cookie->{ 'USER' } and $cookie->{ 'PASSWORD' } )
+    if ( defined $cookie->{ 'PASSWORD' } )
     {
-        $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.json" );
-    
-        if ( exists $session->{ $cookie->{ 'USER' } } and
-             $session->{ $cookie->{ 'USER' } }->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
+        if ( defined $cookie->{ 'USER' } and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" )
         {
-            $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ||= Maasha::BGB::Session::session_new();
-            # $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' }   = $ENV{ 'SSL_SESSION_ID' };
-            $session->{ $cookie->{ 'USER' } }->{ 'TIME' }         = Maasha::Common::time_stamp();
-
-            $cookie->{ 'SESSION_ID' } = $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' };
-            $cookie->{ 'LOGIN' }      = "OK";
-
-            Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/sessions.json", $session );
+            $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json" );
+
+            if ( $session->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
+            {
+                $cookie->{ 'TIME' }          = Maasha::Common::time_stamp();
+                $cookie->{ 'SESSION_ID' }  ||= Maasha::BGB::Session::session_new();
+                $cookie->{ 'LOGIN' }         = 1;
+                $cookie->{ 'LOGIN_ERROR' }   = 0;
+            }
+            else
+            {
+                $cookie->{ 'LOGIN_ERROR' } = 1;
+            }
         }
         else
         {
-            $cookie->{ 'LOGIN' } = "ERROR";
+            $cookie->{ 'LOGIN_ERROR' } = 1;
         }
     }
 }
@@ -420,7 +513,7 @@ sub cookie_move
 
 sub cookie_center
 {
-    # Martin A. Hansen, March 2010
+    # Martin A. Hansen, March 2010.
    
     # Adjust the cookie values for NAV_START and NAV_END based
     # on cookie CENTER value if defined.
@@ -446,24 +539,32 @@ sub cookie_center
 }
 
 
-sub cookie_page_next
+sub cookie_track_status
 {
-    # Martin A. Hansen, November 2009.
-    
-    # Returns the next page in the taxonomy path.
+    # Martin A. Hansen, March 2010.
 
+    #
+    
     my ( $cookie,   # cookie hash
        ) = @_;
 
-    # Returns a string.
+    # Returns nothing.
+
+    my ( $clade, $genome, $assembly, $track );
 
-    my ( $i );
+    $clade    = $cookie->{ 'CLADE' };
+    $genome   = $cookie->{ 'GENOME' };
+    $assembly = $cookie->{ 'ASSEMBLY' };
+    $track    = $cookie->{ 'TRACK' };
 
-    for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
-        last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
+    if ( $track )
+    {
+        if ( $cookie->{ 'TRACK_STATUS' }->{ $clade }->{ $genome }->{ $assembly }->{ $track } ) {
+            $cookie->{ 'TRACK_STATUS' }->{ $clade }->{ $genome }->{ $assembly }->{ $track } = 0;
+        } else {
+            $cookie->{ 'TRACK_STATUS' }->{ $clade }->{ $genome }->{ $assembly }->{ $track } = 1;
+        }
     }
-
-    return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
 }
 
 
@@ -498,8 +599,18 @@ sub page
         push @html, page_dna( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'export' ) {
         push @html, page_export( $cookie );
-    } else {
-        push @html, page_taxonomy( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'export_pdf' ) {
+        push @html, page_export_pdf( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'export_svg' ) {
+        push @html, page_export_svg( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'clade' ) {
+        push @html, page_clade( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'genome' ) {
+        push @html, page_genome( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'assembly' ) {
+        push @html, page_assembly( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'contig' ) {
+        push @html, page_contig( $cookie );
     }
 
     return wantarray ? @html : \@html;
@@ -607,7 +718,107 @@ sub page_export
 }
 
 
-sub page_taxonomy
+sub page_export_pdf
+{
+    # Martin A. Hansen, March 2010.
+
+    # Renders the export PDF page.
+
+    my ( $cookie,
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_export_pdf( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub page_export_svg
+{
+    # Martin A. Hansen, March 2010.
+
+    # Renders the export SVG page.
+
+    my ( $cookie,
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_export_svg( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub page_clade
+{
+    # Martin A. Hansen, March 2010.
+    
+    # Renders the clade selection page.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_clade( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub page_genome
+{
+    # Martin A. Hansen, March 2010.
+    
+    # Renders the genome selection page.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_genome( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub page_assembly
+{
+    # Martin A. Hansen, March 2010.
+    
+    # Renders the clade selection page.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_assembly( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub page_contig
 {
     # Martin A. Hansen, November 2009.
     
@@ -621,7 +832,7 @@ sub page_taxonomy
     my ( @html );
 
     push @html, section_taxonomy_table( $cookie );
-    push @html, section_taxonomy_select( $cookie );
+    push @html, section_contig( $cookie );
 
     return wantarray ? @html : \@html;
 }
@@ -671,7 +882,7 @@ sub section_login
         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_end",   value => $cookie->{ 'NAV_END' } ) );
     }
 
-    if ( $cookie->{ 'LOGIN' } and $cookie->{ 'LOGIN' } eq 'ERROR' ) {
+    if ( $cookie->{ 'LOGIN_ERROR' } ) {
         push @html, Maasha::XHTML::h3( txt => "Bad user or password - please retry", class => 'error' );
     }
 
@@ -720,11 +931,11 @@ sub section_taxonomy_table
 }
 
 
-sub section_taxonomy_select
+sub section_clade
 {
-    # Martin A. Hansen, November 2009.
+    # Martin A. Hansen, March 2010.
     
-    # Returns a HTML section with selection choices
+    # Returns a HTML section with clade selection choices
     # for navigating the taxonomy tree.
 
     my ( $cookie,   # cookie hash
@@ -732,27 +943,128 @@ sub section_taxonomy_select
 
     # Returns a list.
 
-    my ( $list, @html, $item, $href );
+    my ( @html, $clade, $href );
+
+    push @html, Maasha::XHTML::h2( txt => "Select clade", class => 'center' );
 
-    $list = "LIST_" . uc $cookie->{ 'PAGE' };
+    push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
 
-    push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
+    foreach $clade ( @{ $cookie->{ 'LIST_CLADE' } } )
+    {
+        $href  = "$cookie->{ 'SCRIPT' }?page=genome";
+        $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
+        $href .= "&user=$cookie->{ 'USER' }";
+        $href .= "&clade=$clade";
+
+        push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $clade, href => $href ) ] );
+    }
+
+    push @html, Maasha::XHTML::table_end;
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub section_genome
+{
+    # Martin A. Hansen, March 2010.
+    
+    # Returns a HTML section with genome selection choices
+    # for navigating the taxonomy tree.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html, $genome, $href );
+
+    push @html, Maasha::XHTML::h2( txt => "Select genome", class => 'center' );
 
     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
 
-    foreach $item ( @{ $cookie->{ $list } } )
+    foreach $genome ( @{ $cookie->{ 'LIST_GENOME' } } )
     {
-        $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
+        $href  = "$cookie->{ 'SCRIPT' }?page=assembly";
+        $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
+        $href .= "&user=$cookie->{ 'USER' }";
+        $href .= "&clade=$cookie->{ 'CLADE' }";
+        $href .= "&genome=$genome";
+
+        push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $genome, href => $href ) ] );
+    }
 
-        $href  = "$cookie->{ 'SCRIPT' }?page=" . cookie_page_next( $cookie );
+    push @html, Maasha::XHTML::table_end;
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub section_assembly
+{
+    # Martin A. Hansen, March 2010.
+    
+    # Returns a HTML section with assembly selection choices
+    # for navigating the taxonomy tree.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html, $assembly, $href );
+
+    push @html, Maasha::XHTML::h2( txt => "Select assembly", class => 'center' );
+
+    push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
+
+    foreach $assembly ( @{ $cookie->{ 'LIST_ASSEMBLY' } } )
+    {
+        $href  = "$cookie->{ 'SCRIPT' }?page=contig";
+        $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
+        $href .= "&user=$cookie->{ 'USER' }";
+        $href .= "&clade=$cookie->{ 'CLADE' }";
+        $href .= "&genome=$cookie->{ 'GENOME' }";
+        $href .= "&assembly=$assembly";
+
+        push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $assembly, href => $href ) ] );
+    }
+
+    push @html, Maasha::XHTML::table_end;
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub section_contig
+{
+    # Martin A. Hansen, March 2010.
+    
+    # Returns a HTML section with contig selection choices
+    # for navigating the taxonomy tree.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html, $contig, $href );
+
+    push @html, Maasha::XHTML::h2( txt => "Select contig", class => 'center' );
+
+    push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
+
+    foreach $contig ( @{ $cookie->{ 'LIST_CONTIG' } } )
+    {
+        $href  = "$cookie->{ 'SCRIPT' }?page=browse";
         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
         $href .= "&user=$cookie->{ 'USER' }";
         $href .= "&clade=$cookie->{ 'CLADE' }";
-        $href .= "&genome=$cookie->{ 'GENOME' }"     if $cookie->{ 'GENOME' };
-        $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
-        $href .= "&contig=$cookie->{ 'CONTIG' }"     if $cookie->{ 'CONTIG' };
+        $href .= "&genome=$cookie->{ 'GENOME' }";
+        $href .= "&assembly=$cookie->{ 'ASSEMBLY' }";
+        $href .= "&contig=$contig";
 
-        push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
+        push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $contig, href => $href ) ] );
     }
 
     push @html, Maasha::XHTML::table_end;
@@ -826,9 +1138,9 @@ sub section_linkout
 
     # Returns a list.
 
-    my ( $link, @html );
+    my ( $link_out, $link_pdf, $link_svg, @html );
 
-    $link = Maasha::XHTML::ln(
+    $link_out = Maasha::XHTML::ln(
         txt  => 'link',
         href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
                            "clade=$cookie->{ 'CLADE' }",
@@ -841,7 +1153,37 @@ sub section_linkout
         title  => "Static link to this view",
     );
 
-    push @html, Maasha::XHTML::p( txt => $link, class => 'center' );
+    $link_pdf = Maasha::XHTML::ln(
+        txt  => 'PDF',
+        href => join( "&", "$cookie->{ 'SCRIPT' }?page=export_pdf",
+                           "session_id=$cookie->{ 'SESSION_ID' }",
+                           "user=$cookie->{ 'USER' }",
+                           "clade=$cookie->{ 'CLADE' }",
+                           "genome=$cookie->{ 'GENOME' }",
+                           "assembly=$cookie->{ 'ASSEMBLY' }",
+                           "contig=$cookie->{ 'CONTIG' }",
+                           "nav_start=$cookie->{ 'NAV_START' }",
+                           "nav_end=$cookie->{ 'NAV_END' }",
+              ),
+        title  => "Export view in PDF",
+    );
+    
+    $link_svg = Maasha::XHTML::ln(
+        txt  => 'SVG',
+        href => join( "&", "$cookie->{ 'SCRIPT' }?page=export_svg",
+                           "session_id=$cookie->{ 'SESSION_ID' }",
+                           "user=$cookie->{ 'USER' }",
+                           "clade=$cookie->{ 'CLADE' }",
+                           "genome=$cookie->{ 'GENOME' }",
+                           "assembly=$cookie->{ 'ASSEMBLY' }",
+                           "contig=$cookie->{ 'CONTIG' }",
+                           "nav_start=$cookie->{ 'NAV_START' }",
+                           "nav_end=$cookie->{ 'NAV_END' }",
+              ),
+        title  => "Export view in SVG",
+    );
+
+    push @html, Maasha::XHTML::p( txt => "$link_out $link_pdf $link_svg", class => 'center' );
 
     return wantarray ? @html : \@html;
 }
@@ -854,32 +1196,23 @@ sub section_browse
 
     # Returns a list.
 
-    my ( @tracks, $i, @features, $feat, $elem, $surface, $cr, $png_data, @html, @img, $x1, $y1, $x2, $y2, $factor, $center );
+    my ( @track_list, $i, @tracks, $track, $elem, $png_data, @html, @img, $x1, $y1, $x2, $y2, $factor, $center );
 
-    push @features, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
-    push @features, [ Maasha::BGB::Track::track_seq( $cookie ) ];
+    push @tracks, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
+    push @tracks, [ Maasha::BGB::Track::track_seq( $cookie ) ];
 
-    @tracks = Maasha::BGB::Track::path_tracks( $cookie );
+    @track_list = Maasha::BGB::Track::list_track_dir( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' } );
 
-    for ( $i = 0; $i < @tracks; $i++ )
+    for ( $i = 0; $i < @track_list; $i++ )
     {
         $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
 
-        push @features, [ Maasha::BGB::Track::track_feature( $tracks[ $i ], $cookie ) ];
+        push @tracks, [ Maasha::BGB::Track::track_feature( $track_list[ $i ], $cookie ) ];
     }
 
-    $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
-    $cr      = Cairo::Context->create( $surface );
+    unshift @tracks, [ Maasha::BGB::Track::track_grid( $cookie ) ];
 
-    $cr->rectangle ( 0, 0, $cookie->{ 'IMG_WIDTH' },  $cookie->{ 'TRACK_OFFSET' } );
-    $cr->set_source_rgb ( 1, 1, 1 );
-    $cr->fill;
-
-    foreach $feat ( @features ) {
-        Maasha::BGB::Draw::draw_feature( $cr, $feat ) if $feat;
-    }
-
-    $png_data = Maasha::BGB::Draw::base64_png( $surface );
+    $png_data = Maasha::BGB::Draw::render_png( $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' }, \@tracks );
 
     push @img, Maasha::XHTML::img(
         src    => "data:image/png;base64,$png_data",
@@ -904,16 +1237,16 @@ sub section_browse
         $center = int( $cookie->{ 'NAV_START' } + $factor * $i );
 
         push @img, Maasha::XHTML::area(
-            href     => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
-                                   "session_id=$cookie->{ 'SESSION_ID' }",
-                                   "user=$cookie->{ 'USER' }",
-                                   "clade=$cookie->{ 'CLADE' }",
-                                   "genome=$cookie->{ 'GENOME' }",
-                                   "assembly=$cookie->{ 'ASSEMBLY' }",
-                                   "contig=$cookie->{ 'CONTIG' }",
-                                   "nav_start=$cookie->{ 'NAV_START' }",
-                                   "nav_end=$cookie->{ 'NAV_END' }",
-                                   "nav_center=$center",
+            href   => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
+                                 "session_id=$cookie->{ 'SESSION_ID' }",
+                                 "user=$cookie->{ 'USER' }",
+                                 "clade=$cookie->{ 'CLADE' }",
+                                 "genome=$cookie->{ 'GENOME' }",
+                                 "assembly=$cookie->{ 'ASSEMBLY' }",
+                                 "contig=$cookie->{ 'CONTIG' }",
+                                 "nav_start=$cookie->{ 'NAV_START' }",
+                                 "nav_end=$cookie->{ 'NAV_END' }",
+                                 "nav_center=$center",
                       ),
             shape  => 'rect',
             coords => "$x1, $y1, $x2, $y2",
@@ -921,36 +1254,74 @@ sub section_browse
         );
     }
 
-    foreach $feat ( @features )
+    foreach $track ( @tracks )
     {
-        foreach $elem ( @{ $feat } )
+        foreach $elem ( @{ $track } )
         {
-            next if $elem->{ 'type' } eq 'text';
-            next if $elem->{ 'type' } eq 'wiggle';
-
-            #$elem->{ 'strand' } = '&#43' if $elem->{ 'strand' } eq '+';
-
-            $cookie->{ 'Q_ID' }   = $elem->{ 'q_id' };
-            $cookie->{ 'S_BEG' }  = $elem->{ 's_beg' };
-            $cookie->{ 'S_END' }  = $elem->{ 's_end' };
-            $cookie->{ 'STRAND' } = $elem->{ 'strand' };
-
-            push @img, Maasha::XHTML::area(
-                href     => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
-                                       "session_id=$cookie->{ 'SESSION_ID' }",
-                                       "user=$cookie->{ 'USER' }",
-                                       "clade=$cookie->{ 'CLADE' }",
-                                       "genome=$cookie->{ 'GENOME' }",
-                                       "assembly=$cookie->{ 'ASSEMBLY' }",
-                                       "contig=$cookie->{ 'CONTIG' }",
-                                       "s_beg=$cookie->{ 'S_BEG' }",
-                                       "s_end=$cookie->{ 'S_END' }",
-                                       "strand=$cookie->{ 'STRAND' }",
-                          ),
-                shape  => 'rect',
-                coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }",
-                title  => "$elem->{ 'title' }",
-            );
+            next if $elem->{ 'type' } =~ /grid|text|wiggle/;
+
+            if ( $elem->{ 'type' } eq 'track_name' )
+            {
+                $x1 = $elem->{ 'x1' };
+                $y1 = $elem->{ 'y1' } - 10;
+                $x2 = $elem->{ 'x1' } + 6 * length $elem->{ 'txt' };
+                $y2 = $elem->{ 'y1' };
+
+                if ( Maasha::BGB::Track::track_hide( $cookie, $elem->{ 'track' } ) )
+                {
+                    push @img, Maasha::XHTML::area(
+                        href   => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
+                                             "session_id=$cookie->{ 'SESSION_ID' }",
+                                             "user=$cookie->{ 'USER' }",
+                                             "clade=$cookie->{ 'CLADE' }",
+                                             "genome=$cookie->{ 'GENOME' }",
+                                             "assembly=$cookie->{ 'ASSEMBLY' }",
+                                             "contig=$cookie->{ 'CONTIG' }",
+                                             "track=$elem->{ 'track' }",
+                                  ),
+                        shape  => 'rect',
+                        coords => "$x1, $y1, $x2, $y2",
+                        title  => qq(Show track: $elem->{ 'txt' }),
+                    );
+                }
+                else
+                {
+                    push @img, Maasha::XHTML::area(
+                        href   => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
+                                             "session_id=$cookie->{ 'SESSION_ID' }",
+                                             "user=$cookie->{ 'USER' }",
+                                             "clade=$cookie->{ 'CLADE' }",
+                                             "genome=$cookie->{ 'GENOME' }",
+                                             "assembly=$cookie->{ 'ASSEMBLY' }",
+                                             "contig=$cookie->{ 'CONTIG' }",
+                                             "track=$elem->{ 'track' }",
+                                  ),
+                        shape  => 'rect',
+                        coords => "$x1, $y1, $x2, $y2",
+                        title  => qq(Hide track: $elem->{ 'txt' }),
+                    );
+                }
+            }
+            else
+            {
+                push @img, Maasha::XHTML::area(
+                    href     => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
+                                           "session_id=$cookie->{ 'SESSION_ID' }",
+                                           "user=$cookie->{ 'USER' }",
+                                           "clade=$cookie->{ 'CLADE' }",
+                                           "genome=$cookie->{ 'GENOME' }",
+                                           "assembly=$cookie->{ 'ASSEMBLY' }",
+                                           "contig=$cookie->{ 'CONTIG' }",
+                                           "s_beg=$elem->{ 's_beg' }",
+                                           "s_end=$elem->{ 's_end' }",
+                                           "strand=$elem->{ 'strand' }",
+                                           "q_id=$elem->{ 'q_id' }",
+                              ),
+                    shape  => 'rect',
+                    coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }",
+                    title  => "$elem->{ 'title' }",
+                );
+            }
         }
     }
 
@@ -1004,6 +1375,80 @@ sub section_export
 }
 
 
+sub section_export_pdf
+{
+    # Martin A. Hansen, March 2010.
+
+    # Returns a HTML section with export table.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @tracks, @track_list, $i, $file, @html );
+
+    push @tracks, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
+    push @tracks, [ Maasha::BGB::Track::track_seq( $cookie ) ];
+
+    @track_list = Maasha::BGB::Track::list_track_dir( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' } );
+
+    for ( $i = 0; $i < @track_list; $i++ )
+    {
+        $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
+
+        push @tracks, [ Maasha::BGB::Track::track_feature( $track_list[ $i ], $cookie ) ];
+    }
+
+    unshift @tracks, [ Maasha::BGB::Track::track_grid( $cookie ) ];
+
+    $file = "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/BGB_export.pdf";
+
+    Maasha::BGB::Draw::render_pdf_file( $file, $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' }, \@tracks );
+
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::ln( txt => "BGB_export.pdf", href => $file ), class => 'center' );
+
+    return wantarray ? @html : \@html;
+}
+
+
+sub section_export_svg
+{
+    # Martin A. Hansen, March 2010.
+
+    # Export view in SVG format.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @tracks, @track_list, $i, $file, @html );
+
+    push @tracks, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
+    push @tracks, [ Maasha::BGB::Track::track_seq( $cookie ) ];
+
+    @track_list = Maasha::BGB::Track::list_track_dir( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' } );
+
+    for ( $i = 0; $i < @track_list; $i++ )
+    {
+        $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
+
+        push @tracks, [ Maasha::BGB::Track::track_feature( $track_list[ $i ], $cookie ) ];
+    }
+
+    unshift @tracks, [ Maasha::BGB::Track::track_grid( $cookie ) ];
+
+    $file = "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/BGB_export.svg";
+
+    Maasha::BGB::Draw::render_svg_file( $file, $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' }, \@tracks );
+
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::ln( txt => "BGB_export.svg", href => $file ), class => 'center' );
+
+    return wantarray ? @html : \@html;
+}
+
+
 sub section_search
 {
     # Martin A. Hansen, November 2009.
@@ -1127,3 +1572,7 @@ sub section_dna
 
 
 __END__
+
+
+    # my $t0 = Time::HiRes::gettimeofday();
+    # my $t1 = Time::HiRes::gettimeofday(); print STDERR "Time: " . ( $t1 - $t0 ) . "\n";