]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Biopieces.pm
fixed rename bug
[biopieces.git] / code_perl / Maasha / Biopieces.pm
index b42a9945c48b9a1fd25b4e3310637d6862dddea4..6c38e430698324319052305b5c1c3afcf5fd84dc 100644 (file)
@@ -23,7 +23,7 @@ package Maasha::Biopieces;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-# Routines for manipulation, parsing and emitting of human/machine readable biotool records.
+# Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
 
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -51,7 +51,9 @@ use Maasha::NCBI;
 use Maasha::GFF;
 use Maasha::TwoBit;
 use Maasha::Solid;
+use Maasha::Solexa;
 use Maasha::SQL;
+use Maasha::Gwiki;
 
 use vars qw( @ISA @EXPORT_OK );
 
@@ -83,34 +85,32 @@ $SIG{ 'TERM' }    = \&sig_handler;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-my ( $script, $TMP_DIR );
+my ( $script, $BP_TMP );
 
-$script  = &Maasha::Common::get_scriptname();
-$TMP_DIR = &Maasha::Common::get_tmpdir();
+$script  = Maasha::Common::get_scriptname();
+$BP_TMP  = Maasha::Common::get_tmpdir();
 
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-my $log_fh = &Maasha::Common::append_open( $ENV{ "LOG_DIR" } . "/biopieces.log" );
+my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
+my $log_local  = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
 
-$log_fh->autoflush( 1 );
+$log_global->autoflush( 1 );
+$log_local->autoflush( 1 );
 
-&log( $log_fh, $script, \@ARGV );
+&log( $log_global, $script, \@ARGV );
+&log( $log_local, $script, \@ARGV );
 
-close $log_fh;
+close $log_global;
+close $log_local;
 
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-my $t0 = gettimeofday();
-
-&run_script( $script );
-
-my $t1 = gettimeofday();
-
-print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
+run_script( $script );
 
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -131,9 +131,9 @@ sub log
 
     my ( $time_stamp, $user );
 
-    $time_stamp = &Maasha::Common::time_stamp();
+    $time_stamp = Maasha::Common::time_stamp();
 
-    $user = $ENV{ "USER" };
+    $user = $ENV{ 'USER' };
 
     $script = "biopieces" if $script eq "-e";
 
@@ -152,108 +152,121 @@ sub run_script
 
     # Returns nothing.
 
-    my ( $options, $in, $out );
-
-    &script_list_biotools( $ENV{ 'INST_DIR'} . "/biotools/usage/" ) if $script eq "list_biotools";
-
-    &script_print_usage( $ENV{ 'INST_DIR'} . "/biotools/usage/$script" ) if -t STDIN and not @ARGV;
-
-    $options = &get_options( $script );
-
-    $in  = &read_stream( $options->{ "stream_in" } );
-    $out = &write_stream( $options->{ "stream_out" } );
-
-    if    ( $script eq "read_fasta" )               { &script_read_fasta( $in, $out, $options ) }
-    elsif ( $script eq "read_align" )               { &script_read_align( $in, $out, $options ) }
-    elsif ( $script eq "read_tab" )                 { &script_read_tab( $in, $out, $options ) }
-    elsif ( $script eq "read_psl" )                 { &script_read_psl( $in, $out, $options ) }
-    elsif ( $script eq "read_bed" )                 { &script_read_bed( $in, $out, $options ) }
-    elsif ( $script eq "read_blast_tab" )           { &script_read_blast_tab( $in, $out, $options ) }
-    elsif ( $script eq "read_embl" )                { &script_read_embl( $in, $out, $options ) }
-    elsif ( $script eq "read_stockholm" )           { &script_read_stockholm( $in, $out, $options ) }
-    elsif ( $script eq "read_phastcons" )           { &script_read_phastcons( $in, $out, $options ) }
-    elsif ( $script eq "read_soft" )                { &script_read_soft( $in, $out, $options ) }
-    elsif ( $script eq "read_gff" )                 { &script_read_gff( $in, $out, $options ) }
-    elsif ( $script eq "read_2bit" )                { &script_read_2bit( $in, $out, $options ) }
-    elsif ( $script eq "read_solexa" )              { &script_read_solexa( $in, $out, $options ) }
-    elsif ( $script eq "read_solid" )               { &script_read_solid( $in, $out, $options ) }
-    elsif ( $script eq "read_mysql" )               { &script_read_mysql( $in, $out, $options ) }
-    elsif ( $script eq "count_seq" )                { &script_count_seq( $in, $out, $options ) }
-    elsif ( $script eq "length_seq" )               { &script_length_seq( $in, $out, $options ) }
-    elsif ( $script eq "uppercase_seq" )            { &script_uppercase_seq( $in, $out, $options ) }
-    elsif ( $script eq "shuffle_seq" )              { &script_shuffle_seq( $in, $out, $options ) }
-    elsif ( $script eq "analyze_seq" )              { &script_analyze_seq( $in, $out, $options ) }
-    elsif ( $script eq "analyze_tags" )             { &script_analyze_tags( $in, $out, $options ) }
-    elsif ( $script eq "complexity_seq" )           { &script_complexity_seq( $in, $out, $options ) }
-    elsif ( $script eq "oligo_freq" )               { &script_oligo_freq( $in, $out, $options ) }
-    elsif ( $script eq "create_weight_matrix" )     { &script_create_weight_matrix( $in, $out, $options ) }
-    elsif ( $script eq "calc_bit_scores" )          { &script_calc_bit_scores( $in, $out, $options ) }
-    elsif ( $script eq "reverse_seq" )              { &script_reverse_seq( $in, $out, $options ) }
-    elsif ( $script eq "complement_seq" )           { &script_complement_seq( $in, $out, $options ) }
-    elsif ( $script eq "remove_indels" )            { &script_remove_indels( $in, $out, $options ) }
-    elsif ( $script eq "transliterate_seq" )        { &script_transliterate_seq( $in, $out, $options ) }
-    elsif ( $script eq "transliterate_vals" )       { &script_transliterate_vals( $in, $out, $options ) }
-    elsif ( $script eq "translate_seq" )            { &script_translate_seq( $in, $out, $options ) }
-    elsif ( $script eq "extract_seq" )              { &script_extract_seq( $in, $out, $options ) }
-    elsif ( $script eq "get_genome_seq" )           { &script_get_genome_seq( $in, $out, $options ) }
-    elsif ( $script eq "get_genome_align" )         { &script_get_genome_align( $in, $out, $options ) }
-    elsif ( $script eq "get_genome_phastcons" )     { &script_get_genome_phastcons( $in, $out, $options ) }
-    elsif ( $script eq "fold_seq" )                 { &script_fold_seq( $in, $out, $options ) }
-    elsif ( $script eq "split_seq" )                { &script_split_seq( $in, $out, $options ) }
-    elsif ( $script eq "split_bed" )                { &script_split_bed( $in, $out, $options ) }
-    elsif ( $script eq "align_seq" )                { &script_align_seq( $in, $out, $options ) }
-    elsif ( $script eq "tile_seq" )                 { &script_tile_seq( $in, $out, $options ) }
-    elsif ( $script eq "invert_align" )             { &script_invert_align( $in, $out, $options ) }
-    elsif ( $script eq "patscan_seq" )              { &script_patscan_seq( $in, $out, $options ) }
-    elsif ( $script eq "create_blast_db" )          { &script_create_blast_db( $in, $out, $options ) }
-    elsif ( $script eq "blast_seq" )                { &script_blast_seq( $in, $out, $options ) }
-    elsif ( $script eq "blat_seq" )                 { &script_blat_seq( $in, $out, $options ) }
-    elsif ( $script eq "match_seq" )                { &script_match_seq( $in, $out, $options ) }
-    elsif ( $script eq "create_vmatch_index" )      { &script_create_vmatch_index( $in, $out, $options ) }
-    elsif ( $script eq "vmatch_seq" )               { &script_vmatch_seq( $in, $out, $options ) }
-    elsif ( $script eq "write_fasta" )              { &script_write_fasta( $in, $out, $options, $options ) }
-    elsif ( $script eq "write_align" )              { &script_write_align( $in, $out, $options ) }
-    elsif ( $script eq "write_blast" )              { &script_write_blast( $in, $out, $options ) }
-    elsif ( $script eq "write_tab" )                { &script_write_tab( $in, $out, $options ) }
-    elsif ( $script eq "write_bed" )                { &script_write_bed( $in, $out, $options ) }
-    elsif ( $script eq "write_psl" )                { &script_write_psl( $in, $out, $options ) }
-    elsif ( $script eq "write_2bit" )               { &script_write_2bit( $in, $out, $options, $options ) }
-    elsif ( $script eq "write_solid" )              { &script_write_solid( $in, $out, $options, $options ) }
-    elsif ( $script eq "head_records" )             { &script_head_records( $in, $out, $options ) }
-    elsif ( $script eq "remove_keys" )              { &script_remove_keys( $in, $out, $options ) }
-    elsif ( $script eq "rename_keys" )              { &script_rename_keys( $in, $out, $options ) }
-    elsif ( $script eq "uniq_vals" )                { &script_uniq_vals( $in, $out, $options ) }
-    elsif ( $script eq "merge_vals" )               { &script_merge_vals( $in, $out, $options ) }
-    elsif ( $script eq "grab" )                     { &script_grab( $in, $out, $options ) }
-    elsif ( $script eq "compute" )                  { &script_compute( $in, $out, $options ) }
-    elsif ( $script eq "flip_tab" )                 { &script_flip_tab( $in, $out, $options ) }
-    elsif ( $script eq "add_ident" )                { &script_add_ident( $in, $out, $options ) }
-    elsif ( $script eq "count_records" )            { &script_count_records( $in, $out, $options ) }
-    elsif ( $script eq "random_records" )           { &script_random_records( $in, $out, $options ) }
-    elsif ( $script eq "sort_records" )             { &script_sort_records( $in, $out, $options ) }
-    elsif ( $script eq "count_vals" )               { &script_count_vals( $in, $out, $options ) }
-    elsif ( $script eq "plot_histogram" )           { &script_plot_histogram( $in, $out, $options ) }
-    elsif ( $script eq "plot_lendist" )             { &script_plot_lendist( $in, $out, $options ) }
-    elsif ( $script eq "plot_chrdist" )             { &script_plot_chrdist( $in, $out, $options ) }
-    elsif ( $script eq "plot_karyogram" )           { &script_plot_karyogram( $in, $out, $options ) }
-    elsif ( $script eq "plot_matches" )             { &script_plot_matches( $in, $out, $options ) }
-    elsif ( $script eq "plot_seqlogo" )             { &script_plot_seqlogo( $in, $out, $options ) }
-    elsif ( $script eq "plot_phastcons_profiles" )  { &script_plot_phastcons_profiles( $in, $out, $options ) }
-    elsif ( $script eq "analyze_bed" )              { &script_analyze_bed( $in, $out, $options ) }
-    elsif ( $script eq "analyze_vals" )             { &script_analyze_vals( $in, $out, $options ) }
-    elsif ( $script eq "length_vals" )              { &script_length_vals( $in, $out, $options ) }
-    elsif ( $script eq "sum_vals" )                 { &script_sum_vals( $in, $out, $options ) }
-    elsif ( $script eq "mean_vals" )                { &script_mean_vals( $in, $out, $options ) }
-    elsif ( $script eq "median_vals" )              { &script_median_vals( $in, $out, $options ) }
-    elsif ( $script eq "max_vals" )                 { &script_max_vals( $in, $out, $options ) }
-    elsif ( $script eq "min_vals" )                 { &script_min_vals( $in, $out, $options ) }
-    elsif ( $script eq "upload_to_ucsc" )           { &script_upload_to_ucsc( $in, $out, $options ) }
+    my ( $t0, $t1, $options, $in, $out );
+
+    $t0 = gettimeofday();
+
+    $options = get_options( $script );
+
+    $options->{ "SCRIPT" } = $script;
+
+    if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
+        $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
+    }
+
+    $in  = read_stream( $options->{ "stream_in" } );
+    $out = write_stream( $options->{ "stream_out" } );
+
+    if    ( $script eq "print_usage" )              { script_print_usage(               $in, $out, $options ) }
+    elsif ( $script eq "list_biopieces" )           { script_list_biopieces(            $in, $out, $options ) }
+    elsif ( $script eq "list_genomes" )             { script_list_genomes(              $in, $out, $options ) }
+    elsif ( $script eq "read_fasta" )               { script_read_fasta(                $in, $out, $options ) }
+    elsif ( $script eq "read_tab" )                 { script_read_tab(                  $in, $out, $options ) }
+    elsif ( $script eq "read_psl" )                 { script_read_psl(                  $in, $out, $options ) }
+    elsif ( $script eq "read_bed" )                 { script_read_bed(                  $in, $out, $options ) }
+    elsif ( $script eq "read_fixedstep" )           { script_read_fixedstep(            $in, $out, $options ) }
+    elsif ( $script eq "read_blast_tab" )           { script_read_blast_tab(            $in, $out, $options ) }
+    elsif ( $script eq "read_embl" )                { script_read_embl(                 $in, $out, $options ) }
+    elsif ( $script eq "read_stockholm" )           { script_read_stockholm(            $in, $out, $options ) }
+    elsif ( $script eq "read_phastcons" )           { script_read_phastcons(            $in, $out, $options ) }
+    elsif ( $script eq "read_soft" )                { script_read_soft(                 $in, $out, $options ) }
+    elsif ( $script eq "read_gff" )                 { script_read_gff(                  $in, $out, $options ) }
+    elsif ( $script eq "read_2bit" )                { script_read_2bit(                 $in, $out, $options ) }
+    elsif ( $script eq "read_solexa" )              { script_read_solexa(               $in, $out, $options ) }
+    elsif ( $script eq "read_solid" )               { script_read_solid(                $in, $out, $options ) }
+    elsif ( $script eq "read_mysql" )               { script_read_mysql(                $in, $out, $options ) }
+    elsif ( $script eq "format_genome" )            { script_format_genome(             $in, $out, $options ) }
+    elsif ( $script eq "length_seq" )               { script_length_seq(                $in, $out, $options ) }
+    elsif ( $script eq "uppercase_seq" )            { script_uppercase_seq(             $in, $out, $options ) }
+    elsif ( $script eq "shuffle_seq" )              { script_shuffle_seq(               $in, $out, $options ) }
+    elsif ( $script eq "analyze_seq" )              { script_analyze_seq(               $in, $out, $options ) }
+    elsif ( $script eq "analyze_tags" )             { script_analyze_tags(              $in, $out, $options ) }
+    elsif ( $script eq "complexity_seq" )           { script_complexity_seq(            $in, $out, $options ) }
+    elsif ( $script eq "oligo_freq" )               { script_oligo_freq(                $in, $out, $options ) }
+    elsif ( $script eq "create_weight_matrix" )     { script_create_weight_matrix(      $in, $out, $options ) }
+    elsif ( $script eq "calc_bit_scores" )          { script_calc_bit_scores(           $in, $out, $options ) }
+    elsif ( $script eq "calc_fixedstep" )           { script_calc_fixedstep(            $in, $out, $options ) }
+    elsif ( $script eq "reverse_seq" )              { script_reverse_seq(               $in, $out, $options ) }
+    elsif ( $script eq "complement_seq" )           { script_complement_seq(            $in, $out, $options ) }
+    elsif ( $script eq "remove_indels" )            { script_remove_indels(             $in, $out, $options ) }
+    elsif ( $script eq "transliterate_seq" )        { script_transliterate_seq(         $in, $out, $options ) }
+    elsif ( $script eq "transliterate_vals" )       { script_transliterate_vals(        $in, $out, $options ) }
+    elsif ( $script eq "translate_seq" )            { script_translate_seq(             $in, $out, $options ) }
+    elsif ( $script eq "extract_seq" )              { script_extract_seq(               $in, $out, $options ) }
+    elsif ( $script eq "get_genome_seq" )           { script_get_genome_seq(            $in, $out, $options ) }
+    elsif ( $script eq "get_genome_align" )         { script_get_genome_align(          $in, $out, $options ) }
+    elsif ( $script eq "get_genome_phastcons" )     { script_get_genome_phastcons(      $in, $out, $options ) }
+    elsif ( $script eq "fold_seq" )                 { script_fold_seq(                  $in, $out, $options ) }
+    elsif ( $script eq "split_seq" )                { script_split_seq(                 $in, $out, $options ) }
+    elsif ( $script eq "split_bed" )                { script_split_bed(                 $in, $out, $options ) }
+    elsif ( $script eq "align_seq" )                { script_align_seq(                 $in, $out, $options ) }
+    elsif ( $script eq "tile_seq" )                 { script_tile_seq(                  $in, $out, $options ) }
+    elsif ( $script eq "invert_align" )             { script_invert_align(              $in, $out, $options ) }
+    elsif ( $script eq "patscan_seq" )              { script_patscan_seq(               $in, $out, $options ) }
+    elsif ( $script eq "create_blast_db" )          { script_create_blast_db(           $in, $out, $options ) }
+    elsif ( $script eq "blast_seq" )                { script_blast_seq(                 $in, $out, $options ) }
+    elsif ( $script eq "blat_seq" )                 { script_blat_seq(                  $in, $out, $options ) }
+    elsif ( $script eq "soap_seq" )                 { script_soap_seq(                  $in, $out, $options ) }
+    elsif ( $script eq "match_seq" )                { script_match_seq(                 $in, $out, $options ) }
+    elsif ( $script eq "create_vmatch_index" )      { script_create_vmatch_index(       $in, $out, $options ) }
+    elsif ( $script eq "vmatch_seq" )               { script_vmatch_seq(                $in, $out, $options ) }
+    elsif ( $script eq "write_fasta" )              { script_write_fasta(               $in, $out, $options ) }
+    elsif ( $script eq "write_align" )              { script_write_align(               $in, $out, $options ) }
+    elsif ( $script eq "write_blast" )              { script_write_blast(               $in, $out, $options ) }
+    elsif ( $script eq "write_tab" )                { script_write_tab(                 $in, $out, $options ) }
+    elsif ( $script eq "write_bed" )                { script_write_bed(                 $in, $out, $options ) }
+    elsif ( $script eq "write_psl" )                { script_write_psl(                 $in, $out, $options ) }
+    elsif ( $script eq "write_fixedstep" )          { script_write_fixedstep(           $in, $out, $options ) }
+    elsif ( $script eq "write_2bit" )               { script_write_2bit(                $in, $out, $options ) }
+    elsif ( $script eq "write_solid" )              { script_write_solid(               $in, $out, $options ) }
+    elsif ( $script eq "head_records" )             { script_head_records(              $in, $out, $options ) }
+    elsif ( $script eq "remove_keys" )              { script_remove_keys(               $in, $out, $options ) }
+    elsif ( $script eq "remove_adaptor" )           { script_remove_adaptor(            $in, $out, $options ) }
+    elsif ( $script eq "rename_keys" )              { script_rename_keys(               $in, $out, $options ) }
+    elsif ( $script eq "uniq_vals" )                { script_uniq_vals(                 $in, $out, $options ) }
+    elsif ( $script eq "merge_vals" )               { script_merge_vals(                $in, $out, $options ) }
+    elsif ( $script eq "merge_records" )            { script_merge_records(             $in, $out, $options ) }
+    elsif ( $script eq "grab" )                     { script_grab(                      $in, $out, $options ) }
+    elsif ( $script eq "compute" )                  { script_compute(                   $in, $out, $options ) }
+    elsif ( $script eq "flip_tab" )                 { script_flip_tab(                  $in, $out, $options ) }
+    elsif ( $script eq "add_ident" )                { script_add_ident(                 $in, $out, $options ) }
+    elsif ( $script eq "count_records" )            { script_count_records(             $in, $out, $options ) }
+    elsif ( $script eq "random_records" )           { script_random_records(            $in, $out, $options ) }
+    elsif ( $script eq "sort_records" )             { script_sort_records(              $in, $out, $options ) }
+    elsif ( $script eq "count_vals" )               { script_count_vals(                $in, $out, $options ) }
+    elsif ( $script eq "plot_histogram" )           { script_plot_histogram(            $in, $out, $options ) }
+    elsif ( $script eq "plot_lendist" )             { script_plot_lendist(              $in, $out, $options ) }
+    elsif ( $script eq "plot_chrdist" )             { script_plot_chrdist(              $in, $out, $options ) }
+    elsif ( $script eq "plot_karyogram" )           { script_plot_karyogram(            $in, $out, $options ) }
+    elsif ( $script eq "plot_matches" )             { script_plot_matches(              $in, $out, $options ) }
+    elsif ( $script eq "plot_seqlogo" )             { script_plot_seqlogo(              $in, $out, $options ) }
+    elsif ( $script eq "plot_phastcons_profiles" )  { script_plot_phastcons_profiles(   $in, $out, $options ) }
+    elsif ( $script eq "analyze_bed" )              { script_analyze_bed(               $in, $out, $options ) }
+    elsif ( $script eq "analyze_vals" )             { script_analyze_vals(              $in, $out, $options ) }
+    elsif ( $script eq "length_vals" )              { script_length_vals(               $in, $out, $options ) }
+    elsif ( $script eq "sum_vals" )                 { script_sum_vals(                  $in, $out, $options ) }
+    elsif ( $script eq "mean_vals" )                { script_mean_vals(                 $in, $out, $options ) }
+    elsif ( $script eq "median_vals" )              { script_median_vals(               $in, $out, $options ) }
+    elsif ( $script eq "max_vals" )                 { script_max_vals(                  $in, $out, $options ) }
+    elsif ( $script eq "min_vals" )                 { script_min_vals(                  $in, $out, $options ) }
+    elsif ( $script eq "upload_to_ucsc" )           { script_upload_to_ucsc(            $in, $out, $options ) }
 
     close $in if defined $in;
     close $out;
 
