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