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