-    # unset status   - missing
-    # write log file - missing
+    $t1 = gettimeofday();
+
+    print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
 }
 
 
@@ -263,21 +276,20 @@ sub get_options
 
     # Gets options from commandline and checks these vigerously.
 
-    my ( $script,   # name of script
+    my ( $script,     # name of script
        ) = @_;
 
     # Returns hash
 
-    my ( %options, @options, $opt, @genomes );
+    my ( %options, @options, $opt, @genomes, $real );
 
-    if ( $script eq "read_fasta" )
+    if ( $script eq "print_usage" )
     {
         @options = qw(
             data_in|i=s
-            num|n=s
         );
     }
-    elsif ( $script eq "read_align" )
+    elsif ( $script eq "read_fasta" )
     {
         @options = qw(
             data_in|i=s
@@ -309,6 +321,13 @@ sub get_options
             num|n=s
         );
     }
+    elsif ( $script eq "read_fixedstep" )
+    {
+        @options = qw(
+            data_in|i=s
+            num|n=s
+        );
+    }
     elsif ( $script eq "read_blast_tab" )
     {
         @options = qw(
@@ -348,6 +367,7 @@ sub get_options
     {
         @options = qw(
             data_in|i=s
+            samples|s=s
             num|n=s
         );
     }
@@ -371,6 +391,7 @@ sub get_options
         @options = qw(
             data_in|i=s
             num|n=s
+            format|f=s
             quality|q=s
         );
     }
@@ -391,11 +412,13 @@ sub get_options
             password|p=s
         );
     }
-    elsif ( $script eq "count_seq" )
+    elsif ( $script eq "format_genome" )
     {
         @options = qw(
             no_stream|x
-            data_out|o=s
+            dir|d=s
+            genome|g=s
+            formats|f=s
         );
     }
     elsif ( $script eq "length_seq" )
@@ -418,6 +441,13 @@ sub get_options
             percent|p
         );
     }
+    elsif ( $script eq "calc_fixedstep" )
+    {
+        @options = qw(
+            score|S
+            log10|L
+        );
+    }
     elsif ( $script eq "transliterate_seq" )
     {
         @options = qw(
@@ -552,6 +582,17 @@ sub get_options
             ooc|c
         );
     }
+    elsif ( $script eq "soap_seq" )
+    {
+        @options = qw(
+            in_file|i=s
+            genome|g=s
+            seed_size|s=s
+            mismatches|m=s
+            gap_size|G=s
+            cpus|c=s
+        );
+    }
     elsif ( $script eq "match_seq" )
     {
         @options = qw(
@@ -634,6 +675,14 @@ sub get_options
             compress|Z
         );
     }
+    elsif ( $script eq "write_fixedstep" )
+    {
+        @options = qw(
+            no_stream|x
+            data_out|o=s
+            compress|Z
+        );
+    }
     elsif ( $script eq "write_2bit" )
     {
         @options = qw(
@@ -693,6 +742,15 @@ sub get_options
             save_keys|K=s
         );
     }
+    elsif ( $script eq "remove_adaptor" )
+    {
+        @options = qw(
+            adaptor|a=s
+            mismatches|m=s
+            remove|r=s
+            offset|o=s
+        );
+    }
     elsif ( $script eq "rename_keys" )
     {
         @options = qw(
@@ -713,6 +771,13 @@ sub get_options
             delimit|d=s
         );
     }
+    elsif ( $script eq "merge_records" )
+    {
+        @options = qw(
+            keys|k=s
+            merge|m=s
+        );
+    }
     elsif ( $script eq "grab" )
     {
         @options = qw(
@@ -882,7 +947,6 @@ sub get_options
             priority|p=f
             use_score|u
             visibility|v=s
-            wiggle|w
             color|c=s
             chunk_size|C=s
         );
@@ -892,15 +956,22 @@ sub get_options
         stream_in|I=s
         stream_out|O=s
         verbose
+        help|?
     );
 
 #    print STDERR Dumper( \@options );
-
+    
     GetOptions(
         \%options,
         @options,
     );
 
+#    print STDERR Dumper( \%options );
+
+    if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
+        return wantarray ? %options : \%options;
+    }
+
     $options{ "cols" }      = [ split ",", $options{ "cols" } ]      if defined $options{ "cols" };
     $options{ "keys" }      = [ split ",", $options{ "keys" } ]      if defined $options{ "keys" };
     $options{ "no_keys" }   = [ split ",", $options{ "no_keys" } ]   if defined $options{ "no_keys" };
@@ -908,78 +979,97 @@ sub get_options
     $options{ "quals" }     = [ split ",", $options{ "quals" } ]     if defined $options{ "quals" };
     $options{ "feats" }     = [ split ",", $options{ "feats" } ]     if defined $options{ "feats" };
     $options{ "frames" }    = [ split ",", $options{ "frames" } ]    if defined $options{ "frames" };
+    $options{ "formats" }   = [ split ",", $options{ "formats" } ]   if defined $options{ "formats" };
+    $options{ "samples" }   = [ split ",", $options{ "samples" } ]   if defined $options{ "samples" };
     
     # ---- check arguments ----
 
     if ( $options{ 'data_in' } )
     {
-        $options{ "files" } = &getopt_files( $options{ 'data_in' } );
+        $options{ "files" } = getopt_files( $options{ 'data_in' } );
 
-        &Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
+        Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
     }
 
-    map { &Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
+    map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
 
-#    print STDERR Dumper( \%options );
+    # print STDERR Dumper( \%options );
+
+    $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
 
     foreach $opt ( keys %options )
     {
         if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
         {
-            &Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
         }
-        elsif ( $opt =~ /beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|num|skip|cpus|window_size|step_size/ and $options{ $opt } !~ /^\d+$/ )
+        elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
         {
-            &Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
         }
         elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
         {
-            &Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
         }
         elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
         {
-            &Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
         }
         elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
         {
-            &Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
         }
         elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
         {
-            &Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
         }
-        elsif ( $opt eq "genome" )
+        elsif ( $opt eq "genome" and $script ne "format_genome" )
         {
-            @genomes = &Maasha::Config::genomes();
-        
-            if ( not grep $options{ $opt }, @genomes ) {
-                &Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'INST_DIR' }/conf/genomes.conf") );
+            @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
+            map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
+
+            if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
+                Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
             }
         }
-        elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
+        elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
+        {
+            Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
+        }
+        elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
+        {
+            Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
+        }
+        elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
+        {
+            Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
+        }
+        elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
         {
-            &Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
+            Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
         }
-        elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
+        elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
         {
-            &Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
+            Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
         }
     }
 
-    &Maasha::Common::error( qq(no --database specified) )                if $script eq "create_blast_db"     and not $options{ "database" };
-    &Maasha::Common::error( qq(no --index_name specified) )              if $script eq "create_vmatch_index" and not $options{ "index_name" };
-    &Maasha::Common::error( qq(no --database or --genome specified) )    if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
-    &Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
-    &Maasha::Common::error( qq(no --index_name or --genome specified) )  if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
-    &Maasha::Common::error( qq(both --index and --genome specified) )    if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
-    &Maasha::Common::error( qq(no --genome specified) )                  if $script =~ /get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
-    &Maasha::Common::error( qq(no --key specified) )                     if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
-    &Maasha::Common::error( qq(no --keys speficied) )                    if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
+    Maasha::Common::error( qq(no --database specified) )                if $script eq "create_blast_db"     and not $options{ "database" };
+    Maasha::Common::error( qq(no --index_name specified) )              if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
+    Maasha::Common::error( qq(no --database or --genome specified) )    if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
+    Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
+    Maasha::Common::error( qq(no --index_name or --genome specified) )  if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
+    Maasha::Common::error( qq(both --index and --genome specified) )    if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
+    Maasha::Common::error( qq(no --in_file or --genome specified) )     if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
+    Maasha::Common::error( qq(both --in_file and --genome specified) )  if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
+    Maasha::Common::error( qq(no --genome specified) )                  if $script =~ /format_genome|get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
+    Maasha::Common::error( qq(no --key specified) )                     if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
+    Maasha::Common::error( qq(no --keys speficied) )                    if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
 
     if ( $script eq "upload_to_ucsc" )
     {
-        &Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
-        &Maasha::Common::error( qq(no --table specified) )    if not $options{ "table" };
+        Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
+        Maasha::Common::error( qq(no --table specified) )    if not $options{ "table" };
     }
 
     return wantarray ? %options : \%options;
@@ -996,111 +1086,79 @@ sub script_print_usage
     # Retrieves usage information from file and
     # prints this nicely formatted.
 
-    my ( $path,   #   full path to usage file
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
        ) = @_;
 
     # Returns nothing.
 
