1 package Maasha::BioRun;
4 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines that contains Biopieces which are run.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
37 use Maasha::Biopieces;
46 use Maasha::Stockholm;
52 use Maasha::UCSC::BED;
53 use Maasha::UCSC::Wiggle;
62 use vars qw( @ISA @EXPORT_OK );
66 @ISA = qw( Exporter );
74 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
77 my ( $script, $BP_TMP );
79 $script = Maasha::Common::get_scriptname();
80 $BP_TMP = Maasha::Common::get_tmpdir();
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
86 run_script( $script );
89 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
94 # Martin A. Hansen, August 2007.
96 # Run a specific script.
98 my ( $script, # script name
103 my ( $t0, $t1, $options, $in, $out );
105 Maasha::Biopieces::log_biopiece();
107 $t0 = gettimeofday();
109 $options = get_options( $script );
111 $options->{ "SCRIPT" } = $script;
113 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
115 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
116 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
118 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
119 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
120 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
121 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
122 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
123 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
124 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
125 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
126 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
127 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
128 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
129 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
130 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
131 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
132 elsif ( $script eq "read_ucsc_config" ) { script_read_ucsc_config( $in, $out, $options ) }
133 elsif ( $script eq "assemble_tag_contigs" ) { script_assemble_tag_contigs( $in, $out, $options ) }
134 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
135 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
136 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
137 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
138 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
139 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
140 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
141 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
142 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
143 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
144 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
145 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
146 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
147 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
148 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
149 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
150 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
151 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
152 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
153 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
154 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
155 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
156 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
157 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
158 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
159 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
160 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
161 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
162 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
163 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
164 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
165 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
166 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
167 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
168 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
169 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
170 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
171 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
172 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
173 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
174 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
175 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
176 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
177 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
178 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
179 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
180 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
181 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
182 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
183 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
184 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
185 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
186 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
187 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
188 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
189 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
190 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
191 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
192 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
193 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
194 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
195 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
196 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
197 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
198 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
199 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
201 close $in if defined $in;
204 $t1 = gettimeofday();
206 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
212 # Martin A. Hansen, February 2008.
214 # Gets options from commandline and checks these vigerously.
216 my ( $script, # name of script
221 my ( %options, @options, $opt, @genomes, $real );
223 if ( $script eq "print_usage" )
229 elsif ( $script eq "read_psl" )
236 elsif ( $script eq "read_bed" )
245 elsif ( $script eq "read_fixedstep" )
252 elsif ( $script eq "read_blast_tab" )
259 elsif ( $script eq "read_embl" )
269 elsif ( $script eq "read_stockholm" )
276 elsif ( $script eq "read_phastcons" )
287 elsif ( $script eq "read_soft" )
295 elsif ( $script eq "read_gff" )
302 elsif ( $script eq "read_2bit" )
310 elsif ( $script eq "read_solexa" )
319 elsif ( $script eq "read_solid" )
327 elsif ( $script eq "read_mysql" )
336 elsif ( $script eq "read_ucsc_config" )
343 elsif ( $script eq "assemble_tag_contigs" )
349 elsif ( $script eq "calc_fixedstep" )
355 elsif ( $script eq "length_seq" )
362 elsif ( $script eq "oligo_freq" )
369 elsif ( $script eq "create_weight_matrix" )
375 elsif ( $script eq "transliterate_seq" )
383 elsif ( $script eq "transliterate_vals" )
392 elsif ( $script eq "translate_seq" )
398 elsif ( $script eq "get_genome_align" )
409 elsif ( $script eq "get_genome_phastcons" )
420 elsif ( $script eq "split_seq" )
427 elsif ( $script eq "split_bed" )
434 elsif ( $script eq "tile_seq" )
441 elsif ( $script eq "invert_align" )
447 elsif ( $script eq "patscan_seq" )
458 elsif ( $script eq "blat_seq" )
470 elsif ( $script eq "soap_seq" )
481 elsif ( $script eq "match_seq" )
488 elsif ( $script eq "create_vmatch_index" )
496 elsif ( $script eq "write_align" )
506 elsif ( $script eq "write_bed" )
516 elsif ( $script eq "write_psl" )
524 elsif ( $script eq "write_fixedstep" )
532 elsif ( $script eq "write_2bit" )
540 elsif ( $script eq "write_solid" )
549 elsif ( $script eq "write_ucsc_config" )
556 elsif ( $script eq "plot_seqlogo" )
563 elsif ( $script eq "plot_phastcons_profiles" )
578 elsif ( $script eq "analyze_vals" )
585 elsif ( $script eq "head_records" )
591 elsif ( $script eq "remove_keys" )
598 elsif ( $script eq "remove_adaptor" )
607 elsif ( $script eq "remove_mysql_tables" )
618 elsif ( $script eq "remove_ucsc_tracks" )
630 elsif ( $script eq "rename_keys" )
636 elsif ( $script eq "uniq_vals" )
643 elsif ( $script eq "merge_vals" )
650 elsif ( $script eq "merge_records" )
657 elsif ( $script eq "compute" )
663 elsif ( $script eq "add_ident" )
670 elsif ( $script eq "count_records" )
677 elsif ( $script eq "random_records" )
683 elsif ( $script eq "sort_records" )
690 elsif ( $script eq "count_vals" )
696 elsif ( $script eq "plot_histogram" )
709 elsif ( $script eq "plot_lendist" )
721 elsif ( $script eq "plot_chrdist" )
732 elsif ( $script eq "plot_karyogram" )
741 elsif ( $script eq "plot_matches" )
753 elsif ( $script eq "length_vals" )
759 elsif ( $script eq "sum_vals" )
767 elsif ( $script eq "mean_vals" )
775 elsif ( $script eq "median_vals" )
783 elsif ( $script eq "max_vals" )
791 elsif ( $script eq "min_vals" )
799 elsif ( $script eq "upload_to_ucsc" )
823 # print STDERR Dumper( \@options );
830 # print STDERR Dumper( \%options );
832 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
833 return wantarray ? %options : \%options;
836 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
837 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
838 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
839 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
840 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
841 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
842 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
843 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
844 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
845 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
847 # ---- check arguments ----
849 if ( $options{ 'data_in' } )
851 $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
853 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
856 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
858 # print STDERR Dumper( \%options );
860 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
862 foreach $opt ( keys %options )
864 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
866 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
868 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
870 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
872 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
874 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
876 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
878 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
880 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
882 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
884 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
886 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
888 elsif ( $opt eq "genome" )
890 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
891 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
893 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
894 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
897 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
899 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
901 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
903 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
905 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
907 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
909 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
911 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
913 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
915 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
919 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
920 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
921 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
922 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
923 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
924 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
925 Maasha::Common::error( qq(no --keys speficied) ) if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
927 if ( $script eq "upload_to_ucsc" )
929 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
930 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
933 return wantarray ? %options : \%options;
937 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
940 sub script_print_usage
942 # Martin A. Hansen, January 2008.
944 # Retrieves usage information from file and
945 # prints this nicely formatted.
947 my ( $in, # handle to in stream
948 $out, # handle to out stream
949 $options, # options hash
954 my ( $file, $wiki, $lines );
956 if ( $options->{ 'data_in' } ) {
957 $file = $options->{ 'data_in' };
959 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
962 $wiki = Maasha::Gwiki::gwiki_read( $file );
964 ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
966 if ( not $options->{ "help" } ) {
967 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
970 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
972 print STDERR "$_\n" foreach @{ $lines };
980 # Martin A. Hansen, August 2007.
982 # Read psl table from stream or file.
984 my ( $in, # handle to in stream
985 $out, # handle to out stream
986 $options, # options hash
991 my ( $record, $file, $data_in, $num );
993 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
994 Maasha::Biopieces::put_record( $record, $out );
999 foreach $file ( @{ $options->{ "files" } } )
1001 $data_in = Maasha::Common::read_open( $file );
1003 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1005 Maasha::Biopieces::put_record( $record, $out );
1007 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1019 # Martin A. Hansen, August 2007.
1021 # Read bed table from stream or file.
1023 my ( $in, # handle to in stream
1024 $out, # handle to out stream
1025 $options, # options hash
1030 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1032 $cols = $options->{ 'cols' }->[ 0 ];
1034 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1035 Maasha::Biopieces::put_record( $record, $out );
1040 foreach $file ( @{ $options->{ "files" } } )
1042 $data_in = Maasha::Common::read_open( $file );
1044 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1046 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1048 Maasha::Biopieces::put_record( $record, $out );
1050 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1060 close $data_in if $data_in;
1064 sub script_read_fixedstep
1066 # Martin A. Hansen, Juli 2008.
1068 # Read fixedstep wiggle format from stream or file.
1070 my ( $in, # handle to in stream
1071 $out, # handle to out stream
1072 $options, # options hash
1077 my ( $file, $record, $entry, $data_in, $num );
1079 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1080 Maasha::Biopieces::put_record( $record, $out );
1085 foreach $file ( @{ $options->{ "files" } } )
1087 $data_in = Maasha::Common::read_open( $file );
1089 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1091 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1093 Maasha::Biopieces::put_record( $record, $out );
1095 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1105 close $data_in if $data_in;
1109 sub script_read_blast_tab
1111 # Martin A. Hansen, September 2007.
1113 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1115 my ( $in, # handle to in stream
1116 $out, # handle to out stream
1117 $options, # options hash
1122 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1124 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1125 Maasha::Biopieces::put_record( $record, $out );
1130 foreach $file ( @{ $options->{ "files" } } )
1132 $data_in = Maasha::Common::read_open( $file );
1134 while ( $line = <$data_in> )
1138 next if $line =~ /^#/;
1140 @fields = split /\t/, $line;
1142 $record->{ "REC_TYPE" } = "BLAST";
1143 $record->{ "Q_ID" } = $fields[ 0 ];
1144 $record->{ "S_ID" } = $fields[ 1 ];
1145 $record->{ "IDENT" } = $fields[ 2 ];
1146 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1147 $record->{ "MISMATCHES" } = $fields[ 4 ];
1148 $record->{ "GAPS" } = $fields[ 5 ];
1149 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1150 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1151 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1152 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1153 $record->{ "E_VAL" } = $fields[ 10 ];
1154 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1156 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1158 $record->{ "STRAND" } = '-';
1160 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1164 $record->{ "STRAND" } = '+';
1167 Maasha::Biopieces::put_record( $record, $out );
1169 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1179 close $data_in if $data_in;
1183 sub script_read_embl
1185 # Martin A. Hansen, August 2007.
1189 my ( $in, # handle to in stream
1190 $out, # handle to out stream
1191 $options, # options hash
1196 my ( %options2, $file, $data_in, $num, $entry, $record );
1198 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1199 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1200 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1202 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1203 Maasha::Biopieces::put_record( $record, $out );
1208 foreach $file ( @{ $options->{ "files" } } )
1210 $data_in = Maasha::Common::read_open( $file );
1212 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1214 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1216 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1218 $record_copy = dclone $record;
1220 delete $record_copy->{ "FT" };
1222 Maasha::Biopieces::put_record( $record_copy, $out );
1224 delete $record_copy->{ "SEQ" };
1226 foreach $feat ( keys %{ $record->{ "FT" } } )
1228 $record_copy->{ "FEAT_TYPE" } = $feat;
1230 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1232 foreach $qual ( keys %{ $feat2 } )
1234 $qual_val = join "; ", @{ $feat2->{ $qual } };
1239 $record_copy->{ $qual } = $qual_val;
1242 Maasha::Biopieces::put_record( $record_copy, $out );
1246 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1256 close $data_in if $data_in;
1260 sub script_read_stockholm
1262 # Martin A. Hansen, August 2007.
1264 # Read Stockholm format.
1266 my ( $in, # handle to in stream
1267 $out, # handle to out stream
1268 $options, # options hash
1273 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1275 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1276 Maasha::Biopieces::put_record( $record, $out );
1281 foreach $file ( @{ $options->{ "files" } } )
1283 $data_in = Maasha::Common::read_open( $file );
1285 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1287 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1291 foreach $key ( keys %{ $record->{ "GF" } } ) {
1292 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1295 $record_anno->{ "ALIGN" } = $num;
1297 Maasha::Biopieces::put_record( $record_anno, $out );
1299 foreach $seq ( @{ $record->{ "ALIGN" } } )
1301 undef $record_align;
1304 SEQ_NAME => $seq->[ 0 ],
1308 Maasha::Biopieces::put_record( $record_align, $out );
1311 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1321 close $data_in if $data_in;
1325 sub script_read_phastcons
1327 # Martin A. Hansen, December 2007.
1329 # Read PhastCons format.
1331 my ( $in, # handle to in stream
1332 $out, # handle to out stream
1333 $options, # options hash
1338 my ( $data_in, $file, $num, $entry, @records, $record );
1340 $options->{ "min" } ||= 10;
1341 $options->{ "dist" } ||= 25;
1342 $options->{ "threshold" } ||= 0.8;
1343 $options->{ "gap" } ||= 5;
1345 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1346 Maasha::Biopieces::put_record( $record, $out );
1351 foreach $file ( @{ $options->{ "files" } } )
1353 $data_in = Maasha::Common::read_open( $file );
1355 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1357 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1359 foreach $record ( @records )
1361 $record->{ "REC_TYPE" } = "BED";
1362 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1364 Maasha::Biopieces::put_record( $record, $out );
1366 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1377 close $data_in if $data_in;
1381 sub script_read_soft
1383 # Martin A. Hansen, December 2007.
1386 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1388 my ( $in, # handle to in stream
1389 $out, # handle to out stream
1390 $options, # options hash
1395 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1397 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1398 Maasha::Biopieces::put_record( $record, $out );
1403 foreach $file ( @{ $options->{ "files" } } )
1405 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1407 $soft_index = Maasha::NCBI::soft_index_file( $file );
1409 $fh = Maasha::Common::read_open( $file );
1411 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1413 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1415 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1417 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1419 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1421 foreach $sample ( @samples )
1424 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1426 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1428 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1430 foreach $record ( @{ $records } )
1432 Maasha::Biopieces::put_record( $record, $out );
1434 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1439 $old_end = $sample->{ "LINE_END" };
1447 close $data_in if $data_in;
1454 # Martin A. Hansen, February 2008.
1457 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1459 my ( $in, # handle to in stream
1460 $out, # handle to out stream
1461 $options, # options hash
1466 my ( $data_in, $file, $fh, $num, $record, $entry );
1468 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1469 Maasha::Biopieces::put_record( $record, $out );
1474 foreach $file ( @{ $options->{ "files" } } )
1476 $fh = Maasha::Common::read_open( $file );
1478 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1480 Maasha::Biopieces::put_record( $entry, $out );
1482 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1492 close $data_in if $data_in;
1496 sub script_read_2bit
1498 # Martin A. Hansen, March 2008.
1500 # Read sequences from 2bit file.
1502 my ( $in, # handle to in stream
1503 $out, # handle to out stream
1504 $options, # options hash
1509 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1511 $mask = 1 if not $options->{ "no_mask" };
1513 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1514 Maasha::Biopieces::put_record( $record, $out );
1519 foreach $file ( @{ $options->{ "files" } } )
1521 $data_in = Maasha::Common::read_open( $file );
1523 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1525 foreach $line ( @{ $toc } )
1527 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1528 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1529 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1531 Maasha::Biopieces::put_record( $record, $out );
1533 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1543 close $data_in if $data_in;
1547 sub script_read_solexa
1549 # Martin A. Hansen, March 2008.
1551 # Read Solexa sequence reads from file.
1553 my ( $in, # handle to in stream
1554 $out, # handle to out stream
1555 $options, # options hash
1560 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1562 $options->{ "format" } ||= "octal";
1563 $options->{ "quality" } ||= 20;
1565 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1566 Maasha::Biopieces::put_record( $record, $out );
1571 foreach $file ( @{ $options->{ "files" } } )
1573 $data_in = Maasha::Common::read_open( $file );
1575 if ( $options->{ "format" } eq "octal" )
1577 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1579 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1581 Maasha::Biopieces::put_record( $record, $out );
1583 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1590 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1592 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1594 Maasha::Biopieces::put_record( $record, $out );
1596 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1607 close $data_in if $data_in;
1611 sub script_read_solid
1613 # Martin A. Hansen, April 2008.
1615 # Read Solid sequence from file.
1617 my ( $in, # handle to in stream
1618 $out, # handle to out stream
1619 $options, # options hash
1624 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1626 $options->{ "quality" } ||= 15;
1628 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1629 Maasha::Biopieces::put_record( $record, $out );
1634 foreach $file ( @{ $options->{ "files" } } )
1636 $data_in = Maasha::Common::read_open( $file );
1638 while ( $line = <$data_in> )
1642 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1644 @scores = split /,/, $seq_qual;
1645 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1647 for ( $i = 0; $i < @seqs; $i++ ) {
1648 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1652 REC_TYPE => 'SOLID',
1653 SEQ_NAME => $seq_name,
1655 SEQ_QUAL => join( ";", @scores ),
1656 SEQ_LEN => length $seq_cs,
1657 SEQ => join( "", @seqs ),
1658 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1661 Maasha::Biopieces::put_record( $record, $out );
1663 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1673 close $data_in if $data_in;
1677 sub script_read_mysql
1679 # Martin A. Hansen, May 2008.
1681 # Read a MySQL query into stream.
1683 my ( $in, # handle to in stream
1684 $out, # handle to out stream
1685 $options, # options hash
1690 my ( $record, $dbh, $results );
1692 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1693 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1695 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1696 Maasha::Biopieces::put_record( $record, $out );
1699 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1701 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1703 Maasha::SQL::disconnect( $dbh );
1705 map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1709 sub script_read_ucsc_config
1711 # Martin A. Hansen, November 2008.
1713 # Read track entries from UCSC Genome Browser '.ra' files.
1715 my ( $in, # handle to in stream
1716 $out, # handle to out stream
1717 $options, # options hash
1722 my ( $record, $file, $data_in, $entry, $num );
1724 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1725 Maasha::Biopieces::put_record( $record, $out );
1730 foreach $file ( @{ $options->{ "files" } } )
1732 $data_in = Maasha::Common::read_open( $file );
1734 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
1736 $record->{ 'REC_TYPE' } = "UCSC Config";
1738 Maasha::Biopieces::put_record( $record, $out );
1740 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1750 close $data_in if $data_in;
1754 sub script_assemble_tag_contigs
1756 # Martin A. Hansen, November 2008.
1758 # Assemble tags from the stream into
1761 # The current implementation is quite
1762 # slow because of heavy use of temporary
1765 my ( $in, # handle to in stream
1766 $out, # handle to out stream
1767 $options, # options hash
1772 my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
1774 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
1775 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
1776 $cols = 6; # we only need the first 6 BED columns
1778 while ( $record = Maasha::Biopieces::get_record( $in ) )
1780 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
1782 $strand = $record->{ 'STRAND' } || '+';
1784 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
1790 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
1794 foreach $chr ( sort keys %{ $file_hash } )
1796 $bed_file = $file_hash->{ $chr };
1797 $tag_file = "$bed_file.tc";
1799 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
1801 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
1803 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
1805 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
1806 Maasha::Biopieces::put_record( $record, $out );
1818 sub script_length_seq
1820 # Martin A. Hansen, August 2007.
1822 # Determine the length of sequences in stream.
1824 my ( $in, # handle to in stream
1825 $out, # handle to out stream
1826 $options, # options hash
1831 my ( $record, $total );
1833 while ( $record = Maasha::Biopieces::get_record( $in ) )
1835 if ( $record->{ "SEQ" } )
1837 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1838 $total += $record->{ "SEQ_LEN" };
1841 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1844 Maasha::Biopieces::put_record( { TOTAL_SEQ_LEN => $total }, $out );
1848 sub script_shuffle_seq
1850 # Martin A. Hansen, December 2007.
1852 # Shuffle sequences in stream.
1854 my ( $in, # handle to in stream
1855 $out, # handle to out stream
1862 while ( $record = Maasha::Biopieces::get_record( $in ) )
1864 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
1866 Maasha::Biopieces::put_record( $record, $out );
1871 sub script_analyze_seq
1873 # Martin A. Hansen, August 2007.
1875 # Analyze sequence composition of sequences in stream.
1877 my ( $in, # handle to in stream
1878 $out, # handle to out stream
1883 my ( $record, $analysis );
1885 while ( $record = Maasha::Biopieces::get_record( $in ) )
1887 if ( $record->{ "SEQ" } )
1889 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
1891 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
1894 Maasha::Biopieces::put_record( $record, $out );
1899 sub script_analyze_tags
1901 # Martin A. Hansen, August 2008.
1903 # Analyze sequence tags in stream.
1905 my ( $in, # handle to in stream
1906 $out, # handle to out stream
1911 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
1913 while ( $record = Maasha::Biopieces::get_record( $in ) )
1915 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
1917 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
1921 $len_hash{ length( $record->{ "SEQ" } ) }++;
1922 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
1925 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
1927 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
1931 $len_hash{ $record->{ "BED_LEN" } }++;
1932 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
1937 foreach $key ( sort { $a <=> $b } keys %len_hash )
1939 $tag_record->{ "TAG_LEN" } = $key;
1940 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
1941 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
1943 Maasha::Biopieces::put_record( $tag_record, $out );
1948 sub script_complexity_seq
1950 # Martin A. Hansen, May 2008.
1952 # Generates an index calculated as the most common di-residue over
1953 # the sequence length for all sequences in stream.
1955 my ( $in, # handle to in stream
1956 $out, # handle to out stream
1961 my ( $record, $index );
1963 while ( $record = Maasha::Biopieces::get_record( $in ) )
1965 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1967 Maasha::Biopieces::put_record( $record, $out );
1972 sub script_oligo_freq
1974 # Martin A. Hansen, August 2007.
1976 # Determine the length of sequences in stream.
1978 my ( $in, # handle to in stream
1979 $out, # handle to out stream
1980 $options, # options hash
1985 my ( $record, %oligos, @freq_table );
1987 $options->{ "word_size" } ||= 7;
1989 while ( $record = Maasha::Biopieces::get_record( $in ) )
1991 if ( $record->{ "SEQ" } )
1993 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
1995 if ( not $options->{ "all" } )
1997 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1999 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
2005 Maasha::Biopieces::put_record( $record, $out );
2008 if ( $options->{ "all" } )
2010 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2012 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
2017 sub script_create_weight_matrix
2019 # Martin A. Hansen, August 2007.
2021 # Creates a weight matrix from an alignmnet.
2023 my ( $in, # handle to in stream
2024 $out, # handle to out stream
2025 $options, # options hash
2030 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2034 while ( $record = Maasha::Biopieces::get_record( $in ) )
2036 if ( $record->{ "SEQ" } )
2038 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2040 $res = substr $record->{ "SEQ" }, $i, 1;
2042 $freq_hash{ $i }{ $res }++;
2043 $res_hash{ $res } = 1;
2050 Maasha::Biopieces::put_record( $record, $out );
2054 foreach $res ( sort keys %res_hash )
2058 $record->{ "V0" } = $res;
2060 for ( $i = 0; $i < keys %freq_hash; $i++ )
2062 $freq = $freq_hash{ $i }{ $res } || 0;
2064 if ( $options->{ "percent" } ) {
2065 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2068 $record->{ "V" . ( $i + 1 ) } = $freq;
2071 Maasha::Biopieces::put_record( $record, $out );
2076 sub script_calc_bit_scores
2078 # Martin A. Hansen, March 2007.
2080 # Calculates the bit scores for each position from an alignmnet in the stream.
2082 my ( $in, # handle to in stream
2083 $out, # handle to out stream
2088 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2092 while ( $record = Maasha::Biopieces::get_record( $in ) )
2094 if ( $record->{ "SEQ" } )
2096 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2098 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2100 $res = substr $record->{ "SEQ" }, $i, 1;
2102 next if $res =~ /-|_|~|\./;
2104 $freq_hash{ $i }{ $res }++;
2111 Maasha::Biopieces::put_record( $record, $out );
2117 if ( $type eq "protein" ) {
2123 for ( $i = 0; $i < keys %freq_hash; $i++ )
2125 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2127 $bit_diff = $bit_max - $bit_height;
2129 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2132 Maasha::Biopieces::put_record( $record, $out );
2136 sub script_calc_fixedstep
2138 # Martin A. Hansen, September 2008.
2140 # Calculates fixedstep entries from data in the stream.
2142 my ( $in, # handle to in stream
2143 $out, # handle to out stream
2144 $options, # options hash
2149 my ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2151 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2152 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2154 while ( $record = Maasha::Biopieces::get_record( $in ) )
2156 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) {
2157 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } );
2163 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP );
2167 foreach $chr ( sort keys %{ $file_hash } )
2169 $bed_file = $file_hash->{ $chr };
2171 $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } );
2173 #$fixedstep_file = "$bed_file.fixedstep";
2175 # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2177 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2179 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2181 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2182 Maasha::Biopieces::put_record( $record, $out );
2189 unlink $fixedstep_file;
2194 sub script_remove_indels
2196 # Martin A. Hansen, August 2007.
2198 # Remove indels from sequences in stream.
2200 my ( $in, # handle to in stream
2201 $out, # handle to out stream
2208 while ( $record = Maasha::Biopieces::get_record( $in ) )
2210 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2212 Maasha::Biopieces::put_record( $record, $out );
2217 sub script_transliterate_seq
2219 # Martin A. Hansen, August 2007.
2221 # Transliterate chars from sequence in record.
2223 my ( $in, # handle to in stream
2224 $out, # handle to out stream
2225 $options, # options hash
2230 my ( $record, $search, $replace, $delete );
2232 $search = $options->{ "search" } || "";
2233 $replace = $options->{ "replace" } || "";
2234 $delete = $options->{ "delete" } || "";
2236 while ( $record = Maasha::Biopieces::get_record( $in ) )
2238 if ( $record->{ "SEQ" } )
2240 if ( $search and $replace ) {
2241 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2242 } elsif ( $delete ) {
2243 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2247 Maasha::Biopieces::put_record( $record, $out );
2252 sub script_transliterate_vals
2254 # Martin A. Hansen, April 2008.
2256 # Transliterate chars from values in record.
2258 my ( $in, # handle to in stream
2259 $out, # handle to out stream
2260 $options, # options hash
2265 my ( $record, $search, $replace, $delete, $key );
2267 $search = $options->{ "search" } || "";
2268 $replace = $options->{ "replace" } || "";
2269 $delete = $options->{ "delete" } || "";
2271 while ( $record = Maasha::Biopieces::get_record( $in ) )
2273 foreach $key ( @{ $options->{ "keys" } } )
2275 if ( exists $record->{ $key } )
2277 if ( $search and $replace ) {
2278 eval "\$record->{ $key } =~ tr/$search/$replace/";
2279 } elsif ( $delete ) {
2280 eval "\$record->{ $key } =~ tr/$delete//d";
2285 Maasha::Biopieces::put_record( $record, $out );
2290 sub script_translate_seq
2292 # Martin A. Hansen, February 2008.
2294 # Translate DNA sequence into protein sequence.
2296 my ( $in, # handle to in stream
2297 $out, # handle to out stream
2298 $options, # options hash
2303 my ( $record, $frame, %new_record );
2305 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2307 while ( $record = Maasha::Biopieces::get_record( $in ) )
2309 if ( $record->{ "SEQ" } )
2311 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2313 foreach $frame ( @{ $options->{ "frames" } } )
2315 %new_record = %{ $record };
2317 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2318 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2319 $new_record{ "FRAME" } = $frame;
2321 Maasha::Biopieces::put_record( \%new_record, $out );
2327 Maasha::Biopieces::put_record( $record, $out );
2333 sub script_get_genome_align
2335 # Martin A. Hansen, April 2008.
2337 # Gets a subalignment from a multiple genome alignment.
2339 my ( $in, # handle to in stream
2340 $out, # handle to out stream
2341 $options, # options hash
2346 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2348 $options->{ "strand" } ||= "+";
2352 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2354 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2356 $beg = $options->{ "beg" } - 1;
2358 if ( $options->{ "end" } ) {
2359 $end = $options->{ "end" };
2360 } elsif ( $options->{ "len" } ) {
2361 $end = $beg + $options->{ "len" };
2364 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2366 foreach $entry ( @{ $align } )
2368 $entry->{ "CHR" } = $record->{ "CHR" };
2369 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2370 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2371 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2372 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2373 $entry->{ "SCORE" } = $record->{ "SCORE" };
2375 Maasha::Biopieces::put_record( $entry, $out );
2379 while ( $record = Maasha::Biopieces::get_record( $in ) )
2381 if ( $record->{ "REC_TYPE" } eq "BED" )
2383 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2385 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
2387 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
2389 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2391 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2393 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2395 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2398 foreach $entry ( @{ $align } )
2400 $entry->{ "CHR" } = $record->{ "CHR" };
2401 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2402 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2403 $entry->{ "STRAND" } = $record->{ "STRAND" };
2404 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2405 $entry->{ "SCORE" } = $record->{ "SCORE" };
2407 Maasha::Biopieces::put_record( $entry, $out );
2415 sub script_get_genome_phastcons
2417 # Martin A. Hansen, February 2008.
2419 # Get phastcons scores from genome intervals.
2421 my ( $in, # handle to in stream
2422 $out, # handle to out stream
2423 $options, # options hash
2428 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2430 $options->{ "flank" } ||= 0;
2432 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2433 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2435 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
2436 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2438 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2440 $options->{ "beg" } -= 1; # request is 1-based
2441 $options->{ "end" } -= 1; # request is 1-based
2443 if ( $options->{ "len" } ) {
2444 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2447 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2449 $record->{ "CHR" } = $options->{ "chr" };
2450 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2451 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2453 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2454 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2456 Maasha::Biopieces::put_record( $record, $out );
2459 while ( $record = Maasha::Biopieces::get_record( $in ) )
2461 if ( $record->{ "REC_TYPE" } eq "BED" )
2463 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2465 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2467 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2469 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2471 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2474 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2475 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2477 Maasha::Biopieces::put_record( $record, $out );
2480 close $fh_phastcons if $fh_phastcons;
2486 # Martin A. Hansen, December 2007.
2488 # Folds sequences in stream into secondary structures.
2490 my ( $in, # handle to in stream
2491 $out, # handle to out stream
2496 my ( $record, $type, $struct, $index );
2498 while ( $record = Maasha::Biopieces::get_record( $in ) )
2500 if ( $record->{ "SEQ" } )
2503 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2506 if ( $type ne "protein" )
2508 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2509 $record->{ "SEC_STRUCT" } = $struct;
2510 $record->{ "FREE_ENERGY" } = $index;
2511 $record->{ "SCORE" } = abs int $index;
2512 $record->{ "SIZE" } = length $struct;
2513 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2517 Maasha::Biopieces::put_record( $record, $out );
2522 sub script_split_seq
2524 # Martin A. Hansen, August 2007.
2526 # Split a sequence in stream into words.
2528 my ( $in, # handle to in stream
2529 $out, # handle to out stream
2530 $options, # options hash
2535 my ( $record, $new_record, $i, $subseq, %lookup );
2537 $options->{ "word_size" } ||= 7;
2539 while ( $record = Maasha::Biopieces::get_record( $in ) )
2541 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2543 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2545 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2547 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2549 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2550 $new_record->{ "SEQ" } = $subseq;
2552 Maasha::Biopieces::put_record( $new_record, $out );
2554 $lookup{ $subseq } = 1;
2558 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2559 $new_record->{ "SEQ" } = $subseq;
2561 Maasha::Biopieces::put_record( $new_record, $out );
2567 Maasha::Biopieces::put_record( $record, $out );
2573 sub script_split_bed
2575 # Martin A. Hansen, June 2008.
2577 # Split a BED record into overlapping windows.
2579 my ( $in, # handle to in stream
2580 $out, # handle to out stream
2581 $options, # options hash
2586 my ( $record, $new_record, $i );
2588 $options->{ "window_size" } ||= 20;
2589 $options->{ "step_size" } ||= 1;
2591 while ( $record = Maasha::Biopieces::get_record( $in ) )
2593 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2595 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2597 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2599 $new_record->{ "REC_TYPE" } = "BED";
2600 $new_record->{ "CHR" } = $record->{ "CHR" };
2601 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
2602 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2603 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
2604 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
2605 $new_record->{ "SCORE" } = $record->{ "SCORE" };
2606 $new_record->{ "STRAND" } = $record->{ "STRAND" };
2608 Maasha::Biopieces::put_record( $new_record, $out );
2613 Maasha::Biopieces::put_record( $record, $out );
2619 sub script_align_seq
2621 # Martin A. Hansen, August 2007.
2623 # Align sequences in stream.
2625 my ( $in, # handle to in stream
2626 $out, # handle to out stream
2631 my ( $record, @entries, $entry );
2633 while ( $record = Maasha::Biopieces::get_record( $in ) )
2635 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2636 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2637 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
2638 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
2640 Maasha::Biopieces::put_record( $record, $out );
2644 @entries = Maasha::Align::align( \@entries );
2646 foreach $entry ( @entries )
2648 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
2651 SEQ_NAME => $entry->[ SEQ_NAME ],
2652 SEQ => $entry->[ SEQ ],
2655 Maasha::Biopieces::put_record( $record, $out );
2663 # Martin A. Hansen, February 2008.
2665 # Using the first sequence in stream as reference, tile
2666 # all subsequent sequences based on pairwise alignments.
2668 my ( $in, # handle to in stream
2669 $out, # handle to out stream
2670 $options, # options hash
2675 my ( $record, $first, $ref_entry, @entries );
2679 while ( $record = Maasha::Biopieces::get_record( $in ) )
2681 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2685 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2691 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2696 Maasha::Biopieces::put_record( $record, $out );
2700 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2702 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2706 sub script_invert_align
2708 # Martin A. Hansen, February 2008.
2710 # Inverts an alignment showing only non-mathing residues
2711 # using the first sequence as reference.
2713 my ( $in, # handle to in stream
2714 $out, # handle to out stream
2715 $options, # options hash
2720 my ( $record, @entries );
2722 while ( $record = Maasha::Biopieces::get_record( $in ) )
2724 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2726 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2730 Maasha::Biopieces::put_record( $record, $out );
2734 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
2736 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2740 sub script_patscan_seq
2742 # Martin A. Hansen, August 2007.
2744 # Locates patterns in sequences using scan_for_matches.
2746 my ( $in, # handle to in stream
2747 $out, # handle to out stream
2748 $options, # options hash
2753 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
2755 if ( $options->{ "patterns" } ) {
2756 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
2757 } elsif ( -f $options->{ "patterns_in" } ) {
2758 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
2761 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
2763 push @args, "-c" if $options->{ "comp" };
2764 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
2765 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
2767 $seq_file = "$BP_TMP/patscan.seq";
2768 $pat_file = "$BP_TMP/patscan.pat";
2769 $out_file = "$BP_TMP/patscan.out";
2771 $fh_out = Maasha::Common::write_open( $seq_file );
2775 while ( $record = Maasha::Biopieces::get_record( $in ) )
2777 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
2779 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2781 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
2783 $head_hash{ $i } = $record->{ "SEQ_NAME" };
2791 $arg = join " ", @args;
2792 $arg .= " -p" if $type eq "protein";
2794 foreach $pattern ( @{ $patterns } )
2796 $fh_out = Maasha::Common::write_open( $pat_file );
2798 print $fh_out "$pattern\n";
2802 if ( $options->{ 'genome' } ) {
2803 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
2804 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
2806 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
2807 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
2810 $fh_in = Maasha::Common::read_open( $out_file );
2812 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
2814 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
2816 if ( $options->{ 'genome' } )
2818 $result->{ "CHR" } = $result->{ "S_ID" };
2819 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
2820 $result->{ "CHR_END" } = $result->{ "S_END" };
2822 delete $result->{ "S_ID" };
2823 delete $result->{ "S_BEG" };
2824 delete $result->{ "S_END" };
2828 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
2831 Maasha::Biopieces::put_record( $result, $out );
2845 # Martin A. Hansen, August 2007.
2847 # BLATs sequences in stream against a given genome.
2849 my ( $in, # handle to in stream
2850 $out, # handle to out stream
2851 $options, # options hash
2856 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
2858 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2860 $options->{ 'tile_size' } ||= 11;
2861 $options->{ 'one_off' } ||= 0;
2862 $options->{ 'min_identity' } ||= 90;
2863 $options->{ 'min_score' } ||= 0;
2864 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
2866 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
2867 $blat_args .= " -oneOff=$options->{ 'one_off' }";
2868 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
2869 $blat_args .= " -minScore=$options->{ 'min_score' }";
2870 $blat_args .= " -stepSize=$options->{ 'step_size' }";
2871 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
2873 $query_file = "$BP_TMP/blat.seq";
2875 $fh_out = Maasha::Common::write_open( $query_file );
2877 while ( $record = Maasha::Biopieces::get_record( $in ) )
2879 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2881 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
2882 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
2885 Maasha::Biopieces::put_record( $record, $out );
2890 $blat_args .= " -t=dnax" if $type eq "protein";
2891 $blat_args .= " -q=$type";
2893 $result_file = "$BP_TMP/blat.psl";
2895 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
2899 $entries = Maasha::UCSC::psl_get_entries( $result_file );
2901 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $entries };
2903 unlink $result_file;
2909 # Martin A. Hansen, July 2008.
2911 # soap sequences in stream against a given file or genome.
2913 my ( $in, # handle to in stream
2914 $out, # handle to out stream
2915 $options, # options hash
2920 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
2922 $options->{ "seed_size" } ||= 10;
2923 $options->{ "mismatches" } ||= 2;
2924 $options->{ "gap_size" } ||= 0;
2925 $options->{ "cpus" } ||= 1;
2927 if ( $options->{ "genome" } ) {
2928 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2931 $tmp_in = "$BP_TMP/soap_query.seq";
2932 $tmp_out = "$BP_TMP/soap.result";
2934 $fh_out = Maasha::Common::write_open( $tmp_in );
2938 while ( $record = Maasha::Biopieces::get_record( $in ) )
2940 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2942 Maasha::Fasta::put_entry( $entry, $fh_out );
2947 Maasha::Biopieces::put_record( $record, $out );
2955 "-s $options->{ 'seed_size' }",
2958 "-v $options->{ 'mismatches' }",
2959 "-g $options->{ 'gap_size' }",
2960 "-p $options->{ 'cpus' }",
2961 "-d $options->{ 'in_file' }",
2965 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
2967 Maasha::Common::run( "soap", $args, 1 );
2971 $fh_out = Maasha::Common::read_open( $tmp_out );
2975 while ( $line = <$fh_out> )
2979 @fields = split /\t/, $line;
2981 $record->{ "REC_TYPE" } = "SOAP";
2982 $record->{ "Q_ID" } = $fields[ 0 ];
2983 $record->{ "SCORE" } = $fields[ 3 ];
2984 $record->{ "STRAND" } = $fields[ 6 ];
2985 $record->{ "S_ID" } = $fields[ 7 ];
2986 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
2987 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
2989 Maasha::Biopieces::put_record( $record, $out );
2999 sub script_match_seq
3001 # Martin A. Hansen, August 2007.
3003 # BLATs sequences in stream against a given genome.
3005 my ( $in, # handle to in stream
3006 $out, # handle to out stream
3007 $options, # options hash
3012 my ( $record, @entries, $results );
3014 $options->{ "word_size" } ||= 20;
3015 $options->{ "direction" } ||= "both";
3017 while ( $record = Maasha::Biopieces::get_record( $in ) )
3019 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3020 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3023 Maasha::Biopieces::put_record( $record, $out );
3026 if ( @entries == 1 )
3028 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3030 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3032 elsif ( @entries == 2 )
3034 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3036 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3041 sub script_create_vmatch_index
3043 # Martin A. Hansen, January 2008.
3045 # Create a vmatch index from sequences in the stream.
3047 my ( $in, # handle to in stream
3048 $out, # handle to out stream
3049 $options, # options hash
3054 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3056 if ( $options->{ "index_name" } )
3058 $file_tmp = $options->{ 'index_name' };
3059 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3062 while ( $record = Maasha::Biopieces::get_record( $in ) )
3064 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3066 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3068 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3071 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3074 if ( $options->{ "index_name" } )
3078 if ( $type eq "protein" ) {
3079 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3081 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3089 sub script_write_align
3091 # Martin A. Hansen, August 2007.
3093 # Write pretty alignments aligned sequences in stream.
3095 my ( $in, # handle to in stream
3096 $out, # handle to out stream
3097 $options, # options hash
3102 my ( $fh, $record, @entries );
3104 $fh = write_stream( $options->{ "data_out" } ) ;
3106 while ( $record = Maasha::Biopieces::get_record( $in ) )
3108 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3109 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3112 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3115 if ( scalar( @entries ) == 2 ) {
3116 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3117 } elsif ( scalar ( @entries ) > 2 ) {
3118 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3125 sub script_write_bed
3127 # Martin A. Hansen, August 2007.
3129 # Write BED format for the UCSC genome browser using records in stream.
3131 my ( $in, # handle to in stream
3132 $out, # handle to out stream
3133 $options, # options hash
3138 my ( $cols, $fh, $record, $bed_entry, $new_record );
3140 $cols = $options->{ 'cols' }->[ 0 ];
3142 $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
3144 while ( $record = Maasha::Biopieces::get_record( $in ) )
3146 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
3148 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
3149 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
3152 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3159 sub script_write_psl
3161 # Martin A. Hansen, August 2007.
3163 # Write PSL output from stream.
3165 my ( $in, # handle to in stream
3166 $out, # handle to out stream
3167 $options, # options hash
3172 my ( $fh, $record, @output, $first );
3176 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3178 while ( $record = Maasha::Biopieces::get_record( $in ) )
3180 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3182 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3184 Maasha::UCSC::psl_put_header( $fh ) if $first;
3185 Maasha::UCSC::psl_put_entry( $record, $fh );
3194 sub script_write_fixedstep
3196 # Martin A. Hansen, Juli 2008.
3198 # Write fixedStep entries from recrods in the stream.
3200 my ( $in, # handle to in stream
3201 $out, # handle to out stream
3202 $options, # options hash
3207 my ( $fh, $record, $entry );
3209 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3211 while ( $record = Maasha::Biopieces::get_record( $in ) )
3213 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
3214 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
3217 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3224 sub script_write_2bit
3226 # Martin A. Hansen, March 2008.
3228 # Write sequence entries from stream in 2bit format.
3230 my ( $in, # handle to in stream
3231 $out, # handle to out stream
3232 $options, # options hash
3237 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
3239 $mask = 1 if not $options->{ "no_mask" };
3241 $tmp_file = "$BP_TMP/write_2bit.fna";
3242 $fh_tmp = Maasha::Common::write_open( $tmp_file );
3244 $fh_out = write_stream( $options->{ "data_out" } );
3246 while ( $record = Maasha::Biopieces::get_record( $in ) )
3248 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
3249 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3252 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3257 $fh_in = Maasha::Common::read_open( $tmp_file );
3259 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3268 sub script_write_solid
3270 # Martin A. Hansen, April 2008.
3272 # Write di-base encoded Solid sequence from entries in stream.
3274 my ( $in, # handle to in stream
3275 $out, # handle to out stream
3276 $options, # options hash
3281 my ( $record, $fh, $entry );
3283 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3285 while ( $record = Maasha::Biopieces::get_record( $in ) )
3287 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3289 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
3291 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3294 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3301 sub script_write_ucsc_config
3303 # Martin A. Hansen, November 2008.
3305 # Write UCSC Genome Broser configuration (.ra file type) from
3306 # records in the stream.
3308 my ( $in, # handle to in stream
3309 $out, # handle to out stream
3310 $options, # options hash
3315 my ( $record, $fh );
3317 $fh = write_stream( $options->{ "data_out" } );
3319 while ( $record = Maasha::Biopieces::get_record( $in ) )
3321 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
3323 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3330 sub script_plot_seqlogo
3332 # Martin A. Hansen, August 2007.
3334 # Calculates and writes a sequence logo for alignments.
3336 my ( $in, # handle to in stream
3337 $out, # handle to out stream
3338 $options, # options hash
3343 my ( $record, @entries, $logo, $fh );
3345 while ( $record = Maasha::Biopieces::get_record( $in ) )
3347 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3348 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3351 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3354 $logo = Maasha::Plot::seq_logo( \@entries );
3356 $fh = write_stream( $options->{ "data_out" } );
3364 sub script_plot_phastcons_profiles
3366 # Martin A. Hansen, January 2008.
3368 # Plots PhastCons profiles.
3370 my ( $in, # handle to in stream
3371 $out, # handle to out stream
3372 $options, # options hash
3377 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
3379 $options->{ "title" } ||= "PhastCons Profiles";
3381 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3382 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3384 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3385 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3387 while ( $record = Maasha::Biopieces::get_record( $in ) )
3389 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3391 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
3392 $record->{ "CHR_BEG" },
3393 $record->{ "CHR_END" },
3394 $options->{ "flank" } );
3396 push @{ $AoA }, [ @{ $scores } ];
3399 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3402 Maasha::UCSC::phastcons_normalize( $AoA );
3404 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
3405 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
3407 $AoA = Maasha::Matrix::matrix_flip( $AoA );
3409 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
3411 $fh = write_stream( $options->{ "data_out" } );
3413 print $fh "$_\n" foreach @{ $plot };
3419 sub script_analyze_bed
3421 # Martin A. Hansen, March 2008.
3423 # Analyze BED entries in stream.
3425 my ( $in, # handle to in stream
3426 $out, # handle to out stream
3427 $options, # options hash
3434 while ( $record = Maasha::Biopieces::get_record( $in ) )
3436 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
3438 Maasha::Biopieces::put_record( $record, $out );
3443 sub script_analyze_vals
3445 # Martin A. Hansen, August 2007.
3447 # Analyze values for given keys in stream.
3449 my ( $in, # handle to in stream
3450 $out, # handle to out stream
3451 $options, # options hash
3456 my ( $record, $key, @keys, %key_hash, $analysis, $len );
3458 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
3460 while ( $record = Maasha::Biopieces::get_record( $in ) )
3462 foreach $key ( keys %{ $record } )
3464 next if $options->{ "keys" } and not exists $key_hash{ $key };
3466 $analysis->{ $key }->{ "COUNT" }++;
3468 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
3470 $analysis->{ $key }->{ "TYPE" } = "num";
3471 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
3472 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3473 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
3477 $len = length $record->{ $key };
3479 $analysis->{ $key }->{ "TYPE" } = "alph";
3480 $analysis->{ $key }->{ "SUM" } += $len;
3481 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3482 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
3486 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3489 foreach $key ( keys %{ $analysis } )
3491 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
3492 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
3495 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
3505 if ( $options->{ "keys" } ) {
3506 @keys = @{ $options->{ "keys" } };
3508 @keys = keys %{ $analysis };
3511 foreach $key ( @keys )
3513 $keys .= sprintf "% 15s", $key;
3514 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
3515 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
3516 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
3517 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
3518 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
3519 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
3522 print $out "$keys\n";
3523 print $out "$types\n";
3524 print $out "$counts\n";
3525 print $out "$mins\n";
3526 print $out "$maxs\n";
3527 print $out "$sums\n";
3528 print $out "$means\n";
3532 sub script_head_records
3534 # Martin A. Hansen, August 2007.
3536 # Display the first sequences in stream.
3538 my ( $in, # handle to in stream
3539 $out, # handle to out stream
3540 $options, # options hash
3545 my ( $record, $count );
3547 $options->{ "num" } ||= 10;
3551 while ( $record = Maasha::Biopieces::get_record( $in ) )
3555 Maasha::Biopieces::put_record( $record, $out );
3557 last if $count == $options->{ "num" };
3562 sub script_remove_keys
3564 # Martin A. Hansen, August 2007.
3566 # Remove keys from stream.
3568 my ( $in, # handle to in stream
3569 $out, # handle to out stream
3570 $options, # options hash
3575 my ( $record, $new_record );
3577 while ( $record = Maasha::Biopieces::get_record( $in ) )
3579 if ( $options->{ "keys" } )
3581 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
3583 elsif ( $options->{ "save_keys" } )
3585 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
3587 $record = $new_record;
3590 Maasha::Biopieces::put_record( $record, $out ) if keys %{ $record };
3595 sub script_remove_adaptor
3597 # Martin A. Hansen, August 2008.
3599 # Find and remove adaptor from sequences in the stream.
3601 my ( $in, # handle to in stream
3602 $out, # handle to out stream
3603 $options, # options hash
3608 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
3610 $options->{ "remove" } ||= "after";
3612 $max_mismatch = $options->{ "mismatches" } || 0;
3613 $offset = $options->{ "offset" };
3615 if ( not defined $offset ) {
3621 $adaptor = uc $options->{ "adaptor" };
3622 $adaptor_len = length $adaptor;
3624 while ( $record = Maasha::Biopieces::get_record( $in ) )
3626 if ( $record->{ "SEQ" } )
3628 $seq = uc $record->{ "SEQ" };
3629 $seq_len = length $seq;
3631 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
3633 $record->{ "ADAPTOR_POS" } = $pos;
3635 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
3637 if ( $options->{ "remove" } eq "after" )
3639 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
3640 $record->{ "SEQ_LEN" } = $pos;
3644 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
3645 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3649 Maasha::Biopieces::put_record( $record, $out );
3653 Maasha::Biopieces::put_record( $record, $out );
3659 sub script_remove_mysql_tables
3661 # Martin A. Hansen, November 2008.
3663 # Remove MySQL tables from values in stream.
3665 my ( $in, # handle to in stream
3666 $out, # handle to out stream
3667 $options, # options hash
3672 my ( $record, %table_hash, $dbh, $table );
3674 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
3675 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
3677 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
3679 while ( $record = Maasha::Biopieces::get_record( $in ) )
3681 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3683 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3686 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3688 foreach $table ( sort keys %table_hash )
3690 if ( Maasha::SQL::table_exists( $dbh, $table ) )
3692 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3693 Maasha::SQL::delete_table( $dbh, $table );
3694 print STDERR "done.\n" if $options->{ 'verbose' };
3698 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
3702 Maasha::SQL::disconnect( $dbh );
3706 sub script_remove_ucsc_tracks
3708 # Martin A. Hansen, November 2008.
3710 # Remove track from MySQL tables and config file.
3712 my ( $in, # handle to in stream
3713 $out, # handle to out stream
3714 $options, # options hash
3719 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
3721 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
3722 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
3723 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
3725 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
3727 while ( $record = Maasha::Biopieces::get_record( $in ) )
3729 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3731 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3734 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
3736 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
3737 push @tracks, $track;
3742 foreach $track ( @tracks )
3744 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
3745 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
3747 push @new_tracks, $track;
3751 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
3753 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
3755 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
3759 # ---- locate track in database ----
3761 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3763 foreach $track ( sort keys %track_hash )
3765 if ( Maasha::SQL::table_exists( $dbh, $track ) )
3767 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3768 Maasha::SQL::delete_table( $dbh, $track );
3769 print STDERR "done.\n" if $options->{ 'verbose' };
3773 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
3777 Maasha::SQL::disconnect( $dbh );
3779 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
3783 sub script_rename_keys
3785 # Martin A. Hansen, August 2007.
3787 # Rename keys in stream.
3789 my ( $in, # handle to in stream
3790 $out, # handle to out stream
3791 $options, # options hash
3798 while ( $record = Maasha::Biopieces::get_record( $in ) )
3800 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3802 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
3804 delete $record->{ $options->{ "keys" }->[ 0 ] };
3807 Maasha::Biopieces::put_record( $record, $out );
3812 sub script_uniq_vals
3814 # Martin A. Hansen, August 2007.
3816 # Find unique values in stream.
3818 my ( $in, # handle to in stream
3819 $out, # handle to out stream
3820 $options, # options hash
3825 my ( %hash, $record );
3827 while ( $record = Maasha::Biopieces::get_record( $in ) )
3829 if ( $record->{ $options->{ "key" } } )
3831 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
3833 Maasha::Biopieces::put_record( $record, $out );
3835 $hash{ $record->{ $options->{ "key" } } } = 1;
3837 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
3839 Maasha::Biopieces::put_record( $record, $out );
3843 $hash{ $record->{ $options->{ "key" } } } = 1;
3848 Maasha::Biopieces::put_record( $record, $out );
3854 sub script_merge_vals
3856 # Martin A. Hansen, August 2007.
3858 # Rename keys in stream.
3860 my ( $in, # handle to in stream
3861 $out, # handle to out stream
3862 $options, # options hash
3867 my ( $record, @join, $i );
3869 $options->{ "delimit" } ||= '_';
3871 while ( $record = Maasha::Biopieces::get_record( $in ) )
3873 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3875 @join = $record->{ $options->{ "keys" }->[ 0 ] };
3877 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
3878 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
3881 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
3884 Maasha::Biopieces::put_record( $record, $out );
3889 sub script_merge_records
3891 # Martin A. Hansen, July 2008.
3893 # Merges records in the stream based on identical values of two given keys.
3895 my ( $in, # handle to in stream
3896 $out, # handle to out stream
3897 $options, # options hash
3902 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
3903 $num1, $num2, $num, $cmp, $i );
3905 $merge = $options->{ "merge" } || "AandB";
3907 $file1 = "$BP_TMP/merge_records1.tmp";
3908 $file2 = "$BP_TMP/merge_records2.tmp";
3910 $fh1 = Maasha::Common::write_open( $file1 );
3911 $fh2 = Maasha::Common::write_open( $file2 );
3913 $key1 = $options->{ "keys" }->[ 0 ];
3914 $key2 = $options->{ "keys" }->[ 1 ];
3916 $num = $key2 =~ s/n$//;
3920 while ( $record = Maasha::Biopieces::get_record( $in ) )
3922 if ( exists $record->{ $key1 } )
3925 @vals1 = $record->{ $key1 };
3927 delete $record->{ $key1 };
3929 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
3931 print $fh1 join( "\t", @vals1 ), "\n";
3935 elsif ( exists $record->{ $key2 } )
3938 @vals2 = $record->{ $key2 };
3940 delete $record->{ $key2 };
3942 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
3944 print $fh2 join( "\t", @vals2 ), "\n";
3955 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3956 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3960 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3961 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3964 $fh1 = Maasha::Common::read_open( $file1 );
3965 $fh2 = Maasha::Common::read_open( $file2 );
3967 @vals1 = Maasha::Common::get_fields( $fh1 );
3968 @vals2 = Maasha::Common::get_fields( $fh2 );
3970 while ( $num1 > 0 and $num2 > 0 )
3975 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
3977 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
3982 if ( $merge =~ /^(AorB|AnotB)$/ )
3984 for ( $i = 0; $i < @keys1; $i++ ) {
3985 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3988 Maasha::Biopieces::put_record( $record, $out );
3991 @vals1 = Maasha::Common::get_fields( $fh1 );
3996 if ( $merge =~ /^(BorA|BnotA)$/ )
3998 for ( $i = 0; $i < @keys2; $i++ ) {
3999 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4002 Maasha::Biopieces::put_record( $record, $out );
4005 @vals2 = Maasha::Common::get_fields( $fh2 );
4010 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4012 for ( $i = 0; $i < @keys1; $i++ ) {
4013 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4016 for ( $i = 1; $i < @keys2; $i++ ) {
4017 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4020 Maasha::Biopieces::put_record( $record, $out );
4023 @vals1 = Maasha::Common::get_fields( $fh1 );
4024 @vals2 = Maasha::Common::get_fields( $fh2 );
4036 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4040 for ( $i = 0; $i < @keys1; $i++ ) {
4041 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4044 Maasha::Biopieces::put_record( $record, $out );
4047 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4051 for ( $i = 0; $i < @keys2; $i++ ) {
4052 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4055 Maasha::Biopieces::put_record( $record, $out );
4062 # Martin A. Hansen, August 2007.
4064 # Evaluate extression for records in stream.
4066 my ( $in, # handle to in stream
4067 $out, # handle to out stream
4068 $options, # options hash
4073 my ( $record, $eval_key, @keys, $eval_val );
4075 while ( $record = Maasha::Biopieces::get_record( $in ) )
4077 if ( $options->{ "eval" } )
4079 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
4086 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
4088 @keys = grep { exists $record->{ $_ } } @keys;
4091 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
4093 $record->{ $eval_key } = eval "$eval_val";
4094 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
4098 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
4102 Maasha::Biopieces::put_record( $record, $out );
4109 # Martin A. Hansen, June 2008.
4113 my ( $in, # handle to in stream
4114 $out, # handle to out stream
4115 $options, # options hash
4120 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4122 while ( $record = Maasha::Biopieces::get_record( $in ) )
4126 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4128 push @rows, $record->{ $key };
4132 push @matrix, [ @rows ];
4137 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4139 foreach $row ( @matrix )
4141 for ( $i = 0; $i < @{ $row }; $i++ ) {
4142 $record->{ "V$i" } = $row->[ $i ];
4145 Maasha::Biopieces::put_record( $record, $out );
4150 sub script_add_ident
4152 # Martin A. Hansen, May 2008.
4154 # Add a unique identifier to each record in stream.
4156 my ( $in, # handle to in stream
4157 $out, # handle to out stream
4158 $options, # options hash
4163 my ( $record, $key, $prefix, $i );
4165 $key = $options->{ "key" } || "ID";
4166 $prefix = $options->{ "prefix" } || "ID";
4170 while ( $record = Maasha::Biopieces::get_record( $in ) )
4172 $record->{ $key } = sprintf( "$prefix%08d", $i );
4174 Maasha::Biopieces::put_record( $record, $out );
4181 sub script_count_records
4183 # Martin A. Hansen, August 2007.
4185 # Count records in stream.
4187 my ( $in, # handle to in stream
4188 $out, # handle to out stream
4189 $options, # options hash
4194 my ( $record, $count, $result, $fh, $line );
4198 if ( $options->{ "no_stream" } )
4200 while ( $line = <$in> )
4204 $count++ if $line eq "---";
4209 while ( $record = Maasha::Biopieces::get_record( $in ) )
4211 Maasha::Biopieces::put_record( $record, $out );
4217 $result = { "RECORDS_COUNT" => $count };
4219 $fh = write_stream( $options->{ "data_out" } );
4221 Maasha::Biopieces::put_record( $result, $fh );
4227 sub script_random_records
4229 # Martin A. Hansen, August 2007.
4231 # Pick a number or random records from stream.
4233 my ( $in, # handle to in stream
4234 $out, # handle to out stream
4235 $options, # options hash
4240 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4242 $options->{ "num" } ||= 10;
4244 $tmp_file = "$BP_TMP/random_records.tmp";
4246 $fh_out = Maasha::Common::write_open( $tmp_file );
4250 while ( $record = Maasha::Biopieces::get_record( $in ) )
4252 Maasha::Biopieces::put_record( $record, $fh_out );
4262 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4264 while ( $i < $options->{ "num" } )
4266 $rand = int( rand( $count ) );
4268 if ( not exists $rand_hash{ $rand } )
4270 $rand_hash{ $rand } = 1;
4272 $max = $rand if $rand > $max;
4278 $fh_in = Maasha::Common::read_open( $tmp_file );
4282 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4284 Maasha::Biopieces::put_record( $record, $out ) if exists $rand_hash{ $count };
4286 last if $count == $max;
4297 sub script_sort_records
4299 # Martin A. Hansen, August 2007.
4301 # Sort to sort records according to keys.
4303 my ( $in, # handle to in stream
4304 $out, # handle to out stream
4305 $options, # options hash
4310 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4312 foreach $key ( @{ $options->{ "keys" } } )
4314 if ( $key =~ s/n$// ) {
4315 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4317 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4321 $sort_str = join " or ", @sort_cmd;
4322 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4324 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
4325 push @records, $record;
4328 @records = sort $sort_sub @records;
4330 if ( $options->{ "reverse" } )
4332 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4333 Maasha::Biopieces::put_record( $records[ $i ], $out );
4338 for ( $i = 0; $i < scalar @records; $i++ ) {
4339 Maasha::Biopieces::put_record( $records[ $i ], $out );
4345 sub script_count_vals
4347 # Martin A. Hansen, August 2007.
4349 # Count records in stream.
4351 my ( $in, # handle to in stream
4352 $out, # handle to out stream
4353 $options, # options hash
4358 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4360 $tmp_file = "$BP_TMP/count_cache.tmp";
4362 $fh_out = Maasha::Common::write_open( $tmp_file );
4367 while ( $record = Maasha::Biopieces::get_record( $in ) )
4369 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4371 push @records, $record;
4373 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4375 map { Maasha::Biopieces::put_record( $_, $fh_out ) } @records;
4382 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4393 $fh_in = Maasha::Common::read_open( $tmp_file );
4395 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4397 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4399 Maasha::Biopieces::put_record( $record, $out );
4401 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4409 foreach $record ( @records )
4411 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4413 Maasha::Biopieces::put_record( $record, $out );
4420 sub script_plot_histogram
4422 # Martin A. Hansen, September 2007.
4424 # Plot a simple histogram for a given key using GNU plot.
4426 my ( $in, # handle to in stream
4427 $out, # handle to out stream
4428 $options, # options hash
4433 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4435 $options->{ "title" } ||= "Histogram";
4436 $options->{ "sort" } ||= "num";
4438 while ( $record = Maasha::Biopieces::get_record( $in ) )
4440 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4442 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4445 if ( $options->{ "sort" } eq "num" ) {
4446 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4448 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4451 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4453 $fh = write_stream( $options->{ "data_out" } );
4455 print $fh "$_\n" foreach @{ $result };
4461 sub script_plot_lendist
4463 # Martin A. Hansen, August 2007.
4465 # Plot length distribution using GNU plot.
4467 my ( $in, # handle to in stream
4468 $out, # handle to out stream
4469 $options, # options hash
4474 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4476 $options->{ "title" } ||= "Length Distribution";
4478 while ( $record = Maasha::Biopieces::get_record( $in ) )
4480 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4482 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4485 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4487 for ( $i = 0; $i < $max; $i++ ) {
4488 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4491 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4493 $fh = write_stream( $options->{ "data_out" } );
4495 print $fh "$_\n" foreach @{ $result };
4501 sub script_plot_chrdist
4503 # Martin A. Hansen, August 2007.
4505 # Plot chromosome distribution using GNU plot.
4507 my ( $in, # handle to in stream
4508 $out, # handle to out stream
4509 $options, # options hash
4514 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
4516 $options->{ "title" } ||= "Chromosome Distribution";
4518 while ( $record = Maasha::Biopieces::get_record( $in ) )
4520 if ( $record->{ "CHR" } ) { # generic
4521 $data_hash{ $record->{ "CHR" } }++;
4522 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
4523 $data_hash{ $record->{ "S_ID" } }++;
4524 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
4525 $data_hash{ $record->{ "S_ID" } }++;
4526 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
4527 $data_hash{ $record->{ "S_ID" } }++;
4530 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4533 foreach $elem ( keys %data_hash )
4537 $sort_key =~ s/chr//i;
4539 $sort_key =~ s/^X(.*)/99$1/;
4540 $sort_key =~ s/^Y(.*)/99$1/;
4541 $sort_key =~ s/^Z(.*)/999$1/;
4542 $sort_key =~ s/^M(.*)/9999$1/;
4543 $sort_key =~ s/^U(.*)/99999$1/;
4545 $count = $sort_key =~ tr/_//;
4547 $sort_key =~ s/_.*/"999999" x $count/ex;
4549 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
4552 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
4554 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
4556 $fh = write_stream( $options->{ "data_out" } );
4558 print $fh "$_\n" foreach @{ $result };
4564 sub script_plot_karyogram
4566 # Martin A. Hansen, August 2007.
4568 # Plot hits on karyogram.
4570 my ( $in, # handle to in stream
4571 $out, # handle to out stream
4572 $options, # options hash
4577 my ( %options, $record, @data, $fh, $result, %data_hash );
4579 $options->{ "genome" } ||= "human";
4580 $options->{ "feat_color" } ||= "black";
4582 while ( $record = Maasha::Biopieces::get_record( $in ) )
4584 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4586 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
4589 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4592 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
4594 $fh = write_stream( $options->{ "data_out" } );
4602 sub script_plot_matches
4604 # Martin A. Hansen, August 2007.
4606 # Plot matches in 2D generating a dotplot.
4608 my ( $in, # handle to in stream
4609 $out, # handle to out stream
4610 $options, # options hash
4615 my ( $record, @data, $fh, $result, %data_hash );
4617 $options->{ "direction" } ||= "both";
4619 while ( $record = Maasha::Biopieces::get_record( $in ) )
4621 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
4622 push @data, $record;
4625 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4628 $options->{ "title" } ||= "plot_matches";
4629 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
4630 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
4632 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
4634 $fh = write_stream( $options->{ "data_out" } );
4636 print $fh "$_\n" foreach @{ $result };
4642 sub script_length_vals
4644 # Martin A. Hansen, August 2007.
4646 # Determine the length of the value for given keys.
4648 my ( $in, # handle to in stream
4649 $out, # handle to out stream
4650 $options, # options hash
4655 my ( $record, $key );
4657 while ( $record = Maasha::Biopieces::get_record( $in ) )
4659 foreach $key ( @{ $options->{ "keys" } } )
4661 if ( $record->{ $key } ) {
4662 $record->{ $key . "_LEN" } = length $record->{ $key };
4666 Maasha::Biopieces::put_record( $record, $out );
4673 # Martin A. Hansen, August 2007.
4675 # Calculates the sums for values of given keys.
4677 my ( $in, # handle to in stream
4678 $out, # handle to out stream
4679 $options, # options hash
4684 my ( $record, $key, %sum_hash, $fh );
4686 while ( $record = Maasha::Biopieces::get_record( $in ) )
4688 foreach $key ( @{ $options->{ "keys" } } )
4690 if ( $record->{ $key } ) {
4691 $sum_hash{ $key } += $record->{ $key };
4695 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4698 $fh = write_stream( $options->{ "data_out" } );
4700 foreach $key ( @{ $options->{ "keys" } } ) {
4701 Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
4708 sub script_mean_vals
4710 # Martin A. Hansen, August 2007.
4712 # Calculate the mean of values of given keys.
4714 my ( $in, # handle to in stream
4715 $out, # handle to out stream
4716 $options, # options hash
4721 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
4723 while ( $record = Maasha::Biopieces::get_record( $in ) )
4725 foreach $key ( @{ $options->{ "keys" } } )
4727 if ( $record->{ $key } )
4729 $sum_hash{ $key } += $record->{ $key };
4730 $count_hash{ $key }++;
4734 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4737 $fh = write_stream( $options->{ "data_out" } );
4739 foreach $key ( @{ $options->{ "keys" } } )
4741 if ( $count_hash{ $key } ) {
4742 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
4747 Maasha::Biopieces::put_record( { $key . "_MEAN" => $mean } , $fh );
4754 sub script_median_vals
4756 # Martin A. Hansen, March 2008.
4758 # Calculate the median values of given keys.
4760 my ( $in, # handle to in stream
4761 $out, # handle to out stream
4762 $options, # options hash
4767 my ( $record, $key, %median_hash, $median, $fh );
4769 while ( $record = Maasha::Biopieces::get_record( $in ) )
4771 foreach $key ( @{ $options->{ "keys" } } ) {
4772 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
4775 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4778 $fh = write_stream( $options->{ "data_out" } );
4780 foreach $key ( @{ $options->{ "keys" } } )
4782 if ( $median_hash{ $key } ) {
4783 $median = Maasha::Calc::median( $median_hash{ $key } );
4788 Maasha::Biopieces::put_record( { $key . "_MEDIAN" => $median } , $fh );
4797 # Martin A. Hansen, February 2008.
4799 # Determine the maximum values of given keys.
4801 my ( $in, # handle to in stream
4802 $out, # handle to out stream
4803 $options, # options hash
4808 my ( $record, $key, $fh, %max_hash, $max_record );
4810 while ( $record = Maasha::Biopieces::get_record( $in ) )
4812 foreach $key ( @{ $options->{ "keys" } } )
4814 if ( $record->{ $key } )
4816 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
4820 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4823 $fh = write_stream( $options->{ "data_out" } );
4825 foreach $key ( @{ $options->{ "keys" } } )
4827 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
4830 Maasha::Biopieces::put_record( $max_record, $fh );
4838 # Martin A. Hansen, February 2008.
4840 # Determine the minimum values of given keys.
4842 my ( $in, # handle to in stream
4843 $out, # handle to out stream
4844 $options, # options hash
4849 my ( $record, $key, $fh, %min_hash, $min_record );
4851 while ( $record = Maasha::Biopieces::get_record( $in ) )
4853 foreach $key ( @{ $options->{ "keys" } } )
4855 if ( defined $record->{ $key } )
4857 if ( exists $min_hash{ $key } ) {
4858 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
4860 $min_hash{ $key } = $record->{ $key };
4865 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4868 $fh = write_stream( $options->{ "data_out" } );
4870 foreach $key ( @{ $options->{ "keys" } } )
4872 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
4875 Maasha::Biopieces::put_record( $min_record, $fh );
4881 sub script_upload_to_ucsc
4883 # Martin A. Hansen, August 2007.
4885 # Calculate the mean of values of given keys.
4887 # This routine has developed into the most ugly hack. Do something!
4889 my ( $in, # handle to in stream
4890 $out, # handle to out stream
4891 $options, # options hash
4896 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
4898 $options->{ "short_label" } ||= $options->{ 'table' };
4899 $options->{ "long_label" } ||= $options->{ 'table' };
4900 $options->{ "group" } ||= $ENV{ "LOGNAME" };
4901 $options->{ "priority" } ||= 1;
4902 $options->{ "visibility" } ||= "pack";
4903 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
4904 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
4906 $file = "$BP_TMP/ucsc_upload.tmp";
4911 $fh_out = Maasha::Common::write_open( $file );
4913 while ( $record = Maasha::Biopieces::get_record( $in ) )
4915 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4917 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
4921 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4922 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
4925 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
4929 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
4930 Maasha::UCSC::psl_put_entry( $record, $fh_out );
4934 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
4936 # chrom chromStart chromEnd name score strand size secStr conf
4940 print $fh_out join ( "\t",
4942 $record->{ "CHR_BEG" },
4943 $record->{ "CHR_END" } + 1,
4944 $record->{ "Q_ID" },
4945 $record->{ "SCORE" },
4946 $record->{ "STRAND" },
4947 $record->{ "SIZE" },
4948 $record->{ "SEC_STRUCT" },
4949 $record->{ "CONF" },
4952 elsif ( $record->{ "REC_TYPE" } eq "BED" )
4955 $columns = $record->{ "BED_COLS" };
4957 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4958 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4961 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
4966 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4967 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4970 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
4975 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
4977 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4978 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4981 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
4986 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4987 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4991 if ( $i == $options->{ "chunk_size" } )
4995 if ( $format eq "BED" ) {
4996 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4997 } elsif ( $format eq "PSL" ) {
4998 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5007 $fh_out = Maasha::Common::write_open( $file );
5015 if ( exists $options->{ "database" } and $options->{ "table" } )
5017 if ( $format eq "BED" )
5019 $type = "bed $columns";
5021 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5023 elsif ( $format eq "BED_SS" )
5025 $type = "type bed 6 +";
5027 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5029 elsif ( $format eq "PSL" )
5033 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5035 elsif ( $format eq "WIGGLE" )
5037 $options->{ "visibility" } = "full";
5039 $wig_file = "$options->{ 'table' }.wig";
5040 $wib_file = "$options->{ 'table' }.wib";
5042 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5044 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5046 if ( $options->{ 'verbose' } ) {
5047 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
5049 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5052 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5060 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5065 Maasha::UCSC::ucsc_update_config( $options, $type );
5070 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<