]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/BioRun.pm
migrated plot_lendist
[biopieces.git] / code_perl / Maasha / BioRun.pm
1 package Maasha::BioRun;
2
3
4 # Copyright (C) 2007-2009 Martin A. Hansen.
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25
26 # Routines that contains Biopieces which are run.
27
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 use strict;
33 use Data::Dumper;
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
37 use Maasha::Biopieces;
38 use Maasha::Config;
39 use Maasha::Common;
40 use Maasha::Filesys;
41 use Maasha::Fasta;
42 use Maasha::Align;
43 use Maasha::Matrix;
44 use Maasha::Match;
45 use Maasha::EMBL;
46 use Maasha::Stockholm;
47 use Maasha::Seq;
48 use Maasha::Patscan;
49 use Maasha::Plot;
50 use Maasha::Calc;
51 use Maasha::UCSC;
52 use Maasha::UCSC::BED;
53 use Maasha::UCSC::Wiggle;
54 use Maasha::NCBI;
55 use Maasha::GFF;
56 use Maasha::TwoBit;
57 use Maasha::Solid;
58 use Maasha::Solexa;
59 use Maasha::SQL;
60 use Maasha::Gwiki;
61
62 use vars qw( @ISA @EXPORT_OK );
63
64 require Exporter;
65
66 @ISA = qw( Exporter );
67
68 use constant {
69     SEQ_NAME => 0,
70     SEQ      => 1,
71 };
72
73
74 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75
76
77 my ( $script, $BP_TMP );
78
79 $script = Maasha::Common::get_scriptname();
80 $BP_TMP = Maasha::Common::get_tmpdir();
81
82
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
84
85
86 run_script( $script );
87
88
89 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
90
91
92 sub run_script
93 {
94     # Martin A. Hansen, August 2007.
95
96     # Run a specific script.
97
98     my ( $script,   # script name
99        ) = @_;
100
101     # Returns nothing.
102
103     my ( $t0, $t1, $options, $in, $out );
104
105     Maasha::Biopieces::log_biopiece();
106
107     $t0 = gettimeofday();
108
109     $options = get_options( $script );
110
111     $options->{ "SCRIPT" } = $script;
112
113     $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
114
115     $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
116     $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
117
118     if    ( $script eq "print_usage" )              { script_print_usage(               $in, $out, $options ) }
119     elsif ( $script eq "read_psl" )                 { script_read_psl(                  $in, $out, $options ) }
120     elsif ( $script eq "read_bed" )                 { script_read_bed(                  $in, $out, $options ) }
121     elsif ( $script eq "read_fixedstep" )           { script_read_fixedstep(            $in, $out, $options ) }
122     elsif ( $script eq "read_blast_tab" )           { script_read_blast_tab(            $in, $out, $options ) }
123     elsif ( $script eq "read_embl" )                { script_read_embl(                 $in, $out, $options ) }
124     elsif ( $script eq "read_stockholm" )           { script_read_stockholm(            $in, $out, $options ) }
125     elsif ( $script eq "read_phastcons" )           { script_read_phastcons(            $in, $out, $options ) }
126     elsif ( $script eq "read_soft" )                { script_read_soft(                 $in, $out, $options ) }
127     elsif ( $script eq "read_gff" )                 { script_read_gff(                  $in, $out, $options ) }
128     elsif ( $script eq "read_2bit" )                { script_read_2bit(                 $in, $out, $options ) }
129     elsif ( $script eq "read_solexa" )              { script_read_solexa(               $in, $out, $options ) }
130     elsif ( $script eq "read_solid" )               { script_read_solid(                $in, $out, $options ) }
131     elsif ( $script eq "read_mysql" )               { script_read_mysql(                $in, $out, $options ) }
132     elsif ( $script eq "read_ucsc_config" )         { script_read_ucsc_config(          $in, $out, $options ) }
133     elsif ( $script eq "uppercase_seq" )            { script_uppercase_seq(             $in, $out, $options ) }
134     elsif ( $script eq "complexity_seq" )           { script_complexity_seq(            $in, $out, $options ) }
135     elsif ( $script eq "get_genome_align" )         { script_get_genome_align(          $in, $out, $options ) }
136     elsif ( $script eq "get_genome_phastcons" )     { script_get_genome_phastcons(      $in, $out, $options ) }
137     elsif ( $script eq "soap_seq" )                 { script_soap_seq(                  $in, $out, $options ) }
138     elsif ( $script eq "write_bed" )                { script_write_bed(                 $in, $out, $options ) }
139     elsif ( $script eq "write_psl" )                { script_write_psl(                 $in, $out, $options ) }
140     elsif ( $script eq "write_fixedstep" )          { script_write_fixedstep(           $in, $out, $options ) }
141     elsif ( $script eq "write_2bit" )               { script_write_2bit(                $in, $out, $options ) }
142     elsif ( $script eq "write_solid" )              { script_write_solid(               $in, $out, $options ) }
143     elsif ( $script eq "write_ucsc_config" )        { script_write_ucsc_config(         $in, $out, $options ) }
144     elsif ( $script eq "remove_mysql_tables" )      { script_remove_mysql_tables(       $in, $out, $options ) }
145     elsif ( $script eq "remove_ucsc_tracks" )       { script_remove_ucsc_tracks(        $in, $out, $options ) }
146     elsif ( $script eq "plot_matches" )             { script_plot_matches(              $in, $out, $options ) }
147     elsif ( $script eq "plot_seqlogo" )             { script_plot_seqlogo(              $in, $out, $options ) }
148     elsif ( $script eq "plot_phastcons_profiles" )  { script_plot_phastcons_profiles(   $in, $out, $options ) }
149     elsif ( $script eq "upload_to_ucsc" )           { script_upload_to_ucsc(            $in, $out, $options ) }
150
151     close $in if defined $in;
152     close $out;
153
154     $t1 = gettimeofday();
155
156     print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
157 }
158
159
160 sub get_options
161 {
162     # Martin A. Hansen, February 2008.
163
164     # Gets options from commandline and checks these vigerously.
165
166     my ( $script,     # name of script
167        ) = @_;
168
169     # Returns hash
170
171     my ( %options, @options, $opt, @genomes, $real );
172
173     if ( $script eq "print_usage" )
174     {
175         @options = qw(
176             data_in|i=s
177         );
178     }
179     elsif ( $script eq "read_psl" )
180     {
181         @options = qw(
182             data_in|i=s
183             num|n=s
184         );
185     }
186     elsif ( $script eq "read_bed" )
187     {
188         @options = qw(
189             data_in|i=s
190             cols|c=s
191             num|n=s
192             check|C
193         );
194     }
195     elsif ( $script eq "read_fixedstep" )
196     {
197         @options = qw(
198             data_in|i=s
199             num|n=s
200         );
201     }
202     elsif ( $script eq "read_blast_tab" )
203     {
204         @options = qw(
205             data_in|i=s
206             num|n=s
207         );
208     }
209     elsif ( $script eq "read_embl" )
210     {
211         @options = qw(
212             data_in|i=s
213             num|n=s
214             keys|k=s
215             feats|f=s
216             quals|q=s
217         );
218     }
219     elsif ( $script eq "read_stockholm" )
220     {
221         @options = qw(
222             data_in|i=s
223             num|n=s
224         );
225     }
226     elsif ( $script eq "read_phastcons" )
227     {
228         @options = qw(
229             data_in|i=s
230             num|n=s
231             min|m=s
232             dist|d=s
233             threshold|t=f
234             gap|g=s
235         );
236     }
237     elsif ( $script eq "read_soft" )
238     {
239         @options = qw(
240             data_in|i=s
241             samples|s=s
242             num|n=s
243         );
244     }
245     elsif ( $script eq "read_gff" )
246     {
247         @options = qw(
248             data_in|i=s
249             num|n=s
250         );
251     }
252     elsif ( $script eq "read_2bit" )
253     {
254         @options = qw(
255             data_in|i=s
256             num|n=s
257             no_mask|N
258         );
259     }
260     elsif ( $script eq "read_solexa" )
261     {
262         @options = qw(
263             data_in|i=s
264             num|n=s
265             format|f=s
266             quality|q=s
267         );
268     }
269     elsif ( $script eq "read_solid" )
270     {
271         @options = qw(
272             data_in|i=s
273             num|n=s
274             quality|q=s
275         );
276     }
277     elsif ( $script eq "read_mysql" )
278     {
279         @options = qw(
280             database|d=s
281             query|q=s
282             user|u=s
283             password|p=s
284         );
285     }
286     elsif ( $script eq "read_ucsc_config" )
287     {
288         @options = qw(
289             data_in|i=s
290             num|n=s
291         );
292     }
293     elsif ( $script eq "get_genome_align" )
294     {
295         @options = qw(
296             genome|g=s
297             chr|c=s
298             beg|b=s
299             end|e=s
300             len|l=s
301             strand|s=s
302         );
303     }
304     elsif ( $script eq "get_genome_phastcons" )
305     {
306         @options = qw(
307             genome|g=s
308             chr|c=s
309             beg|b=s
310             end|e=s
311             len|l=s
312             flank|f=s
313         );
314     }
315     elsif ( $script eq "soap_seq" )
316     {
317         @options = qw(
318             in_file|i=s
319             genome|g=s
320             seed_size|s=s
321             mismatches|m=s
322             gap_size|G=s
323             cpus|c=s
324         );
325     }
326     elsif ( $script eq "write_bed" )
327     {
328         @options = qw(
329             cols|c=s
330             check|C
331             no_stream|x
332             data_out|o=s
333             compress|Z
334         );
335     }
336     elsif ( $script eq "write_psl" )
337     {
338         @options = qw(
339             no_stream|x
340             data_out|o=s
341             compress|Z
342         );
343     }
344     elsif ( $script eq "write_fixedstep" )
345     {
346         @options = qw(
347             no_stream|x
348             data_out|o=s
349             compress|Z
350         );
351     }
352     elsif ( $script eq "write_2bit" )
353     {
354         @options = qw(
355             no_stream|x
356             data_out|o=s
357             no_mask|N
358         );
359     }
360     elsif ( $script eq "write_solid" )
361     {
362         @options = qw(
363             wrap|w=s
364             no_stream|x
365             data_out|o=s
366             compress|Z
367         );
368     }
369     elsif ( $script eq "write_ucsc_config" )
370     {
371         @options = qw(
372             no_stream|x
373             data_out|o=s
374         );
375     }
376     elsif ( $script eq "plot_seqlogo" )
377     {
378         @options = qw(
379             no_stream|x
380             data_out|o=s
381         );
382     }
383     elsif ( $script eq "plot_phastcons_profiles" )
384     {
385         @options = qw(
386             no_stream|x
387             data_out|o=s
388             genome|g=s
389             mean|m
390             median|M
391             flank|f=s
392             terminal|t=s
393             title|T=s
394             xlabel|X=s
395             ylabel|Y=s
396         );
397     }
398     elsif ( $script eq "remove_mysql_tables" )
399     {
400         @options = qw(
401             database|d=s
402             tables|t=s
403             keys|k=s
404             user|u=s
405             password|p=s
406             no_stream|x
407         );
408     }
409     elsif ( $script eq "remove_ucsc_tracks" )
410     {
411         @options = qw(
412             database|d=s
413             tracks|t=s
414             keys|k=s
415             config_file|c=s
416             user|u=s
417             password|p=s
418             no_stream|x
419         );
420     }
421     elsif ( $script eq "plot_matches" )
422     {
423         @options = qw(
424             no_stream|x
425             data_out|o=s
426             terminal|t=s
427             title|T=s
428             xlabel|X=s
429             ylabel|Y=s
430             direction|d=s
431         );
432     }
433     elsif ( $script eq "upload_to_ucsc" )
434     {
435         @options = qw(
436             no_stream|x
437             database|d=s
438             table|t=s
439             short_label|s=s
440             long_label|l=s
441             group|g=s
442             priority|p=f
443             use_score|u
444             visibility|V=s
445             color|c=s
446             check|C
447         );
448     }
449
450     push @options, qw(
451         stream_in|I=s
452         stream_out|O=s
453         verbose|v
454         help|?
455     );
456
457 #    print STDERR Dumper( \@options );
458     
459     GetOptions(
460         \%options,
461         @options,
462     );
463
464 #    print STDERR Dumper( \%options );
465
466     if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
467         return wantarray ? %options : \%options;
468     }
469
470     $options{ "cols" }      = [ split ",", $options{ "cols" } ]      if defined $options{ "cols" };
471     $options{ "keys" }      = [ split ",", $options{ "keys" } ]      if defined $options{ "keys" };
472     $options{ "no_keys" }   = [ split ",", $options{ "no_keys" } ]   if defined $options{ "no_keys" };
473     $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
474     $options{ "quals" }     = [ split ",", $options{ "quals" } ]     if defined $options{ "quals" };
475     $options{ "feats" }     = [ split ",", $options{ "feats" } ]     if defined $options{ "feats" };
476     $options{ "frames" }    = [ split ",", $options{ "frames" } ]    if defined $options{ "frames" };
477     $options{ "samples" }   = [ split ",", $options{ "samples" } ]   if defined $options{ "samples" };
478     $options{ "tables" }    = [ split ",", $options{ "tables" } ]    if defined $options{ "tables" };
479     $options{ "tracks" }    = [ split ",", $options{ "tracks" } ]    if defined $options{ "tracks" };
480     
481     # ---- check arguments ----
482
483     if ( $options{ 'data_in' } )
484     {
485         $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
486
487         Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
488     }
489
490     map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
491
492     # print STDERR Dumper( \%options );
493
494     $real = "beg|end|word_size|wrap|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
495
496     foreach $opt ( keys %options )
497     {
498         if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
499         {
500             Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
501         }
502         elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
503         {
504             Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
505         }
506         elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
507         {
508             Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
509         }
510         elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
511         {
512             Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
513         }
514         elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
515         {
516             Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
517         }
518         elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
519         {
520             Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
521         }
522         elsif ( $opt eq "genome" )
523         {
524             @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
525             map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
526
527             if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
528                 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
529             }
530         }
531         elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
532         {
533             Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
534         }
535         elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
536         {
537             Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
538         }
539         elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
540         {
541             Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
542         }
543         elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
544         {
545             Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
546         }
547     }
548
549     Maasha::Common::error( qq(no --database specified) )                if $script eq "remove_ucsc_tracks"  and not $options{ "database" };
550     Maasha::Common::error( qq(no --in_file or --genome specified) )     if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
551     Maasha::Common::error( qq(both --in_file and --genome specified) )  if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
552     Maasha::Common::error( qq(no --genome specified) )                  if $script =~ /get_genome_align|get_genome_phastcons|plot_phastcons_profiles/ and not $options{ "genome" };
553
554     if ( $script eq "upload_to_ucsc" )
555     {
556         Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
557         Maasha::Common::error( qq(no --table specified) )    if not $options{ "table" };
558     }
559
560     return wantarray ? %options : \%options;
561 }
562
563
564 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
565
566
567 sub script_print_usage
568 {
569     # Martin A. Hansen, January 2008.
570
571     # Retrieves usage information from file and
572     # prints this nicely formatted.
573
574     my ( $in,        # handle to in stream
575          $out,       # handle to out stream
576          $options,   # options hash
577        ) = @_;
578
579     # Returns nothing.
580
581     my ( $file, $wiki, $lines );
582
583     if ( $options->{ 'data_in' } ) {
584         $file = $options->{ 'data_in' };
585     } else {
586         $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
587     }
588
589     $wiki = Maasha::Gwiki::gwiki_read( $file );
590
591     ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
592
593     if ( not $options->{ "help" } ) {
594         @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
595     }
596
597     $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
598
599     print STDERR "$_\n" foreach @{ $lines };
600
601     exit;
602 }
603
604
605 sub script_read_psl
606 {
607     # Martin A. Hansen, August 2007.
608
609     # Read psl table from stream or file.
610
611     my ( $in,        # handle to in stream
612          $out,       # handle to out stream
613          $options,   # options hash
614        ) = @_;
615
616     # Returns nothing.
617
618     my ( $record, $file, $data_in, $num );
619
620     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
621         Maasha::Biopieces::put_record( $record, $out );
622     }
623
624     $num = 1;
625
626     foreach $file ( @{ $options->{ "files" } } )
627     {
628         $data_in = Maasha::Common::read_open( $file );
629
630         while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) ) 
631         {
632             Maasha::Biopieces::put_record( $record, $out );
633
634             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
635
636             $num++;
637         }
638     }
639
640     NUM:
641 }
642
643
644 sub script_read_bed
645 {
646     # Martin A. Hansen, August 2007.
647
648     # Read bed table from stream or file.
649
650     my ( $in,        # handle to in stream
651          $out,       # handle to out stream
652          $options,   # options hash
653        ) = @_;
654
655     # Returns nothing.
656
657     my ( $cols, $file, $record, $bed_entry, $data_in, $num );
658
659     $cols = $options->{ 'cols' }->[ 0 ];
660
661     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
662         Maasha::Biopieces::put_record( $record, $out );
663     }
664
665     $num = 1;
666
667     foreach $file ( @{ $options->{ "files" } } )
668     {
669         $data_in = Maasha::Common::read_open( $file );
670
671         while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
672         {
673             $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
674
675             Maasha::Biopieces::put_record( $record, $out );
676
677             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
678
679             $num++;
680         }
681
682         close $data_in;
683     }
684
685     NUM:
686
687     close $data_in if $data_in;
688 }
689
690
691 sub script_read_fixedstep
692 {
693     # Martin A. Hansen, Juli 2008.
694
695     # Read fixedstep wiggle format from stream or file.
696
697     my ( $in,        # handle to in stream
698          $out,       # handle to out stream
699          $options,   # options hash
700        ) = @_;
701
702     # Returns nothing.
703
704     my ( $file, $record, $entry, $data_in, $num );
705
706     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
707         Maasha::Biopieces::put_record( $record, $out );
708     }
709
710     $num = 1;
711
712     foreach $file ( @{ $options->{ "files" } } )
713     {
714         $data_in = Maasha::Common::read_open( $file );
715
716         while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
717         {
718             $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
719
720             Maasha::Biopieces::put_record( $record, $out );
721
722             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
723
724             $num++;
725         }
726
727         close $data_in;
728     }
729
730     NUM:
731
732     close $data_in if $data_in;
733 }
734
735
736 sub script_read_blast_tab
737 {
738     # Martin A. Hansen, September 2007.
739
740     # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
741
742     my ( $in,        # handle to in stream
743          $out,       # handle to out stream
744          $options,   # options hash
745        ) = @_;
746
747     # Returns nothing.
748
749     my ( $file, $line, @fields, $strand, $record, $data_in, $num );
750
751     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
752         Maasha::Biopieces::put_record( $record, $out );
753     }
754
755     $num = 1;
756
757     foreach $file ( @{ $options->{ "files" } } )
758     {
759         $data_in = Maasha::Common::read_open( $file );
760
761         while ( $line = <$data_in> )
762         {
763             chomp $line;
764
765             next if $line =~ /^#/;
766
767             @fields = split /\t/, $line;
768
769             $record->{ "REC_TYPE" }   = "BLAST";
770             $record->{ "Q_ID" }       = $fields[ 0 ];
771             $record->{ "S_ID" }       = $fields[ 1 ];
772             $record->{ "IDENT" }      = $fields[ 2 ];
773             $record->{ "ALIGN_LEN" }  = $fields[ 3 ];
774             $record->{ "MISMATCHES" } = $fields[ 4 ];
775             $record->{ "GAPS" }       = $fields[ 5 ];
776             $record->{ "Q_BEG" }      = $fields[ 6 ] - 1; # BLAST is 1-based
777             $record->{ "Q_END" }      = $fields[ 7 ] - 1; # BLAST is 1-based
778             $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # BLAST is 1-based
779             $record->{ "S_END" }      = $fields[ 9 ] - 1; # BLAST is 1-based
780             $record->{ "E_VAL" }      = $fields[ 10 ];
781             $record->{ "BIT_SCORE" }  = $fields[ 11 ];
782
783             if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
784             {
785                 $record->{ "STRAND" } = '-';
786
787                 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
788             }
789             else
790             {
791                 $record->{ "STRAND" } = '+';
792             }
793
794             Maasha::Biopieces::put_record( $record, $out );
795
796             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
797
798             $num++;
799         }
800
801         close $data_in;
802     }
803
804     NUM:
805
806     close $data_in if $data_in;
807 }
808
809
810 sub script_read_embl
811 {
812     # Martin A. Hansen, August 2007.
813
814     # Read EMBL format.
815
816     my ( $in,        # handle to in stream
817          $out,       # handle to out stream
818          $options,   # options hash
819        ) = @_;
820
821     # Returns nothing.
822
823     my ( %options2, $file, $data_in, $num, $entry, $record );
824
825     map { $options2{ "keys" }{ $_ } = 1 }  @{ $options->{ "keys" } };
826     map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
827     map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
828
829     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
830         Maasha::Biopieces::put_record( $record, $out );
831     }
832
833     $num = 1;
834
835     foreach $file ( @{ $options->{ "files" } } )
836     {
837         $data_in = Maasha::Common::read_open( $file );
838
839         while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) ) 
840         {
841             $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
842
843             my ( $feat, $feat2, $qual, $qual_val, $record_copy );
844
845             $record_copy = dclone $record;
846
847             delete $record_copy->{ "FT" };
848
849             Maasha::Biopieces::put_record( $record_copy, $out );
850
851             delete $record_copy->{ "SEQ" };
852
853             foreach $feat ( keys %{ $record->{ "FT" } } )
854             {
855                 $record_copy->{ "FEAT_TYPE" } = $feat;
856
857                 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
858                 {
859                     foreach $qual ( keys %{ $feat2 } )
860                     {
861                         $qual_val = join "; ", @{ $feat2->{ $qual } };
862
863                         $qual =~ s/^_//;
864                         $qual = uc $qual;
865
866                         $record_copy->{ $qual } = $qual_val;
867                     }
868
869                     Maasha::Biopieces::put_record( $record_copy, $out );
870                 }
871             }
872
873             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
874
875             $num++;
876         }
877
878         close $data_in;
879     }
880
881     NUM:
882
883     close $data_in if $data_in;
884 }
885
886
887 sub script_read_stockholm
888 {
889     # Martin A. Hansen, August 2007.
890
891     # Read Stockholm format.
892
893     my ( $in,        # handle to in stream
894          $out,       # handle to out stream
895          $options,   # options hash
896        ) = @_;
897
898     # Returns nothing.
899
900     my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
901
902     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
903         Maasha::Biopieces::put_record( $record, $out );
904     }
905
906     $num = 1;
907
908     foreach $file ( @{ $options->{ "files" } } )
909     {
910         $data_in = Maasha::Common::read_open( $file );
911
912         while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) ) 
913         {
914             $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
915
916             undef $record_anno;
917
918             foreach $key ( keys %{ $record->{ "GF" } } ) {
919                 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
920             }
921
922             $record_anno->{ "ALIGN" } = $num;
923
924             Maasha::Biopieces::put_record( $record_anno, $out );
925
926             foreach $seq ( @{ $record->{ "ALIGN" } } )
927             {
928                 undef $record_align;
929             
930                 $record_align = {
931                     SEQ_NAME  => $seq->[ 0 ],
932                     SEQ       => $seq->[ 1 ],
933                 };
934             
935                 Maasha::Biopieces::put_record( $record_align, $out );
936             }
937
938             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
939
940             $num++;
941         }
942
943         close $data_in;
944     }
945
946     NUM:
947
948     close $data_in if $data_in;
949 }
950
951
952 sub script_read_phastcons
953 {
954     # Martin A. Hansen, December 2007.
955
956     # Read PhastCons format.
957
958     my ( $in,        # handle to in stream
959          $out,       # handle to out stream
960          $options,   # options hash
961        ) = @_;
962
963     # Returns nothing.
964
965     my ( $data_in, $file, $num, $entry, @records, $record );
966
967     $options->{ "min" }       ||= 10;
968     $options->{ "dist" }      ||= 25;
969     $options->{ "threshold" } ||= 0.8;
970     $options->{ "gap" }       ||= 5;
971
972     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
973         Maasha::Biopieces::put_record( $record, $out );
974     }
975
976     $num = 1;
977
978     foreach $file ( @{ $options->{ "files" } } )
979     {
980         $data_in = Maasha::Common::read_open( $file );
981
982         while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) ) 
983         {
984             @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
985
986             foreach $record ( @records )
987             {
988                 $record->{ "REC_TYPE" } = "BED";
989                 $record->{ "BED_LEN" }  = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
990
991                 Maasha::Biopieces::put_record( $record, $out );
992
993                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
994
995                 $num++;
996             }
997         }
998
999         close $data_in;
1000     }
1001
1002     NUM:
1003
1004     close $data_in if $data_in;
1005 }
1006
1007
1008 sub script_read_soft
1009 {
1010     # Martin A. Hansen, December 2007.
1011
1012     # Read soft format.
1013     # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1014
1015     my ( $in,        # handle to in stream
1016          $out,       # handle to out stream
1017          $options,   # options hash
1018        ) = @_;
1019
1020     # Returns nothing.
1021
1022     my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1023
1024     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1025         Maasha::Biopieces::put_record( $record, $out );
1026     }
1027
1028     $num = 1;
1029
1030     foreach $file ( @{ $options->{ "files" } } )
1031     {
1032         print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1033
1034         $soft_index = Maasha::NCBI::soft_index_file( $file );
1035
1036         $fh         = Maasha::Common::read_open( $file );
1037
1038         @platforms  = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1039
1040         print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1041
1042         $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1043
1044         @samples    = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1045
1046         $old_end    = $platforms[ -1 ]->{ "LINE_END" };
1047
1048         foreach $sample ( @samples )
1049         {
1050             $skip = 0;
1051             $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1052
1053             print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1054
1055             $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1056
1057             foreach $record ( @{ $records } )
1058             {
1059                 Maasha::Biopieces::put_record( $record, $out );
1060
1061                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1062
1063                 $num++;
1064             }
1065
1066             $old_end = $sample->{ "LINE_END" };
1067         }
1068
1069         close $fh;
1070     }
1071
1072     NUM:
1073
1074     close $data_in if $data_in;
1075     close $fh if $fh;
1076 }
1077
1078
1079 sub script_read_gff
1080 {
1081     # Martin A. Hansen, February 2008.
1082
1083     # Read soft format.
1084     # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1085
1086     my ( $in,        # handle to in stream
1087          $out,       # handle to out stream
1088          $options,   # options hash
1089        ) = @_;
1090
1091     # Returns nothing.
1092
1093     my ( $data_in, $file, $fh, $num, $record, $entry );
1094
1095     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1096         Maasha::Biopieces::put_record( $record, $out );
1097     }
1098
1099     $num = 1;
1100
1101     foreach $file ( @{ $options->{ "files" } } )
1102     {
1103         $fh = Maasha::Common::read_open( $file );
1104
1105         while ( $entry = Maasha::GFF::get_entry( $fh ) )
1106         {
1107             Maasha::Biopieces::put_record( $entry, $out );
1108
1109             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1110
1111             $num++;
1112         }
1113
1114         close $fh;
1115     }
1116
1117     NUM:
1118
1119     close $data_in if $data_in;
1120 }
1121
1122
1123 sub script_read_2bit
1124 {
1125     # Martin A. Hansen, March 2008.
1126
1127     # Read sequences from 2bit file.
1128
1129     my ( $in,        # handle to in stream
1130          $out,       # handle to out stream
1131          $options,   # options hash
1132        ) = @_;
1133
1134     # Returns nothing.
1135
1136     my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1137
1138     $mask = 1 if not $options->{ "no_mask" };
1139
1140     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1141         Maasha::Biopieces::put_record( $record, $out );
1142     }
1143
1144     $num = 1;
1145
1146     foreach $file ( @{ $options->{ "files" } } )
1147     {
1148         $data_in = Maasha::Common::read_open( $file );
1149
1150         $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1151
1152         foreach $line ( @{ $toc } )
1153         {
1154             $record->{ "SEQ_NAME" } = $line->[ 0 ];
1155             $record->{ "SEQ" }      = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1156             $record->{ "SEQ_LEN" }  = length $record->{ "SEQ" };
1157
1158             Maasha::Biopieces::put_record( $record, $out );
1159
1160             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1161
1162             $num++;
1163         }
1164
1165         close $data_in;
1166     }
1167
1168     NUM:
1169
1170     close $data_in if $data_in;
1171 }
1172
1173
1174 sub script_read_solexa
1175 {
1176     # Martin A. Hansen, March 2008.
1177
1178     # Read Solexa sequence reads from file.
1179
1180     my ( $in,        # handle to in stream
1181          $out,       # handle to out stream
1182          $options,   # options hash
1183        ) = @_;
1184
1185     # Returns nothing.
1186
1187     my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1188
1189     $options->{ "format" }  ||= "octal";
1190     $options->{ "quality" } ||= 20;
1191
1192     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1193         Maasha::Biopieces::put_record( $record, $out );
1194     }
1195
1196     $num = 1;
1197
1198     foreach $file ( @{ $options->{ "files" } } )
1199     {
1200         $data_in = Maasha::Common::read_open( $file );
1201
1202         if ( $options->{ "format" } eq "octal" )
1203         {
1204             while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1205             {
1206                 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1207
1208                 Maasha::Biopieces::put_record( $record, $out );
1209
1210                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1211
1212                 $num++;
1213             }
1214         }
1215         else
1216         {
1217             while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1218             {
1219                 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1220
1221                 Maasha::Biopieces::put_record( $record, $out );
1222
1223                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1224
1225                 $num++;
1226             }
1227         }
1228
1229         close $data_in;
1230     }
1231
1232     NUM:
1233
1234     close $data_in if $data_in;
1235 }
1236
1237
1238 sub script_read_solid
1239 {
1240     # Martin A. Hansen, April 2008.
1241
1242     # Read Solid sequence from file.
1243
1244     my ( $in,        # handle to in stream
1245          $out,       # handle to out stream
1246          $options,   # options hash
1247        ) = @_;
1248
1249     # Returns nothing.
1250
1251     my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1252
1253     $options->{ "quality" } ||= 15;
1254
1255     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1256         Maasha::Biopieces::put_record( $record, $out );
1257     }
1258
1259     $num = 1;
1260
1261     foreach $file ( @{ $options->{ "files" } } )
1262     {
1263         $data_in = Maasha::Common::read_open( $file );
1264
1265         while ( $line = <$data_in> )
1266         {
1267             chomp $line;
1268
1269             ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1270
1271             @scores = split /,/, $seq_qual;
1272             @seqs   = split //, Maasha::Solid::color_space2seq( $seq_cs );
1273
1274             for ( $i = 0; $i < @seqs; $i++ ) {
1275                 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1276             }
1277
1278             $record = {
1279                 REC_TYPE   => 'SOLID',
1280                 SEQ_NAME   => $seq_name,
1281                 SEQ_CS     => $seq_cs,
1282                 SEQ_QUAL   => join( ";", @scores ),
1283                 SEQ_LEN    => length $seq_cs,
1284                 SEQ        => join( "", @seqs ),
1285                 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1286             };
1287
1288             Maasha::Biopieces::put_record( $record, $out );
1289
1290             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1291
1292             $num++;
1293         }
1294
1295         close $data_in;
1296     }
1297
1298     NUM:
1299
1300     close $data_in if $data_in;
1301 }
1302
1303
1304 sub script_read_mysql
1305 {
1306     # Martin A. Hansen, May 2008.
1307
1308     # Read a MySQL query into stream.
1309
1310     my ( $in,        # handle to in stream
1311          $out,       # handle to out stream
1312          $options,   # options hash
1313        ) = @_;
1314
1315     # Returns nothing.
1316
1317     my ( $record, $dbh, $results );
1318
1319     $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
1320     $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1321
1322     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1323         Maasha::Biopieces::put_record( $record, $out );
1324     }
1325
1326     $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1327
1328     $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1329
1330     Maasha::SQL::disconnect( $dbh );
1331
1332     map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1333 }
1334
1335
1336 sub script_read_ucsc_config
1337 {
1338     # Martin A. Hansen, November 2008.
1339
1340     # Read track entries from UCSC Genome Browser '.ra' files.
1341
1342     my ( $in,        # handle to in stream
1343          $out,       # handle to out stream
1344          $options,   # options hash
1345        ) = @_;
1346
1347     # Returns nothing.
1348
1349     my ( $record, $file, $data_in, $entry, $num );
1350
1351     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1352         Maasha::Biopieces::put_record( $record, $out );
1353     }
1354
1355     $num = 1;
1356
1357     foreach $file ( @{ $options->{ "files" } } )
1358     {
1359         $data_in = Maasha::Common::read_open( $file );
1360
1361         while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) ) 
1362         {
1363             $record->{ 'REC_TYPE' } = "UCSC Config";
1364
1365             Maasha::Biopieces::put_record( $record, $out );
1366
1367             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1368
1369             $num++;
1370         }
1371
1372         close $data_in;
1373     }
1374
1375     NUM:
1376
1377     close $data_in if $data_in;
1378 }
1379
1380
1381 sub script_complexity_seq
1382 {
1383     # Martin A. Hansen, May 2008.
1384
1385     # Generates an index calculated as the most common di-residue over
1386     # the sequence length for all sequences in stream.
1387
1388     my ( $in,     # handle to in stream
1389          $out,    # handle to out stream
1390        ) = @_;
1391
1392     # Returns nothing.
1393
1394     my ( $record, $index );
1395
1396     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1397     {
1398         $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1399
1400         Maasha::Biopieces::put_record( $record, $out );
1401     }
1402 }
1403
1404
1405 sub script_get_genome_align
1406 {
1407     # Martin A. Hansen, April 2008.
1408
1409     # Gets a subalignment from a multiple genome alignment.
1410
1411     my ( $in,        # handle to in stream
1412          $out,       # handle to out stream
1413          $options,   # options hash
1414        ) = @_;
1415
1416     # Returns nothing.
1417
1418     my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
1419
1420     $options->{ "strand" } ||= "+";
1421
1422     $align_num = 1;
1423
1424     $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
1425
1426     if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
1427     {
1428         $beg = $options->{ "beg" } - 1;
1429         
1430         if ( $options->{ "end" } ) {
1431             $end = $options->{ "end" };
1432         } elsif ( $options->{ "len" } ) {
1433             $end = $beg + $options->{ "len" };
1434         }
1435
1436         $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
1437
1438         foreach $entry ( @{ $align } )
1439         {
1440             $entry->{ "CHR" }     = $record->{ "CHR" };
1441             $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
1442             $entry->{ "CHR_END" } = $record->{ "CHR_END" };
1443             $entry->{ "STRAND" }  = $record->{ "STRAND" } || '+';
1444             $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
1445             $entry->{ "SCORE" }   = $record->{ "SCORE" };
1446
1447             Maasha::Biopieces::put_record( $entry, $out );
1448         }
1449     }
1450
1451     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1452     {
1453         if ( $record->{ "REC_TYPE" } eq "BED" )
1454         {
1455             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
1456         }
1457         elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
1458         {
1459             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
1460         }
1461         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
1462         {
1463             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
1464         }
1465         elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
1466         {
1467             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
1468         }
1469
1470         foreach $entry ( @{ $align } )
1471         {
1472             $entry->{ "CHR" }     = $record->{ "CHR" };
1473             $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
1474             $entry->{ "CHR_END" } = $record->{ "CHR_END" };
1475             $entry->{ "STRAND" }  = $record->{ "STRAND" };
1476             $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
1477             $entry->{ "SCORE" }   = $record->{ "SCORE" };
1478
1479             Maasha::Biopieces::put_record( $entry, $out );
1480         }
1481
1482         $align_num++;
1483     }
1484 }
1485
1486
1487 sub script_get_genome_phastcons
1488 {
1489     # Martin A. Hansen, February 2008.
1490
1491     # Get phastcons scores from genome intervals.
1492
1493     my ( $in,        # handle to in stream
1494          $out,       # handle to out stream
1495          $options,   # options hash
1496        ) = @_;
1497
1498     # Returns nothing.
1499
1500     my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
1501
1502     $options->{ "flank" } ||= 0;
1503
1504     $phastcons_file  = Maasha::Config::genome_phastcons( $options->{ "genome" } );
1505     $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
1506
1507     $index           = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
1508     $fh_phastcons    = Maasha::Common::read_open( $phastcons_file );
1509
1510     if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
1511     {
1512         $options->{ "beg" } -= 1;   # request is 1-based
1513         $options->{ "end" } -= 1;   # request is 1-based
1514
1515         if ( $options->{ "len" } ) {
1516             $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
1517         }
1518
1519         $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
1520
1521         $record->{ "CHR" }       = $options->{ "chr" };
1522         $record->{ "CHR_BEG" }   = $options->{ "beg" } - $options->{ "flank" };
1523         $record->{ "CHR_END" }   = $options->{ "end" } + $options->{ "flank" };
1524         
1525         $record->{ "PHASTCONS" }   = join ",", @{ $scores };
1526         $record->{ "PHAST_COUNT" } = scalar @{ $scores };  # DEBUG
1527
1528         Maasha::Biopieces::put_record( $record, $out );
1529     }   
1530
1531     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1532     {
1533         if ( $record->{ "REC_TYPE" } eq "BED" )
1534         {
1535             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
1536         }
1537         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
1538         {
1539             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
1540         }
1541         elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
1542         {
1543             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
1544         }
1545
1546         $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
1547 #        $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores };  # DEBUG
1548
1549         Maasha::Biopieces::put_record( $record, $out );
1550     }
1551
1552     close $fh_phastcons if $fh_phastcons;                                                                                                                                          
1553 }
1554
1555
1556 sub script_soap_seq
1557 {
1558     # Martin A. Hansen, July 2008.
1559
1560     # soap sequences in stream against a given file or genome.
1561
1562     my ( $in,        # handle to in stream
1563          $out,       # handle to out stream
1564          $options,   # options hash
1565        ) = @_;
1566
1567     # Returns nothing.
1568
1569     my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
1570
1571     $options->{ "seed_size" }  ||= 10;
1572     $options->{ "mismatches" } ||= 2;
1573     $options->{ "gap_size" }   ||= 0;
1574     $options->{ "cpus" }       ||= 1;
1575
1576     if ( $options->{ "genome" } ) {
1577         $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
1578     }
1579
1580     $tmp_in  = "$BP_TMP/soap_query.seq";
1581     $tmp_out = "$BP_TMP/soap.result";
1582
1583     $fh_out = Maasha::Common::write_open( $tmp_in );
1584  
1585     $count = 0;
1586
1587     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1588     {
1589         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
1590         {
1591             Maasha::Fasta::put_entry( $entry, $fh_out );
1592
1593             $count++;
1594         }
1595
1596         Maasha::Biopieces::put_record( $record, $out );
1597     }
1598
1599     close $fh_out;
1600
1601     if ( $count > 0 )
1602     {
1603         $args = join( " ",
1604             "-s $options->{ 'seed_size' }",
1605             "-r 2",
1606             "-a $tmp_in",
1607             "-v $options->{ 'mismatches' }",
1608             "-g $options->{ 'gap_size' }",
1609             "-p $options->{ 'cpus' }",
1610             "-d $options->{ 'in_file' }",
1611             "-o $tmp_out",
1612         );
1613
1614         $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
1615
1616         Maasha::Common::run( "soap", $args, 1 );
1617
1618         unlink $tmp_in;
1619
1620         $fh_out = Maasha::Common::read_open( $tmp_out );
1621
1622         undef $record;
1623
1624         while ( $line = <$fh_out> )
1625         {
1626             chomp $line;
1627
1628             @fields = split /\t/, $line;
1629
1630             $record->{ "REC_TYPE" }   = "SOAP";
1631             $record->{ "Q_ID" }       = $fields[ 0 ];
1632             $record->{ "SCORE" }      = $fields[ 3 ];
1633             $record->{ "STRAND" }     = $fields[ 6 ];
1634             $record->{ "S_ID" }       = $fields[ 7 ];
1635             $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # soap is 1-based
1636             $record->{ "S_END" }      = $fields[ 8 ] + $fields[ 5 ] - 2;
1637
1638             Maasha::Biopieces::put_record( $record, $out );
1639         }
1640
1641         close $fh_out;
1642     }
1643
1644     unlink $tmp_out;
1645 }
1646
1647
1648 sub script_write_bed
1649 {
1650     # Martin A. Hansen, August 2007.
1651
1652     # Write BED format for the UCSC genome browser using records in stream.
1653
1654     my ( $in,        # handle to in stream
1655          $out,       # handle to out stream
1656          $options,   # options hash
1657        ) = @_;
1658
1659     # Returns nothing.
1660
1661     my ( $cols, $fh, $record, $bed_entry, $new_record );
1662
1663     $cols = $options->{ 'cols' }->[ 0 ];
1664
1665     $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
1666
1667     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1668     {
1669         $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
1670
1671         if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
1672             Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
1673         }
1674
1675         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
1676     }
1677
1678     close $fh;
1679 }
1680
1681
1682 sub script_write_psl
1683 {
1684     # Martin A. Hansen, August 2007.
1685
1686     # Write PSL output from stream.
1687
1688     my ( $in,        # handle to in stream
1689          $out,       # handle to out stream
1690          $options,   # options hash
1691        ) = @_;
1692
1693     # Returns nothing.
1694
1695     my ( $fh, $record, @output, $first );
1696
1697     $first = 1;
1698
1699     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
1700
1701     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1702     {
1703         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1704
1705         if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
1706         {
1707             Maasha::UCSC::psl_put_header( $fh ) if $first;
1708             Maasha::UCSC::psl_put_entry( $record, $fh );
1709             $first = 0;
1710         }
1711     }
1712
1713     close $fh;
1714 }
1715
1716
1717 sub script_write_fixedstep
1718 {
1719     # Martin A. Hansen, Juli 2008.
1720
1721     # Write fixedStep entries from recrods in the stream.
1722
1723     my ( $in,        # handle to in stream
1724          $out,       # handle to out stream
1725          $options,   # options hash
1726        ) = @_;
1727
1728     # Returns nothing.
1729
1730     my ( $fh, $record, $entry );
1731
1732     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
1733
1734     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1735     {
1736         if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
1737             Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
1738         }
1739
1740         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1741     }
1742
1743     close $fh;
1744 }
1745
1746
1747 sub script_write_2bit
1748 {
1749     # Martin A. Hansen, March 2008.
1750
1751     # Write sequence entries from stream in 2bit format.
1752
1753     my ( $in,        # handle to in stream
1754          $out,       # handle to out stream
1755          $options,   # options hash
1756        ) = @_;
1757
1758     # Returns nothing.
1759
1760     my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
1761
1762     $mask = 1 if not $options->{ "no_mask" };
1763
1764     $tmp_file = "$BP_TMP/write_2bit.fna";
1765     $fh_tmp   = Maasha::Common::write_open( $tmp_file );
1766
1767     $fh_out = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
1768
1769     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1770     {
1771         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
1772             Maasha::Fasta::put_entry( $entry, $fh_tmp );
1773         }
1774
1775         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1776     }
1777
1778     close $fh_tmp;
1779
1780     $fh_in = Maasha::Common::read_open( $tmp_file );
1781
1782     Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
1783
1784     close $fh_in;
1785     close $fh_out;
1786
1787     unlink $tmp_file;
1788 }
1789
1790
1791 sub script_write_solid
1792 {
1793     # Martin A. Hansen, April 2008.
1794
1795     # Write di-base encoded Solid sequence from entries in stream.
1796
1797     my ( $in,        # handle to in stream
1798          $out,       # handle to out stream
1799          $options,   # options hash
1800        ) = @_;
1801
1802     # Returns nothing.
1803
1804     my ( $record, $fh, $entry );
1805
1806     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
1807
1808     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1809     {
1810         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
1811         {
1812             $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
1813
1814             Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
1815         }
1816
1817         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1818     }
1819
1820     close $fh;
1821 }
1822
1823
1824 sub script_write_ucsc_config
1825 {
1826     # Martin A. Hansen, November 2008.
1827
1828     # Write UCSC Genome Broser configuration (.ra file type) from
1829     # records in the stream.
1830
1831     my ( $in,        # handle to in stream
1832          $out,       # handle to out stream
1833          $options,   # options hash
1834        ) = @_;
1835
1836     # Returns nothing.
1837
1838     my ( $record, $fh );
1839
1840     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
1841
1842     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1843     {
1844         Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
1845
1846         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1847     }
1848
1849     close $fh;
1850 }
1851
1852
1853 sub script_plot_seqlogo
1854 {
1855     # Martin A. Hansen, August 2007.
1856
1857     # Calculates and writes a sequence logo for alignments.
1858
1859     my ( $in,        # handle to in stream
1860          $out,       # handle to out stream
1861          $options,   # options hash
1862        ) = @_;
1863
1864     # Returns nothing.
1865
1866     my ( $record, @entries, $logo, $fh );
1867
1868     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1869     {
1870         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
1871             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
1872         }
1873
1874         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1875     }
1876
1877     $logo = Maasha::Plot::seq_logo( \@entries );
1878
1879     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
1880
1881     print $fh $logo;
1882
1883     close $fh;
1884 }
1885
1886
1887 sub script_plot_phastcons_profiles
1888 {
1889     # Martin A. Hansen, January 2008.
1890
1891     # Plots PhastCons profiles.
1892
1893     my ( $in,        # handle to in stream
1894          $out,       # handle to out stream
1895          $options,   # options hash
1896        ) = @_;
1897
1898     # Returns nothing.
1899
1900     my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
1901
1902     $options->{ "title" } ||= "PhastCons Profiles";
1903
1904     $phastcons_file  = Maasha::Config::genome_phastcons( $options->{ "genome" } );
1905     $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
1906
1907     $index           = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
1908     $fh_phastcons    = Maasha::Common::read_open( $phastcons_file );
1909
1910     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1911     {
1912         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
1913         {
1914             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
1915                                                                                    $record->{ "CHR_BEG" },
1916                                                                                    $record->{ "CHR_END" },
1917                                                                                    $options->{ "flank" } );
1918
1919             push @{ $AoA }, [ @{ $scores } ];
1920         }
1921
1922         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1923     }
1924
1925     Maasha::UCSC::phastcons_normalize( $AoA );
1926
1927     $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
1928     $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
1929
1930     $AoA = Maasha::Matrix::matrix_flip( $AoA );
1931
1932     $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
1933
1934     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
1935
1936     print $fh "$_\n" foreach @{ $plot };
1937
1938     close $fh;
1939 }
1940
1941
1942 sub script_remove_mysql_tables
1943 {
1944     # Martin A. Hansen, November 2008.
1945
1946     # Remove MySQL tables from values in stream.
1947
1948     my ( $in,        # handle to in stream
1949          $out,       # handle to out stream
1950          $options,   # options hash
1951        ) = @_;
1952
1953     # Returns nothing.
1954
1955     my ( $record, %table_hash, $dbh, $table );
1956
1957     $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
1958     $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1959
1960     map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
1961
1962     while ( $record = Maasha::Biopieces::get_record( $in ) )
1963     {
1964         map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
1965
1966         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
1967     }
1968
1969     $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1970
1971     foreach $table ( sort keys %table_hash )
1972     {
1973         if ( Maasha::SQL::table_exists( $dbh, $table ) )
1974         {
1975             print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
1976             Maasha::SQL::delete_table( $dbh, $table );
1977             print STDERR "done.\n" if $options->{ 'verbose' };
1978         }
1979         else
1980         {
1981             print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
1982         }
1983     }
1984
1985     Maasha::SQL::disconnect( $dbh );
1986 }
1987
1988
1989 sub script_remove_ucsc_tracks
1990 {
1991     # Martin A. Hansen, November 2008.
1992
1993     # Remove track from MySQL tables and config file.
1994
1995     my ( $in,        # handle to in stream
1996          $out,       # handle to out stream
1997          $options,   # options hash
1998        ) = @_;
1999
2000     # Returns nothing.
2001
2002     my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
2003
2004     $options->{ 'user' }        ||= Maasha::UCSC::ucsc_get_user();
2005     $options->{ 'password' }    ||= Maasha::UCSC::ucsc_get_password();
2006     $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
2007
2008     map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
2009
2010     while ( $record = Maasha::Biopieces::get_record( $in ) )
2011     {
2012         map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
2013
2014         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
2015     }
2016
2017     $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
2018     
2019     while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
2020         push @tracks, $track;
2021     }
2022
2023     close $fh_in;
2024
2025     foreach $track ( @tracks )
2026     {
2027         if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
2028             print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
2029         } else {
2030             push @new_tracks, $track;
2031         }
2032     }
2033
2034     rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
2035
2036     $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
2037
2038     map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
2039
2040     close $fh_out;
2041
2042     # ---- locate track in database ----
2043
2044     $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2045
2046     foreach $track ( sort keys %track_hash )
2047     {
2048         if ( Maasha::SQL::table_exists( $dbh, $track ) )
2049         {
2050             print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
2051             Maasha::SQL::delete_table( $dbh, $track );
2052             print STDERR "done.\n" if $options->{ 'verbose' };
2053         }
2054         else
2055         {
2056             print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
2057         }
2058     }
2059
2060     Maasha::SQL::disconnect( $dbh );
2061
2062     Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
2063 }
2064
2065
2066 sub script_plot_matches
2067 {
2068     # Martin A. Hansen, August 2007.
2069
2070     # Plot matches in 2D generating a dotplot.
2071
2072     my ( $in,        # handle to in stream
2073          $out,       # handle to out stream
2074          $options,   # options hash
2075        ) = @_;
2076
2077     # Returns nothing.
2078
2079     my ( $record, @data, $fh, $result, %data_hash );
2080
2081     $options->{ "direction" } ||= "both";
2082
2083     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2084     {
2085         if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
2086             push @data, $record;
2087         }
2088
2089         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2090     }
2091
2092     $options->{ "title" }  ||= "plot_matches";
2093     $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
2094     $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
2095
2096     $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
2097
2098     $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } );
2099
2100     print $fh "$_\n" foreach @{ $result };
2101
2102     close $fh;
2103 }
2104
2105
2106 sub script_upload_to_ucsc
2107 {
2108     # Martin A. Hansen, August 2007.
2109
2110     # Calculate the mean of values of given keys.
2111
2112     # This routine has developed into the most ugly hack. Do something!
2113
2114     my ( $in,        # handle to in stream
2115          $out,       # handle to out stream
2116          $options,   # options hash
2117        ) = @_;
2118
2119     # Returns nothing.
2120
2121     my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
2122
2123     $options->{ "short_label" } ||= $options->{ 'table' };
2124     $options->{ "long_label" }  ||= $options->{ 'table' };
2125     $options->{ "group" }       ||= $ENV{ "LOGNAME" };
2126     $options->{ "priority" }    ||= 1;
2127     $options->{ "visibility" }  ||= "pack";
2128     $options->{ "color" }       ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
2129     $options->{ "chunk_size" }  ||= 10_000_000_000;    # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
2130
2131     $file   = "$BP_TMP/ucsc_upload.tmp";
2132     $append = 0;
2133     $first  = 1;
2134     $i      = 0;
2135
2136     $fh_out = Maasha::Common::write_open( $file );
2137
2138     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2139     {
2140         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2141
2142         if ( $record->{ "REC_TYPE" } eq "fixed_step" )
2143         {
2144             $format = "WIGGLE";
2145
2146             if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
2147                 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
2148             }
2149         }
2150         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2151         {
2152             $format = "PSL";
2153
2154             Maasha::UCSC::psl_put_header( $fh_out ) if $first;
2155             Maasha::UCSC::psl_put_entry( $record, $fh_out );
2156             
2157             $first = 0;
2158         }
2159         elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
2160         {
2161             # chrom chromStart  chromEnd    name    score   strand  size    secStr  conf 
2162
2163             $format  = "BED_SS";
2164
2165             print $fh_out join ( "\t",
2166                 $record->{ "CHR" },
2167                 $record->{ "CHR_BEG" },
2168                 $record->{ "CHR_END" } + 1,
2169                 $record->{ "Q_ID" },
2170                 $record->{ "SCORE" },
2171                 $record->{ "STRAND" },
2172                 $record->{ "SIZE" },
2173                 $record->{ "SEC_STRUCT" },
2174                 $record->{ "CONF" },
2175             ), "\n";
2176         }
2177         elsif ( $record->{ "REC_TYPE" } eq "BED" )
2178         {
2179             $format  = "BED";
2180             $columns = $record->{ "BED_COLS" };
2181
2182             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
2183                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
2184             }
2185         }
2186         elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
2187         {
2188             $format  = "BED";
2189             $columns = 6;
2190
2191             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
2192                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
2193             }
2194         }
2195         elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
2196         {
2197             $format  = "BED";
2198             $columns = 6;
2199
2200             $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
2201
2202             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
2203                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
2204             }
2205         }
2206         elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
2207         {
2208             $format  = "BED";
2209             $columns = 6;
2210
2211             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
2212                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
2213             }
2214         }
2215
2216         if ( $i == $options->{ "chunk_size" } )
2217         {
2218             close $fh_out;
2219
2220             if ( $format eq "BED" ) {
2221                 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
2222             } elsif ( $format eq "PSL" ) {
2223                 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
2224             }
2225
2226             unlink $file;
2227
2228             $first = 1;
2229
2230             $append = 1;
2231
2232             $fh_out = Maasha::Common::write_open( $file );
2233         }
2234
2235         $i++;
2236     }
2237
2238     close $fh_out;
2239
2240     if ( exists $options->{ "database" } and $options->{ "table" } )
2241     {
2242         if ( $format eq "BED" )
2243         {
2244             $type = "bed $columns";
2245
2246             Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
2247         }
2248         elsif ( $format eq "BED_SS" )
2249         {
2250             $type = "type bed 6 +";
2251         
2252             Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
2253         }
2254         elsif ( $format eq "PSL" )
2255         {
2256             $type = "psl";
2257
2258             Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
2259         }
2260         elsif ( $format eq "WIGGLE" )
2261         {
2262             $options->{ "visibility" } = "full";
2263
2264             $wig_file = "$options->{ 'table' }.wig";
2265             $wib_file = "$options->{ 'table' }.wib";
2266
2267             $wib_dir  = "$ENV{ 'HOME' }/ucsc/wib";
2268
2269             Maasha::Common::dir_create_if_not_exists( $wib_dir );
2270
2271             if ( $options->{ 'verbose' } ) {
2272                 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
2273             } else {
2274                 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
2275             }
2276
2277             Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
2278
2279             unlink $file;
2280
2281             $file = $wig_file;
2282
2283             $type = "wig 0";
2284
2285             Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
2286         }
2287
2288         unlink $file;
2289
2290         Maasha::UCSC::ucsc_update_config( $options, $type );
2291     }
2292 }
2293
2294
2295 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2296
2297 1;
2298
2299 __END__
2300