-    my ( $script, $fh, $line, @lines, @list, %hash, $key );
-
-    $script = ( split "/", $path )[ -1 ];
-
-    $fh = &Maasha::Common::read_open( $path );
-
-    push @list, "Program name";
-
-    $hash{ "Program name" } = [ $script ];
-
-    while ( $line = <$fh> )
-    {
-        chomp $line;
-
-        $line =~ s/\$script/$script/g;
+    my ( $file, $wiki, $lines );
 
-        if ( $line =~ /^([^:]+):\s+(.+)$/ )
-        {
-            push @list, $1 if not exists $hash{ $1 }; 
-            push @{ $hash{ $1 } }, $2;
-        }
+    if ( $options->{ 'data_in' } ) {
+        $file = $options->{ 'data_in' };
+    } else {
+        $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
     }
 
-    close $fh;
-
-    print "\n";
-
-    foreach $key ( @list )
-    {
-        if ( scalar @{ $hash{ $key } } == 1 )
-        {
-            @lines = &Maasha::Common::wrap_line( $hash{ $key }->[ 0 ], 80 );
-
-            printf( "%-15s%s\n", "$key:", shift @lines );
-
-            map { printf( "%-15s%s\n", "", $_ ) } @lines;
+    $wiki = Maasha::Gwiki::gwiki_read( $file );
 
-            print "\n";
-        }
-        else
-        {
-            print "$key:\n";
+    if ( not $options->{ "help" } ) {
+        @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
+    }
 
-            map { print "   $_\n" } @{ $hash{ $key } };
+    $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
 
-            print "\n";
-        }
-    }
+    print STDERR "$_\n" foreach @{ $lines };
 
     exit;
 }
 
 
-sub script_list_biotools
+sub script_list_biopieces
 {
     # Martin A. Hansen, January 2008.
 
-    # Prints the description from the usage for each of the biotools.
+    # Prints the synopsis from the usage for each of the biopieces.
 
-    my ( $path,    # full path to usage directory
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
        ) = @_;
 
     # Returns nothing.
 
-    my ( @files, $file, $fh, $line, @lines, $program );
+    my ( @files, $file, $wiki, $program, $synopsis );
 
-    @files = &Maasha::Common::ls_files( $path );
+    @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
 
     foreach $file ( sort @files )
     {
-        $program = ( split "/", $file )[ -1 ];
-
-        $fh = &Maasha::Common::read_open( $file );
-
-        while ( $line = <$fh> )
+        if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
         {
-            chomp $line;
+            $program = $1;
 
-            if ( $line =~ /^Description:\s+(.+)/ )
-            {
-                @lines = &Maasha::Common::wrap_line( $1, 60 );
+            $wiki = Maasha::Gwiki::gwiki_read( $file );
 
-                printf( "%-30s%s\n", $program, shift @lines );
+            @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ }  @{ $wiki };
+            @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' }  =~ /paragraph/ } @{ $wiki };
 
-                map { printf( "%-30s%s\n", "", $_ ) } @lines;
-            }
-        }
+            $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
+            $synopsis =~ s/!(\w)/$1/g;
 
-        close $fh;
+            printf( "%-30s%s\n", $program, $synopsis );
+        }
     }
 
     exit;
 }
 
 
-sub script_read_fasta
+sub script_list_genomes
 {
-    # Martin A. Hansen, August 2007.
+    # Martin A. Hansen, January 2008.
 
-    # Read sequences from FASTA file.
+    # Prints the synopsis from the usage for each of the biopieces.
 
     my ( $in,        # handle to in stream
          $out,       # handle to out stream
@@ -1109,50 +1167,56 @@ sub script_read_fasta
 
     # Returns nothing.
 
-    my ( $record, $file, $data_in, $entry, $num );
-
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
-    }
+    my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
 
-    $num = 1;
+    @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
 
-    foreach $file ( @{ $options->{ "files" } } )
+    foreach $genome ( @genomes )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        next if $genome =~ /\.$/;
 
-        while ( $entry = &Maasha::Fasta::get_entry( $data_in ) ) 
+        @formats = Maasha::Common::ls_dirs( $genome );
+
+        foreach $format ( @formats )
         {
-            if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
+            if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
             {
-                $record = {
-                    SEQ_NAME => $entry->[ SEQ_NAME ],
-                    SEQ      => $entry->[ SEQ ],
-                    SEQ_LEN  => length $entry->[ SEQ ],
-                };
+                $hash{ $1 }{ $2 } = 1;
 
-                &put_record( $record, $out );
+                $found{ $2 } = 1;
             }
+        }
+    }
 
-            goto NUM if $options->{ "num" } and $num == $options->{ "num" };
+    @row = "Genome";
 
-            $num++;
-        }
+    map { push @row, $_ } sort keys %found;
 
-        close $data_in;
-    }
+    print join( "\t", @row ), "\n";
 
-    NUM:
+    foreach $genome ( sort keys %hash )
+    {
+        @row = $genome;
 
-    close $data_in if $data_in;
+        foreach $format ( sort keys %found )
+        {
+            if ( exists $hash{ $genome }{ $format } ) {
+                push @row, "yes";
+            } else {
+                push @row, "no";
+            }
+        }
+
+        print join( "\t", @row ), "\n";
+    }
 }
 
 
-sub script_read_align
+sub script_read_fasta
 {
     # Martin A. Hansen, August 2007.
 
-    # Read aligned sequences from FASTA file.
+    # Read sequences from FASTA file.
 
     my ( $in,        # handle to in stream
          $out,       # handle to out stream
@@ -1161,30 +1225,29 @@ sub script_read_align
 
     # Returns nothing.
 
-    my ( $entry, $record, $file, $data_in, $num );
+    my ( $record, $file, $data_in, $entry, $num );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        while ( $entry = &Maasha::Fasta::get_entry( $data_in ) ) 
+        while ( $entry = Maasha::Fasta::get_entry( $data_in ) ) 
         {
-            if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
+            if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
             {
                 $record = {
-                    ALIGN     => 1,
-                    SEQ_NAME  => $entry->[ SEQ_NAME ],
-                    SEQ       => $entry->[ SEQ ],
-                    ALIGN_LEN => length $entry->[ SEQ ],
+                    SEQ_NAME => $entry->[ SEQ_NAME ],
+                    SEQ      => $entry->[ SEQ ],
+                    SEQ_LEN  => length $entry->[ SEQ ],
                 };
 
-                &put_record( $record, $out );
+                put_record( $record, $out );
             }
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
@@ -1218,8 +1281,8 @@ sub script_read_tab
 
     $options->{ 'delimit' } ||= '\s+';
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $skip = $options->{ 'skip' } ||= 0;
@@ -1227,7 +1290,7 @@ sub script_read_tab
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
         while ( $line = <$data_in> ) 
         {
@@ -1261,7 +1324,7 @@ sub script_read_tab
                 }
             }
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1290,21 +1353,21 @@ sub script_read_psl
 
     # Returns nothing.
 
-    my ( $record, @files, $file, $entries, $entry, $num );
+    my ( $record, $file, $data_in, $num );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $entries = &Maasha::UCSC::psl_get_entries( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        foreach $entry ( @{ $entries } )
+        while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) ) 
         {
-            &put_record( $entry, $out );
+            put_record( $record, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1331,19 +1394,73 @@ sub script_read_bed
 
     my ( $file, $record, $entry, $data_in, $num );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
+    }
+
+    $num = 1;
+
+    foreach $file ( @{ $options->{ "files" } } )
+    {
+        $data_in = Maasha::Common::read_open( $file );
+
+        while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
+        {
+            put_record( $entry, $out );
+
+            goto NUM if $options->{ "num" } and $num == $options->{ "num" };
+
+            $num++;
+        }
+
+        close $data_in;
+    }
+
+    NUM:
+
+    close $data_in if $data_in;
+}
+
+
+sub script_read_fixedstep
+{
+    # Martin A. Hansen, Juli 2008.
+
+    # Read fixedstep wiggle format from stream or file.
+
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
+
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        while ( $entry = &Maasha::UCSC::bed_get_entry( $data_in ) )
+        while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
         {
-            &put_record( $entry, $out );
+            $head = shift @{ $entry };
+
+            if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
+            {
+                $record->{ "REC_TYPE" } = "fixed_step";
+                $record->{ "CHR" }      = $1;
+                $record->{ "CHR_BEG" }  = $2;
+                $record->{ "STEP" }     = $3;
+                $record->{ "VALS" }     = join ";", @{ $entry };
+            }
+
+            put_record( $record, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1374,15 +1491,15 @@ sub script_read_blast_tab
 
     my ( $file, $line, @fields, $strand, $record, $data_in, $num );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
         while ( $line = <$data_in> )
         {
@@ -1417,7 +1534,7 @@ sub script_read_blast_tab
                 $record->{ "STRAND" } = '+';
             }
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1452,19 +1569,19 @@ sub script_read_embl
     map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
     map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        while ( $entry = &Maasha::EMBL::get_embl_entry( $data_in ) ) 
+        while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) ) 
         {
-            $record = &Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
+            $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
 
             my ( $feat, $feat2, $qual, $qual_val, $record_copy );
 
@@ -1472,7 +1589,7 @@ sub script_read_embl
 
             delete $record_copy->{ "FT" };
 
-            &put_record( $record_copy, $out );
+            put_record( $record_copy, $out );
 
             delete $record_copy->{ "SEQ" };
 
@@ -1492,7 +1609,7 @@ sub script_read_embl
                         $record_copy->{ $qual } = $qual_val;
                     }
 
-                    &put_record( $record_copy, $out );
+                    put_record( $record_copy, $out );
                 }
             }
 
@@ -1525,19 +1642,19 @@ sub script_read_stockholm
 
     my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        while ( $entry = &Maasha::Stockholm::get_stockholm_entry( $data_in ) ) 
