]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/BioRun.pm
ebbd3c53578deafe5451648d72596112021a580f
[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 "assemble_tag_contigs" )     { script_assemble_tag_contigs(      $in, $out, $options ) }
134     elsif ( $script eq "length_seq" )               { script_length_seq(                $in, $out, $options ) }
135     elsif ( $script eq "uppercase_seq" )            { script_uppercase_seq(             $in, $out, $options ) }
136     elsif ( $script eq "analyze_seq" )              { script_analyze_seq(               $in, $out, $options ) }
137     elsif ( $script eq "analyze_tags" )             { script_analyze_tags(              $in, $out, $options ) }
138     elsif ( $script eq "complexity_seq" )           { script_complexity_seq(            $in, $out, $options ) }
139     elsif ( $script eq "oligo_freq" )               { script_oligo_freq(                $in, $out, $options ) }
140     elsif ( $script eq "create_weight_matrix" )     { script_create_weight_matrix(      $in, $out, $options ) }
141     elsif ( $script eq "calc_bit_scores" )          { script_calc_bit_scores(           $in, $out, $options ) }
142     elsif ( $script eq "calc_fixedstep" )           { script_calc_fixedstep(            $in, $out, $options ) }
143     elsif ( $script eq "remove_indels" )            { script_remove_indels(             $in, $out, $options ) }
144     elsif ( $script eq "transliterate_seq" )        { script_transliterate_seq(         $in, $out, $options ) }
145     elsif ( $script eq "transliterate_vals" )       { script_transliterate_vals(        $in, $out, $options ) }
146     elsif ( $script eq "translate_seq" )            { script_translate_seq(             $in, $out, $options ) }
147     elsif ( $script eq "get_genome_align" )         { script_get_genome_align(          $in, $out, $options ) }
148     elsif ( $script eq "get_genome_phastcons" )     { script_get_genome_phastcons(      $in, $out, $options ) }
149     elsif ( $script eq "fold_seq" )                 { script_fold_seq(                  $in, $out, $options ) }
150     elsif ( $script eq "split_seq" )                { script_split_seq(                 $in, $out, $options ) }
151     elsif ( $script eq "split_bed" )                { script_split_bed(                 $in, $out, $options ) }
152     elsif ( $script eq "align_seq" )                { script_align_seq(                 $in, $out, $options ) }
153     elsif ( $script eq "tile_seq" )                 { script_tile_seq(                  $in, $out, $options ) }
154     elsif ( $script eq "invert_align" )             { script_invert_align(              $in, $out, $options ) }
155     elsif ( $script eq "patscan_seq" )              { script_patscan_seq(               $in, $out, $options ) }
156     elsif ( $script eq "blat_seq" )                 { script_blat_seq(                  $in, $out, $options ) }
157     elsif ( $script eq "soap_seq" )                 { script_soap_seq(                  $in, $out, $options ) }
158     elsif ( $script eq "match_seq" )                { script_match_seq(                 $in, $out, $options ) }
159     elsif ( $script eq "create_vmatch_index" )      { script_create_vmatch_index(       $in, $out, $options ) }
160     elsif ( $script eq "write_align" )              { script_write_align(               $in, $out, $options ) }
161     elsif ( $script eq "write_bed" )                { script_write_bed(                 $in, $out, $options ) }
162     elsif ( $script eq "write_psl" )                { script_write_psl(                 $in, $out, $options ) }
163     elsif ( $script eq "write_fixedstep" )          { script_write_fixedstep(           $in, $out, $options ) }
164     elsif ( $script eq "write_2bit" )               { script_write_2bit(                $in, $out, $options ) }
165     elsif ( $script eq "write_solid" )              { script_write_solid(               $in, $out, $options ) }
166     elsif ( $script eq "write_ucsc_config" )        { script_write_ucsc_config(         $in, $out, $options ) }
167     elsif ( $script eq "head_records" )             { script_head_records(              $in, $out, $options ) }
168     elsif ( $script eq "remove_keys" )              { script_remove_keys(               $in, $out, $options ) }
169     elsif ( $script eq "remove_adaptor" )           { script_remove_adaptor(            $in, $out, $options ) }
170     elsif ( $script eq "remove_mysql_tables" )      { script_remove_mysql_tables(       $in, $out, $options ) }
171     elsif ( $script eq "remove_ucsc_tracks" )       { script_remove_ucsc_tracks(        $in, $out, $options ) }
172     elsif ( $script eq "rename_keys" )              { script_rename_keys(               $in, $out, $options ) }
173     elsif ( $script eq "uniq_vals" )                { script_uniq_vals(                 $in, $out, $options ) }
174     elsif ( $script eq "merge_vals" )               { script_merge_vals(                $in, $out, $options ) }
175     elsif ( $script eq "merge_records" )            { script_merge_records(             $in, $out, $options ) }
176     elsif ( $script eq "compute" )                  { script_compute(                   $in, $out, $options ) }
177     elsif ( $script eq "flip_tab" )                 { script_flip_tab(                  $in, $out, $options ) }
178     elsif ( $script eq "count_records" )            { script_count_records(             $in, $out, $options ) }
179     elsif ( $script eq "random_records" )           { script_random_records(            $in, $out, $options ) }
180     elsif ( $script eq "sort_records" )             { script_sort_records(              $in, $out, $options ) }
181     elsif ( $script eq "count_vals" )               { script_count_vals(                $in, $out, $options ) }
182     elsif ( $script eq "plot_histogram" )           { script_plot_histogram(            $in, $out, $options ) }
183     elsif ( $script eq "plot_lendist" )             { script_plot_lendist(              $in, $out, $options ) }
184     elsif ( $script eq "plot_chrdist" )             { script_plot_chrdist(              $in, $out, $options ) }
185     elsif ( $script eq "plot_karyogram" )           { script_plot_karyogram(            $in, $out, $options ) }
186     elsif ( $script eq "plot_matches" )             { script_plot_matches(              $in, $out, $options ) }
187     elsif ( $script eq "plot_seqlogo" )             { script_plot_seqlogo(              $in, $out, $options ) }
188     elsif ( $script eq "plot_phastcons_profiles" )  { script_plot_phastcons_profiles(   $in, $out, $options ) }
189     elsif ( $script eq "analyze_bed" )              { script_analyze_bed(               $in, $out, $options ) }
190     elsif ( $script eq "analyze_vals" )             { script_analyze_vals(              $in, $out, $options ) }
191     elsif ( $script eq "length_vals" )              { script_length_vals(               $in, $out, $options ) }
192     elsif ( $script eq "sum_vals" )                 { script_sum_vals(                  $in, $out, $options ) }
193     elsif ( $script eq "mean_vals" )                { script_mean_vals(                 $in, $out, $options ) }
194     elsif ( $script eq "median_vals" )              { script_median_vals(               $in, $out, $options ) }
195     elsif ( $script eq "max_vals" )                 { script_max_vals(                  $in, $out, $options ) }
196     elsif ( $script eq "min_vals" )                 { script_min_vals(                  $in, $out, $options ) }
197     elsif ( $script eq "upload_to_ucsc" )           { script_upload_to_ucsc(            $in, $out, $options ) }
198
199     close $in if defined $in;
200     close $out;
201
202     $t1 = gettimeofday();
203
204     print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
205 }
206
207
208 sub get_options
209 {
210     # Martin A. Hansen, February 2008.
211
212     # Gets options from commandline and checks these vigerously.
213
214     my ( $script,     # name of script
215        ) = @_;
216
217     # Returns hash
218
219     my ( %options, @options, $opt, @genomes, $real );
220
221     if ( $script eq "print_usage" )
222     {
223         @options = qw(
224             data_in|i=s
225         );
226     }
227     elsif ( $script eq "read_psl" )
228     {
229         @options = qw(
230             data_in|i=s
231             num|n=s
232         );
233     }
234     elsif ( $script eq "read_bed" )
235     {
236         @options = qw(
237             data_in|i=s
238             cols|c=s
239             num|n=s
240             check|C
241         );
242     }
243     elsif ( $script eq "read_fixedstep" )
244     {
245         @options = qw(
246             data_in|i=s
247             num|n=s
248         );
249     }
250     elsif ( $script eq "read_blast_tab" )
251     {
252         @options = qw(
253             data_in|i=s
254             num|n=s
255         );
256     }
257     elsif ( $script eq "read_embl" )
258     {
259         @options = qw(
260             data_in|i=s
261             num|n=s
262             keys|k=s
263             feats|f=s
264             quals|q=s
265         );
266     }
267     elsif ( $script eq "read_stockholm" )
268     {
269         @options = qw(
270             data_in|i=s
271             num|n=s
272         );
273     }
274     elsif ( $script eq "read_phastcons" )
275     {
276         @options = qw(
277             data_in|i=s
278             num|n=s
279             min|m=s
280             dist|d=s
281             threshold|t=f
282             gap|g=s
283         );
284     }
285     elsif ( $script eq "read_soft" )
286     {
287         @options = qw(
288             data_in|i=s
289             samples|s=s
290             num|n=s
291         );
292     }
293     elsif ( $script eq "read_gff" )
294     {
295         @options = qw(
296             data_in|i=s
297             num|n=s
298         );
299     }
300     elsif ( $script eq "read_2bit" )
301     {
302         @options = qw(
303             data_in|i=s
304             num|n=s
305             no_mask|N
306         );
307     }
308     elsif ( $script eq "read_solexa" )
309     {
310         @options = qw(
311             data_in|i=s
312             num|n=s
313             format|f=s
314             quality|q=s
315         );
316     }
317     elsif ( $script eq "read_solid" )
318     {
319         @options = qw(
320             data_in|i=s
321             num|n=s
322             quality|q=s
323         );
324     }
325     elsif ( $script eq "read_mysql" )
326     {
327         @options = qw(
328             database|d=s
329             query|q=s
330             user|u=s
331             password|p=s
332         );
333     }
334     elsif ( $script eq "read_ucsc_config" )
335     {
336         @options = qw(
337             data_in|i=s
338             num|n=s
339         );
340     }
341     elsif ( $script eq "assemble_tag_contigs" )
342     {
343         @options = qw(
344             check|C
345         );
346     }
347     elsif ( $script eq "calc_fixedstep" )
348     {
349         @options = qw(
350             check|C
351         );
352     }
353     elsif ( $script eq "length_seq" )
354     {
355         @options = qw(
356             no_stream|x
357             data_out|o=s
358         );
359     }
360     elsif ( $script eq "oligo_freq" )
361     {
362         @options = qw(
363             word_size|w=s
364             all|a
365         );
366     }
367     elsif ( $script eq "create_weight_matrix" )
368     {
369         @options = qw(
370             percent|p
371         );
372     }
373     elsif ( $script eq "transliterate_seq" )
374     {
375         @options = qw(
376             search|s=s
377             replace|r=s
378             delete|d=s
379         );
380     }
381     elsif ( $script eq "transliterate_vals" )
382     {
383         @options = qw(
384             keys|k=s
385             search|s=s
386             replace|r=s
387             delete|d=s
388         );
389     }
390     elsif ( $script eq "translate_seq" )
391     {
392         @options = qw(
393             frames|f=s
394         );
395     }
396     elsif ( $script eq "get_genome_align" )
397     {
398         @options = qw(
399             genome|g=s
400             chr|c=s
401             beg|b=s
402             end|e=s
403             len|l=s
404             strand|s=s
405         );
406     }
407     elsif ( $script eq "get_genome_phastcons" )
408     {
409         @options = qw(
410             genome|g=s
411             chr|c=s
412             beg|b=s
413             end|e=s
414             len|l=s
415             flank|f=s
416         );
417     }
418     elsif ( $script eq "split_seq" )
419     {
420         @options = qw(
421             word_size|w=s
422             uniq|u
423         );
424     }
425     elsif ( $script eq "split_bed" )
426     {
427         @options = qw(
428             window_size|w=s
429             step_size|s=s
430         );
431     }
432     elsif ( $script eq "tile_seq" )
433     {
434         @options = qw(
435             identity|i=s
436             supress_indels|s
437         );
438     }
439     elsif ( $script eq "invert_align" )
440     {
441         @options = qw(
442             soft|s
443         );
444     }
445     elsif ( $script eq "patscan_seq" )
446     {
447         @options = qw(
448             patterns|p=s
449             patterns_in|P=s
450             comp|c
451             max_hits|h=s
452             max_misses|m=s
453             genome|g=s
454         );
455     }
456     elsif ( $script eq "blat_seq" )
457     {
458         @options = qw(
459             genome|g=s
460             tile_size|t=s
461             step_size|s=s
462             min_identity|m=s
463             min_score|M=s
464             one_off|o=s
465             ooc|c
466         );
467     }
468     elsif ( $script eq "soap_seq" )
469     {
470         @options = qw(
471             in_file|i=s
472             genome|g=s
473             seed_size|s=s
474             mismatches|m=s
475             gap_size|G=s
476             cpus|c=s
477         );
478     }
479     elsif ( $script eq "match_seq" )
480     {
481         @options = qw(
482             word_size|w=s
483             direction|d=s
484         );
485     }
486     elsif ( $script eq "create_vmatch_index" )
487     {
488         @options = qw(
489             index_name|i=s
490             prefix_length|p=s
491             no_stream|x
492         );
493     }
494     elsif ( $script eq "write_align" )
495     {
496         @options = qw(
497             wrap|w=s
498             no_stream|x
499             no_ruler|R
500             no_consensus|C
501             data_out|o=s
502         );
503     }
504     elsif ( $script eq "write_bed" )
505     {
506         @options = qw(
507             cols|c=s
508             check|C
509             no_stream|x
510             data_out|o=s
511             compress|Z
512         );
513     }
514     elsif ( $script eq "write_psl" )
515     {
516         @options = qw(
517             no_stream|x
518             data_out|o=s
519             compress|Z
520         );
521     }
522     elsif ( $script eq "write_fixedstep" )
523     {
524         @options = qw(
525             no_stream|x
526             data_out|o=s
527             compress|Z
528         );
529     }
530     elsif ( $script eq "write_2bit" )
531     {
532         @options = qw(
533             no_stream|x
534             data_out|o=s
535             no_mask|N
536         );
537     }
538     elsif ( $script eq "write_solid" )
539     {
540         @options = qw(
541             wrap|w=s
542             no_stream|x
543             data_out|o=s
544             compress|Z
545         );
546     }
547     elsif ( $script eq "write_ucsc_config" )
548     {
549         @options = qw(
550             no_stream|x
551             data_out|o=s
552         );
553     }
554     elsif ( $script eq "plot_seqlogo" )
555     {
556         @options = qw(
557             no_stream|x
558             data_out|o=s
559         );
560     }
561     elsif ( $script eq "plot_phastcons_profiles" )
562     {
563         @options = qw(
564             no_stream|x
565             data_out|o=s
566             genome|g=s
567             mean|m
568             median|M
569             flank|f=s
570             terminal|t=s
571             title|T=s
572             xlabel|X=s
573             ylabel|Y=s
574         );
575     }
576     elsif ( $script eq "analyze_vals" )
577     {
578         @options = qw(
579             no_stream|x
580             keys|k=s
581         );
582     }
583     elsif ( $script eq "head_records" )
584     {
585         @options = qw(
586             num|n=s
587         );
588     }
589     elsif ( $script eq "remove_keys" )
590     {
591         @options = qw(
592             keys|k=s
593             save_keys|K=s
594         );
595     }
596     elsif ( $script eq "remove_adaptor" )
597     {
598         @options = qw(
599             adaptor|a=s
600             mismatches|m=s
601             remove|r=s
602             offset|o=s
603         );
604     }
605     elsif ( $script eq "remove_mysql_tables" )
606     {
607         @options = qw(
608             database|d=s
609             tables|t=s
610             keys|k=s
611             user|u=s
612             password|p=s
613             no_stream|x
614         );
615     }
616     elsif ( $script eq "remove_ucsc_tracks" )
617     {
618         @options = qw(
619             database|d=s
620             tracks|t=s
621             keys|k=s
622             config_file|c=s
623             user|u=s
624             password|p=s
625             no_stream|x
626         );
627     }
628     elsif ( $script eq "rename_keys" )
629     {
630         @options = qw(
631             keys|k=s
632         );
633     }
634     elsif ( $script eq "uniq_vals" )
635     {
636         @options = qw(
637             key|k=s
638             invert|i
639         );
640     }
641     elsif ( $script eq "merge_vals" )
642     {
643         @options = qw(
644             keys|k=s
645             delimit|d=s
646         );
647     }
648     elsif ( $script eq "merge_records" )
649     {
650         @options = qw(
651             keys|k=s
652             merge|m=s
653         );
654     }
655     elsif ( $script eq "compute" )
656     {
657         @options = qw(
658             eval|e=s
659         );
660     }
661     elsif ( $script eq "count_records" )
662     {
663         @options = qw(
664             no_stream|x
665             data_out|o=s
666         );
667     }
668     elsif ( $script eq "random_records" )
669     {
670         @options = qw(
671             num|n=s
672         );
673     }
674     elsif ( $script eq "sort_records" )
675     {
676         @options = qw(
677             reverse|r
678             keys|k=s
679         );
680     }
681     elsif ( $script eq "count_vals" )
682     {
683         @options = qw(
684             keys|k=s
685         );
686     }
687     elsif ( $script eq "plot_histogram" )
688     {
689         @options = qw(
690             no_stream|x
691             data_out|o=s
692             terminal|t=s
693             title|T=s
694             xlabel|X=s
695             ylabel|Y=s
696             key|k=s
697             sort|s=s
698         );
699     }
700     elsif ( $script eq "plot_lendist" )
701     {
702         @options = qw(
703             no_stream|x
704             data_out|o=s
705             terminal|t=s
706             title|T=s
707             xlabel|X=s
708             ylabel|Y=s
709             key|k=s
710         );
711     }
712     elsif ( $script eq "plot_chrdist" )
713     {
714         @options = qw(
715             no_stream|x
716             data_out|o=s
717             terminal|t=s
718             title|T=s
719             xlabel|X=s
720             ylabel|Y=s
721         );
722     }
723     elsif ( $script eq "plot_karyogram" )
724     {
725         @options = qw(
726             no_stream|x
727             data_out|o=s
728             genome|g=s
729             feat_color|f=s
730         );
731     }
732     elsif ( $script eq "plot_matches" )
733     {
734         @options = qw(
735             no_stream|x
736             data_out|o=s
737             terminal|t=s
738             title|T=s
739             xlabel|X=s
740             ylabel|Y=s
741             direction|d=s
742         );
743     }
744     elsif ( $script eq "length_vals" )
745     {
746         @options = qw(
747             keys|k=s
748         );
749     }
750     elsif ( $script eq "sum_vals" )
751     {
752         @options = qw(
753             no_stream|x
754             data_out|o=s
755             keys|k=s
756         );
757     }
758     elsif ( $script eq "mean_vals" )
759     {
760         @options = qw(
761             no_stream|x
762             data_out|o=s
763             keys|k=s
764         );
765     }
766     elsif ( $script eq "median_vals" )
767     {
768         @options = qw(
769             no_stream|x
770             data_out|o=s
771             keys|k=s
772         );
773     }
774     elsif ( $script eq "max_vals" )
775     {
776         @options = qw(
777             no_stream|x
778             data_out|o=s
779             keys|k=s
780         );
781     }
782     elsif ( $script eq "min_vals" )
783     {
784         @options = qw(
785             no_stream|x
786             data_out|o=s
787             keys|k=s
788         );
789     }
790     elsif ( $script eq "upload_to_ucsc" )
791     {
792         @options = qw(
793             no_stream|x
794             database|d=s
795             table|t=s
796             short_label|s=s
797             long_label|l=s
798             group|g=s
799             priority|p=f
800             use_score|u
801             visibility|V=s
802             color|c=s
803             check|C
804         );
805     }
806
807     push @options, qw(
808         stream_in|I=s
809         stream_out|O=s
810         verbose|v
811         help|?
812     );
813
814 #    print STDERR Dumper( \@options );
815     
816     GetOptions(
817         \%options,
818         @options,
819     );
820
821 #    print STDERR Dumper( \%options );
822
823     if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
824         return wantarray ? %options : \%options;
825     }
826
827     $options{ "cols" }      = [ split ",", $options{ "cols" } ]      if defined $options{ "cols" };
828     $options{ "keys" }      = [ split ",", $options{ "keys" } ]      if defined $options{ "keys" };
829     $options{ "no_keys" }   = [ split ",", $options{ "no_keys" } ]   if defined $options{ "no_keys" };
830     $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
831     $options{ "quals" }     = [ split ",", $options{ "quals" } ]     if defined $options{ "quals" };
832     $options{ "feats" }     = [ split ",", $options{ "feats" } ]     if defined $options{ "feats" };
833     $options{ "frames" }    = [ split ",", $options{ "frames" } ]    if defined $options{ "frames" };
834     $options{ "samples" }   = [ split ",", $options{ "samples" } ]   if defined $options{ "samples" };
835     $options{ "tables" }    = [ split ",", $options{ "tables" } ]    if defined $options{ "tables" };
836     $options{ "tracks" }    = [ split ",", $options{ "tracks" } ]    if defined $options{ "tracks" };
837     
838     # ---- check arguments ----
839
840     if ( $options{ 'data_in' } )
841     {
842         $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
843
844         Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
845     }
846
847     map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
848
849     # print STDERR Dumper( \%options );
850
851     $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
852
853     foreach $opt ( keys %options )
854     {
855         if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
856         {
857             Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
858         }
859         elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
860         {
861             Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
862         }
863         elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
864         {
865             Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
866         }
867         elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
868         {
869             Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
870         }
871         elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
872         {
873             Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
874         }
875         elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
876         {
877             Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
878         }
879         elsif ( $opt eq "genome" )
880         {
881             @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
882             map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
883
884             if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
885                 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
886             }
887         }
888         elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
889         {
890             Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
891         }
892         elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
893         {
894             Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
895         }
896         elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
897         {
898             Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
899         }
900         elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
901         {
902             Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
903         }
904         elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
905         {
906             Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
907         }
908     }
909
910     Maasha::Common::error( qq(no --database specified) )                if $script eq "remove_ucsc_tracks"  and not $options{ "database" };
911     Maasha::Common::error( qq(no --index_name specified) )              if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
912     Maasha::Common::error( qq(no --in_file or --genome specified) )     if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
913     Maasha::Common::error( qq(both --in_file and --genome specified) )  if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
914     Maasha::Common::error( qq(no --genome specified) )                  if $script =~ /get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
915     Maasha::Common::error( qq(no --key specified) )                     if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
916     Maasha::Common::error( qq(no --keys speficied) )                    if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
917
918     if ( $script eq "upload_to_ucsc" )
919     {
920         Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
921         Maasha::Common::error( qq(no --table specified) )    if not $options{ "table" };
922     }
923
924     return wantarray ? %options : \%options;
925 }
926
927
928 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
929
930
931 sub script_print_usage
932 {
933     # Martin A. Hansen, January 2008.
934
935     # Retrieves usage information from file and
936     # prints this nicely formatted.
937
938     my ( $in,        # handle to in stream
939          $out,       # handle to out stream
940          $options,   # options hash
941        ) = @_;
942
943     # Returns nothing.
944
945     my ( $file, $wiki, $lines );
946
947     if ( $options->{ 'data_in' } ) {
948         $file = $options->{ 'data_in' };
949     } else {
950         $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
951     }
952
953     $wiki = Maasha::Gwiki::gwiki_read( $file );
954
955     ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
956
957     if ( not $options->{ "help" } ) {
958         @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
959     }
960
961     $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
962
963     print STDERR "$_\n" foreach @{ $lines };
964
965     exit;
966 }
967
968
969 sub script_read_psl
970 {
971     # Martin A. Hansen, August 2007.
972
973     # Read psl table from stream or file.
974
975     my ( $in,        # handle to in stream
976          $out,       # handle to out stream
977          $options,   # options hash
978        ) = @_;
979
980     # Returns nothing.
981
982     my ( $record, $file, $data_in, $num );
983
984     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
985         Maasha::Biopieces::put_record( $record, $out );
986     }
987
988     $num = 1;
989
990     foreach $file ( @{ $options->{ "files" } } )
991     {
992         $data_in = Maasha::Common::read_open( $file );
993
994         while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) ) 
995         {
996             Maasha::Biopieces::put_record( $record, $out );
997
998             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
999
1000             $num++;
1001         }
1002     }
1003
1004     NUM:
1005 }
1006
1007
1008 sub script_read_bed
1009 {
1010     # Martin A. Hansen, August 2007.
1011
1012     # Read bed table from stream or file.
1013
1014     my ( $in,        # handle to in stream
1015          $out,       # handle to out stream
1016          $options,   # options hash
1017        ) = @_;
1018
1019     # Returns nothing.
1020
1021     my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1022
1023     $cols = $options->{ 'cols' }->[ 0 ];
1024
1025     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1026         Maasha::Biopieces::put_record( $record, $out );
1027     }
1028
1029     $num = 1;
1030
1031     foreach $file ( @{ $options->{ "files" } } )
1032     {
1033         $data_in = Maasha::Common::read_open( $file );
1034
1035         while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1036         {
1037             $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1038
1039             Maasha::Biopieces::put_record( $record, $out );
1040
1041             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1042
1043             $num++;
1044         }
1045
1046         close $data_in;
1047     }
1048
1049     NUM:
1050
1051     close $data_in if $data_in;
1052 }
1053
1054
1055 sub script_read_fixedstep
1056 {
1057     # Martin A. Hansen, Juli 2008.
1058
1059     # Read fixedstep wiggle format from stream or file.
1060
1061     my ( $in,        # handle to in stream
1062          $out,       # handle to out stream
1063          $options,   # options hash
1064        ) = @_;
1065
1066     # Returns nothing.
1067
1068     my ( $file, $record, $entry, $data_in, $num );
1069
1070     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1071         Maasha::Biopieces::put_record( $record, $out );
1072     }
1073
1074     $num = 1;
1075
1076     foreach $file ( @{ $options->{ "files" } } )
1077     {
1078         $data_in = Maasha::Common::read_open( $file );
1079
1080         while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1081         {
1082             $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1083
1084             Maasha::Biopieces::put_record( $record, $out );
1085
1086             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1087
1088             $num++;
1089         }
1090
1091         close $data_in;
1092     }
1093
1094     NUM:
1095
1096     close $data_in if $data_in;
1097 }
1098
1099
1100 sub script_read_blast_tab
1101 {
1102     # Martin A. Hansen, September 2007.
1103
1104     # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1105
1106     my ( $in,        # handle to in stream
1107          $out,       # handle to out stream
1108          $options,   # options hash
1109        ) = @_;
1110
1111     # Returns nothing.
1112
1113     my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1114
1115     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1116         Maasha::Biopieces::put_record( $record, $out );
1117     }
1118
1119     $num = 1;
1120
1121     foreach $file ( @{ $options->{ "files" } } )
1122     {
1123         $data_in = Maasha::Common::read_open( $file );
1124
1125         while ( $line = <$data_in> )
1126         {
1127             chomp $line;
1128
1129             next if $line =~ /^#/;
1130
1131             @fields = split /\t/, $line;
1132
1133             $record->{ "REC_TYPE" }   = "BLAST";
1134             $record->{ "Q_ID" }       = $fields[ 0 ];
1135             $record->{ "S_ID" }       = $fields[ 1 ];
1136             $record->{ "IDENT" }      = $fields[ 2 ];
1137             $record->{ "ALIGN_LEN" }  = $fields[ 3 ];
1138             $record->{ "MISMATCHES" } = $fields[ 4 ];
1139             $record->{ "GAPS" }       = $fields[ 5 ];
1140             $record->{ "Q_BEG" }      = $fields[ 6 ] - 1; # BLAST is 1-based
1141             $record->{ "Q_END" }      = $fields[ 7 ] - 1; # BLAST is 1-based
1142             $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # BLAST is 1-based
1143             $record->{ "S_END" }      = $fields[ 9 ] - 1; # BLAST is 1-based
1144             $record->{ "E_VAL" }      = $fields[ 10 ];
1145             $record->{ "BIT_SCORE" }  = $fields[ 11 ];
1146
1147             if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1148             {
1149                 $record->{ "STRAND" } = '-';
1150
1151                 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1152             }
1153             else
1154             {
1155                 $record->{ "STRAND" } = '+';
1156             }
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_embl
1175 {
1176     # Martin A. Hansen, August 2007.
1177
1178     # Read EMBL format.
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 ( %options2, $file, $data_in, $num, $entry, $record );
1188
1189     map { $options2{ "keys" }{ $_ } = 1 }  @{ $options->{ "keys" } };
1190     map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1191     map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1192
1193     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1194         Maasha::Biopieces::put_record( $record, $out );
1195     }
1196
1197     $num = 1;
1198
1199     foreach $file ( @{ $options->{ "files" } } )
1200     {
1201         $data_in = Maasha::Common::read_open( $file );
1202
1203         while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) ) 
1204         {
1205             $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1206
1207             my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1208
1209             $record_copy = dclone $record;
1210
1211             delete $record_copy->{ "FT" };
1212
1213             Maasha::Biopieces::put_record( $record_copy, $out );
1214
1215             delete $record_copy->{ "SEQ" };
1216
1217             foreach $feat ( keys %{ $record->{ "FT" } } )
1218             {
1219                 $record_copy->{ "FEAT_TYPE" } = $feat;
1220
1221                 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1222                 {
1223                     foreach $qual ( keys %{ $feat2 } )
1224                     {
1225                         $qual_val = join "; ", @{ $feat2->{ $qual } };
1226
1227                         $qual =~ s/^_//;
1228                         $qual = uc $qual;
1229
1230                         $record_copy->{ $qual } = $qual_val;
1231                     }
1232
1233                     Maasha::Biopieces::put_record( $record_copy, $out );
1234                 }
1235             }
1236
1237             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1238
1239             $num++;
1240         }
1241
1242         close $data_in;
1243     }
1244
1245     NUM:
1246
1247     close $data_in if $data_in;
1248 }
1249
1250
1251 sub script_read_stockholm
1252 {
1253     # Martin A. Hansen, August 2007.
1254
1255     # Read Stockholm format.
1256
1257     my ( $in,        # handle to in stream
1258          $out,       # handle to out stream
1259          $options,   # options hash
1260        ) = @_;
1261
1262     # Returns nothing.
1263
1264     my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1265
1266     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1267         Maasha::Biopieces::put_record( $record, $out );
1268     }
1269
1270     $num = 1;
1271
1272     foreach $file ( @{ $options->{ "files" } } )
1273     {
1274         $data_in = Maasha::Common::read_open( $file );
1275
1276         while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) ) 
1277         {
1278             $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1279
1280             undef $record_anno;
1281
1282             foreach $key ( keys %{ $record->{ "GF" } } ) {
1283                 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1284             }
1285
1286             $record_anno->{ "ALIGN" } = $num;
1287
1288             Maasha::Biopieces::put_record( $record_anno, $out );
1289
1290             foreach $seq ( @{ $record->{ "ALIGN" } } )
1291             {
1292                 undef $record_align;
1293             
1294                 $record_align = {
1295                     SEQ_NAME  => $seq->[ 0 ],
1296                     SEQ       => $seq->[ 1 ],
1297                 };
1298             
1299                 Maasha::Biopieces::put_record( $record_align, $out );
1300             }
1301
1302             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1303
1304             $num++;
1305         }
1306
1307         close $data_in;
1308     }
1309
1310     NUM:
1311
1312     close $data_in if $data_in;
1313 }
1314
1315
1316 sub script_read_phastcons
1317 {
1318     # Martin A. Hansen, December 2007.
1319
1320     # Read PhastCons format.
1321
1322     my ( $in,        # handle to in stream
1323          $out,       # handle to out stream
1324          $options,   # options hash
1325        ) = @_;
1326
1327     # Returns nothing.
1328
1329     my ( $data_in, $file, $num, $entry, @records, $record );
1330
1331     $options->{ "min" }       ||= 10;
1332     $options->{ "dist" }      ||= 25;
1333     $options->{ "threshold" } ||= 0.8;
1334     $options->{ "gap" }       ||= 5;
1335
1336     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1337         Maasha::Biopieces::put_record( $record, $out );
1338     }
1339
1340     $num = 1;
1341
1342     foreach $file ( @{ $options->{ "files" } } )
1343     {
1344         $data_in = Maasha::Common::read_open( $file );
1345
1346         while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) ) 
1347         {
1348             @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1349
1350             foreach $record ( @records )
1351             {
1352                 $record->{ "REC_TYPE" } = "BED";
1353                 $record->{ "BED_LEN" }  = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1354
1355                 Maasha::Biopieces::put_record( $record, $out );
1356
1357                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1358
1359                 $num++;
1360             }
1361         }
1362
1363         close $data_in;
1364     }
1365
1366     NUM:
1367
1368     close $data_in if $data_in;
1369 }
1370
1371
1372 sub script_read_soft
1373 {
1374     # Martin A. Hansen, December 2007.
1375
1376     # Read soft format.
1377     # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1378
1379     my ( $in,        # handle to in stream
1380          $out,       # handle to out stream
1381          $options,   # options hash
1382        ) = @_;
1383
1384     # Returns nothing.
1385
1386     my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1387
1388     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1389         Maasha::Biopieces::put_record( $record, $out );
1390     }
1391
1392     $num = 1;
1393
1394     foreach $file ( @{ $options->{ "files" } } )
1395     {
1396         print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1397
1398         $soft_index = Maasha::NCBI::soft_index_file( $file );
1399
1400         $fh         = Maasha::Common::read_open( $file );
1401
1402         @platforms  = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1403
1404         print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1405
1406         $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1407
1408         @samples    = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1409
1410         $old_end    = $platforms[ -1 ]->{ "LINE_END" };
1411
1412         foreach $sample ( @samples )
1413         {
1414             $skip = 0;
1415             $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1416
1417             print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1418
1419             $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1420
1421             foreach $record ( @{ $records } )
1422             {
1423                 Maasha::Biopieces::put_record( $record, $out );
1424
1425                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1426
1427                 $num++;
1428             }
1429
1430             $old_end = $sample->{ "LINE_END" };
1431         }
1432
1433         close $fh;
1434     }
1435
1436     NUM:
1437
1438     close $data_in if $data_in;
1439     close $fh if $fh;
1440 }
1441
1442
1443 sub script_read_gff
1444 {
1445     # Martin A. Hansen, February 2008.
1446
1447     # Read soft format.
1448     # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1449
1450     my ( $in,        # handle to in stream
1451          $out,       # handle to out stream
1452          $options,   # options hash
1453        ) = @_;
1454
1455     # Returns nothing.
1456
1457     my ( $data_in, $file, $fh, $num, $record, $entry );
1458
1459     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1460         Maasha::Biopieces::put_record( $record, $out );
1461     }
1462
1463     $num = 1;
1464
1465     foreach $file ( @{ $options->{ "files" } } )
1466     {
1467         $fh = Maasha::Common::read_open( $file );
1468
1469         while ( $entry = Maasha::GFF::get_entry( $fh ) )
1470         {
1471             Maasha::Biopieces::put_record( $entry, $out );
1472
1473             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1474
1475             $num++;
1476         }
1477
1478         close $fh;
1479     }
1480
1481     NUM:
1482
1483     close $data_in if $data_in;
1484 }
1485
1486
1487 sub script_read_2bit
1488 {
1489     # Martin A. Hansen, March 2008.
1490
1491     # Read sequences from 2bit file.
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 ( $record, $file, $data_in, $mask, $toc, $line, $num );
1501
1502     $mask = 1 if not $options->{ "no_mask" };
1503
1504     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1505         Maasha::Biopieces::put_record( $record, $out );
1506     }
1507
1508     $num = 1;
1509
1510     foreach $file ( @{ $options->{ "files" } } )
1511     {
1512         $data_in = Maasha::Common::read_open( $file );
1513
1514         $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1515
1516         foreach $line ( @{ $toc } )
1517         {
1518             $record->{ "SEQ_NAME" } = $line->[ 0 ];
1519             $record->{ "SEQ" }      = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1520             $record->{ "SEQ_LEN" }  = length $record->{ "SEQ" };
1521
1522             Maasha::Biopieces::put_record( $record, $out );
1523
1524             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1525
1526             $num++;
1527         }
1528
1529         close $data_in;
1530     }
1531
1532     NUM:
1533
1534     close $data_in if $data_in;
1535 }
1536
1537
1538 sub script_read_solexa
1539 {
1540     # Martin A. Hansen, March 2008.
1541
1542     # Read Solexa sequence reads from file.
1543
1544     my ( $in,        # handle to in stream
1545          $out,       # handle to out stream
1546          $options,   # options hash
1547        ) = @_;
1548
1549     # Returns nothing.
1550
1551     my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1552
1553     $options->{ "format" }  ||= "octal";
1554     $options->{ "quality" } ||= 20;
1555
1556     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1557         Maasha::Biopieces::put_record( $record, $out );
1558     }
1559
1560     $num = 1;
1561
1562     foreach $file ( @{ $options->{ "files" } } )
1563     {
1564         $data_in = Maasha::Common::read_open( $file );
1565
1566         if ( $options->{ "format" } eq "octal" )
1567         {
1568             while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1569             {
1570                 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1571
1572                 Maasha::Biopieces::put_record( $record, $out );
1573
1574                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1575
1576                 $num++;
1577             }
1578         }
1579         else
1580         {
1581             while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1582             {
1583                 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1584
1585                 Maasha::Biopieces::put_record( $record, $out );
1586
1587                 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1588
1589                 $num++;
1590             }
1591         }
1592
1593         close $data_in;
1594     }
1595
1596     NUM:
1597
1598     close $data_in if $data_in;
1599 }
1600
1601
1602 sub script_read_solid
1603 {
1604     # Martin A. Hansen, April 2008.
1605
1606     # Read Solid sequence from file.
1607
1608     my ( $in,        # handle to in stream
1609          $out,       # handle to out stream
1610          $options,   # options hash
1611        ) = @_;
1612
1613     # Returns nothing.
1614
1615     my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1616
1617     $options->{ "quality" } ||= 15;
1618
1619     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1620         Maasha::Biopieces::put_record( $record, $out );
1621     }
1622
1623     $num = 1;
1624
1625     foreach $file ( @{ $options->{ "files" } } )
1626     {
1627         $data_in = Maasha::Common::read_open( $file );
1628
1629         while ( $line = <$data_in> )
1630         {
1631             chomp $line;
1632
1633             ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1634
1635             @scores = split /,/, $seq_qual;
1636             @seqs   = split //, Maasha::Solid::color_space2seq( $seq_cs );
1637
1638             for ( $i = 0; $i < @seqs; $i++ ) {
1639                 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1640             }
1641
1642             $record = {
1643                 REC_TYPE   => 'SOLID',
1644                 SEQ_NAME   => $seq_name,
1645                 SEQ_CS     => $seq_cs,
1646                 SEQ_QUAL   => join( ";", @scores ),
1647                 SEQ_LEN    => length $seq_cs,
1648                 SEQ        => join( "", @seqs ),
1649                 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1650             };
1651
1652             Maasha::Biopieces::put_record( $record, $out );
1653
1654             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1655
1656             $num++;
1657         }
1658
1659         close $data_in;
1660     }
1661
1662     NUM:
1663
1664     close $data_in if $data_in;
1665 }
1666
1667
1668 sub script_read_mysql
1669 {
1670     # Martin A. Hansen, May 2008.
1671
1672     # Read a MySQL query into stream.
1673
1674     my ( $in,        # handle to in stream
1675          $out,       # handle to out stream
1676          $options,   # options hash
1677        ) = @_;
1678
1679     # Returns nothing.
1680
1681     my ( $record, $dbh, $results );
1682
1683     $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
1684     $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1685
1686     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1687         Maasha::Biopieces::put_record( $record, $out );
1688     }
1689
1690     $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1691
1692     $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1693
1694     Maasha::SQL::disconnect( $dbh );
1695
1696     map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1697 }
1698
1699
1700 sub script_read_ucsc_config
1701 {
1702     # Martin A. Hansen, November 2008.
1703
1704     # Read track entries from UCSC Genome Browser '.ra' files.
1705
1706     my ( $in,        # handle to in stream
1707          $out,       # handle to out stream
1708          $options,   # options hash
1709        ) = @_;
1710
1711     # Returns nothing.
1712
1713     my ( $record, $file, $data_in, $entry, $num );
1714
1715     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1716         Maasha::Biopieces::put_record( $record, $out );
1717     }
1718
1719     $num = 1;
1720
1721     foreach $file ( @{ $options->{ "files" } } )
1722     {
1723         $data_in = Maasha::Common::read_open( $file );
1724
1725         while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) ) 
1726         {
1727             $record->{ 'REC_TYPE' } = "UCSC Config";
1728
1729             Maasha::Biopieces::put_record( $record, $out );
1730
1731             goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1732
1733             $num++;
1734         }
1735
1736         close $data_in;
1737     }
1738
1739     NUM:
1740
1741     close $data_in if $data_in;
1742 }
1743
1744
1745 sub script_assemble_tag_contigs
1746 {
1747     # Martin A. Hansen, November 2008.
1748
1749     # Assemble tags from the stream into
1750     # tag contigs.
1751
1752     # The current implementation is quite
1753     # slow because of heavy use of temporary
1754     # files.
1755
1756     my ( $in,        # handle to in stream
1757          $out,       # handle to out stream
1758          $options,   # options hash
1759        ) = @_;
1760
1761     # Returns nothing.
1762
1763     my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
1764     
1765     $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
1766     $fh_out   = Maasha::Filesys::file_write_open( $bed_file );
1767     $cols     = 6; # we only need the first 6 BED columns
1768
1769     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1770     {
1771         if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
1772         {
1773             $strand = $record->{ 'STRAND' } || '+';
1774
1775             Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
1776         }
1777     }
1778
1779     close $fh_out;
1780
1781     $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
1782
1783     unlink $bed_file;
1784
1785     foreach $chr ( sort keys %{ $file_hash } )
1786     {
1787         $bed_file = $file_hash->{ $chr };
1788         $tag_file = "$bed_file.tc";
1789
1790         Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
1791
1792         $fh_in = Maasha::Filesys::file_read_open( $tag_file );
1793
1794         while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
1795         {
1796             if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
1797                 Maasha::Biopieces::put_record( $record, $out );
1798             }
1799         }
1800
1801         close $fh_in;
1802
1803         unlink $bed_file;
1804         unlink $tag_file;
1805     }
1806 }
1807
1808
1809 sub script_length_seq
1810 {
1811     # Martin A. Hansen, August 2007.
1812
1813     # Determine the length of sequences in stream.
1814
1815     my ( $in,        # handle to in stream
1816          $out,       # handle to out stream
1817          $options,   # options hash
1818        ) = @_;
1819
1820     # Returns nothing.
1821
1822     my ( $record, $total );
1823
1824     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1825     {
1826         if ( $record->{ "SEQ" } )
1827         {
1828             $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1829             $total += $record->{ "SEQ_LEN" };
1830         }
1831
1832         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1833     }
1834
1835     Maasha::Biopieces::put_record( { TOTAL_SEQ_LEN => $total }, $out );
1836 }
1837
1838
1839 sub script_analyze_seq
1840 {
1841     # Martin A. Hansen, August 2007.
1842
1843     # Analyze sequence composition of sequences in stream.
1844
1845     my ( $in,     # handle to in stream
1846          $out,    # handle to out stream
1847        ) = @_;
1848
1849     # Returns nothing.
1850
1851     my ( $record, $analysis );
1852
1853     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1854     {
1855         if ( $record->{ "SEQ" } )
1856         {
1857             $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
1858
1859             map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
1860         }
1861
1862         Maasha::Biopieces::put_record( $record, $out );
1863     }
1864 }
1865
1866
1867 sub script_analyze_tags
1868 {
1869     # Martin A. Hansen, August 2008.
1870
1871     # Analyze sequence tags in stream.
1872
1873     my ( $in,     # handle to in stream
1874          $out,    # handle to out stream
1875        ) = @_;
1876
1877     # Returns nothing.
1878
1879     my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
1880
1881     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1882     {
1883         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
1884         {
1885             if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
1886             {
1887                 $clones = $1;
1888
1889                 $len_hash{ length( $record->{ "SEQ" } ) }++;
1890                 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
1891             }
1892         }
1893         elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
1894         {
1895             if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
1896             {
1897                 $clones = $1;
1898
1899                 $len_hash{ $record->{ "BED_LEN" } }++;
1900                 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
1901             }
1902         }
1903     }
1904
1905     foreach $key ( sort { $a <=> $b } keys %len_hash )
1906     {
1907         $tag_record->{ "TAG_LEN" }    = $key;
1908         $tag_record->{ "TAG_COUNT" }  = $len_hash{ $key };
1909         $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
1910  
1911         Maasha::Biopieces::put_record( $tag_record, $out );
1912     }
1913 }
1914
1915
1916 sub script_complexity_seq
1917 {
1918     # Martin A. Hansen, May 2008.
1919
1920     # Generates an index calculated as the most common di-residue over
1921     # the sequence length for all sequences in stream.
1922
1923     my ( $in,     # handle to in stream
1924          $out,    # handle to out stream
1925        ) = @_;
1926
1927     # Returns nothing.
1928
1929     my ( $record, $index );
1930
1931     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1932     {
1933         $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1934
1935         Maasha::Biopieces::put_record( $record, $out );
1936     }
1937 }
1938
1939
1940 sub script_oligo_freq
1941 {
1942     # Martin A. Hansen, August 2007.
1943
1944     # Determine the length of sequences in stream.
1945
1946     my ( $in,        # handle to in stream
1947          $out,       # handle to out stream
1948          $options,   # options hash
1949        ) = @_;
1950
1951     # Returns nothing.
1952
1953     my ( $record, %oligos, @freq_table );
1954
1955     $options->{ "word_size" } ||= 7;
1956
1957     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
1958     {
1959         if ( $record->{ "SEQ" } )
1960         {
1961             map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
1962
1963             if ( not $options->{ "all" } )
1964             {
1965                 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1966
1967                 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1968             
1969                 undef %oligos;
1970             }
1971         }
1972
1973         Maasha::Biopieces::put_record( $record, $out );
1974     }
1975
1976     if ( $options->{ "all" } )
1977     {
1978         @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1979
1980         map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1981     }
1982 }
1983
1984
1985 sub script_create_weight_matrix
1986 {
1987     # Martin A. Hansen, August 2007.
1988
1989     # Creates a weight matrix from an alignmnet.
1990
1991     my ( $in,        # handle to in stream
1992          $out,       # handle to out stream
1993          $options,   # options hash
1994        ) = @_;
1995
1996     # Returns nothing.
1997
1998     my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
1999
2000     $count = 0;
2001     
2002     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2003     {
2004         if ( $record->{ "SEQ" } )
2005         {
2006             for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2007             {
2008                 $res = substr $record->{ "SEQ" }, $i, 1;
2009
2010                 $freq_hash{ $i }{ $res }++;
2011                 $res_hash{ $res } = 1;
2012             }
2013
2014             $count++;
2015         }
2016         else
2017         {
2018             Maasha::Biopieces::put_record( $record, $out );
2019         }
2020     }
2021
2022     foreach $res ( sort keys %res_hash )
2023     {
2024         undef $record;
2025
2026         $record->{ "V0" } = $res;
2027
2028         for ( $i = 0; $i < keys %freq_hash; $i++ )
2029         {
2030             $freq = $freq_hash{ $i }{ $res } || 0;
2031
2032             if ( $options->{ "percent" } ) {
2033                 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2034             }
2035
2036             $record->{ "V" . ( $i + 1 ) } = $freq;
2037         }
2038
2039         Maasha::Biopieces::put_record( $record, $out );
2040     }
2041 }
2042
2043
2044 sub script_calc_bit_scores
2045 {
2046     # Martin A. Hansen, March 2007.
2047
2048     # Calculates the bit scores for each position from an alignmnet in the stream.
2049
2050     my ( $in,        # handle to in stream
2051          $out,       # handle to out stream
2052        ) = @_;
2053
2054     # Returns nothing.
2055
2056     my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2057
2058     $count = 0;
2059
2060     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2061     {
2062         if ( $record->{ "SEQ" } )
2063         {
2064             $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2065
2066             for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2067             {
2068                 $res = substr $record->{ "SEQ" }, $i, 1;
2069
2070                 next if $res =~ /-|_|~|\./;
2071
2072                 $freq_hash{ $i }{ $res }++;
2073             }
2074
2075             $count++;
2076         }
2077         else
2078         {
2079             Maasha::Biopieces::put_record( $record, $out );
2080         }
2081     }
2082
2083     undef $record;
2084
2085     if ( $type eq "protein" ) {
2086         $bit_max = 4;
2087     } else {
2088         $bit_max = 2;
2089     }
2090
2091     for ( $i = 0; $i < keys %freq_hash; $i++ )
2092     {
2093         $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2094
2095         $bit_diff = $bit_max - $bit_height;
2096
2097         $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2098     }
2099
2100     Maasha::Biopieces::put_record( $record, $out );
2101 }
2102
2103
2104 sub script_calc_fixedstep
2105 {
2106     # Martin A. Hansen, September 2008.
2107
2108     # Calculates fixedstep entries from data in the stream.
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 ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2118
2119     $bed_file = "$BP_TMP/calc_fixedstep.bed";
2120     $fh_out   = Maasha::Filesys::file_write_open( $bed_file );
2121
2122     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2123     {
2124         if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) {
2125             Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } );
2126         }
2127     }
2128
2129     close $fh_out;
2130
2131     $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP );
2132
2133     unlink $bed_file;
2134
2135     foreach $chr ( sort keys %{ $file_hash } )
2136     {
2137         $bed_file       = $file_hash->{ $chr };
2138
2139         $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } );        
2140
2141         #$fixedstep_file = "$bed_file.fixedstep";
2142         
2143         # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2144
2145         $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2146
2147         while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2148         {
2149             if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2150                 Maasha::Biopieces::put_record( $record, $out );
2151             }
2152         }
2153
2154         close $fh_in;
2155
2156         unlink $bed_file;
2157         unlink $fixedstep_file;
2158     }
2159 }
2160
2161
2162 sub script_remove_indels
2163 {
2164     # Martin A. Hansen, August 2007.
2165
2166     # Remove indels from sequences in stream.
2167
2168     my ( $in,     # handle to in stream
2169          $out,    # handle to out stream
2170        ) = @_;
2171
2172     # Returns nothing.
2173
2174     my ( $record );
2175
2176     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2177     {
2178         $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2179
2180         Maasha::Biopieces::put_record( $record, $out );
2181     }
2182 }
2183
2184
2185 sub script_transliterate_seq
2186 {
2187     # Martin A. Hansen, August 2007.
2188
2189     # Transliterate chars from sequence in record.
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 ( $record, $search, $replace, $delete );
2199
2200     $search  = $options->{ "search" }  || "";
2201     $replace = $options->{ "replace" } || "";
2202     $delete  = $options->{ "delete" }  || "";
2203
2204     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2205     {
2206         if ( $record->{ "SEQ" } )
2207         {
2208             if ( $search and $replace ) {
2209                 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2210             } elsif ( $delete ) {
2211                 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2212             }
2213         }
2214
2215         Maasha::Biopieces::put_record( $record, $out );
2216     }
2217 }
2218
2219
2220 sub script_transliterate_vals
2221 {
2222     # Martin A. Hansen, April 2008.
2223
2224     # Transliterate chars from values in record.
2225
2226     my ( $in,        # handle to in stream
2227          $out,       # handle to out stream
2228          $options,   # options hash
2229        ) = @_;
2230
2231     # Returns nothing.
2232
2233     my ( $record, $search, $replace, $delete, $key );
2234
2235     $search  = $options->{ "search" }  || "";
2236     $replace = $options->{ "replace" } || "";
2237     $delete  = $options->{ "delete" }  || "";
2238
2239     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2240     {
2241         foreach $key ( @{ $options->{ "keys" } } )
2242         {
2243             if ( exists $record->{ $key } )
2244             {
2245                 if ( $search and $replace ) {
2246                     eval "\$record->{ $key } =~ tr/$search/$replace/";
2247                 } elsif ( $delete ) {
2248                     eval "\$record->{ $key } =~ tr/$delete//d";
2249                 }
2250             }
2251         }
2252
2253         Maasha::Biopieces::put_record( $record, $out );
2254     }
2255 }
2256
2257
2258 sub script_translate_seq
2259 {
2260     # Martin A. Hansen, February 2008.
2261
2262     # Translate DNA sequence into protein sequence.
2263
2264     my ( $in,        # handle to in stream
2265          $out,       # handle to out stream
2266          $options,   # options hash
2267        ) = @_;
2268
2269     # Returns nothing.
2270
2271     my ( $record, $frame, %new_record );
2272
2273     $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2274
2275     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2276     {
2277         if ( $record->{ "SEQ" } )
2278         {
2279             if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2280             {
2281                 foreach $frame ( @{ $options->{ "frames" } } )
2282                 {
2283                     %new_record = %{ $record };
2284
2285                     $new_record{ "SEQ" }     = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2286                     $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2287                     $new_record{ "FRAME" }   = $frame;
2288
2289                     Maasha::Biopieces::put_record( \%new_record, $out );
2290                 }
2291             }
2292         }
2293         else
2294         {
2295             Maasha::Biopieces::put_record( $record, $out );
2296         }
2297     }
2298 }
2299
2300
2301 sub script_get_genome_align
2302 {
2303     # Martin A. Hansen, April 2008.
2304
2305     # Gets a subalignment from a multiple genome alignment.
2306
2307     my ( $in,        # handle to in stream
2308          $out,       # handle to out stream
2309          $options,   # options hash
2310        ) = @_;
2311
2312     # Returns nothing.
2313
2314     my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2315
2316     $options->{ "strand" } ||= "+";
2317
2318     $align_num = 1;
2319
2320     $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2321
2322     if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2323     {
2324         $beg = $options->{ "beg" } - 1;
2325         
2326         if ( $options->{ "end" } ) {
2327             $end = $options->{ "end" };
2328         } elsif ( $options->{ "len" } ) {
2329             $end = $beg + $options->{ "len" };
2330         }
2331
2332         $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2333
2334         foreach $entry ( @{ $align } )
2335         {
2336             $entry->{ "CHR" }     = $record->{ "CHR" };
2337             $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2338             $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2339             $entry->{ "STRAND" }  = $record->{ "STRAND" } || '+';
2340             $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
2341             $entry->{ "SCORE" }   = $record->{ "SCORE" };
2342
2343             Maasha::Biopieces::put_record( $entry, $out );
2344         }
2345     }
2346
2347     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2348     {
2349         if ( $record->{ "REC_TYPE" } eq "BED" )
2350         {
2351             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2352         }
2353         elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
2354         {
2355             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
2356         }
2357         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2358         {
2359             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2360         }
2361         elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2362         {
2363             $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2364         }
2365
2366         foreach $entry ( @{ $align } )
2367         {
2368             $entry->{ "CHR" }     = $record->{ "CHR" };
2369             $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2370             $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2371             $entry->{ "STRAND" }  = $record->{ "STRAND" };
2372             $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
2373             $entry->{ "SCORE" }   = $record->{ "SCORE" };
2374
2375             Maasha::Biopieces::put_record( $entry, $out );
2376         }
2377
2378         $align_num++;
2379     }
2380 }
2381
2382
2383 sub script_get_genome_phastcons
2384 {
2385     # Martin A. Hansen, February 2008.
2386
2387     # Get phastcons scores from genome intervals.
2388
2389     my ( $in,        # handle to in stream
2390          $out,       # handle to out stream
2391          $options,   # options hash
2392        ) = @_;
2393
2394     # Returns nothing.
2395
2396     my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2397
2398     $options->{ "flank" } ||= 0;
2399
2400     $phastcons_file  = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2401     $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2402
2403     $index           = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
2404     $fh_phastcons    = Maasha::Common::read_open( $phastcons_file );
2405
2406     if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2407     {
2408         $options->{ "beg" } -= 1;   # request is 1-based
2409         $options->{ "end" } -= 1;   # request is 1-based
2410
2411         if ( $options->{ "len" } ) {
2412             $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2413         }
2414
2415         $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2416
2417         $record->{ "CHR" }       = $options->{ "chr" };
2418         $record->{ "CHR_BEG" }   = $options->{ "beg" } - $options->{ "flank" };
2419         $record->{ "CHR_END" }   = $options->{ "end" } + $options->{ "flank" };
2420         
2421         $record->{ "PHASTCONS" }   = join ",", @{ $scores };
2422         $record->{ "PHAST_COUNT" } = scalar @{ $scores };  # DEBUG
2423
2424         Maasha::Biopieces::put_record( $record, $out );
2425     }   
2426
2427     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2428     {
2429         if ( $record->{ "REC_TYPE" } eq "BED" )
2430         {
2431             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2432         }
2433         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2434         {
2435             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2436         }
2437         elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2438         {
2439             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2440         }
2441
2442         $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2443 #        $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores };  # DEBUG
2444
2445         Maasha::Biopieces::put_record( $record, $out );
2446     }
2447
2448     close $fh_phastcons if $fh_phastcons;                                                                                                                                          
2449 }
2450
2451
2452 sub script_fold_seq
2453 {
2454     # Martin A. Hansen, December 2007.
2455
2456     # Folds sequences in stream into secondary structures.
2457
2458     my ( $in,     # handle to in stream
2459          $out,    # handle to out stream
2460        ) = @_;
2461
2462     # Returns nothing.
2463
2464     my ( $record, $type, $struct, $index );
2465
2466     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2467     {
2468         if ( $record->{ "SEQ" } )
2469         {
2470             if ( not $type ) {
2471                 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2472             }
2473             
2474             if ( $type ne "protein" )
2475             {
2476                 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2477                 $record->{ "SEC_STRUCT" }  = $struct;
2478                 $record->{ "FREE_ENERGY" } = $index;
2479                 $record->{ "SCORE" }       = abs int $index;
2480                 $record->{ "SIZE" }        = length $struct;
2481                 $record->{ "CONF" }        = "1," x $record->{ "SIZE" };
2482             }
2483         }
2484
2485         Maasha::Biopieces::put_record( $record, $out );
2486     }
2487 }
2488
2489
2490 sub script_split_seq
2491 {
2492     # Martin A. Hansen, August 2007.
2493
2494     # Split a sequence in stream into words.
2495
2496     my ( $in,        # handle to in stream
2497          $out,       # handle to out stream
2498          $options,   # options hash
2499        ) = @_;
2500
2501     # Returns nothing.
2502
2503     my ( $record, $new_record, $i, $subseq, %lookup );
2504
2505     $options->{ "word_size" } ||= 7;
2506
2507     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2508     {
2509         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2510         {
2511             for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2512             {
2513                 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2514
2515                 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2516                 {
2517                     $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2518                     $new_record->{ "SEQ" }      = $subseq;
2519
2520                     Maasha::Biopieces::put_record( $new_record, $out );
2521
2522                     $lookup{ $subseq } = 1;
2523                 }
2524                 else
2525                 {
2526                     $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2527                     $new_record->{ "SEQ" }      = $subseq;
2528
2529                     Maasha::Biopieces::put_record( $new_record, $out );
2530                 }
2531             }
2532         }
2533         else
2534         {
2535             Maasha::Biopieces::put_record( $record, $out );
2536         }
2537     }
2538 }
2539
2540
2541 sub script_split_bed
2542 {
2543     # Martin A. Hansen, June 2008.
2544
2545     # Split a BED record into overlapping windows.
2546
2547     my ( $in,        # handle to in stream
2548          $out,       # handle to out stream
2549          $options,   # options hash
2550        ) = @_;
2551
2552     # Returns nothing.
2553
2554     my ( $record, $new_record, $i );
2555
2556     $options->{ "window_size" } ||= 20;
2557     $options->{ "step_size" }   ||= 1;
2558
2559     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2560     {
2561         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2562         {
2563             $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2564
2565             for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2566             {
2567                 $new_record->{ "REC_TYPE" } = "BED";
2568                 $new_record->{ "CHR" }      = $record->{ "CHR" };
2569                 $new_record->{ "CHR_BEG" }  = $record->{ "CHR_BEG" } + $i;
2570                 $new_record->{ "CHR_END" }  = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2571                 $new_record->{ "BED_LEN" }  = $options->{ "window_size" };
2572                 $new_record->{ "Q_ID" }     = $record->{ "Q_ID" } . "_$i";
2573                 $new_record->{ "SCORE" }    = $record->{ "SCORE" };
2574                 $new_record->{ "STRAND" }   = $record->{ "STRAND" };
2575
2576                 Maasha::Biopieces::put_record( $new_record, $out );
2577             }
2578         }
2579         else
2580         {
2581             Maasha::Biopieces::put_record( $record, $out );
2582         }
2583     }
2584 }
2585
2586
2587 sub script_align_seq
2588 {
2589     # Martin A. Hansen, August 2007.
2590
2591     # Align sequences in stream.
2592
2593     my ( $in,    # handle to in stream
2594          $out,   # handle to out stream
2595        ) = @_;
2596
2597     # Returns nothing.
2598
2599     my ( $record, @entries, $entry );
2600
2601     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2602     {
2603         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2604             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2605         } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
2606             push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
2607         } else {
2608             Maasha::Biopieces::put_record( $record, $out );
2609         }
2610     }
2611
2612     @entries = Maasha::Align::align( \@entries );
2613
2614     foreach $entry ( @entries )
2615     {
2616         if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
2617         {
2618             $record = {
2619                 SEQ_NAME => $entry->[ SEQ_NAME ],
2620                 SEQ      => $entry->[ SEQ ],
2621             };
2622
2623             Maasha::Biopieces::put_record( $record, $out );
2624         }
2625     }
2626 }
2627
2628
2629 sub script_tile_seq
2630 {
2631     # Martin A. Hansen, February 2008.
2632
2633     # Using the first sequence in stream as reference, tile
2634     # all subsequent sequences based on pairwise alignments.
2635
2636     my ( $in,        # handle to in stream
2637          $out,       # handle to out stream
2638          $options,   # options hash
2639        ) = @_;
2640
2641     # Returns nothing.
2642
2643     my ( $record, $first, $ref_entry, @entries );
2644
2645     $first = 1;
2646
2647     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2648     {
2649         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2650         {
2651             if ( $first )
2652             {
2653                 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2654
2655                 $first = 0;
2656             }
2657             else
2658             {
2659                 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2660             }
2661         }
2662         else
2663         {
2664             Maasha::Biopieces::put_record( $record, $out );
2665         }
2666     }
2667
2668     @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2669
2670     map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2671 }
2672
2673
2674 sub script_invert_align
2675 {
2676     # Martin A. Hansen, February 2008.
2677
2678     # Inverts an alignment showing only non-mathing residues
2679     # using the first sequence as reference.
2680
2681     my ( $in,        # handle to in stream
2682          $out,       # handle to out stream
2683          $options,   # options hash
2684        ) = @_;
2685
2686     # Returns nothing.
2687
2688     my ( $record, @entries );
2689
2690     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2691     {
2692         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2693         {
2694             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2695         }
2696         else
2697         {
2698             Maasha::Biopieces::put_record( $record, $out );
2699         }
2700     }
2701
2702     Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
2703
2704     map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2705 }
2706
2707
2708 sub script_patscan_seq
2709 {
2710     # Martin A. Hansen, August 2007.
2711
2712     # Locates patterns in sequences using scan_for_matches.
2713
2714     my ( $in,        # handle to in stream
2715          $out,       # handle to out stream
2716          $options,   # options hash
2717        ) = @_;
2718
2719     # Returns nothing.
2720
2721     my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
2722
2723     if ( $options->{ "patterns" } ) {
2724         $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
2725     } elsif ( -f $options->{ "patterns_in" } ) {
2726         $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
2727     }
2728
2729     $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
2730
2731     push @args, "-c"                            if $options->{ "comp" };
2732     push @args, "-m $options->{ 'max_hits' }"   if $options->{ 'max_hits' };
2733     push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
2734
2735     $seq_file = "$BP_TMP/patscan.seq";
2736     $pat_file = "$BP_TMP/patscan.pat";
2737     $out_file = "$BP_TMP/patscan.out";
2738
2739     $fh_out = Maasha::Common::write_open( $seq_file );
2740
2741     $i = 0;
2742
2743     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2744     {
2745         if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
2746         {
2747             $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2748
2749             Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
2750
2751             $head_hash{ $i } = $record->{ "SEQ_NAME" };
2752
2753             $i++;
2754         }
2755     }
2756
2757     close $fh_out;
2758
2759     $arg  = join " ", @args;
2760     $arg .= " -p" if $type eq "protein";
2761
2762     foreach $pattern ( @{ $patterns } )
2763     {
2764         $fh_out = Maasha::Common::write_open( $pat_file );
2765
2766         print $fh_out "$pattern\n";
2767
2768         close $fh_out;
2769
2770         if ( $options->{ 'genome' } ) {
2771             `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
2772             # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
2773         } else {
2774             `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
2775             # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
2776         }
2777
2778         $fh_in = Maasha::Common::read_open( $out_file );
2779
2780         while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
2781         {
2782             $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
2783
2784             if ( $options->{ 'genome' } )
2785             {
2786                 $result->{ "CHR" }     = $result->{ "S_ID" };
2787                 $result->{ "CHR_BEG" } = $result->{ "S_BEG" }; 
2788                 $result->{ "CHR_END" } = $result->{ "S_END" }; 
2789
2790                 delete $result->{ "S_ID" };
2791                 delete $result->{ "S_BEG" };
2792                 delete $result->{ "S_END" };
2793             }
2794             else
2795             {
2796                 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
2797             }
2798
2799             Maasha::Biopieces::put_record( $result, $out );
2800         }
2801
2802         close $fh_in;
2803     }
2804
2805     unlink $pat_file;
2806     unlink $seq_file;
2807     unlink $out_file;
2808 }
2809
2810
2811 sub script_blat_seq
2812 {
2813     # Martin A. Hansen, August 2007.
2814
2815     # BLATs sequences in stream against a given genome.
2816
2817     my ( $in,        # handle to in stream
2818          $out,       # handle to out stream
2819          $options,   # options hash
2820        ) = @_;
2821
2822     # Returns nothing.
2823
2824     my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
2825
2826     $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2827
2828     $options->{ 'tile_size' }    ||= 11;
2829     $options->{ 'one_off' }      ||= 0;
2830     $options->{ 'min_identity' } ||= 90;
2831     $options->{ 'min_score' }    ||= 0;
2832     $options->{ 'step_size' }    ||= $options->{ 'tile_size' };
2833
2834     $blat_args .= " -tileSize=$options->{ 'tile_size' }";
2835     $blat_args .= " -oneOff=$options->{ 'one_off' }";
2836     $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
2837     $blat_args .= " -minScore=$options->{ 'min_score' }";
2838     $blat_args .= " -stepSize=$options->{ 'step_size' }";
2839 #    $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
2840
2841     $query_file = "$BP_TMP/blat.seq";
2842
2843     $fh_out = Maasha::Common::write_open( $query_file );
2844
2845     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2846     {
2847         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2848         {
2849             $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
2850             Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
2851         }
2852
2853         Maasha::Biopieces::put_record( $record, $out );
2854     }
2855
2856     close $fh_out;
2857
2858     $blat_args .= " -t=dnax" if $type eq "protein";
2859     $blat_args .= " -q=$type";
2860
2861     $result_file = "$BP_TMP/blat.psl";
2862
2863     Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
2864
2865     unlink $query_file;
2866
2867     $entries = Maasha::UCSC::psl_get_entries( $result_file );
2868
2869     map { Maasha::Biopieces::put_record( $_, $out ) } @{ $entries };
2870
2871     unlink $result_file;
2872 }
2873
2874
2875 sub script_soap_seq
2876 {
2877     # Martin A. Hansen, July 2008.
2878
2879     # soap sequences in stream against a given file or genome.
2880
2881     my ( $in,        # handle to in stream
2882          $out,       # handle to out stream
2883          $options,   # options hash
2884        ) = @_;
2885
2886     # Returns nothing.
2887
2888     my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
2889
2890     $options->{ "seed_size" }  ||= 10;
2891     $options->{ "mismatches" } ||= 2;
2892     $options->{ "gap_size" }   ||= 0;
2893     $options->{ "cpus" }       ||= 1;
2894
2895     if ( $options->{ "genome" } ) {
2896         $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2897     }
2898
2899     $tmp_in  = "$BP_TMP/soap_query.seq";
2900     $tmp_out = "$BP_TMP/soap.result";
2901
2902     $fh_out = Maasha::Common::write_open( $tmp_in );
2903  
2904     $count = 0;
2905
2906     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2907     {
2908         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2909         {
2910             Maasha::Fasta::put_entry( $entry, $fh_out );
2911
2912             $count++;
2913         }
2914
2915         Maasha::Biopieces::put_record( $record, $out );
2916     }
2917
2918     close $fh_out;
2919
2920     if ( $count > 0 )
2921     {
2922         $args = join( " ",
2923             "-s $options->{ 'seed_size' }",
2924             "-r 2",
2925             "-a $tmp_in",
2926             "-v $options->{ 'mismatches' }",
2927             "-g $options->{ 'gap_size' }",
2928             "-p $options->{ 'cpus' }",
2929             "-d $options->{ 'in_file' }",
2930             "-o $tmp_out",
2931         );
2932
2933         $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
2934
2935         Maasha::Common::run( "soap", $args, 1 );
2936
2937         unlink $tmp_in;
2938
2939         $fh_out = Maasha::Common::read_open( $tmp_out );
2940
2941         undef $record;
2942
2943         while ( $line = <$fh_out> )
2944         {
2945             chomp $line;
2946
2947             @fields = split /\t/, $line;
2948
2949             $record->{ "REC_TYPE" }   = "SOAP";
2950             $record->{ "Q_ID" }       = $fields[ 0 ];
2951             $record->{ "SCORE" }      = $fields[ 3 ];
2952             $record->{ "STRAND" }     = $fields[ 6 ];
2953             $record->{ "S_ID" }       = $fields[ 7 ];
2954             $record->{ "S_BEG" }      = $fields[ 8 ] - 1; # soap is 1-based
2955             $record->{ "S_END" }      = $fields[ 8 ] + $fields[ 5 ] - 2;
2956
2957             Maasha::Biopieces::put_record( $record, $out );
2958         }
2959
2960         close $fh_out;
2961     }
2962
2963     unlink $tmp_out;
2964 }
2965
2966
2967 sub script_match_seq
2968 {
2969     # Martin A. Hansen, August 2007.
2970
2971     # BLATs sequences in stream against a given genome.
2972
2973     my ( $in,        # handle to in stream
2974          $out,       # handle to out stream
2975          $options,   # options hash
2976        ) = @_;
2977
2978     # Returns nothing.
2979
2980     my ( $record, @entries, $results );
2981
2982     $options->{ "word_size" } ||= 20;
2983     $options->{ "direction" } ||= "both";
2984
2985     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
2986     {
2987         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2988             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2989         }
2990
2991         Maasha::Biopieces::put_record( $record, $out );
2992     }
2993
2994     if ( @entries == 1 )
2995     {
2996         $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
2997
2998         map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
2999     }
3000     elsif ( @entries == 2 )
3001     {
3002         $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3003
3004         map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3005     }
3006 }
3007
3008
3009 sub script_create_vmatch_index
3010 {
3011     # Martin A. Hansen, January 2008.
3012
3013     # Create a vmatch index from sequences in the stream.
3014
3015     my ( $in,        # handle to in stream
3016          $out,       # handle to out stream
3017          $options,   # options hash
3018        ) = @_;
3019
3020     # Returns nothing.
3021
3022     my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3023
3024     if ( $options->{ "index_name" } )
3025     {
3026         $file_tmp = $options->{ 'index_name' };
3027         $fh_tmp   = Maasha::Common::write_open( $file_tmp );
3028     }
3029
3030     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3031     {
3032         if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3033         {
3034             Maasha::Fasta::put_entry( $entry, $fh_tmp );
3035
3036             $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3037         }
3038
3039         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3040     }
3041
3042     if ( $options->{ "index_name" } )
3043     {
3044         close $fh_tmp;
3045     
3046         if ( $type eq "protein" ) {
3047             Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3048         } else {
3049             Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3050         }
3051
3052         unlink $file_tmp;
3053     }
3054 }
3055
3056
3057 sub script_write_align
3058 {
3059     # Martin A. Hansen, August 2007.
3060
3061     # Write pretty alignments aligned sequences in stream.
3062
3063     my ( $in,        # handle to in stream
3064          $out,       # handle to out stream
3065          $options,   # options hash
3066        ) = @_;
3067
3068     # Returns nothing.
3069
3070     my ( $fh, $record, @entries );
3071
3072     $fh = write_stream( $options->{ "data_out" } ) ;
3073
3074     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3075     {
3076         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3077             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3078         }
3079
3080         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3081     }
3082
3083     if ( scalar( @entries ) == 2 ) {
3084         Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3085     } elsif ( scalar ( @entries ) > 2 ) {
3086         Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3087     }
3088
3089     close $fh if $fh;
3090 }
3091
3092
3093 sub script_write_bed
3094 {
3095     # Martin A. Hansen, August 2007.
3096
3097     # Write BED format for the UCSC genome browser using records in stream.
3098
3099     my ( $in,        # handle to in stream
3100          $out,       # handle to out stream
3101          $options,   # options hash
3102        ) = @_;
3103
3104     # Returns nothing.
3105
3106     my ( $cols, $fh, $record, $bed_entry, $new_record );
3107
3108     $cols = $options->{ 'cols' }->[ 0 ];
3109
3110     $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
3111
3112     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3113     {
3114         $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
3115
3116         if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
3117             Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
3118         }
3119
3120         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3121     }
3122
3123     close $fh;
3124 }
3125
3126
3127 sub script_write_psl
3128 {
3129     # Martin A. Hansen, August 2007.
3130
3131     # Write PSL output from stream.
3132
3133     my ( $in,        # handle to in stream
3134          $out,       # handle to out stream
3135          $options,   # options hash
3136        ) = @_;
3137
3138     # Returns nothing.
3139
3140     my ( $fh, $record, @output, $first );
3141
3142     $first = 1;
3143
3144     $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3145
3146     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3147     {
3148         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3149
3150         if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3151         {
3152             Maasha::UCSC::psl_put_header( $fh ) if $first;
3153             Maasha::UCSC::psl_put_entry( $record, $fh );
3154             $first = 0;
3155         }
3156     }
3157
3158     close $fh;
3159 }
3160
3161
3162 sub script_write_fixedstep
3163 {
3164     # Martin A. Hansen, Juli 2008.
3165
3166     # Write fixedStep entries from recrods in the stream.
3167
3168     my ( $in,        # handle to in stream
3169          $out,       # handle to out stream
3170          $options,   # options hash
3171        ) = @_;
3172
3173     # Returns nothing.
3174
3175     my ( $fh, $record, $entry );
3176
3177     $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3178
3179     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3180     {
3181         if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
3182             Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
3183         }
3184
3185         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3186     }
3187
3188     close $fh;
3189 }
3190
3191
3192 sub script_write_2bit
3193 {
3194     # Martin A. Hansen, March 2008.
3195
3196     # Write sequence entries from stream in 2bit format.
3197
3198     my ( $in,        # handle to in stream
3199          $out,       # handle to out stream
3200          $options,   # options hash
3201        ) = @_;
3202
3203     # Returns nothing.
3204
3205     my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
3206
3207     $mask = 1 if not $options->{ "no_mask" };
3208
3209     $tmp_file = "$BP_TMP/write_2bit.fna";
3210     $fh_tmp   = Maasha::Common::write_open( $tmp_file );
3211
3212     $fh_out = write_stream( $options->{ "data_out" } );
3213
3214     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3215     {
3216         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
3217             Maasha::Fasta::put_entry( $entry, $fh_tmp );
3218         }
3219
3220         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3221     }
3222
3223     close $fh_tmp;
3224
3225     $fh_in = Maasha::Common::read_open( $tmp_file );
3226
3227     Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3228
3229     close $fh_in;
3230     close $fh_out;
3231
3232     unlink $tmp_file;
3233 }
3234
3235
3236 sub script_write_solid
3237 {
3238     # Martin A. Hansen, April 2008.
3239
3240     # Write di-base encoded Solid sequence from entries in stream.
3241
3242     my ( $in,        # handle to in stream
3243          $out,       # handle to out stream
3244          $options,   # options hash
3245        ) = @_;
3246
3247     # Returns nothing.
3248
3249     my ( $record, $fh, $entry );
3250
3251     $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3252
3253     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3254     {
3255         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3256         {
3257             $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
3258
3259             Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3260         }
3261
3262         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3263     }
3264
3265     close $fh;
3266 }
3267
3268
3269 sub script_write_ucsc_config
3270 {
3271     # Martin A. Hansen, November 2008.
3272
3273     # Write UCSC Genome Broser configuration (.ra file type) from
3274     # records in the stream.
3275
3276     my ( $in,        # handle to in stream
3277          $out,       # handle to out stream
3278          $options,   # options hash
3279        ) = @_;
3280
3281     # Returns nothing.
3282
3283     my ( $record, $fh );
3284
3285     $fh = write_stream( $options->{ "data_out" } );
3286
3287     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3288     {
3289         Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
3290
3291         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3292     }
3293
3294     close $fh;
3295 }
3296
3297
3298 sub script_plot_seqlogo
3299 {
3300     # Martin A. Hansen, August 2007.
3301
3302     # Calculates and writes a sequence logo for alignments.
3303
3304     my ( $in,        # handle to in stream
3305          $out,       # handle to out stream
3306          $options,   # options hash
3307        ) = @_;
3308
3309     # Returns nothing.
3310
3311     my ( $record, @entries, $logo, $fh );
3312
3313     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3314     {
3315         if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3316             push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3317         }
3318
3319         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3320     }
3321
3322     $logo = Maasha::Plot::seq_logo( \@entries );
3323
3324     $fh = write_stream( $options->{ "data_out" } );
3325
3326     print $fh $logo;
3327
3328     close $fh;
3329 }
3330
3331
3332 sub script_plot_phastcons_profiles
3333 {
3334     # Martin A. Hansen, January 2008.
3335
3336     # Plots PhastCons profiles.
3337
3338     my ( $in,        # handle to in stream
3339          $out,       # handle to out stream
3340          $options,   # options hash
3341        ) = @_;
3342
3343     # Returns nothing.
3344
3345     my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
3346
3347     $options->{ "title" } ||= "PhastCons Profiles";
3348
3349     $phastcons_file  = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3350     $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3351
3352     $index           = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3353     $fh_phastcons    = Maasha::Common::read_open( $phastcons_file );
3354
3355     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3356     {
3357         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3358         {
3359             $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
3360                                                                                    $record->{ "CHR_BEG" },
3361                                                                                    $record->{ "CHR_END" },
3362                                                                                    $options->{ "flank" } );
3363
3364             push @{ $AoA }, [ @{ $scores } ];
3365         }
3366
3367         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3368     }
3369
3370     Maasha::UCSC::phastcons_normalize( $AoA );
3371
3372     $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
3373     $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
3374
3375     $AoA = Maasha::Matrix::matrix_flip( $AoA );
3376
3377     $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
3378
3379     $fh = write_stream( $options->{ "data_out" } );
3380
3381     print $fh "$_\n" foreach @{ $plot };
3382
3383     close $fh;
3384 }
3385
3386
3387 sub script_analyze_bed
3388 {
3389     # Martin A. Hansen, March 2008.
3390
3391     # Analyze BED entries in stream.
3392
3393     my ( $in,        # handle to in stream
3394          $out,       # handle to out stream
3395          $options,   # options hash
3396        ) = @_;
3397
3398     # Returns nothing.
3399
3400     my ( $record );
3401
3402     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3403     {
3404         $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
3405
3406         Maasha::Biopieces::put_record( $record, $out );
3407     }
3408 }
3409
3410
3411 sub script_analyze_vals
3412 {
3413     # Martin A. Hansen, August 2007.
3414
3415     # Analyze values for given keys in stream.
3416
3417     my ( $in,        # handle to in stream
3418          $out,       # handle to out stream
3419          $options,   # options hash
3420        ) = @_;
3421
3422     # Returns nothing.
3423
3424     my ( $record, $key, @keys, %key_hash, $analysis, $len );
3425
3426     map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
3427
3428     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3429     {
3430         foreach $key ( keys %{ $record } )
3431         {
3432             next if $options->{ "keys" } and not exists $key_hash{ $key };
3433
3434             $analysis->{ $key }->{ "COUNT" }++;
3435
3436             if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
3437             {
3438                 $analysis->{ $key }->{ "TYPE" } = "num";
3439                 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
3440                 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3441                 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
3442             }
3443             else
3444             {
3445                 $len = length $record->{ $key };
3446
3447                 $analysis->{ $key }->{ "TYPE" } = "alph";
3448                 $analysis->{ $key }->{ "SUM" } += $len;
3449                 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3450                 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
3451             }
3452         }
3453
3454         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3455     }
3456
3457     foreach $key ( keys %{ $analysis } )
3458     {
3459         $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
3460         $analysis->{ $key }->{ "SUM" }  = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
3461     }
3462
3463     my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
3464
3465     $keys   = "KEY  ";
3466     $types  = "TYPE ";
3467     $counts = "COUNT";
3468     $mins   = "MIN  ";
3469     $maxs   = "MAX  ";
3470     $sums   = "SUM  ";
3471     $means  = "MEAN ";
3472
3473     if ( $options->{ "keys" } ) {
3474         @keys = @{ $options->{ "keys" } };
3475     } else {
3476         @keys = keys %{ $analysis };
3477     }
3478
3479     foreach $key ( @keys )
3480     {
3481         $keys   .= sprintf "% 15s", $key;
3482         $types  .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
3483         $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
3484         $mins   .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
3485         $maxs   .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
3486         $sums   .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
3487         $means  .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
3488     }
3489
3490     print $out "$keys\n";
3491     print $out "$types\n";
3492     print $out "$counts\n";
3493     print $out "$mins\n";
3494     print $out "$maxs\n";
3495     print $out "$sums\n";
3496     print $out "$means\n";
3497 }
3498
3499
3500 sub script_head_records
3501 {
3502     # Martin A. Hansen, August 2007.
3503
3504     # Display the first sequences in stream.
3505
3506     my ( $in,        # handle to in stream
3507          $out,       # handle to out stream
3508          $options,   # options hash
3509        ) = @_;
3510
3511     # Returns nothing.
3512
3513     my ( $record, $count );
3514
3515     $options->{ "num" } ||= 10;
3516
3517     $count = 0;
3518
3519     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3520     {
3521         $count++;
3522
3523         Maasha::Biopieces::put_record( $record, $out );
3524
3525         last if $count == $options->{ "num" };
3526     }
3527 }
3528
3529
3530 sub script_remove_keys
3531 {
3532     # Martin A. Hansen, August 2007.
3533
3534     # Remove keys from stream.
3535
3536     my ( $in,        # handle to in stream
3537          $out,       # handle to out stream
3538          $options,   # options hash
3539        ) = @_;
3540
3541     # Returns nothing.
3542
3543     my ( $record, $new_record );
3544
3545     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3546     {
3547         if ( $options->{ "keys" } )
3548         {
3549             map { delete $record->{ $_ } } @{ $options->{ "keys" } };
3550         }
3551         elsif ( $options->{ "save_keys" } )
3552         {
3553             map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
3554
3555             $record = $new_record;
3556         }
3557
3558         Maasha::Biopieces::put_record( $record, $out ) if keys %{ $record };
3559     }
3560 }
3561
3562
3563 sub script_remove_adaptor
3564 {
3565     # Martin A. Hansen, August 2008.
3566
3567     # Find and remove adaptor from sequences in the stream.
3568
3569     my ( $in,        # handle to in stream
3570          $out,       # handle to out stream
3571          $options,   # options hash
3572        ) = @_;
3573
3574     # Returns nothing.
3575
3576     my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
3577
3578     $options->{ "remove" } ||= "after";
3579
3580     $max_mismatch = $options->{ "mismatches" } || 0;
3581     $offset       = $options->{ "offset" };
3582
3583     if ( not defined $offset ) {
3584         $offset = 0;
3585     } else {
3586         $offset--;
3587     }
3588
3589     $adaptor      = uc $options->{ "adaptor" };
3590     $adaptor_len  = length $adaptor;
3591
3592     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3593     {
3594         if ( $record->{ "SEQ" } )
3595         {
3596             $seq     = uc $record->{ "SEQ" };
3597             $seq_len = length $seq;
3598
3599             $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
3600
3601             $record->{ "ADAPTOR_POS" } = $pos;
3602
3603             if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
3604             {
3605                 if ( $options->{ "remove" } eq "after" )
3606                 {
3607                     $record->{ "SEQ" }     = substr $record->{ "SEQ" }, 0, $pos;
3608                     $record->{ "SEQ_LEN" } = $pos;
3609                 }
3610                 else
3611                 {
3612                     $record->{ "SEQ" }     = substr $record->{ "SEQ" }, $pos + $adaptor_len;
3613                     $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3614                 }
3615             }
3616
3617             Maasha::Biopieces::put_record( $record, $out );
3618         }
3619         else
3620         {
3621             Maasha::Biopieces::put_record( $record, $out );
3622         }
3623     }
3624 }
3625
3626
3627 sub script_remove_mysql_tables
3628 {
3629     # Martin A. Hansen, November 2008.
3630
3631     # Remove MySQL tables from values in stream.
3632
3633     my ( $in,        # handle to in stream
3634          $out,       # handle to out stream
3635          $options,   # options hash
3636        ) = @_;
3637
3638     # Returns nothing.
3639
3640     my ( $record, %table_hash, $dbh, $table );
3641
3642     $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
3643     $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
3644
3645     map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
3646
3647     while ( $record = Maasha::Biopieces::get_record( $in ) )
3648     {
3649         map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3650
3651         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3652     }
3653
3654     $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3655
3656     foreach $table ( sort keys %table_hash )
3657     {
3658         if ( Maasha::SQL::table_exists( $dbh, $table ) )
3659         {
3660             print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3661             Maasha::SQL::delete_table( $dbh, $table );
3662             print STDERR "done.\n" if $options->{ 'verbose' };
3663         }
3664         else
3665         {
3666             print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
3667         }
3668     }
3669
3670     Maasha::SQL::disconnect( $dbh );
3671 }
3672
3673
3674 sub script_remove_ucsc_tracks
3675 {
3676     # Martin A. Hansen, November 2008.
3677
3678     # Remove track from MySQL tables and config file.
3679
3680     my ( $in,        # handle to in stream
3681          $out,       # handle to out stream
3682          $options,   # options hash
3683        ) = @_;
3684
3685     # Returns nothing.
3686
3687     my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
3688
3689     $options->{ 'user' }        ||= Maasha::UCSC::ucsc_get_user();
3690     $options->{ 'password' }    ||= Maasha::UCSC::ucsc_get_password();
3691     $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
3692
3693     map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
3694
3695     while ( $record = Maasha::Biopieces::get_record( $in ) )
3696     {
3697         map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3698
3699         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3700     }
3701
3702     $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
3703     
3704     while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
3705         push @tracks, $track;
3706     }
3707
3708     close $fh_in;
3709
3710     foreach $track ( @tracks )
3711     {
3712         if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
3713             print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
3714         } else {
3715             push @new_tracks, $track;
3716         }
3717     }
3718
3719     rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
3720
3721     $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
3722
3723     map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
3724
3725     close $fh_out;
3726
3727     # ---- locate track in database ----
3728
3729     $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3730
3731     foreach $track ( sort keys %track_hash )
3732     {
3733         if ( Maasha::SQL::table_exists( $dbh, $track ) )
3734         {
3735             print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3736             Maasha::SQL::delete_table( $dbh, $track );
3737             print STDERR "done.\n" if $options->{ 'verbose' };
3738         }
3739         else
3740         {
3741             print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
3742         }
3743     }
3744
3745     Maasha::SQL::disconnect( $dbh );
3746
3747     Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
3748 }
3749
3750
3751 sub script_rename_keys
3752 {
3753     # Martin A. Hansen, August 2007.
3754
3755     # Rename keys in stream.
3756
3757     my ( $in,        # handle to in stream
3758          $out,       # handle to out stream
3759          $options,   # options hash
3760        ) = @_;
3761
3762     # Returns nothing.
3763
3764     my ( $record );
3765
3766     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3767     {
3768         if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3769         {
3770             $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
3771
3772             delete $record->{ $options->{ "keys" }->[ 0 ] };
3773         }
3774
3775         Maasha::Biopieces::put_record( $record, $out );
3776     }
3777 }
3778
3779
3780 sub script_uniq_vals
3781 {
3782     # Martin A. Hansen, August 2007.
3783
3784     # Find unique values in stream.
3785
3786     my ( $in,        # handle to in stream
3787          $out,       # handle to out stream
3788          $options,   # options hash
3789        ) = @_;
3790
3791     # Returns nothing.
3792
3793     my ( %hash, $record );
3794
3795     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3796     {
3797         if ( $record->{ $options->{ "key" } } )
3798         {
3799             if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
3800             {
3801                 Maasha::Biopieces::put_record( $record, $out );
3802
3803                 $hash{ $record->{ $options->{ "key" } } } = 1;
3804             }
3805             elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
3806             {
3807                 Maasha::Biopieces::put_record( $record, $out );
3808             }
3809             else
3810             {
3811                 $hash{ $record->{ $options->{ "key" } } } = 1;
3812             }
3813         }
3814         else
3815         {
3816             Maasha::Biopieces::put_record( $record, $out );
3817         }
3818     }
3819 }
3820
3821
3822 sub script_merge_vals
3823 {
3824     # Martin A. Hansen, August 2007.
3825
3826     # Rename keys in stream.
3827
3828     my ( $in,        # handle to in stream
3829          $out,       # handle to out stream
3830          $options,   # options hash
3831        ) = @_;
3832
3833     # Returns nothing.
3834
3835     my ( $record, @join, $i );
3836
3837     $options->{ "delimit" } ||= '_';
3838
3839     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3840     {
3841         if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3842         {
3843             @join = $record->{ $options->{ "keys" }->[ 0 ] };
3844             
3845             for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
3846                 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
3847             }
3848
3849             $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
3850         }
3851
3852         Maasha::Biopieces::put_record( $record, $out );
3853     }
3854 }
3855
3856
3857 sub script_merge_records
3858 {
3859     # Martin A. Hansen, July 2008.
3860
3861     # Merges records in the stream based on identical values of two given keys.
3862
3863     my ( $in,        # handle to in stream
3864          $out,       # handle to out stream
3865          $options,   # options hash
3866        ) = @_;
3867
3868     # Returns nothing.
3869
3870     my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
3871          $num1, $num2, $num, $cmp, $i );
3872
3873     $merge = $options->{ "merge" } || "AandB";
3874
3875     $file1 = "$BP_TMP/merge_records1.tmp";
3876     $file2 = "$BP_TMP/merge_records2.tmp";
3877
3878     $fh1   = Maasha::Common::write_open( $file1 );
3879     $fh2   = Maasha::Common::write_open( $file2 );
3880
3881     $key1  = $options->{ "keys" }->[ 0 ];
3882     $key2  = $options->{ "keys" }->[ 1 ];
3883
3884     $num   = $key2 =~ s/n$//;
3885     $num1  = 0;
3886     $num2  = 0;
3887
3888     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
3889     {
3890         if ( exists $record->{ $key1 } )
3891         {
3892             @keys1 = $key1;
3893             @vals1 = $record->{ $key1 };
3894
3895             delete $record->{ $key1 };
3896
3897             map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
3898
3899             print $fh1 join( "\t", @vals1 ), "\n";
3900
3901             $num1++;
3902         }
3903         elsif ( exists $record->{ $key2 } )
3904         {
3905             @keys2 = $key2;
3906             @vals2 = $record->{ $key2 };
3907
3908             delete $record->{ $key2 };
3909
3910             map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
3911
3912             print $fh2 join( "\t", @vals2 ), "\n";
3913
3914             $num2++;
3915         }
3916     }
3917
3918     close $fh1;
3919     close $fh2;
3920
3921     if ( $num )
3922     {
3923         Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3924         Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3925     }
3926     else
3927     {
3928         Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3929         Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3930     }
3931
3932     $fh1 = Maasha::Common::read_open( $file1 );
3933     $fh2 = Maasha::Common::read_open( $file2 );
3934
3935     @vals1 = Maasha::Common::get_fields( $fh1 );
3936     @vals2 = Maasha::Common::get_fields( $fh2 );
3937
3938     while ( $num1 > 0 and $num2 > 0 )
3939     {
3940         undef $record;
3941
3942         if ( $num ) {
3943             $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
3944         } else {
3945             $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
3946         }
3947
3948         if ( $cmp < 0 )
3949         {
3950             if ( $merge =~ /^(AorB|AnotB)$/ )
3951             {
3952                 for ( $i = 0; $i < @keys1; $i++ ) {
3953                     $record->{ $keys1[ $i ] } = $vals1[ $i ];
3954                 }
3955
3956                 Maasha::Biopieces::put_record( $record, $out );
3957             }
3958
3959             @vals1 = Maasha::Common::get_fields( $fh1 );
3960             $num1--;
3961         }
3962         elsif ( $cmp > 0 )
3963         {
3964             if ( $merge =~ /^(BorA|BnotA)$/ )
3965             {
3966                 for ( $i = 0; $i < @keys2; $i++ ) {
3967                     $record->{ $keys2[ $i ] } = $vals2[ $i ];
3968                 }
3969
3970                 Maasha::Biopieces::put_record( $record, $out );
3971             }
3972
3973             @vals2 = Maasha::Common::get_fields( $fh2 );
3974             $num2--;
3975         }
3976         else
3977         {
3978             if ( $merge =~ /^(AandB|AorB|BorA)$/ )
3979             {
3980                 for ( $i = 0; $i < @keys1; $i++ ) {
3981                     $record->{ $keys1[ $i ] } = $vals1[ $i ];
3982                 }
3983
3984                 for ( $i = 1; $i < @keys2; $i++ ) {
3985                     $record->{ $keys2[ $i ] } = $vals2[ $i ];
3986                 }
3987             
3988                 Maasha::Biopieces::put_record( $record, $out );
3989             }
3990
3991             @vals1 = Maasha::Common::get_fields( $fh1 );
3992             @vals2 = Maasha::Common::get_fields( $fh2 );
3993             $num1--;
3994             $num2--;
3995         }
3996     }
3997
3998     close $fh1;
3999     close $fh2;
4000
4001     unlink $file1;
4002     unlink $file2;
4003
4004     if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4005     {
4006         undef $record;
4007
4008         for ( $i = 0; $i < @keys1; $i++ ) {
4009             $record->{ $keys1[ $i ] } = $vals1[ $i ];
4010         }
4011
4012         Maasha::Biopieces::put_record( $record, $out );
4013     }
4014
4015     if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4016     {
4017         undef $record;
4018
4019         for ( $i = 0; $i < @keys2; $i++ ) {
4020             $record->{ $keys2[ $i ] } = $vals2[ $i ];
4021         }
4022
4023         Maasha::Biopieces::put_record( $record, $out );
4024     }
4025 }
4026
4027
4028 sub script_compute
4029 {
4030     # Martin A. Hansen, August 2007.
4031
4032     # Evaluate extression for records in stream.
4033
4034     my ( $in,        # handle to in stream
4035          $out,       # handle to out stream
4036          $options,   # options hash
4037        ) = @_;
4038
4039     # Returns nothing.
4040
4041     my ( $record, $eval_key, @keys, $eval_val );
4042
4043     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4044     {
4045         if ( $options->{ "eval" } )
4046         {
4047             if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
4048             {
4049                 $eval_key = $1;
4050                 $eval_val = $2;
4051
4052                 if ( not @keys )
4053                 {
4054                     @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
4055
4056                     @keys = grep { exists $record->{ $_ } } @keys;
4057                 }
4058
4059                 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
4060
4061                 $record->{ $eval_key } = eval "$eval_val";
4062                 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
4063             }
4064             else
4065             {
4066                 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
4067             }
4068         } 
4069
4070         Maasha::Biopieces::put_record( $record, $out );
4071     }
4072 }
4073
4074
4075 sub script_flip_tab
4076 {
4077     # Martin A. Hansen, June 2008.
4078
4079     # Flip a table.
4080
4081     my ( $in,        # handle to in stream
4082          $out,       # handle to out stream
4083          $options,   # options hash
4084        ) = @_;
4085
4086     # Returns nothing.
4087
4088     my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4089
4090     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4091     {
4092         undef @rows;
4093
4094         foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4095         {
4096             push @rows, $record->{ $key };
4097
4098         }
4099
4100         push @matrix, [ @rows ];
4101     }
4102
4103     undef $record;
4104
4105     @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4106
4107     foreach $row ( @matrix )
4108     {
4109         for ( $i = 0; $i < @{ $row }; $i++ ) {
4110             $record->{ "V$i" } = $row->[ $i ];
4111         }
4112
4113         Maasha::Biopieces::put_record( $record, $out );
4114     }
4115 }
4116
4117
4118 sub script_count_records
4119 {
4120     # Martin A. Hansen, August 2007.
4121
4122     # Count records in stream.
4123
4124     my ( $in,        # handle to in stream
4125          $out,       # handle to out stream
4126          $options,   # options hash
4127        ) = @_;
4128
4129     # Returns nothing.
4130
4131     my ( $record, $count, $result, $fh, $line );
4132
4133     $count = 0;
4134
4135     if ( $options->{ "no_stream" } )
4136     {
4137         while ( $line = <$in> )
4138         {
4139             chomp $line;
4140
4141             $count++ if $line eq "---";
4142         }
4143     }
4144     else
4145     {
4146         while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4147         {
4148             Maasha::Biopieces::put_record( $record, $out );
4149
4150             $count++;
4151         }
4152     }
4153
4154     $result = { "RECORDS_COUNT" => $count };
4155
4156     $fh = write_stream( $options->{ "data_out" } );
4157
4158     Maasha::Biopieces::put_record( $result, $fh );
4159
4160     close $fh;
4161 }
4162
4163
4164 sub script_random_records
4165 {
4166     # Martin A. Hansen, August 2007.
4167
4168     # Pick a number or random records from stream.
4169
4170     my ( $in,        # handle to in stream
4171          $out,       # handle to out stream
4172          $options,   # options hash
4173        ) = @_;
4174
4175     # Returns nothing.
4176
4177     my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4178
4179     $options->{ "num" } ||= 10;
4180
4181     $tmp_file = "$BP_TMP/random_records.tmp";
4182
4183     $fh_out = Maasha::Common::write_open( $tmp_file );
4184
4185     $count = 0;
4186
4187     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4188     {
4189         Maasha::Biopieces::put_record( $record, $fh_out );
4190
4191         $count++;
4192     }
4193
4194     close $fh_out;
4195
4196     $max = 0;
4197     $i   = 0;
4198
4199     Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4200
4201     while ( $i < $options->{ "num" } )
4202     {
4203         $rand = int( rand( $count ) );
4204     
4205         if ( not exists $rand_hash{ $rand } )
4206         {
4207             $rand_hash{ $rand } = 1;
4208
4209             $max = $rand if $rand > $max;
4210
4211             $i++;
4212         }
4213     }
4214
4215     $fh_in = Maasha::Common::read_open( $tmp_file );
4216
4217     $count = 0;
4218
4219     while ( $record = Maasha::Biopieces::get_record( $fh_in ) ) 
4220     {
4221         Maasha::Biopieces::put_record( $record, $out ) if exists $rand_hash{ $count };
4222
4223         last if $count == $max;
4224
4225         $count++;
4226     }
4227
4228     close $fh_in;
4229
4230     unlink $tmp_file;
4231 }
4232
4233
4234 sub script_sort_records
4235 {
4236     # Martin A. Hansen, August 2007.
4237
4238     # Sort to sort records according to keys.
4239
4240     my ( $in,        # handle to in stream
4241          $out,       # handle to out stream
4242          $options,   # options hash
4243        ) = @_;
4244
4245     # Returns nothing.
4246
4247     my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4248
4249     foreach $key ( @{ $options->{ "keys" } } )
4250     {
4251         if ( $key =~ s/n$// ) {
4252             push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4253         } else {
4254             push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4255         }
4256     }
4257
4258     $sort_str = join " or ", @sort_cmd;
4259     $sort_sub = eval "sub { $sort_str }";   # NB security issue!
4260
4261     while ( $record = Maasha::Biopieces::get_record( $in ) ) {
4262         push @records, $record;
4263     }
4264
4265     @records = sort $sort_sub @records;
4266
4267     if ( $options->{ "reverse" } )
4268     {
4269         for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4270             Maasha::Biopieces::put_record( $records[ $i ], $out );
4271         }
4272     }
4273     else
4274     {
4275         for ( $i = 0; $i < scalar @records; $i++ ) {
4276             Maasha::Biopieces::put_record( $records[ $i ], $out );
4277         }
4278     }
4279 }
4280
4281
4282 sub script_count_vals
4283 {
4284     # Martin A. Hansen, August 2007.
4285
4286     # Count records in stream.
4287
4288     my ( $in,        # handle to in stream
4289          $out,       # handle to out stream
4290          $options,   # options hash
4291        ) = @_;
4292
4293     # Returns nothing.
4294
4295     my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4296
4297     $tmp_file = "$BP_TMP/count_cache.tmp";
4298
4299     $fh_out   = Maasha::Common::write_open( $tmp_file );
4300
4301     $cache    = 0;
4302     $num      = 0;
4303
4304     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4305     {
4306         map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4307
4308         push @records, $record;
4309
4310         if ( scalar @records > 5_000_000 )   # too many records to hold in memory - use disk cache
4311         {
4312             map { Maasha::Biopieces::put_record( $_, $fh_out ) } @records;
4313
4314             undef @records;
4315
4316             $cache = 1;
4317         }
4318
4319         print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4320
4321         $num++;
4322     }
4323
4324     close $fh_out;
4325
4326     if ( $cache )
4327     {
4328         $num   = 0;
4329
4330         $fh_in = Maasha::Common::read_open( $tmp_file );
4331
4332         while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4333         {
4334             map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4335
4336             Maasha::Biopieces::put_record( $record, $out );
4337
4338             print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4339
4340             $num++;
4341         }
4342     
4343         close $fh_in;
4344     }
4345
4346     foreach $record ( @records )
4347     {
4348         map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4349
4350         Maasha::Biopieces::put_record( $record, $out );
4351     }
4352
4353     unlink $tmp_file;
4354 }
4355
4356
4357 sub script_plot_histogram
4358 {
4359     # Martin A. Hansen, September 2007.
4360
4361     # Plot a simple histogram for a given key using GNU plot.
4362
4363     my ( $in,        # handle to in stream
4364          $out,       # handle to out stream
4365          $options,   # options hash
4366        ) = @_;
4367
4368     # Returns nothing.
4369
4370     my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4371
4372     $options->{ "title" } ||= "Histogram";
4373     $options->{ "sort" }  ||= "num";
4374
4375     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4376     {
4377         $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4378
4379         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4380     }
4381
4382     if ( $options->{ "sort" } eq "num" ) {
4383         map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4384     } else {
4385         map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4386     }
4387
4388     $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4389
4390     $fh = write_stream( $options->{ "data_out" } );
4391
4392     print $fh "$_\n" foreach @{ $result };
4393
4394     close $fh;
4395 }
4396
4397
4398 sub script_plot_lendist
4399 {
4400     # Martin A. Hansen, August 2007.
4401
4402     # Plot length distribution using GNU plot.
4403
4404     my ( $in,        # handle to in stream
4405          $out,       # handle to out stream
4406          $options,   # options hash
4407        ) = @_;
4408
4409     # Returns nothing.
4410
4411     my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4412
4413     $options->{ "title" } ||= "Length Distribution";
4414
4415     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4416     {
4417         $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4418
4419         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4420     }
4421
4422     $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4423
4424     for ( $i = 0; $i < $max; $i++ ) {
4425         push @data_list, [ $i, $data_hash{ $i } || 0 ];
4426     }
4427
4428     $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4429
4430     $fh = write_stream( $options->{ "data_out" } );
4431
4432     print $fh "$_\n" foreach @{ $result };
4433
4434     close $fh;
4435 }
4436
4437
4438 sub script_plot_chrdist
4439 {
4440     # Martin A. Hansen, August 2007.
4441
4442     # Plot chromosome distribution using GNU plot.
4443
4444     my ( $in,        # handle to in stream
4445          $out,       # handle to out stream
4446          $options,   # options hash
4447        ) = @_;
4448
4449     # Returns nothing.
4450
4451     my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
4452
4453     $options->{ "title" } ||= "Chromosome Distribution";
4454
4455     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4456     {
4457         if ( $record->{ "CHR" } ) {                                                             # generic
4458             $data_hash{ $record->{ "CHR" } }++;
4459         } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) {   # patscan
4460             $data_hash{ $record->{ "S_ID" } }++;
4461         } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) {       # BLAT / PSL
4462             $data_hash{ $record->{ "S_ID" } }++;
4463         } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) {     # BLAST
4464             $data_hash{ $record->{ "S_ID" } }++;
4465         }
4466
4467         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4468     }
4469
4470     foreach $elem ( keys %data_hash )
4471     {
4472         $sort_key = $elem;
4473
4474         $sort_key =~ s/chr//i;
4475     
4476         $sort_key =~ s/^X(.*)/99$1/;
4477         $sort_key =~ s/^Y(.*)/99$1/;
4478         $sort_key =~ s/^Z(.*)/999$1/;
4479         $sort_key =~ s/^M(.*)/9999$1/;
4480         $sort_key =~ s/^U(.*)/99999$1/;
4481
4482         $count = $sort_key =~ tr/_//;
4483
4484         $sort_key =~ s/_.*/"999999" x $count/ex;
4485
4486         push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
4487     }
4488
4489     @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
4490
4491     $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
4492
4493     $fh = write_stream( $options->{ "data_out" } );
4494
4495     print $fh "$_\n" foreach @{ $result };
4496
4497     close $fh;
4498 }
4499
4500
4501 sub script_plot_karyogram
4502 {
4503     # Martin A. Hansen, August 2007.
4504
4505     # Plot hits on karyogram.
4506
4507     my ( $in,        # handle to in stream
4508          $out,       # handle to out stream
4509          $options,   # options hash
4510        ) = @_;
4511
4512     # Returns nothing.
4513
4514     my ( %options, $record, @data, $fh, $result, %data_hash );
4515
4516     $options->{ "genome" }     ||= "human";
4517     $options->{ "feat_color" } ||= "black";
4518
4519     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4520     {
4521         if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4522         {
4523             push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
4524         }
4525
4526         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4527     }
4528
4529     $result = Maasha::Plot::karyogram( \%data_hash, \%options );
4530
4531     $fh = write_stream( $options->{ "data_out" } );
4532
4533     print $fh $result;
4534
4535     close $fh;
4536 }
4537
4538
4539 sub script_plot_matches
4540 {
4541     # Martin A. Hansen, August 2007.
4542
4543     # Plot matches in 2D generating a dotplot.
4544
4545     my ( $in,        # handle to in stream
4546          $out,       # handle to out stream
4547          $options,   # options hash
4548        ) = @_;
4549
4550     # Returns nothing.
4551
4552     my ( $record, @data, $fh, $result, %data_hash );
4553
4554     $options->{ "direction" } ||= "both";
4555
4556     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4557     {
4558         if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
4559             push @data, $record;
4560         }
4561
4562         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4563     }
4564
4565     $options->{ "title" }  ||= "plot_matches";
4566     $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
4567     $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
4568
4569     $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
4570
4571     $fh = write_stream( $options->{ "data_out" } );
4572
4573     print $fh "$_\n" foreach @{ $result };
4574
4575     close $fh;
4576 }
4577
4578
4579 sub script_length_vals
4580 {
4581     # Martin A. Hansen, August 2007.
4582
4583     # Determine the length of the value for given keys.
4584
4585     my ( $in,        # handle to in stream
4586          $out,       # handle to out stream
4587          $options,   # options hash
4588        ) = @_;
4589
4590     # Returns nothing.
4591
4592     my ( $record, $key );
4593
4594     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4595     {
4596         foreach $key ( @{ $options->{ "keys" } } )
4597         {
4598             if ( $record->{ $key } ) {
4599                 $record->{ $key . "_LEN" } = length $record->{ $key };
4600             }
4601         }
4602
4603         Maasha::Biopieces::put_record( $record, $out );
4604     }
4605 }
4606
4607
4608 sub script_sum_vals
4609 {
4610     # Martin A. Hansen, August 2007.
4611
4612     # Calculates the sums for values of given keys.
4613
4614     my ( $in,        # handle to in stream
4615          $out,       # handle to out stream
4616          $options,   # options hash
4617        ) = @_;
4618
4619     # Returns nothing.
4620
4621     my ( $record, $key, %sum_hash, $fh );
4622
4623     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4624     {
4625         foreach $key ( @{ $options->{ "keys" } } )
4626         {
4627             if ( $record->{ $key } ) {
4628                 $sum_hash{ $key } += $record->{ $key };
4629             }
4630         }
4631
4632         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4633     }
4634
4635     $fh = write_stream( $options->{ "data_out" } );
4636
4637     foreach $key ( @{ $options->{ "keys" } } ) {
4638         Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
4639     }
4640
4641     close $fh;
4642 }
4643
4644
4645 sub script_mean_vals
4646 {
4647     # Martin A. Hansen, August 2007.
4648
4649     # Calculate the mean of values of given keys.
4650
4651     my ( $in,        # handle to in stream
4652          $out,       # handle to out stream
4653          $options,   # options hash
4654        ) = @_;
4655
4656     # Returns nothing.
4657
4658     my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
4659
4660     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4661     {
4662         foreach $key ( @{ $options->{ "keys" } } )
4663         {
4664             if ( $record->{ $key } )
4665             {
4666                 $sum_hash{ $key } += $record->{ $key };
4667                 $count_hash{ $key }++;
4668             }
4669         }
4670
4671         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4672     }
4673
4674     $fh = write_stream( $options->{ "data_out" } );
4675
4676     foreach $key ( @{ $options->{ "keys" } } )
4677     {
4678         if ( $count_hash{ $key } ) {
4679             $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
4680         } else {
4681             $mean = "N/A";
4682         }
4683
4684         Maasha::Biopieces::put_record( { $key . "_MEAN" => $mean } , $fh );
4685     }
4686
4687     close $fh;
4688 }
4689
4690
4691 sub script_median_vals
4692 {
4693     # Martin A. Hansen, March 2008.
4694
4695     # Calculate the median values of given keys.
4696
4697     my ( $in,        # handle to in stream
4698          $out,       # handle to out stream
4699          $options,   # options hash
4700        ) = @_;
4701
4702     # Returns nothing.
4703
4704     my ( $record, $key, %median_hash, $median, $fh );
4705
4706     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4707     {
4708         foreach $key ( @{ $options->{ "keys" } } ) {
4709             push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
4710         }
4711
4712         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4713     }
4714
4715     $fh = write_stream( $options->{ "data_out" } );
4716
4717     foreach $key ( @{ $options->{ "keys" } } )
4718     {
4719         if ( $median_hash{ $key } ) {
4720             $median = Maasha::Calc::median( $median_hash{ $key } );
4721         } else {
4722             $median = "N/A";
4723         }
4724
4725         Maasha::Biopieces::put_record( { $key . "_MEDIAN" => $median } , $fh );
4726     }
4727
4728     close $fh;
4729 }
4730
4731
4732 sub script_max_vals
4733 {
4734     # Martin A. Hansen, February 2008.
4735
4736     # Determine the maximum values of given keys.
4737
4738     my ( $in,        # handle to in stream
4739          $out,       # handle to out stream
4740          $options,   # options hash
4741        ) = @_;
4742
4743     # Returns nothing.
4744
4745     my ( $record, $key, $fh, %max_hash, $max_record );
4746
4747     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4748     {
4749         foreach $key ( @{ $options->{ "keys" } } )
4750         {
4751             if ( $record->{ $key } )
4752             {
4753                 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
4754             }
4755         }
4756
4757         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4758     }
4759
4760     $fh = write_stream( $options->{ "data_out" } );
4761
4762     foreach $key ( @{ $options->{ "keys" } } )
4763     {
4764         $max_record->{ $key . "_MAX" } = $max_hash{ $key };
4765     }
4766
4767     Maasha::Biopieces::put_record( $max_record, $fh );
4768
4769     close $fh;
4770 }
4771
4772
4773 sub script_min_vals
4774 {
4775     # Martin A. Hansen, February 2008.
4776
4777     # Determine the minimum values of given keys.
4778
4779     my ( $in,        # handle to in stream
4780          $out,       # handle to out stream
4781          $options,   # options hash
4782        ) = @_;
4783
4784     # Returns nothing.
4785
4786     my ( $record, $key, $fh, %min_hash, $min_record );
4787
4788     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4789     {
4790         foreach $key ( @{ $options->{ "keys" } } )
4791         {
4792             if ( defined $record->{ $key } )
4793             {
4794                 if ( exists $min_hash{ $key } ) {
4795                     $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
4796                 } else {
4797                     $min_hash{ $key } = $record->{ $key }; 
4798                 }
4799             }
4800         }
4801
4802         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4803     }
4804
4805     $fh = write_stream( $options->{ "data_out" } );
4806
4807     foreach $key ( @{ $options->{ "keys" } } )
4808     {
4809         $min_record->{ $key . "_MIN" } = $min_hash{ $key };
4810     }
4811
4812     Maasha::Biopieces::put_record( $min_record, $fh );
4813
4814     close $fh;
4815 }
4816
4817
4818 sub script_upload_to_ucsc
4819 {
4820     # Martin A. Hansen, August 2007.
4821
4822     # Calculate the mean of values of given keys.
4823
4824     # This routine has developed into the most ugly hack. Do something!
4825
4826     my ( $in,        # handle to in stream
4827          $out,       # handle to out stream
4828          $options,   # options hash
4829        ) = @_;
4830
4831     # Returns nothing.
4832
4833     my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
4834
4835     $options->{ "short_label" } ||= $options->{ 'table' };
4836     $options->{ "long_label" }  ||= $options->{ 'table' };
4837     $options->{ "group" }       ||= $ENV{ "LOGNAME" };
4838     $options->{ "priority" }    ||= 1;
4839     $options->{ "visibility" }  ||= "pack";
4840     $options->{ "color" }       ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
4841     $options->{ "chunk_size" }  ||= 10_000_000_000;    # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
4842
4843     $file   = "$BP_TMP/ucsc_upload.tmp";
4844     $append = 0;
4845     $first  = 1;
4846     $i      = 0;
4847
4848     $fh_out = Maasha::Common::write_open( $file );
4849
4850     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
4851     {
4852         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4853
4854         if ( $record->{ "REC_TYPE" } eq "fixed_step" )
4855         {
4856             $format = "WIGGLE";
4857
4858             if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4859                 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
4860             }
4861         }
4862         elsif ( $record->{ "REC_TYPE" } eq "PSL" )
4863         {
4864             $format = "PSL";
4865
4866             Maasha::UCSC::psl_put_header( $fh_out ) if $first;
4867             Maasha::UCSC::psl_put_entry( $record, $fh_out );
4868             
4869             $first = 0;
4870         }
4871         elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
4872         {
4873             # chrom chromStart  chromEnd    name    score   strand  size    secStr  conf 
4874
4875             $format  = "BED_SS";
4876
4877             print $fh_out join ( "\t",
4878                 $record->{ "CHR" },
4879                 $record->{ "CHR_BEG" },
4880                 $record->{ "CHR_END" } + 1,
4881                 $record->{ "Q_ID" },
4882                 $record->{ "SCORE" },
4883                 $record->{ "STRAND" },
4884                 $record->{ "SIZE" },
4885                 $record->{ "SEC_STRUCT" },
4886                 $record->{ "CONF" },
4887             ), "\n";
4888         }
4889         elsif ( $record->{ "REC_TYPE" } eq "BED" )
4890         {
4891             $format  = "BED";
4892             $columns = $record->{ "BED_COLS" };
4893
4894             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4895                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4896             }
4897         }
4898         elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
4899         {
4900             $format  = "BED";
4901             $columns = 6;
4902
4903             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4904                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4905             }
4906         }
4907         elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
4908         {
4909             $format  = "BED";
4910             $columns = 6;
4911
4912             $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
4913
4914             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4915                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4916             }
4917         }
4918         elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
4919         {
4920             $format  = "BED";
4921             $columns = 6;
4922
4923             if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4924                 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4925             }
4926         }
4927
4928         if ( $i == $options->{ "chunk_size" } )
4929         {
4930             close $fh_out;
4931
4932             if ( $format eq "BED" ) {
4933                 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4934             } elsif ( $format eq "PSL" ) {
4935                 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
4936             }
4937
4938             unlink $file;
4939
4940             $first = 1;
4941
4942             $append = 1;
4943
4944             $fh_out = Maasha::Common::write_open( $file );
4945         }
4946
4947         $i++;
4948     }
4949
4950     close $fh_out;
4951
4952     if ( exists $options->{ "database" } and $options->{ "table" } )
4953     {
4954         if ( $format eq "BED" )
4955         {
4956             $type = "bed $columns";
4957
4958             Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4959         }
4960         elsif ( $format eq "BED_SS" )
4961         {
4962             $type = "type bed 6 +";
4963         
4964             Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4965         }
4966         elsif ( $format eq "PSL" )
4967         {
4968             $type = "psl";
4969
4970             Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append ); 
4971         }
4972         elsif ( $format eq "WIGGLE" )
4973         {
4974             $options->{ "visibility" } = "full";
4975
4976             $wig_file = "$options->{ 'table' }.wig";
4977             $wib_file = "$options->{ 'table' }.wib";
4978
4979             $wib_dir  = "$ENV{ 'HOME' }/ucsc/wib";
4980
4981             Maasha::Common::dir_create_if_not_exists( $wib_dir );
4982
4983             if ( $options->{ 'verbose' } ) {
4984                 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
4985             } else {
4986                 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
4987             }
4988
4989             Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
4990
4991             unlink $file;
4992
4993             $file = $wig_file;
4994
4995             $type = "wig 0";
4996
4997             Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
4998         }
4999
5000         unlink $file;
5001
5002         Maasha::UCSC::ucsc_update_config( $options, $type );
5003     }
5004 }
5005
5006
5007 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5008
5009 1;
5010
5011 __END__
5012