+        while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) ) 
         {
-            $record = &Maasha::Stockholm::parse_stockholm_entry( $entry );
+            $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
 
             undef $record_anno;
 
@@ -1547,19 +1664,18 @@ sub script_read_stockholm
 
             $record_anno->{ "ALIGN" } = $num;
 
-            &put_record( $record_anno, $out );
+            put_record( $record_anno, $out );
 
             foreach $seq ( @{ $record->{ "ALIGN" } } )
             {
                 undef $record_align;
             
                 $record_align = {
-                    ALIGN     => $num,
                     SEQ_NAME  => $seq->[ 0 ],
                     SEQ       => $seq->[ 1 ],
                 };
             
-                &put_record( $record_align, $out );
+                put_record( $record_align, $out );
             }
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
@@ -1596,26 +1712,26 @@ sub script_read_phastcons
     $options->{ "threshold" } ||= 0.8;
     $options->{ "gap" }       ||= 5;
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        while ( $entry = &Maasha::UCSC::phastcons_get_entry( $data_in ) ) 
+        while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) ) 
         {
-            @records = &Maasha::UCSC::phastcons_parse_entry( $entry, $options );
+            @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
 
             foreach $record ( @records )
             {
                 $record->{ "REC_TYPE" } = "BED";
                 $record->{ "BED_LEN" }  = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
 
-                &put_record( $record, $out );
+                put_record( $record, $out );
 
                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1646,42 +1762,51 @@ sub script_read_soft
 
     # Returns nothing.
 
-    my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
+    my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $soft_index = &Maasha::NCBI::soft_index_file( $file );
+        print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
+
+        $soft_index = Maasha::NCBI::soft_index_file( $file );
+
+        $fh         = Maasha::Common::read_open( $file );
 
-        $fh         = &Maasha::Common::read_open( $file );
+        @platforms  = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
 
-        @platforms  = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
+        print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
 
-        $plat_table = &Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
+        $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
 
-        @samples    = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
+        @samples    = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
 
-        $old_end    = $platforms[ -1 ]->[ 2 ];
+        $old_end    = $platforms[ -1 ]->{ "LINE_END" };
 
         foreach $sample ( @samples )
         {
-            $records = &Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
+            $skip = 0;
+            $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
+
+            print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
+
+            $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
 
             foreach $record ( @{ $records } )
             {
-                &put_record( $record, $out );
+                put_record( $record, $out );
 
                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
                 $num++;
             }
 
-            $old_end = $sample->[ 2 ];
+            $old_end = $sample->{ "LINE_END" };
         }
 
         close $fh;
@@ -1710,19 +1835,19 @@ sub script_read_gff
 
     my ( $data_in, $file, $fh, $num, $record, $entry );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $fh = &Maasha::Common::read_open( $file );
+        $fh = Maasha::Common::read_open( $file );
 
-        while ( $entry = &Maasha::GFF::get_entry( $fh ) )
+        while ( $entry = Maasha::GFF::get_entry( $fh ) )
         {
-            &put_record( $entry, $out );
+            put_record( $entry, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1755,25 +1880,25 @@ sub script_read_2bit
 
     $mask = 1 if not $options->{ "no_mask" };
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
-        $toc = &Maasha::TwoBit::twobit_get_TOC( $data_in );
+        $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
 
         foreach $line ( @{ $toc } )
         {
             $record->{ "SEQ_NAME" } = $line->[ 0 ];
-            $record->{ "SEQ" }      = &Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
+            $record->{ "SEQ" }      = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
             $record->{ "SEQ_LEN" }  = length $record->{ "SEQ" };
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1802,47 +1927,46 @@ sub script_read_solexa
 
     # Returns nothing.
 
-    my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
+    my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
 
+    $options->{ "format" }  ||= "octal";
     $options->{ "quality" } ||= 20;
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in   = &Maasha::Common::read_open( $file );
-        $base_name = &Maasha::Common::get_basename( $file );
-        $base_name =~ s/\..*//;
+        $data_in = Maasha::Common::read_open( $file );
 
-        $seq_count = 0;
-
-        while ( $line = <$data_in> )
+        if ( $options->{ "format" } eq "octal" )
         {
-            @fields = split /:/, $line;
-            @seqs   = split //, $fields[ 5 ];
-            @scores = split / /, $fields[ -1 ];
+            while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
+            {
+                $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
 
-            for ( $i = 0; $i < @scores; $i++ ) {
-                $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
-            }
+                put_record( $record, $out );
 
-            $seq = join "", @seqs;
+                goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
-            $record->{ "SEQ_NAME" }     = sprintf( "%s_ID%08d", $base_name, $seq_count );
-            $record->{ "SEQ" }          = $seq;
-            $record->{ "SEQ_LEN" }      = length $seq;
-            $record->{ "SCORE_MEAN" }   = sprintf ( "%.2f", &Maasha::Calc::mean( \@scores ) );
+                $num++;
+            }
+        }
+        else
+        {
+            while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
+            {
+                $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
 
-            &put_record( $record, $out );
+                put_record( $record, $out );
 
-            goto NUM if $options->{ "num" } and $num == $options->{ "num" };
+                goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
-            $seq_count++;
-            $num++;
+                $num++;
+            }
         }
 
         close $data_in;
@@ -1871,15 +1995,15 @@ sub script_read_solid
 
     $options->{ "quality" } ||= 15;
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
     $num = 1;
 
     foreach $file ( @{ $options->{ "files" } } )
     {
-        $data_in = &Maasha::Common::read_open( $file );
+        $data_in = Maasha::Common::read_open( $file );
 
         while ( $line = <$data_in> )
         {
@@ -1888,7 +2012,7 @@ sub script_read_solid
             ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
 
             @scores = split /,/, $seq_qual;
-            @seqs   = split //, &Maasha::Solid::color_space2seq( $seq_cs );
+            @seqs   = split //, Maasha::Solid::color_space2seq( $seq_cs );
 
             for ( $i = 0; $i < @seqs; $i++ ) {
                 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
@@ -1900,10 +2024,10 @@ sub script_read_solid
                 SEQ_QUAL   => $seq_qual,
                 SEQ_LEN    => length $seq_cs,
                 SEQ        => join( "", @seqs ),
-                SCORE_MEAN => sprintf( "%.2f", &Maasha::Calc::mean( \@scores ) ),
+                SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
             };
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
 
             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
 
@@ -1934,28 +2058,28 @@ sub script_read_mysql
 
     my ( $record, $dbh, $results );
 
-    $options->{ "user" }     ||= &Maasha::UCSC::ucsc_get_user();
-    $options->{ "password" } ||= &Maasha::UCSC::ucsc_get_password();
+    $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
+    $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
+    while ( $record = get_record( $in ) ) {
+        put_record( $record, $out );
     }
 
-    $dbh = &Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
+    $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
 
-    $results = &Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
+    $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
 
-    &Maasha::SQL::disconnect( $dbh );
+    Maasha::SQL::disconnect( $dbh );
 
-    map { &put_record( $_ ) } @{ $results };
+    map { put_record( $_ ) } @{ $results };
 }
 
 
-sub script_count_seq
+sub script_format_genome
 {
-    # Martin A. Hansen, August 2007.
+    # Martin A. Hansen, Juli 2008.
 
-    # Count sequences in stream.
+    # Format a genome to speficed formats.
 
     my ( $in,        # handle to in stream
          $out,       # handle to out stream
@@ -1964,24 +2088,69 @@ sub script_count_seq
 
     # Returns nothing.
 
-    my ( $record, $count, $result, $fh );
+    my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
 
-    $count = 0;
+    $dir    = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
+    $genome = $options->{ 'genome' };
+
+    Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
+    Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
+    Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
 
-    while ( $record = &get_record( $in ) ) 
+    if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
     {
-        $count++ if $record->{ "SEQ" };
+        if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
+        {
+            $fasta_dir = "$dir/genomes/$genome/fasta";
+        }
+        else
+        {
+            Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+            $fasta_dir = "$dir/genomes/$genome/fasta";
+
+            $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
+        }
     }
+    elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
+    {
+        Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
+    
+        $phastcons_dir = "$dir/genomes/$genome/phastcons";
 
-    $result = { "count_seq" => $count };
+        $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
+    }
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( $fh_out and $entry = record2fasta( $record ) )
+        {
+            Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
+        }
+        elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
+        {
+            print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
 
-    &put_record( $result, $fh );
+            $vals = $record->{ 'VALS' };
 
-    close $fh;
+            $vals =~ tr/,/\n/;
+
+            print $fh_out "$vals\n";
+        }
+
+        put_record( $record, $out ) if not $options->{ "no_stream" };
+    }
+
+    foreach $format ( @{ $options->{ 'formats' } } )
+    {
+        if    ( $format =~ /^fasta$/i )     { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
+        elsif ( $format =~ /^blast$/i )     { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
+        elsif ( $format =~ /^blat$/i )      { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
+        elsif ( $format =~ /^vmatch$/i )    { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
+        elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
+    }
+
+    close $fh_out if $fh_out;
 }
 
 
@@ -2000,7 +2169,7 @@ sub script_length_seq
 
     my ( $record, $total );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
@@ -2008,10 +2177,10 @@ sub script_length_seq
             $total += $record->{ "SEQ_LEN" };
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    &put_record( { TOTAL_SEQ_LEN => $total }, $out );
+    put_record( { TOTAL_SEQ_LEN => $total }, $out );
 }
 
 
@@ -2029,11 +2198,11 @@ sub script_uppercase_seq
 
     my ( $record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2052,11 +2221,11 @@ sub script_shuffle_seq
 
     my ( $record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        $record->{ "SEQ" } = &Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
+        $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2075,16 +2244,16 @@ sub script_analyze_seq
 
     my ( $record, $analysis );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
-            $analysis = &Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
+            $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
 
             map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2103,7 +2272,7 @@ sub script_analyze_tags
 
     my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
         {
@@ -2133,7 +2302,7 @@ sub script_analyze_tags
         $tag_record->{ "TAG_COUNT" }  = $len_hash{ $key };
         $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
  
-        &put_record( $tag_record, $out );
+        put_record( $tag_record, $out );
     }
 }
 
@@ -2153,11 +2322,11 @@ sub script_complexity_seq
 
     my ( $record, $index );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", &Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
+        $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2179,30 +2348,30 @@ sub script_oligo_freq
 
     $options->{ "word_size" } ||= 7;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
-            map { $oligos{ $_ }++ } &Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
+            map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
 
             if ( not $options->{ "all" } )
             {
-                @freq_table = &Maasha::Seq::oligo_freq( \%oligos );
+                @freq_table = Maasha::Seq::oligo_freq( \%oligos );
 
-                map { &put_record( $_, $out ) } @freq_table;
+                map { put_record( $_, $out ) } @freq_table;
             
                 undef %oligos;
             }
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     if ( $options->{ "all" } )
     {
-        @freq_table = &Maasha::Seq::oligo_freq( \%oligos );
+        @freq_table = Maasha::Seq::oligo_freq( \%oligos );
 
-        map { &put_record( $_, $out ) } @freq_table;
+        map { put_record( $_, $out ) } @freq_table;
     }
 }
 
@@ -2224,7 +2393,7 @@ sub script_create_weight_matrix
 
     $count = 0;
     
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
@@ -2240,7 +2409,7 @@ sub script_create_weight_matrix
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 
@@ -2261,7 +2430,7 @@ sub script_create_weight_matrix
             $record->{ "V" . ( $i + 1 ) } = $freq;
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2282,11 +2451,11 @@ sub script_calc_bit_scores
 
     $count = 0;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
-            $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
+            $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
 
             for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
             {
@@ -2301,7 +2470,7 @@ sub script_calc_bit_scores
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 
@@ -2315,73 +2484,183 @@ sub script_calc_bit_scores
 
     for ( $i = 0; $i < keys %freq_hash; $i++ )
     {
-        $bit_height = &Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
+        $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
 
         $bit_diff = $bit_max - $bit_height;
 
         $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
     }
 
-    &put_record( $record, $out );
+    put_record( $record, $out );
 }
 
 
-sub script_reverse_seq
+sub script_calc_fixedstep
 {
-    # Martin A. Hansen, August 2007.
+    # Martin A. Hansen, September 2008.
 
-    # Reverse sequence in record.
+    # Calculates fixedstep entries from data in the stream.
 
-    my ( $in,    # handle to in stream
-         $out,   # handle to out stream
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
        ) = @_;
 
     # Returns nothing.
 
-    my ( $record );
+    my ( $record, %fh_hash, $fh_in, $fh_out, $chr, $chr, $beg, $end, $q_id, $block, $entry, $clones, $beg_block, $max, $i );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ" } ) {
-            $record->{ "SEQ" } = reverse $record->{ "SEQ" };
-        }
-
-        &put_record( $record, $out );
-    }
-}
+        $record->{ "CHR" }     = $record->{ "S_ID" }  if not defined $record->{ "CHR" };
+        $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
+        $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
 
+        if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
+        {
+            $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
 
-sub script_complement_seq
-{
-    # Martin A. Hansen, August 2007.
+            $fh_out = $fh_hash{ $record->{ "CHR" } };
+        
+            Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
+        }
+    }
 
-    # Complement sequence in record.
+    map { close $_ } keys %fh_hash;
 
-    my ( $in,     # handle to in stream
-         $out,    # handle to out stream
-       ) = @_;
+    foreach $chr ( sort keys %fh_hash )
+    {
+        Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
 
-    # Returns nothing.
+        $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
 
-    my ( $record, $type );
+        undef $block;
 
-    while ( $record = &get_record( $in ) ) 
-    {
-        if ( $record->{ "SEQ" } )
+        while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
         {
-            if ( not $type ) {
-                $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
-            }
+            $chr  = $entry->{ 'CHR' };
+            $beg  = $entry->{ 'CHR_BEG' };
+            $end  = $entry->{ 'CHR_END' };
+            $q_id = $entry->{ 'Q_ID' };
             
-            if ( $type eq "rna" ) {
-                &Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
-            } elsif ( $type eq "dna" ) {
-                &Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
+            if ( $options->{ "score" } ) {
+                $clones = $entry->{ 'SCORE' };
+            } elsif ( $q_id =~ /_(\d+)$/ ) {
+                $clones = $1;
+            } else {
+                $clones = 1;
             }
-        }
 
-        &put_record( $record, $out );
-    }
+            if ( $block )
+            {
+                if ( $beg > $max )
+                {
+                    map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
+
+                    $record->{ "CHR" }      = $chr;
+                    $record->{ "CHR_BEG" }  = $beg_block;
+                    $record->{ "STEP" }     = 1;
+                    $record->{ "VALS" }     = join ";", @{ $block };
+                    $record->{ "REC_TYPE" } = "fixed_step";
+
+                    put_record( $record, $out );
+
+                    undef $block;
+                }
+                else
+                {
+                    for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
+                        $block->[ $i ] += $clones;
+                    }
+
+                    $max = Maasha::Calc::max( $max, $end );
+                }
+            }
+
+            if ( not $block )
+            {
+                $beg_block = $beg;
+                $max       = $end;
+
+                for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
+                    $block->[ $i ] += $clones;
+                }
+            }
+        }
+
+        close $fh_in;
+
+        map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
+
+        $record->{ "CHR" }      = $chr;
+        $record->{ "CHR_BEG" }  = $beg_block;
+        $record->{ "STEP" }     = 1;
+        $record->{ "VALS" }     = join ";", @{ $block };
+        $record->{ "REC_TYPE" } = "fixed_step";
+
+        put_record( $record, $out );
+
+        unlink "$BP_TMP/$chr";
+    }
+}
+
+
+sub script_reverse_seq
+{
+    # Martin A. Hansen, August 2007.
+
+    # Reverse sequence in record.
+
+    my ( $in,    # handle to in stream
+         $out,   # handle to out stream
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $record );
+
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( $record->{ "SEQ" } ) {
+            $record->{ "SEQ" } = reverse $record->{ "SEQ" };
+        }
+
+        put_record( $record, $out );
+    }
+}
+
+
+sub script_complement_seq
+{
+    # Martin A. Hansen, August 2007.
+
+    # Complement sequence in record.
+
+    my ( $in,     # handle to in stream
+         $out,    # handle to out stream
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $record, $type );
+
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( $record->{ "SEQ" } )
+        {
+            if ( not $type ) {
+                $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
+            }
+            
+            if ( $type eq "rna" ) {
+                Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
+            } elsif ( $type eq "dna" ) {
+                Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
+            }
+        }
+
+        put_record( $record, $out );
+    }
 }
 
 
@@ -2399,11 +2678,11 @@ sub script_remove_indels
 
     my ( $record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2427,7 +2706,7 @@ sub script_transliterate_seq
     $replace = $options->{ "replace" } || "";
     $delete  = $options->{ "delete" }  || "";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
@@ -2438,7 +2717,7 @@ sub script_transliterate_seq
             }
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2462,7 +2741,7 @@ sub script_transliterate_vals
     $replace = $options->{ "replace" } || "";
     $delete  = $options->{ "delete" }  || "";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } )
         {
@@ -2476,7 +2755,7 @@ sub script_transliterate_vals
             }
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2498,27 +2777,27 @@ sub script_translate_seq
 
     $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
-            if ( &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
+            if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
             {
                 foreach $frame ( @{ $options->{ "frames" } } )
                 {
                     %new_record = %{ $record };
 
-                    $new_record{ "SEQ" }     = &Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
-                    $new_record{ "SEQ_LEN" } = length $record->{ "SEQ" };
+                    $new_record{ "SEQ" }     = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
+                    $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
                     $new_record{ "FRAME" }   = $frame;
 
-                    &put_record( \%new_record, $out );
+                    put_record( \%new_record, $out );
                 }
             }
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 }
@@ -2555,7 +2834,7 @@ sub script_extract_seq
 
 #    print "beg->$beg,  end->$end,  len->$len\n";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
@@ -2581,7 +2860,9 @@ sub script_extract_seq
             }
         }
 
-        &put_record( $record, $out );
+        $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
+
+        put_record( $record, $out );
     }
 }
 
@@ -2599,17 +2880,19 @@ sub script_get_genome_seq
 
     # Returns nothing.
 
-    my ( $record, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
+    my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
 
     $options->{ "flank" } ||= 0;
 
     if ( $options->{ "genome" } ) 
     {
-        $genome_file = &Maasha::Config::genome_fasta( $options->{ 'genome' } );
-        $index_file  = &Maasha::Config::genome_fasta_index( $options->{ 'genome' } );
+        $genome      = $options->{ "genome" };
+
+        $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
+        $index_file  = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
 
-        $fh          = &Maasha::Common::read_open( $genome_file );
-        $index       = &Maasha::Fasta::index_retrieve( $index_file );
+        $fh          = Maasha::Common::read_open( $genome_file );
+        $index       = Maasha::Fasta::index_retrieve( $index_file );
 
         shift @{ $index }; # Get rid of the file size info
 
@@ -2644,14 +2927,14 @@ sub script_get_genome_seq
             $record->{ "CHR_BEG" } = $beg - $index_beg;
             $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
             
-            $record->{ "SEQ" }     = &Maasha::Common::file_read( $fh, $beg, $len );
+            $record->{ "SEQ" }     = Maasha::Common::file_read( $fh, $beg, $len );
             $record->{ "SEQ_LEN" } = $len;
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }   
     }
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $options->{ "genome" } and not $record->{ "SEQ" } )
         {
@@ -2693,11 +2976,11 @@ sub script_get_genome_seq
             $record->{ "CHR_BEG" } = $beg - $index_beg;
             $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
 
-            $record->{ "SEQ" } = &Maasha::Common::file_read( $fh, $beg, $len );
+            $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
 
             if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
             {
-                &Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
+                Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
                 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
             }
 
@@ -2717,7 +3000,7 @@ sub script_get_genome_seq
             }
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     close $fh if $fh;                                                                                                                                          
@@ -2743,7 +3026,7 @@ sub script_get_genome_align
 
     $align_num = 1;
 
-    $maf_track = &Maasha::Config::maf_track( $options->{ "genome" } );
+    $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
 
     if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
     {
@@ -2755,11 +3038,10 @@ sub script_get_genome_align
             $end = $beg + $options->{ "len" };
         }
 
-        $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
+        $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
 
         foreach $entry ( @{ $align } )
         {
-            $entry->{ "ALIGN" }   = $align_num;
             $entry->{ "CHR" }     = $record->{ "CHR" };
             $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
             $entry->{ "CHR_END" } = $record->{ "CHR_END" };
@@ -2767,28 +3049,27 @@ sub script_get_genome_align
             $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
             $entry->{ "SCORE" }   = $record->{ "SCORE" };
 
-            &put_record( $entry, $out );
+            put_record( $entry, $out );
         }
     }
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "REC_TYPE" } eq "BED" )
         {
-            $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
+            $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
         }
         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
         {
-            $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
+            $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
         }
         elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
         {
-            $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
+            $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
         }
 
         foreach $entry ( @{ $align } )
         {
-            $entry->{ "ALIGN" }   = $align_num;
             $entry->{ "CHR" }     = $record->{ "CHR" };
             $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
             $entry->{ "CHR_END" } = $record->{ "CHR_END" };
@@ -2796,7 +3077,7 @@ sub script_get_genome_align
             $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
             $entry->{ "SCORE" }   = $record->{ "SCORE" };
 
-            &put_record( $entry, $out );
+            put_record( $entry, $out );
         }
 
         $align_num++;
@@ -2821,11 +3102,11 @@ sub script_get_genome_phastcons
 
     $options->{ "flank" } ||= 0;
 
-    $phastcons_file  = &Maasha::Config::genome_phastcons( $options->{ "genome" } );
-    $phastcons_index = &Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
+    $phastcons_file  = Maasha::Config::genome_phastcons( $options->{ "genome" } );
+    $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
 
-    $index           = &Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
-    $fh_phastcons    = &Maasha::Common::read_open( $phastcons_file );
+    $index           = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
+    $fh_phastcons    = Maasha::Common::read_open( $phastcons_file );
 
     if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
     {
@@ -2836,7 +3117,7 @@ sub script_get_genome_phastcons
             $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
         }
 
-        $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
+        $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
 
         $record->{ "CHR" }       = $options->{ "chr" };
         $record->{ "CHR_BEG" }   = $options->{ "beg" } - $options->{ "flank" };
@@ -2845,28 +3126,28 @@ sub script_get_genome_phastcons
         $record->{ "PHASTCONS" }   = join ",", @{ $scores };
         $record->{ "PHAST_COUNT" } = scalar @{ $scores };  # DEBUG
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }   
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "REC_TYPE" } eq "BED" )
         {
-            $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
+            $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
         }
         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
         {
-            $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
+            $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
         }
         elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
         {
-            $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
+            $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
         }
 
         $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
 #        $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores };  # DEBUG
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     close $fh_phastcons if $fh_phastcons;                                                                                                                                          
@@ -2887,17 +3168,17 @@ sub script_fold_seq
 
     my ( $record, $type, $struct, $index );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } )
         {
             if ( not $type ) {
-                $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
+                $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
             }
             
             if ( $type ne "protein" )
             {
-                ( $struct, $index ) = &Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
+                ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
                 $record->{ "SEC_STRUCT" }  = $struct;
                 $record->{ "FREE_ENERGY" } = $index;
                 $record->{ "SCORE" }       = abs int $index;
@@ -2906,7 +3187,7 @@ sub script_fold_seq
             }
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -2928,7 +3209,7 @@ sub script_split_seq
 
     $options->{ "word_size" } ||= 7;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
         {
@@ -2938,27 +3219,25 @@ sub script_split_seq
 
                 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
                 {
-                    $new_record->{ "REC_TYPE" } = "SPLIT";
                     $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
                     $new_record->{ "SEQ" }      = $subseq;
 
-                    &put_record( $new_record, $out );
+                    put_record( $new_record, $out );
 
                     $lookup{ $subseq } = 1;
                 }
                 else
                 {
-                    $new_record->{ "REC_TYPE" } = "SPLIT";
                     $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
                     $new_record->{ "SEQ" }      = $subseq;
 
-                    &put_record( $new_record, $out );
+                    put_record( $new_record, $out );
                 }
             }
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 }
@@ -2982,7 +3261,7 @@ sub script_split_bed
     $options->{ "window_size" } ||= 20;
     $options->{ "step_size" }   ||= 1;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
         {
@@ -2999,12 +3278,12 @@ sub script_split_bed
                 $new_record->{ "SCORE" }    = $record->{ "SCORE" };
                 $new_record->{ "STRAND" }   = $record->{ "STRAND" };
 
-                &put_record( $new_record, $out );
+                put_record( $new_record, $out );
             }
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 }
@@ -3024,30 +3303,29 @@ sub script_align_seq
 
     my ( $record, @entries, $entry );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
         } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
             push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
         } else {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 
-    @entries = &Maasha::Align::align( \@entries );
+    @entries = Maasha::Align::align( \@entries );
 
     foreach $entry ( @entries )
     {
         if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
         {
             $record = {
-                ALIGN    => 1,
                 SEQ_NAME => $entry->[ SEQ_NAME ],
                 SEQ      => $entry->[ SEQ ],
             };
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 }
@@ -3071,7 +3349,7 @@ sub script_tile_seq
 
     $first = 1;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
         {
@@ -3088,13 +3366,13 @@ sub script_tile_seq
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 
-    @entries = &Maasha::Align::align_tile( $ref_entry, \@entries, $options );
+    @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
 
-    map { &put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ], ALIGN => 1 }, $out ) } @entries;
+    map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
 }
 
 
@@ -3114,21 +3392,21 @@ sub script_invert_align
 
     my ( $record, @entries );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } and $record->{ "ALIGN" } )
+        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
         {
             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 
-    &Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
+    Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
 
-    map { &put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ], ALIGN => 1 }, $out ) } @entries;
+    map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
 }
 
 
@@ -3148,39 +3426,37 @@ sub script_patscan_seq
     my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
 
     if ( $options->{ "patterns" } ) {
-        $patterns = &Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
+        $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
     } elsif ( -f $options->{ "patterns_in" } ) {
-        $patterns = &Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
+        $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
     }
 
-    $genome_file = &Maasha::Config::genome_fasta( $options->{ 'genome' } ) if $options->{ 'genome' };
+    $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
 
     push @args, "-c"                            if $options->{ "comp" };
     push @args, "-m $options->{ 'max_hits' }"   if $options->{ 'max_hits' };
     push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
 
-    $seq_file = "$TMP_DIR/patscan.seq";
-    $pat_file = "$TMP_DIR/patscan.pat";
-    $out_file = "$TMP_DIR/patscan.out";
+    $seq_file = "$BP_TMP/patscan.seq";
+    $pat_file = "$BP_TMP/patscan.pat";
+    $out_file = "$BP_TMP/patscan.out";
 
-    $fh_out = &Maasha::Common::write_open( $seq_file );
+    $fh_out = Maasha::Common::write_open( $seq_file );
 
     $i = 0;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
         {
-            $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
+            $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
 
-            &Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
+            Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
 
             $head_hash{ $i } = $record->{ "SEQ_NAME" };
 
             $i++;
         }
-
-#        &put_record( $record, $out );
     }
 
     close $fh_out;
@@ -3190,7 +3466,7 @@ sub script_patscan_seq
 
     foreach $pattern ( @{ $patterns } )
     {
-        $fh_out = &Maasha::Common::write_open( $pat_file );
+        $fh_out = Maasha::Common::write_open( $pat_file );
 
         print $fh_out "$pattern\n";
 
@@ -3198,17 +3474,17 @@ sub script_patscan_seq
 
         if ( $options->{ 'genome' } ) {
             `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
-            # &Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
+            # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
         } else {
             `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
-            # &Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
+            # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
         }
 
-        $fh_in = &Maasha::Common::read_open( $out_file );
+        $fh_in = Maasha::Common::read_open( $out_file );
 
-        while ( $entry = &Maasha::Fasta::get_entry( $fh_in ) )
+        while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
         {
-            $result = &Maasha::Patscan::parse_scan_result( $entry, $pattern );
+            $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
 
             if ( $options->{ 'genome' } )
             {
@@ -3225,7 +3501,7 @@ sub script_patscan_seq
                 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
             }
 
-            &put_record( $result, $out );
+            put_record( $result, $out );
         }
 
         close $fh_in;
@@ -3250,30 +3526,30 @@ sub script_create_blast_db
 
     # Returns nothing.
 
-    my ( $fh, $seq_type, $path, $record );
+    my ( $fh, $seq_type, $path, $record, $entry );
 
     $path = $options->{ "database" };
 
-    $fh = &Maasha::Common::write_open( $path );
+    $fh = Maasha::Common::write_open( $path );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
 
-        if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
+        if ( $entry = record2fasta( $record ) )
         {
-            $seq_type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
+            $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
 
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
+            Maasha::Fasta::put_entry( $entry, $fh );
         }
     }
 
     close $fh;
 
     if ( $seq_type eq "protein" ) {
-        &Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
+        Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
     } else {
-        &Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
+        Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
     }
 
     unlink $path;
@@ -3293,30 +3569,30 @@ sub script_blast_seq
 
     # Returns nothing.
 
-    my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
+    my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
 
     $options->{ "e_val" }  = 10 if not defined $options->{ "e_val" };
     $options->{ "filter" } = "F";
     $options->{ "filter" } = "T" if $options->{ "filter" };
     $options->{ "cpus" } ||= 1;
 
-    $options->{ "database" } = &Maasha::Config::genome_blast( $options->{ 'genome' } ) if $options->{ 'genome' };
+    $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
 
-    $tmp_in  = "$TMP_DIR/blast_query.seq";
-    $tmp_out = "$TMP_DIR/blast.result";
+    $tmp_in  = "$BP_TMP/blast_query.seq";
+    $tmp_out = "$BP_TMP/blast.result";
 
-    $fh_out = &Maasha::Common::write_open( $tmp_in );
+    $fh_out = Maasha::Common::write_open( $tmp_in );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
+        if ( $entry = record2fasta( $record ) )
         {
-            $q_type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
+            $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
 
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
+            Maasha::Fasta::put_entry( $entry, $fh_out );
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     close $fh_out;
@@ -3340,11 +3616,45 @@ sub script_blast_seq
         }
     }
 
-    &Maasha::Common::run( "blastall", "-p $options->{ 'program' } -e $options->{ 'e_val' } -a $options->{ 'cpus' } -m 8 -i $tmp_in -d $options->{ 'database' } -F $options->{ 'filter' } -o $tmp_out > /dev/null 2>&1", 1 );
+    if ( $options->{ 'verbose' } )
+    {
+        Maasha::Common::run(
+            "blastall",
+            join( " ",
+                "-p $options->{ 'program' }",
+                "-e $options->{ 'e_val' }",
+                "-a $options->{ 'cpus' }",
+                "-m 8",
+                "-i $tmp_in",
+                "-d $options->{ 'database' }",
+                "-F $options->{ 'filter' }",
+                "-o $tmp_out",
+            ),
+            1
+        );
+    }
+    else
+    {
+        Maasha::Common::run(
+            "blastall",
+            join( " ",
+                "-p $options->{ 'program' }",
+                "-e $options->{ 'e_val' }",
+                "-a $options->{ 'cpus' }",
+                "-m 8",
+                "-i $tmp_in",
+                "-d $options->{ 'database' }",
+                "-F $options->{ 'filter' }",
+                "-o $tmp_out",
+                "> /dev/null 2>&1"
+            ),
+            1
+        );
+    }
 
     unlink $tmp_in;
 
-    $fh_out = &Maasha::Common::read_open( $tmp_out );
+    $fh_out = Maasha::Common::read_open( $tmp_out );
 
     undef $record;
 
@@ -3381,7 +3691,7 @@ sub script_blast_seq
             $record->{ "STRAND" } = '+';
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     close $fh_out;
@@ -3403,9 +3713,9 @@ sub script_blat_seq
 
     # Returns nothing.
 
-    my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
+    my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
 
-    $genome_file = &Maasha::Config::genome_fasta( $options->{ "genome" } );
+    $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
 
     $options->{ 'tile_size' }    ||= 11;
     $options->{ 'one_off' }      ||= 0;
@@ -3418,21 +3728,21 @@ sub script_blat_seq
     $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
     $blat_args .= " -minScore=$options->{ 'min_score' }";
     $blat_args .= " -stepSize=$options->{ 'step_size' }";
-    $blat_args .= " -ooc=" . &Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
+#    $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
 
-    $query_file = "$TMP_DIR/blat.seq";
+    $query_file = "$BP_TMP/blat.seq";
 
-    $fh_out = &Maasha::Common::write_open( $query_file );
+    $fh_out = Maasha::Common::write_open( $query_file );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
+        if ( $entry = record2fasta( $record ) )
         {
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
-            $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
+            $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
+            Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     close $fh_out;
@@ -3440,20 +3750,112 @@ sub script_blat_seq
     $blat_args .= " -t=dnax" if $type eq "protein";
     $blat_args .= " -q=$type";
 
-    $result_file = "$TMP_DIR/blat.psl";
+    $result_file = "$BP_TMP/blat.psl";
 
-    &Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
+    Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
 
     unlink $query_file;
 
-    $entries = &Maasha::UCSC::psl_get_entries( $result_file );
+    $entries = Maasha::UCSC::psl_get_entries( $result_file );
 
-    map { &put_record( $_, $out ) } @{ $entries };
+    map { put_record( $_, $out ) } @{ $entries };
 
     unlink $result_file;
 }
 
 
+sub script_soap_seq
+{
+    # Martin A. Hansen, July 2008.
+
+    # soap sequences in stream against a given file or genome.
+
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
+
+    $options->{ "seed_size" }  ||= 10;
+    $options->{ "mismatches" } ||= 2;
+    $options->{ "gap_size" }   ||= 0;
+    $options->{ "cpus" }       ||= 1;
+
+    if ( $options->{ "genome" } ) {
+        $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
+    }
+
+    $tmp_in  = "$BP_TMP/soap_query.seq";
+    $tmp_out = "$BP_TMP/soap.result";
+
+    $fh_out = Maasha::Common::write_open( $tmp_in );
+    $count = 0;
+
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( $entry = record2fasta( $record ) )
+        {
+            Maasha::Fasta::put_entry( $entry, $fh_out );
+
+            $count++;
+        }
+
+        put_record( $record, $out );
+    }
+
+    close $fh_out;
+
+    if ( $count > 0 )
+    {
+        $args = join( " ",
+            "-s $options->{ 'seed_size' }",
+            "-r 2",
+            "-a $tmp_in",
+            "-v $options->{ 'mismatches' }",
+            "-g $options->{ 'gap_size' }",
+            "-p $options->{ 'cpus' }",
+            "-d $options->{ 'in_file' }",
+            "-o $tmp_out",
+        );
+
+        $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
+
+        Maasha::Common::run( "soap", $args, 1 );
+
+        unlink $tmp_in;
+
+        $fh_out = Maasha::Common::read_open( $tmp_out );
+
+        undef $record;
+
+        while ( $line = <$fh_out> )
+        {
+            chomp $line;
+
+            @fields = split /\t/, $line;
+
+            $record->{ "REC_TYPE" }   = "SOAP";
+            $record->{ "Q_ID" }       = $fields[ 0 ];
+            $record->{ "SCORE" }      = $fields[ 3 ];
+            $record->{ "STRAND" }     = $fields[ 6 ];
+            $record->{ "S_ID" }       = $fields[ 7 ];
+            $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # soap is 1-based
+            $record->{ "S_END" }      = $fields[ 8 ] + $fields[ 5 ] - 2;
+
+            put_record( $record, $out );
+        }
+
+        close $fh_out;
+    }
+
+    unlink $tmp_out;
+}
+
+
 sub script_match_seq
 {
     # Martin A. Hansen, August 2007.
@@ -3472,26 +3874,26 @@ sub script_match_seq
     $options->{ "word_size" } ||= 20;
     $options->{ "direction" } ||= "both";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     if ( @entries == 1 )
     {
-        $results = &Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $TMP_DIR );
+        $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
 
-        map { &put_record( $_, $out ) } @{ $results };
+        map { put_record( $_, $out ) } @{ $results };
     }
     elsif ( @entries == 2 )
     {
-        $results = &Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $TMP_DIR );
+        $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
 
-        map { &put_record( $_, $out ) } @{ $results };
+        map { put_record( $_, $out ) } @{ $results };
     }
 }
 
@@ -3509,24 +3911,24 @@ sub script_create_vmatch_index
 
     # Returns nothing.
 
-    my ( $record, $file_tmp, $fh_tmp, $type );
+    my ( $record, $file_tmp, $fh_tmp, $type, $entry );
 
     if ( $options->{ "index_name" } )
     {
         $file_tmp = $options->{ 'index_name' };
-        $fh_tmp   = &Maasha::Common::write_open( $file_tmp );
+        $fh_tmp   = Maasha::Common::write_open( $file_tmp );
     }
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
+        if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
         {
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
+            Maasha::Fasta::put_entry( $entry, $fh_tmp );
 
-            $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
+            $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     if ( $options->{ "index_name" } )
@@ -3534,9 +3936,9 @@ sub script_create_vmatch_index
         close $fh_tmp;
     
         if ( $type eq "protein" ) {
-            &Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
+            Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
         } else {
-            &Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
+            Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
         }
 
         unlink $file_tmp;
@@ -3557,31 +3959,38 @@ sub script_vmatch_seq
 
     # Returns nothing.
 
-    my ( @index_files, @records, $result_file, $fh_in, $record );
+    my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
 
     $options->{ 'count' } = 1 if $options->{ 'max_hits' };
 
-    if ( $options->{ "index_name" } ) {
+    if ( $options->{ "index_name" } )
+    {
         @index_files = $options->{ "index_name" };
-    } else {
-        @index_files = &Maasha::Config::genome_vmatch( $options->{ "genome" } );
     }
+    else
+    {
+        @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
 
-    while ( $record = &get_record( $in ) ) 
+        map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
+
+        @index_files = sort keys %hash;
+    }
+
+    while ( $record = get_record( $in ) ) 
     {
         push @records, $record;
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
-    $result_file = &Maasha::Match::match_vmatch( $TMP_DIR, \@records, \@index_files, $options );
+    $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
 
     undef @records;
 
-    $fh_in = &Maasha::Common::read_open( $result_file );
+    $fh_in = Maasha::Common::read_open( $result_file );
 
-    while ( $record = &Maasha::Match::vmatch_get_entry( $fh_in ) ) {
-        &put_record( $record, $out );
+    while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
+        put_record( $record, $out );
     }
 
     close $fh_in;
@@ -3603,17 +4012,17 @@ sub script_write_fasta
 
     # Returns nothing.
 
-    my ( $record, $fh );
+    my ( $record, $fh, $entry );
 
-    $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
+        if ( $entry = record2fasta( $record ) ) {
+            Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     close $fh;
@@ -3633,46 +4042,23 @@ sub script_write_align
 
     # Returns nothing.
 
-    my ( $fh, $record, $align, $old_align, @entries );
+    my ( $fh, $record, @entries );
 
-    $fh = &write_stream( $options->{ "data_out" } ) ;
+    $fh = write_stream( $options->{ "data_out" } ) ;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "ALIGN" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
-        {
-            $align = $record->{ "ALIGN" };
-
-            if ( not $old_align )
-            {
-                push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
-
-                $old_align = $align;
-            }
-            elsif ( $align == $old_align )
-            {
-                push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
-            }
-            else
-            {
-                if ( scalar( @entries ) == 2 ) {
-                    &Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
-                } elsif ( scalar ( @entries ) > 2 ) {
-                    &Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
-                }
-
-                undef @entries;
-                $old_align = $align;
-            }
+        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
+            push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     if ( scalar( @entries ) == 2 ) {
-        &Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
+        Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
     } elsif ( scalar ( @entries ) > 2 ) {
-        &Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
+        Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
     }
 
     close $fh if $fh;
@@ -3694,11 +4080,11 @@ sub script_write_blast
 
     my ( $fh, $record, $first );
 
-    $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
 
     $first = 1;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "REC_TYPE" } eq "BLAST" )
         {
@@ -3729,7 +4115,7 @@ sub script_write_blast
             ), "\n";
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     close $fh;
@@ -3755,9 +4141,9 @@ sub script_write_tab
 
     map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
 
-    $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         undef @vals;
         $ok = 1;
@@ -3798,7 +4184,7 @@ sub script_write_tab
 
         print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     close $fh;
@@ -3811,8 +4197,6 @@ sub script_write_bed
 
     # Write BED format for the UCSC genome browser using records in stream.
 
-    # Crude - needs lots of work!
-
     my ( $in,        # handle to in stream
          $out,       # handle to out stream
          $options,   # options hash
@@ -3822,13 +4206,13 @@ sub script_write_bed
 
     my ( $fh, $record, $new_record );
 
-    $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "REC_TYPE" } eq "BED" )                                             # ---- Hits from BED ----
         {
-            &Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
+            Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
         }
         elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i )       # ---- Hits from BLAT (PSL) ----
         {
@@ -3839,11 +4223,11 @@ sub script_write_bed
             $new_record->{ "SCORE" }   = $record->{ "SCORE" } || 999;
             $new_record->{ "STRAND" }  = $record->{ "STRAND" };
 
-            &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
+            Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
         }
         elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )               # ---- Hits from patscan_seq ----
         {
-            &Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
+            Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
         }
         elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i )     # ---- Hits from BLAST ----
         {
@@ -3854,7 +4238,7 @@ sub script_write_bed
             $new_record->{ "SCORE" }   = $record->{ "SCORE" } || 999; # or use E_VAL somehow
             $new_record->{ "STRAND" }  = $record->{ "STRAND" };
 
-            &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
+            Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
         }
         elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )    # ---- Hits from Vmatch ----
         {
@@ -3865,14 +4249,25 @@ sub script_write_bed
             $new_record->{ "SCORE" }   = $record->{ "SCORE" } || 999; # or use E_VAL somehow
             $new_record->{ "STRAND" }  = $record->{ "STRAND" };
 
-            &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
+            Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
+        }
+        elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i )    # ---- Hits from Vmatch ----
+        {
+            $new_record->{ "CHR" }     = $record->{ "S_ID" };
+            $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
+            $new_record->{ "CHR_END" } = $record->{ "S_END" };
+            $new_record->{ "Q_ID" }    = $record->{ "Q_ID" };
+            $new_record->{ "SCORE" }   = $record->{ "SCORE" } || 999;
+            $new_record->{ "STRAND" }  = $record->{ "STRAND" };
+
+            Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
         }
         elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )  # ---- Generic data from tables ----
         {
-            &Maasha::UCSC::bed_put_entry( $record, $fh );
+            Maasha::UCSC::bed_put_entry( $record, $fh );
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     close $fh;
@@ -3896,16 +4291,16 @@ sub script_write_psl
 
     $first = 1;
 
-    $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
 
         if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
         {
-            &Maasha::UCSC::psl_put_header( $fh ) if $first;
-            &Maasha::UCSC::psl_put_entry( $record, $fh );
+            Maasha::UCSC::psl_put_header( $fh ) if $first;
+            Maasha::UCSC::psl_put_entry( $record, $fh );
             $first = 0;
         }
     }
@@ -3914,6 +4309,43 @@ sub script_write_psl
 }
 
 
+sub script_write_fixedstep
+{
+    # Martin A. Hansen, Juli 2008.
+
+    # Write fixedStep entries from recrods in the stream.
+
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $fh, $record, $vals );
+
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
+        {
+            print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
+
+            $vals = $record->{ 'VALS' };
+
+            $vals =~ tr/;/\n/;
+
+            print $fh "$vals\n";
+        }
+
+        put_record( $record, $out ) if not $options->{ "no_stream" };
+    }
+
+    close $fh;
+}
+
+
 sub script_write_2bit
 {
     # Martin A. Hansen, March 2008.
@@ -3927,29 +4359,29 @@ sub script_write_2bit
 
     # Returns nothing.
 
-    my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
+    my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
 
     $mask = 1 if not $options->{ "no_mask" };
 
-    $tmp_file = "$TMP_DIR/write_2bit.fna";
-    $fh_tmp   = &Maasha::Common::write_open( $tmp_file );
+    $tmp_file = "$BP_TMP/write_2bit.fna";
+    $fh_tmp   = Maasha::Common::write_open( $tmp_file );
 
-    $fh_out = &write_stream( $options->{ "data_out" } );
+    $fh_out = write_stream( $options->{ "data_out" } );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
+        if ( $entry = record2fasta( $record ) ) {
+            Maasha::Fasta::put_entry( $entry, $fh_tmp );
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     close $fh_tmp;
 
-    $fh_in = &Maasha::Common::read_open( $tmp_file );
+    $fh_in = Maasha::Common::read_open( $tmp_file );
 
-    &Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
+    Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
 
     close $fh_in;
     close $fh_out;
@@ -3971,20 +4403,20 @@ sub script_write_solid
 
     # Returns nothing.
 
-    my ( $record, $fh, $seq_cs );
+    my ( $record, $fh, $entry );
 
-    $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+    $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
+        if ( $entry = record2fasta( $record ) )
         {
-            $seq_cs = &Maasha::Solid::seq2color_space( $record->{ "SEQ" } );
+            $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
 
-            &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
+            Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     close $fh;
@@ -4006,18 +4438,18 @@ sub script_plot_seqlogo
 
     my ( $record, @entries, $logo, $fh );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $logo = &Maasha::Plot::seq_logo( \@entries );
+    $logo = Maasha::Plot::seq_logo( \@entries );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh $logo;
 
@@ -4042,34 +4474,34 @@ sub script_plot_phastcons_profiles
 
     $options->{ "title" } ||= "PhastCons Profiles";
 
-    $phastcons_file  = &Maasha::Config::genome_phastcons( $options->{ "genome" } );
-    $phastcons_index = &Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
+    $phastcons_file  = Maasha::Config::genome_phastcons( $options->{ "genome" } );
+    $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
 
-    $index           = &Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
-    $fh_phastcons    = &Maasha::Common::read_open( $phastcons_file );
+    $index           = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
+    $fh_phastcons    = Maasha::Common::read_open( $phastcons_file );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
         {
-            $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
+            $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
 
             push @{ $AoA }, [ @{ $scores } ];
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    &Maasha::UCSC::phastcons_normalize( $AoA );
+    Maasha::UCSC::phastcons_normalize( $AoA );
 
-    $AoA = [ [ &Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
-    $AoA = [ [ &Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
+    $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
+    $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
 
-    $AoA = &Maasha::Matrix::matrix_flip( $AoA );
+    $AoA = Maasha::Matrix::matrix_flip( $AoA );
 
-    $plot = &Maasha::Plot::lineplot_simple( $AoA, $options, $TMP_DIR );
+    $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh "$_\n" foreach @{ $plot };
 
@@ -4092,11 +4524,11 @@ sub script_analyze_bed
 
     my ( $record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        $record = &Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
+        $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -4118,7 +4550,7 @@ sub script_analyze_vals
 
     map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( keys %{ $record } )
         {
@@ -4126,7 +4558,7 @@ sub script_analyze_vals
 
             $analysis->{ $key }->{ "COUNT" }++;
 
-            if ( &Maasha::Calc::is_a_number( $record->{ $key } ) )
+            if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
             {
                 $analysis->{ $key }->{ "TYPE" } = "num";
                 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
@@ -4144,13 +4576,13 @@ sub script_analyze_vals
             }
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     foreach $key ( keys %{ $analysis } )
     {
         $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
-        $analysis->{ $key }->{ "SUM" }  = sprintf "%.2f", $analysis->{ $key }->{ "SUN" };
+        $analysis->{ $key }->{ "SUM" }  = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
     }
 
     my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
@@ -4209,11 +4641,11 @@ sub script_head_records
 
     $count = 0;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         $count++;
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
 
         last if $count == $options->{ "num" };
     }
@@ -4235,7 +4667,7 @@ sub script_remove_keys
 
     my ( $record, $new_record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $options->{ "keys" } )
         {
@@ -4248,7 +4680,71 @@ sub script_remove_keys
             $record = $new_record;
         }
 
-        &put_record( $record, $out ) if keys %{ $record };
+        put_record( $record, $out ) if keys %{ $record };
+    }
+}
+
+
+sub script_remove_adaptor
+{
+    # Martin A. Hansen, August 2008.
+
+    # Find and remove adaptor from sequences in the stream.
+
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
+
+    $options->{ "remove" } ||= "after";
+
+    $max_mismatch = $options->{ "mismatches" } || 0;
+    $offset       = $options->{ "offset" };
+
+    if ( not defined $offset ) {
+        $offset = 0;
+    } else {
+        $offset--;
+    }
+
+    $adaptor      = uc $options->{ "adaptor" };
+    $adaptor_len  = length $adaptor;
+
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( $record->{ "SEQ" } )
+        {
+            $seq     = uc $record->{ "SEQ" };
+            $seq_len = length $seq;
+
+            $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
+
+            $record->{ "ADAPTOR_POS" } = $pos;
+
+            if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
+            {
+                if ( $options->{ "remove" } eq "after" )
+                {
+                    $record->{ "SEQ" }     = substr $record->{ "SEQ" }, 0, $pos;
+                    $record->{ "SEQ_LEN" } = $pos;
+                }
+                else
+                {
+                    $record->{ "SEQ" }     = substr $record->{ "SEQ" }, $pos + $adaptor_len;
+                    $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
+                }
+            }
+
+            put_record( $record, $out );
+        }
+        else
+        {
+            put_record( $record, $out );
+        }
     }
 }
 
@@ -4268,7 +4764,7 @@ sub script_rename_keys
 
     my ( $record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
         {
@@ -4277,7 +4773,7 @@ sub script_rename_keys
             delete $record->{ $options->{ "keys" }->[ 0 ] };
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -4297,19 +4793,19 @@ sub script_uniq_vals
 
     my ( %hash, $record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ $options->{ "key" } } )
         {
             if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
             {
-                &put_record( $record, $out );
+                put_record( $record, $out );
 
                 $hash{ $record->{ $options->{ "key" } } } = 1;
             }
             elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
             {
-                &put_record( $record, $out );
+                put_record( $record, $out );
             }
             else
             {
@@ -4318,7 +4814,7 @@ sub script_uniq_vals
         }
         else
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 }
@@ -4341,7 +4837,7 @@ sub script_merge_vals
 
     $options->{ "delimit" } ||= '_';
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
         {
@@ -4354,7 +4850,178 @@ sub script_merge_vals
             $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
+    }
+}
+
+
+sub script_merge_records
+{
+    # Martin A. Hansen, July 2008.
+
+    # Merges records in the stream based on identical values of two given keys.
+
+    my ( $in,        # handle to in stream
+         $out,       # handle to out stream
+         $options,   # options hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
+         $num1, $num2, $num, $cmp, $i );
+
+    $merge = $options->{ "merge" } || "AandB";
+
+    $file1 = "$BP_TMP/merge_records1.tmp";
+    $file2 = "$BP_TMP/merge_records2.tmp";
+
+    $fh1   = Maasha::Common::write_open( $file1 );
+    $fh2   = Maasha::Common::write_open( $file2 );
+
+    $key1  = $options->{ "keys" }->[ 0 ];
+    $key2  = $options->{ "keys" }->[ 1 ];
+
+    $num   = $key2 =~ s/n$//;
+    $num1  = 0;
+    $num2  = 0;
+
+    while ( $record = get_record( $in ) ) 
+    {
+        if ( exists $record->{ $key1 } )
+        {
+            @keys1 = $key1;
+            @vals1 = $record->{ $key1 };
+
+            delete $record->{ $key1 };
+
+            map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
+
+            print $fh1 join( "\t", @vals1 ), "\n";
+
+            $num1++;
+        }
+        elsif ( exists $record->{ $key2 } )
+        {
+            @keys2 = $key2;
+            @vals2 = $record->{ $key2 };
+
+            delete $record->{ $key2 };
+
+            map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
+
+            print $fh2 join( "\t", @vals2 ), "\n";
+
+            $num2++;
+        }
+    }
+
+    close $fh1;
+    close $fh2;
+
+    if ( $num )
+    {
+        Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
+        Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
+    }
+    else
+    {
+        Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
+        Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
+    }
+
+    $fh1 = Maasha::Common::read_open( $file1 );
+    $fh2 = Maasha::Common::read_open( $file2 );
+
+    @vals1 = Maasha::Common::get_fields( $fh1 );
+    @vals2 = Maasha::Common::get_fields( $fh2 );
+
+    while ( $num1 > 0 and $num2 > 0 )
+    {
+        undef $record;
+
+        if ( $num ) {
+            $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
+        } else {
+            $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
+        }
+
+        if ( $cmp < 0 )
+        {
+            if ( $merge =~ /^(AorB|AnotB)$/ )
+            {
+                for ( $i = 0; $i < @keys1; $i++ ) {
+                    $record->{ $keys1[ $i ] } = $vals1[ $i ];
+                }
+
+                put_record( $record, $out );
+            }
+
+            @vals1 = Maasha::Common::get_fields( $fh1 );
+            $num1--;
+        }
+        elsif ( $cmp > 0 )
+        {
+            if ( $merge =~ /^(BorA|BnotA)$/ )
+            {
+                for ( $i = 0; $i < @keys2; $i++ ) {
+                    $record->{ $keys2[ $i ] } = $vals2[ $i ];
+                }
+
+                put_record( $record, $out );
+            }
+
+            @vals2 = Maasha::Common::get_fields( $fh2 );
+            $num2--;
+        }
+        else
+        {
+            if ( $merge =~ /^(AandB|AorB|BorA)$/ )
+            {
+                for ( $i = 0; $i < @keys1; $i++ ) {
+                    $record->{ $keys1[ $i ] } = $vals1[ $i ];
+                }
+
+                for ( $i = 1; $i < @keys2; $i++ ) {
+                    $record->{ $keys2[ $i ] } = $vals2[ $i ];
+                }
+            
+                put_record( $record, $out );
+            }
+
+            @vals1 = Maasha::Common::get_fields( $fh1 );
+            @vals2 = Maasha::Common::get_fields( $fh2 );
+            $num1--;
+            $num2--;
+        }
+    }
+
+    close $fh1;
+    close $fh2;
+
+    unlink $file1;
+    unlink $file2;
+
+    if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
+    {
+        undef $record;
+
+        for ( $i = 0; $i < @keys1; $i++ ) {
+            $record->{ $keys1[ $i ] } = $vals1[ $i ];
+        }
+
+        put_record( $record, $out );
+    }
+
+    if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
+    {
+        undef $record;
+
+        for ( $i = 0; $i < @keys2; $i++ ) {
+            $record->{ $keys2[ $i ] } = $vals2[ $i ];
+        }
+
+        put_record( $record, $out );
     }
 }
 
@@ -4380,11 +5047,11 @@ sub script_grab
     }
     elsif ( -f $options->{ "patterns_in" } )
     {
-        $patterns = &Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
+        $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
     }
     elsif ( -f $options->{ "exact_in" } )
     {
-        $patterns = &Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
+        $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
 
         map { $lookup_hash{ $_ } = 1 } @{ $patterns };
 
@@ -4401,7 +5068,7 @@ sub script_grab
         }
     } 
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         $pos = -1;
         
@@ -4546,9 +5213,9 @@ sub script_grab
         FOUND:
 
         if ( $pos >= 0 and not $options->{ "invert" } ) {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         } elsif ( $pos < 0 and $options->{ "invert" } ) {
-            &put_record( $record, $out );
+            put_record( $record, $out );
         }
     }
 }
@@ -4569,7 +5236,7 @@ sub script_compute
 
     my ( $record, $eval_key, $eval_val, $check, @keys );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $options->{ "eval" } )
         {
@@ -4589,10 +5256,10 @@ sub script_compute
 
             map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
 
-            $record->{ $eval_key } = eval "$eval_val" or &Maasha::Common::error( "eval failed -> $@" );
+            $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
         } 
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -4612,7 +5279,7 @@ sub script_flip_tab
 
     my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         undef @rows;
 
@@ -4627,7 +5294,7 @@ sub script_flip_tab
 
     undef $record;
 
-    @matrix = &Maasha::Matrix::matrix_flip( \@matrix );
+    @matrix = Maasha::Matrix::matrix_flip( \@matrix );
 
     foreach $row ( @matrix )
     {
@@ -4635,7 +5302,7 @@ sub script_flip_tab
             $record->{ "V$i" } = $row->[ $i ];
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -4660,11 +5327,11 @@ sub script_add_ident
 
     $i = 0;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         $record->{ $key } = sprintf( "$prefix%08d", $i );
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
 
         $i++;
     }
@@ -4699,19 +5366,19 @@ sub script_count_records
     }
     else
     {
-        while ( $record = &get_record( $in ) ) 
+        while ( $record = get_record( $in ) ) 
         {
-            &put_record( $record, $out );
+            put_record( $record, $out );
 
             $count++;
         }
     }
 
-    $result = { "count_records" => $count };
+    $result = { "RECORDS_COUNT" => $count };
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
-    &put_record( $result, $fh );
+    put_record( $result, $fh );
 
     close $fh;
 }
@@ -4734,15 +5401,15 @@ sub script_random_records
 
     $options->{ "num" } ||= 10;
 
-    $tmp_file = "$TMP_DIR/random_records.tmp";
+    $tmp_file = "$BP_TMP/random_records.tmp";
 
-    $fh_out = &Maasha::Common::write_open( $tmp_file );
+    $fh_out = Maasha::Common::write_open( $tmp_file );
 
     $count = 0;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        &put_record( $record, $fh_out );
+        put_record( $record, $fh_out );
 
         $count++;
     }
@@ -4752,7 +5419,7 @@ sub script_random_records
     $max = 0;
     $i   = 0;
 
-    &Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
+    Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
 
     while ( $i < $options->{ "num" } )
     {
@@ -4768,13 +5435,13 @@ sub script_random_records
         }
     }
 
-    $fh_in = &Maasha::Common::read_open( $tmp_file );
+    $fh_in = Maasha::Common::read_open( $tmp_file );
 
     $count = 0;
 
-    while ( $record = &get_record( $fh_in ) ) 
+    while ( $record = get_record( $fh_in ) ) 
     {
-        &put_record( $record, $out ) if exists $rand_hash{ $count };
+        put_record( $record, $out ) if exists $rand_hash{ $count };
 
         last if $count == $max;
 
@@ -4814,7 +5481,7 @@ sub script_sort_records
     $sort_str = join " or ", @sort_cmd;
     $sort_sub = eval "sub { $sort_str }";   # NB security issue!
 
-    while ( $record = &get_record( $in ) ) {
+    while ( $record = get_record( $in ) ) {
         push @records, $record;
     }
 
@@ -4823,13 +5490,13 @@ sub script_sort_records
     if ( $options->{ "reverse" } )
     {
         for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
-            &put_record( $records[ $i ], $out );
+            put_record( $records[ $i ], $out );
         }
     }
     else
     {
         for ( $i = 0; $i < scalar @records; $i++ ) {
-            &put_record( $records[ $i ], $out );
+            put_record( $records[ $i ], $out );
         }
     }
 }
@@ -4850,13 +5517,14 @@ sub script_count_vals
 
     my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
 
-    $tmp_file = "$TMP_DIR/count_cache.tmp";
+    $tmp_file = "$BP_TMP/count_cache.tmp";
 
-    $fh_out   = &Maasha::Common::write_open( $tmp_file );
+    $fh_out   = Maasha::Common::write_open( $tmp_file );
 
+    $cache    = 0;
     $num      = 0;
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
 
@@ -4864,7 +5532,7 @@ sub script_count_vals
 
         if ( scalar @records > 5_000_000 )   # too many records to hold in memory - use disk cache
         {
-            map { &put_record( $_, $fh_out ) } @records;
+            map { put_record( $_, $fh_out ) } @records;
 
             undef @records;
 
@@ -4880,15 +5548,15 @@ sub script_count_vals
 
     if ( $cache )
     {
-        $num      = 0;
+        $num   = 0;
 
-        $fh_in = &Maasha::Common::read_open( $tmp_file );
+        $fh_in = Maasha::Common::read_open( $tmp_file );
 
-        while ( $record = &get_record( $fh_in ) )
+        while ( $record = get_record( $fh_in ) )
         {
             map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
 
-            &put_record( $record, $out );
+            put_record( $record, $out );
 
             print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
 
@@ -4902,7 +5570,7 @@ sub script_count_vals
     {
         map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 
     unlink $tmp_file;
@@ -4927,11 +5595,11 @@ sub script_plot_histogram
     $options->{ "title" } ||= "Histogram";
     $options->{ "sort" }  ||= "num";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
+        $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     if ( $options->{ "sort" } eq "num" ) {
@@ -4940,9 +5608,9 @@ sub script_plot_histogram
         map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
     }
 
-    $result = &Maasha::Plot::histogram_simple( \@data_list, $options );
+    $result = Maasha::Plot::histogram_simple( \@data_list, $options );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh "$_\n" foreach @{ $result };
 
@@ -4967,22 +5635,22 @@ sub script_plot_lendist
 
     $options->{ "title" } ||= "Length Distribution";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
-        $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
+        $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $max = &Maasha::Calc::list_max( [ keys %data_hash ] );
+    $max = Maasha::Calc::list_max( [ keys %data_hash ] );
 
     for ( $i = 0; $i < $max; $i++ ) {
         push @data_list, [ $i, $data_hash{ $i } || 0 ];
     }
 
-    $result = &Maasha::Plot::histogram_lendist( \@data_list, $options );
+    $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh "$_\n" foreach @{ $result };
 
@@ -5007,7 +5675,7 @@ sub script_plot_chrdist
 
     $options->{ "title" } ||= "Chromosome Distribution";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "CHR" } ) {                                                             # generic
             $data_hash{ $record->{ "CHR" } }++;
@@ -5019,7 +5687,7 @@ sub script_plot_chrdist
             $data_hash{ $record->{ "S_ID" } }++;
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     foreach $elem ( keys %data_hash )
@@ -5043,9 +5711,9 @@ sub script_plot_chrdist
 
     @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
 
-    $result = &Maasha::Plot::histogram_chrdist( \@data_list, $options );
+    $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh "$_\n" foreach @{ $result };
 
@@ -5071,19 +5739,19 @@ sub script_plot_karyogram
     $options->{ "genome" }     ||= "human";
     $options->{ "feat_color" } ||= "black";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
         {
             push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $result = &Maasha::Plot::karyogram( \%data_hash, \%options );
+    $result = Maasha::Plot::karyogram( \%data_hash, \%options );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh $result;
 
@@ -5108,22 +5776,22 @@ sub script_plot_matches
 
     $options->{ "direction" } ||= "both";
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
             push @data, $record;
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
     $options->{ "title" }  ||= "plot_matches";
     $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
     $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
 
-    $result = &Maasha::Plot::dotplot_matches( \@data, $options, $TMP_DIR );
+    $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     print $fh "$_\n" foreach @{ $result };
 
@@ -5146,7 +5814,7 @@ sub script_length_vals
 
     my ( $record, $key );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } )
         {
@@ -5155,7 +5823,7 @@ sub script_length_vals
             }
         }
 
-        &put_record( $record, $out );
+        put_record( $record, $out );
     }
 }
 
@@ -5175,7 +5843,7 @@ sub script_sum_vals
 
     my ( $record, $key, %sum_hash, $fh );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } )
         {
@@ -5184,13 +5852,13 @@ sub script_sum_vals
             }
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     foreach $key ( @{ $options->{ "keys" } } ) {
-        &put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
+        put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
     }
 
     close $fh;
@@ -5212,7 +5880,7 @@ sub script_mean_vals
 
     my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } )
         {
@@ -5223,10 +5891,10 @@ sub script_mean_vals
             }
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     foreach $key ( @{ $options->{ "keys" } } )
     {
@@ -5236,7 +5904,7 @@ sub script_mean_vals
             $mean = "N/A";
         }
 
-        &put_record( { $key . "_MEAN" => $mean } , $fh );
+        put_record( { $key . "_MEAN" => $mean } , $fh );
     }
 
     close $fh;
@@ -5258,26 +5926,26 @@ sub script_median_vals
 
     my ( $record, $key, %median_hash, $median, $fh );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } ) {
             push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     foreach $key ( @{ $options->{ "keys" } } )
     {
         if ( $median_hash{ $key } ) {
-            $median = &Maasha::Calc::median( $median_hash{ $key } );
+            $median = Maasha::Calc::median( $median_hash{ $key } );
         } else {
             $median = "N/A";
         }
 
-        &put_record( { $key . "_MEDIAN" => $median } , $fh );
+        put_record( { $key . "_MEDIAN" => $median } , $fh );
     }
 
     close $fh;
@@ -5299,7 +5967,7 @@ sub script_max_vals
 
     my ( $record, $key, $fh, %max_hash, $max_record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } )
         {
@@ -5309,17 +5977,17 @@ sub script_max_vals
             }
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     foreach $key ( @{ $options->{ "keys" } } )
     {
         $max_record->{ $key . "_MAX" } = $max_hash{ $key };
     }
 
-    &put_record( $max_record, $fh );
+    put_record( $max_record, $fh );
 
     close $fh;
 }
@@ -5340,7 +6008,7 @@ sub script_min_vals
 
     my ( $record, $key, $fh, %min_hash, $min_record );
 
-    while ( $record = &get_record( $in ) ) 
+    while ( $record = get_record( $in ) ) 
     {
         foreach $key ( @{ $options->{ "keys" } } )
         {
@@ -5354,17 +6022,17 @@ sub script_min_vals
             }
         }
 
-        &put_record( $record, $out ) if not $options->{ "no_stream" };
+        put_record( $record, $out ) if not $options->{ "no_stream" };
     }
 
-    $fh = &write_stream( $options->{ "data_out" } );
+    $fh = write_stream( $options->{ "data_out" } );
 
     foreach $key ( @{ $options->{ "keys" } } )
     {
         $min_record->{ $key . "_MIN" } = $min_hash{ $key };
     }
 
-    &put_record( $min_record, $fh );
+    put_record( $min_record, $fh );
 
     close $fh;
 }
@@ -5376,6 +6044,8 @@ sub script_upload_to_ucsc
 
     # Calculate the mean of values of given keys.
 
+    # This routine has developed into the most ugly hack. Do something!
+
     my ( $in,        # handle to in stream
          $out,       # handle to out stream
          $options,   # options hash
@@ -5383,8 +6053,7 @@ sub script_upload_to_ucsc
 
     # Returns nothing.
 
-    my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
-         $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
+    my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $vals );
 
     $options->{ "short_label" } ||= $options->{ 'table' };
     $options->{ "long_label" }  ||= $options->{ 'table' };
@@ -5394,7 +6063,7 @@ sub script_upload_to_ucsc
     $options->{ "color" }       ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
     $options->{ "chunk_size" }  ||= 10_000_000_000;    # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
 
-    $file = "$TMP_DIR/ucsc_upload.tmp";
+    $file = "$BP_TMP/ucsc_upload.tmp";
 
     $append = 0;
 
@@ -5402,201 +6071,109 @@ sub script_upload_to_ucsc
 
     $i = 0;
 
-    if ( $options->{ 'wiggle' } )
+    $fh_out = Maasha::Common::write_open( $file );
+
+    while ( $record = get_record( $in ) ) 
     {
-        $options->{ "visibility" } = "full";
+        put_record( $record, $out ) if not $options->{ "no_stream" };
 
-        while ( $record = &get_record( $in ) )
+        if ( $record->{ "REC_TYPE" } eq "fixed_step" )
         {
-            &put_record( $record, $out ) if not $options->{ "no_stream" };
-
-            $record->{ "CHR" }     = $record->{ "S_ID" }  if not defined $record->{ "CHR" };
-            $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
-            $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
+            $vals = $record->{ "VALS" };
+            $vals =~ tr/;/\n/;
 
-            $fh_hash{ $record->{ "CHR" } } = &Maasha::Common::write_open( "$TMP_DIR/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
+            print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
+            print $fh_out "$vals\n";
 
-            $fh_out = $fh_hash{ $record->{ "CHR" } };
-            
-            &Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
+            $format = "WIGGLE" if not $format;
         }
-
-        map { close $_ } keys %fh_hash;
-
-        $fh_out = &Maasha::Common::write_open( $file );
-
-        foreach $chr ( sort keys %fh_hash )
+        elsif ( $record->{ "REC_TYPE" } eq "PSL" )
         {
-            &Maasha::Common::run( "bedSort", "$TMP_DIR/$chr $TMP_DIR/$chr" );
-
-            $fh_in = &Maasha::Common::read_open( "$TMP_DIR/$chr" );
-
-            undef $block;
-
-            while ( $entry = &Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
-            {
-                $chr  = $entry->{ 'CHR' };
-                $beg  = $entry->{ 'CHR_BEG' };
-                $end  = $entry->{ 'CHR_END' };
-                $q_id = $entry->{ 'Q_ID' };
-                
-                if ( $q_id =~ /_(\d+)$/ ) {
-                    $clones = $1;
-                } else {
-                    $clones = 1;
-                }
-
-                if ( $block )
-                {
-                    if ( $beg > $max )
-                    {
-                        &Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
-                        undef $block;
-                    }
-                    else
-                    {
-                        for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
-                            $block->[ $i ] += $clones;
-                        }
-
-                        $max = &Maasha::Calc::max( $max, $end );
-                    }
-                }
-
-                if ( not $block )
-                {
-                    $beg_block = $beg;
-                    $max       = $end;
-
-                    for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
-                        $block->[ $i ] += $clones;
-                    }
-                }
-            }
-
-            close $fh_in;
-
-            &Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
+            Maasha::UCSC::psl_put_header( $fh_out ) if $first;
+            Maasha::UCSC::psl_put_entry( $record, $fh_out );
+            
+            $first = 0;
 
-            unlink "$TMP_DIR/$chr";
+            $format = "PSL" if not $format;
         }
+        elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
+        {
+            # chrom chromStart  chromEnd    name    score   strand  size    secStr  conf 
 
-        close $fh_out;
-
-        $wig_file = "$options->{ 'table' }.wig";
-        $wib_file = "$options->{ 'table' }.wib";
-
-        $wib_dir  = "$ENV{ 'DATA_DIR' }/genomes/$options->{ 'database' }/wib";
-
-        &Maasha::Common::dir_create_if_not_exists( $wib_dir );
-
-        # &Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
-
-        `cd $TMP_DIR && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
-        &Maasha::Common::run( "mv", "$TMP_DIR/$wib_file $wib_dir" );
-
-        unlink $file;
+            print $fh_out join ( "\t",
+                $record->{ "CHR" },
+                $record->{ "CHR_BEG" },
+                $record->{ "CHR_END" } + 1,
+                $record->{ "Q_ID" },
+                $record->{ "SCORE" },
+                $record->{ "STRAND" },
+                $record->{ "SIZE" },
+                $record->{ "SEC_STRUCT" },
+                $record->{ "CONF" },
+            ), "\n";
 
-        $file = $wig_file;
+            $format  = "BED_SS" if not $format;
+        }
+        elsif ( $record->{ "REC_TYPE" } eq "BED" )
+        {
+            Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
 
-        $format = "WIGGLE";
-    }
-    else
-    {
-        $fh_out = &Maasha::Common::write_open( $file );
-    
-        while ( $record = &get_record( $in ) ) 
+            $format  = "BED"                   if not $format;
+            $columns = $record->{ "BED_COLS" } if not $columns;
+        }
+        elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
         {
-            &put_record( $record, $out ) if not $options->{ "no_stream" };
+            Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
 
-            if ( $record->{ "REC_TYPE" } eq "PSL" )
-            {
-                &Maasha::UCSC::psl_put_header( $fh_out ) if $first;
-                &Maasha::UCSC::psl_put_entry( $record, $fh_out );
-                
-                $first = 0;
+            $format  = "BED" if not $format;
+            $columns = 6     if not $columns;
+        }
+        elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
+        {
+            $record->{ "CHR" }     = $record->{ "S_ID" };
+            $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
+            $record->{ "CHR_END" } = $record->{ "S_END" };
+            $record->{ "SCORE" }   = $record->{ "BIT_SCORE" } * 1000;
 
-                $format = "PSL" if not $format;
-            }
-            elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
-            {
-                # chrom chromStart  chromEnd    name    score   strand  size    secStr  conf 
-
-                print $fh_out join ( "\t",
-                    $record->{ "CHR" },
-                    $record->{ "CHR_BEG" },
-                    $record->{ "CHR_END" } + 1,
-                    $record->{ "Q_ID" },
-                    $record->{ "SCORE" },
-                    $record->{ "STRAND" },
-                    $record->{ "SIZE" },
-                    $record->{ "SEC_STRUCT" },
-                    $record->{ "CONF" },
-                ), "\n";
-
-                $format  = "BED_SS" if not $format;
-            }
-            elsif ( $record->{ "REC_TYPE" } eq "BED" )
-            {
-                &Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
+            $format  = "BED" if not $format;
+            $columns = 6     if not $columns;
 
-                $format  = "BED"                   if not $format;
-                $columns = $record->{ "BED_COLS" } if not $columns;
-            }
-            elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
-            {
-                &Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
-
-                $format  = "BED" if not $format;
-                $columns = 6     if not $columns;
-            }
-            elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
-            {
-                $record->{ "CHR" }     = $record->{ "S_ID" };
-                $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
-                $record->{ "CHR_END" } = $record->{ "S_END" };
-                $record->{ "SCORE" }   = $record->{ "BIT_SCORE" } * 1000;
+            Maasha::UCSC::bed_put_entry( $record, $fh_out );
+        }
+        elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
+        {
+            $record->{ "CHR" }     = $record->{ "S_ID" };
+            $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
+            $record->{ "CHR_END" } = $record->{ "S_END" };
+            $record->{ "SCORE" }   = $record->{ "SCORE" } || 999;
+            $record->{ "SCORE" }   = int( $record->{ "SCORE" } );
 
-                $format  = "BED" if not $format;
-                $columns = 6     if not $columns;
+            $format  = "BED" if not $format;
+            $columns = 6     if not $columns;
 
-                &Maasha::UCSC::bed_put_entry( $record, $fh_out );
-            }
-            elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
-            {
-                $record->{ "CHR" }     = $record->{ "S_ID" };
-                $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
-                $record->{ "CHR_END" } = $record->{ "S_END" };
-                $record->{ "SCORE" }   = $record->{ "SCORE" } || 999;
-                $record->{ "SCORE" }   = int( $record->{ "SCORE" } );
+            Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
+        }
 
-                $format  = "BED" if not $format;
-                $columns = 6     if not $columns;
+        if ( $i == $options->{ "chunk_size" } )
+        {
+            close $fh_out;
 
-                &Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
+            if ( $format eq "BED" ) {
+                Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
+            } elsif ( $format eq "PSL" ) {
+                Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
             }
 
-            if ( $i == $options->{ "chunk_size" } )
-            {
-                close $fh_out;
-
-                if ( $format eq "BED" ) {
-                    &Maasha::UCSC::bed_upload_to_ucsc( $TMP_DIR, $file, $options, $append );
-                } elsif ( $format eq "PSL" ) {
-                    &Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
-                }
-
-                unlink $file;
-
-                $first = 1;
+            unlink $file;
 
-                $append = 1;
+            $first = 1;
 
-                $fh_out = &Maasha::Common::write_open( $file );
-            }
+            $append = 1;
 
-            $i++;
+            $fh_out = Maasha::Common::write_open( $file );
         }
+
+        $i++;
     }
 
     close $fh_out;
@@ -5607,7 +6184,7 @@ sub script_upload_to_ucsc
         {
             $type = "bed $columns";
 
-            &Maasha::UCSC::bed_upload_to_ucsc( $TMP_DIR, $file, $options, $append );
+            Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
         }
         elsif ( $format eq "BED_SS" )
         {
@@ -5615,24 +6192,45 @@ sub script_upload_to_ucsc
 
             $type = "sec_struct";
         
-            &Maasha::UCSC::bed_upload_to_ucsc( $TMP_DIR, $file, $options, $append );
+            Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
         }
         elsif ( $format eq "PSL" )
         {
             $type = "psl";
 
-            &Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
+            Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
         }
         elsif ( $format eq "WIGGLE" )
         {
+            $options->{ "visibility" } = "full";
+
+            $wig_file = "$options->{ 'table' }.wig";
+            $wib_file = "$options->{ 'table' }.wib";
+
+            $wib_dir  = "$ENV{ 'HOME' }/ucsc/wib";
+
+            Maasha::Common::dir_create_if_not_exists( $wib_dir );
+
+            if ( $options->{ 'verbose' } ) {
+                `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
+            } else {
+                `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
+            }
+
+            Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
+
+            unlink $file;
+
+            $file = $wig_file;
+
             $type = "wig 0";
 
-            &Maasha::UCSC::wiggle_upload_to_ucsc( $TMP_DIR, $wib_dir, $file, $options );
+            Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
         }
 
         unlink $file;
 
-        &Maasha::UCSC::update_my_tracks( $options, $type );
+        Maasha::UCSC::update_my_tracks( $options, $type );
     }
 }
 
@@ -5640,6 +6238,32 @@ sub script_upload_to_ucsc
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+sub record2fasta
+{
+    # Martin A. Hansen, July 2008.
+
+    # Given a biopiece record converts it to a FASTA record.
+    # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
+    # tried in that order.
+
+    my ( $record,    # record
+       ) = @_;
+
+    # Returns a tuple.
+
+    my ( $seq_name, $seq );
+
+    $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" }  || $record->{ "S_ID" };
+    $seq      = $record->{ "SEQ" }      || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
+
+    if ( defined $seq_name and defined $seq ) {
+        return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
+    } else {
+        return;
+    }
+}
+
+
 sub read_stream
 {
     # Martin A. Hansen, July 2007.
@@ -5654,14 +6278,14 @@ sub read_stream
     my ( $fh );
 
     if ( not -t STDIN ) {
-        $fh = &Maasha::Common::read_stdin();
+        $fh = Maasha::Common::read_stdin();
     } elsif ( not $path ) {
-#        &Maasha::Common::error( qq(no data stream) );
+#        Maasha::Common::error( qq(no data stream) );
     } else {
-        $fh = &Maasha::Common::read_open( $path );
+        $fh = Maasha::Common::read_open( $path );
     }
     
-#    $fh->autoflush(1) if $fh;
+#    $fh->autoflush(1) if $fh;  # Disable file buffer for debugging.
 
     return $fh;
 }
@@ -5682,9 +6306,9 @@ sub write_stream
     my ( $fh );
 
     if ( $path ) {
-        $fh = &Maasha::Common::write_open( $path, $gzip );
+        $fh = Maasha::Common::write_open( $path, $gzip );
     } else {
-        $fh = &Maasha::Common::write_stdout();
+        $fh = Maasha::Common::write_stdout();
     }
 
     return $fh;
@@ -5698,10 +6322,10 @@ sub get_record
     # Reads one record at a time and converts that record
     # to a Perl data structure (a hash) which is returned.
 
-    my ( $fh,
+    my ( $fh,   # handle to stream
        ) = @_;
 
-    # Returns data structure
+    # Returns a hash
 
     my ( $block, @lines, $line, $key, $value, %record );
 
@@ -5717,7 +6341,7 @@ sub get_record
 
     foreach $line ( @lines )
     {
-        ( $key, $value ) = split ": ", $line;
+        ( $key, $value ) = split ": ", $line, 2;
 
         $record{ $key } = $value;
     }
@@ -5805,7 +6429,7 @@ sub sig_handler
 
     sleep 1;
 
-    if ( -d $TMP_DIR )
+    if ( -d $BP_TMP )
     {
         if ( $sig =~ /MAASHA_ERROR/ ) {
             print STDERR "\nProgram '$script' had an error"                     . "  -  Please wait for temporary data to be removed\n";
@@ -5817,69 +6441,63 @@ sub sig_handler
             print STDERR "\nProgram '$script' died->$sig"                       . "  -  Please wait for temporary data to be removed\n";
         }
 
-        # This is a really bad solution, potentially, anyone can include this module and set
-        # the TMP_DIR to point at any dir and thus take out the machine !!!
-
-        &Maasha::Common::dir_remove( $TMP_DIR );
+        clean_tmp();
     }
 
     exit( 0 );
 }
 
 
-END
+sub clean_tmp
 {
-    # This is a really bad solution, potentially, anyone can include this module and set
-    # the TMP_DIR to point at any dir and thus take out the machine !!!
-
-    &Maasha::Common::dir_remove( $TMP_DIR );
-}
+    # Martin A. Hansen, July 2008.
 
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-1;
-
-__END__
-
-
-sub script_read_soft
-{
-    # Martin A. Hansen, December 2007.
-
-    # Read soft format.
-    # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
-
-    my ( $in,        # handle to in stream
-         $out,       # handle to out stream
-         $options,   # options hash
-       ) = @_;
+    # Cleans out any unused temporary files and directories in BP_TMP.
 
     # Returns nothing.
 
-    my ( $data_in, $file, $num, $records, $record );
+    my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
 
-    while ( $record = &get_record( $in ) ) {
-        &put_record( $record, $out );
-    }
+    $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
 
-    $num = 1;
+    $curr_pid = Maasha::Common::get_processid();
 
-    foreach $file ( @{ $options->{ "files" } } )
-    {
-        $records = &Maasha::NCBI::soft_parse( $file );
+    @dirs = Maasha::Common::ls_dirs( $tmpdir );
 
-        foreach $record ( @{ $records } )
+    foreach $dir ( @dirs )
+    {
+        if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
         {
-            &put_record( $record, $out );
-
-            goto NUM if $options->{ "num" } and $num == $options->{ "num" };
+            $user = $1;
+            $sid  = $2;
+            $pid  = $3;
 
-            $num++;
+            if ( $user eq Maasha::Common::get_user() )
+            {
+                if ( not Maasha::Common::process_running( $pid ) )
+                {
+                    # print STDERR "Removing stale dir: $dir\n";
+                    Maasha::Common::dir_remove( $dir );
+                }
+                elsif ( $pid == $curr_pid )
+                {
+                    # print STDERR "Removing current dir: $dir\n";
+                    # Maasha::Common::dir_remove( $dir );
+                }
+            }
         }
     }
+}
 
-    NUM:
 
-    close $data_in if $data_in;
+END
+{
+    clean_tmp();
 }
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+1;
+
+__END__