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 "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
137 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
138 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
139 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
140 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
141 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
142 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
143 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
144 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
145 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
146 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
147 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
148 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
149 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
150 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
151 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
152 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
153 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
154 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
155 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
156 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
157 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
158 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
159 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
160 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
161 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
162 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
163 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
164 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
165 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
166 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
167 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
168 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
169 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
170 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
171 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
172 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
173 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
174 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
175 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
176 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
177 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
178 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
179 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
180 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
181 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
182 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
183 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
184 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
185 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
186 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
187 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
188 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
189 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
190 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
191 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
192 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
193 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
194 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
195 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
196 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
197 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
199 close $in if defined $in;
202 $t1 = gettimeofday();
204 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
210 # Martin A. Hansen, February 2008.
212 # Gets options from commandline and checks these vigerously.
214 my ( $script, # name of script
219 my ( %options, @options, $opt, @genomes, $real );
221 if ( $script eq "print_usage" )
227 elsif ( $script eq "read_psl" )
234 elsif ( $script eq "read_bed" )
243 elsif ( $script eq "read_fixedstep" )
250 elsif ( $script eq "read_blast_tab" )
257 elsif ( $script eq "read_embl" )
267 elsif ( $script eq "read_stockholm" )
274 elsif ( $script eq "read_phastcons" )
285 elsif ( $script eq "read_soft" )
293 elsif ( $script eq "read_gff" )
300 elsif ( $script eq "read_2bit" )
308 elsif ( $script eq "read_solexa" )
317 elsif ( $script eq "read_solid" )
325 elsif ( $script eq "read_mysql" )
334 elsif ( $script eq "read_ucsc_config" )
341 elsif ( $script eq "assemble_tag_contigs" )
347 elsif ( $script eq "calc_fixedstep" )
353 elsif ( $script eq "length_seq" )
360 elsif ( $script eq "oligo_freq" )
367 elsif ( $script eq "create_weight_matrix" )
373 elsif ( $script eq "transliterate_seq" )
381 elsif ( $script eq "transliterate_vals" )
390 elsif ( $script eq "translate_seq" )
396 elsif ( $script eq "get_genome_align" )
407 elsif ( $script eq "get_genome_phastcons" )
418 elsif ( $script eq "split_seq" )
425 elsif ( $script eq "split_bed" )
432 elsif ( $script eq "tile_seq" )
439 elsif ( $script eq "invert_align" )
445 elsif ( $script eq "patscan_seq" )
456 elsif ( $script eq "blat_seq" )
468 elsif ( $script eq "soap_seq" )
479 elsif ( $script eq "match_seq" )
486 elsif ( $script eq "create_vmatch_index" )
494 elsif ( $script eq "write_align" )
504 elsif ( $script eq "write_bed" )
514 elsif ( $script eq "write_psl" )
522 elsif ( $script eq "write_fixedstep" )
530 elsif ( $script eq "write_2bit" )
538 elsif ( $script eq "write_solid" )
547 elsif ( $script eq "write_ucsc_config" )
554 elsif ( $script eq "plot_seqlogo" )
561 elsif ( $script eq "plot_phastcons_profiles" )
576 elsif ( $script eq "analyze_vals" )
583 elsif ( $script eq "head_records" )
589 elsif ( $script eq "remove_keys" )
596 elsif ( $script eq "remove_adaptor" )
605 elsif ( $script eq "remove_mysql_tables" )
616 elsif ( $script eq "remove_ucsc_tracks" )
628 elsif ( $script eq "rename_keys" )
634 elsif ( $script eq "uniq_vals" )
641 elsif ( $script eq "merge_vals" )
648 elsif ( $script eq "merge_records" )
655 elsif ( $script eq "compute" )
661 elsif ( $script eq "count_records" )
668 elsif ( $script eq "random_records" )
674 elsif ( $script eq "sort_records" )
681 elsif ( $script eq "count_vals" )
687 elsif ( $script eq "plot_histogram" )
700 elsif ( $script eq "plot_lendist" )
712 elsif ( $script eq "plot_chrdist" )
723 elsif ( $script eq "plot_karyogram" )
732 elsif ( $script eq "plot_matches" )
744 elsif ( $script eq "length_vals" )
750 elsif ( $script eq "sum_vals" )
758 elsif ( $script eq "mean_vals" )
766 elsif ( $script eq "median_vals" )
774 elsif ( $script eq "max_vals" )
782 elsif ( $script eq "min_vals" )
790 elsif ( $script eq "upload_to_ucsc" )
814 # print STDERR Dumper( \@options );
821 # print STDERR Dumper( \%options );
823 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
824 return wantarray ? %options : \%options;
827 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
828 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
829 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
830 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
831 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
832 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
833 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
834 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
835 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
836 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
838 # ---- check arguments ----
840 if ( $options{ 'data_in' } )
842 $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
844 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
847 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
849 # print STDERR Dumper( \%options );
851 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
853 foreach $opt ( keys %options )
855 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
857 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
859 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
861 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
863 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
865 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
867 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
869 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
871 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
873 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
875 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
877 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
879 elsif ( $opt eq "genome" )
881 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
882 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
884 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
885 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
888 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
890 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
892 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
894 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
896 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
898 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
900 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
902 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
904 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
906 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
910 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
911 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
912 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
913 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
914 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
915 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
916 Maasha::Common::error( qq(no --keys speficied) ) if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
918 if ( $script eq "upload_to_ucsc" )
920 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
921 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
924 return wantarray ? %options : \%options;
928 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
931 sub script_print_usage
933 # Martin A. Hansen, January 2008.
935 # Retrieves usage information from file and
936 # prints this nicely formatted.
938 my ( $in, # handle to in stream
939 $out, # handle to out stream
940 $options, # options hash
945 my ( $file, $wiki, $lines );
947 if ( $options->{ 'data_in' } ) {
948 $file = $options->{ 'data_in' };
950 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
953 $wiki = Maasha::Gwiki::gwiki_read( $file );
955 ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
957 if ( not $options->{ "help" } ) {
958 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
961 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
963 print STDERR "$_\n" foreach @{ $lines };
971 # Martin A. Hansen, August 2007.
973 # Read psl table from stream or file.
975 my ( $in, # handle to in stream
976 $out, # handle to out stream
977 $options, # options hash
982 my ( $record, $file, $data_in, $num );
984 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
985 Maasha::Biopieces::put_record( $record, $out );
990 foreach $file ( @{ $options->{ "files" } } )
992 $data_in = Maasha::Common::read_open( $file );
994 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
996 Maasha::Biopieces::put_record( $record, $out );
998 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1010 # Martin A. Hansen, August 2007.
1012 # Read bed table from stream or file.
1014 my ( $in, # handle to in stream
1015 $out, # handle to out stream
1016 $options, # options hash
1021 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1023 $cols = $options->{ 'cols' }->[ 0 ];
1025 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1026 Maasha::Biopieces::put_record( $record, $out );
1031 foreach $file ( @{ $options->{ "files" } } )
1033 $data_in = Maasha::Common::read_open( $file );
1035 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1037 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1039 Maasha::Biopieces::put_record( $record, $out );
1041 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1051 close $data_in if $data_in;
1055 sub script_read_fixedstep
1057 # Martin A. Hansen, Juli 2008.
1059 # Read fixedstep wiggle format from stream or file.
1061 my ( $in, # handle to in stream
1062 $out, # handle to out stream
1063 $options, # options hash
1068 my ( $file, $record, $entry, $data_in, $num );
1070 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1071 Maasha::Biopieces::put_record( $record, $out );
1076 foreach $file ( @{ $options->{ "files" } } )
1078 $data_in = Maasha::Common::read_open( $file );
1080 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1082 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1084 Maasha::Biopieces::put_record( $record, $out );
1086 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1096 close $data_in if $data_in;
1100 sub script_read_blast_tab
1102 # Martin A. Hansen, September 2007.
1104 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1106 my ( $in, # handle to in stream
1107 $out, # handle to out stream
1108 $options, # options hash
1113 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1115 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1116 Maasha::Biopieces::put_record( $record, $out );
1121 foreach $file ( @{ $options->{ "files" } } )
1123 $data_in = Maasha::Common::read_open( $file );
1125 while ( $line = <$data_in> )
1129 next if $line =~ /^#/;
1131 @fields = split /\t/, $line;
1133 $record->{ "REC_TYPE" } = "BLAST";
1134 $record->{ "Q_ID" } = $fields[ 0 ];
1135 $record->{ "S_ID" } = $fields[ 1 ];
1136 $record->{ "IDENT" } = $fields[ 2 ];
1137 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1138 $record->{ "MISMATCHES" } = $fields[ 4 ];
1139 $record->{ "GAPS" } = $fields[ 5 ];
1140 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1141 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1142 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1143 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1144 $record->{ "E_VAL" } = $fields[ 10 ];
1145 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1147 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1149 $record->{ "STRAND" } = '-';
1151 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1155 $record->{ "STRAND" } = '+';
1158 Maasha::Biopieces::put_record( $record, $out );
1160 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1170 close $data_in if $data_in;
1174 sub script_read_embl
1176 # Martin A. Hansen, August 2007.
1180 my ( $in, # handle to in stream
1181 $out, # handle to out stream
1182 $options, # options hash
1187 my ( %options2, $file, $data_in, $num, $entry, $record );
1189 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1190 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1191 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1193 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1194 Maasha::Biopieces::put_record( $record, $out );
1199 foreach $file ( @{ $options->{ "files" } } )
1201 $data_in = Maasha::Common::read_open( $file );
1203 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1205 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1207 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1209 $record_copy = dclone $record;
1211 delete $record_copy->{ "FT" };
1213 Maasha::Biopieces::put_record( $record_copy, $out );
1215 delete $record_copy->{ "SEQ" };
1217 foreach $feat ( keys %{ $record->{ "FT" } } )
1219 $record_copy->{ "FEAT_TYPE" } = $feat;
1221 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1223 foreach $qual ( keys %{ $feat2 } )
1225 $qual_val = join "; ", @{ $feat2->{ $qual } };
1230 $record_copy->{ $qual } = $qual_val;
1233 Maasha::Biopieces::put_record( $record_copy, $out );
1237 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1247 close $data_in if $data_in;
1251 sub script_read_stockholm
1253 # Martin A. Hansen, August 2007.
1255 # Read Stockholm format.
1257 my ( $in, # handle to in stream
1258 $out, # handle to out stream
1259 $options, # options hash
1264 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1266 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1267 Maasha::Biopieces::put_record( $record, $out );
1272 foreach $file ( @{ $options->{ "files" } } )
1274 $data_in = Maasha::Common::read_open( $file );
1276 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1278 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1282 foreach $key ( keys %{ $record->{ "GF" } } ) {
1283 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1286 $record_anno->{ "ALIGN" } = $num;
1288 Maasha::Biopieces::put_record( $record_anno, $out );
1290 foreach $seq ( @{ $record->{ "ALIGN" } } )
1292 undef $record_align;
1295 SEQ_NAME => $seq->[ 0 ],
1299 Maasha::Biopieces::put_record( $record_align, $out );
1302 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1312 close $data_in if $data_in;
1316 sub script_read_phastcons
1318 # Martin A. Hansen, December 2007.
1320 # Read PhastCons format.
1322 my ( $in, # handle to in stream
1323 $out, # handle to out stream
1324 $options, # options hash
1329 my ( $data_in, $file, $num, $entry, @records, $record );
1331 $options->{ "min" } ||= 10;
1332 $options->{ "dist" } ||= 25;
1333 $options->{ "threshold" } ||= 0.8;
1334 $options->{ "gap" } ||= 5;
1336 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1337 Maasha::Biopieces::put_record( $record, $out );
1342 foreach $file ( @{ $options->{ "files" } } )
1344 $data_in = Maasha::Common::read_open( $file );
1346 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1348 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1350 foreach $record ( @records )
1352 $record->{ "REC_TYPE" } = "BED";
1353 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1355 Maasha::Biopieces::put_record( $record, $out );
1357 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1368 close $data_in if $data_in;
1372 sub script_read_soft
1374 # Martin A. Hansen, December 2007.
1377 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1379 my ( $in, # handle to in stream
1380 $out, # handle to out stream
1381 $options, # options hash
1386 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1388 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1389 Maasha::Biopieces::put_record( $record, $out );
1394 foreach $file ( @{ $options->{ "files" } } )
1396 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1398 $soft_index = Maasha::NCBI::soft_index_file( $file );
1400 $fh = Maasha::Common::read_open( $file );
1402 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1404 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1406 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1408 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1410 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1412 foreach $sample ( @samples )
1415 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1417 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1419 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1421 foreach $record ( @{ $records } )
1423 Maasha::Biopieces::put_record( $record, $out );
1425 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1430 $old_end = $sample->{ "LINE_END" };
1438 close $data_in if $data_in;
1445 # Martin A. Hansen, February 2008.
1448 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1450 my ( $in, # handle to in stream
1451 $out, # handle to out stream
1452 $options, # options hash
1457 my ( $data_in, $file, $fh, $num, $record, $entry );
1459 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1460 Maasha::Biopieces::put_record( $record, $out );
1465 foreach $file ( @{ $options->{ "files" } } )
1467 $fh = Maasha::Common::read_open( $file );
1469 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1471 Maasha::Biopieces::put_record( $entry, $out );
1473 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1483 close $data_in if $data_in;
1487 sub script_read_2bit
1489 # Martin A. Hansen, March 2008.
1491 # Read sequences from 2bit file.
1493 my ( $in, # handle to in stream
1494 $out, # handle to out stream
1495 $options, # options hash
1500 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1502 $mask = 1 if not $options->{ "no_mask" };
1504 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1505 Maasha::Biopieces::put_record( $record, $out );
1510 foreach $file ( @{ $options->{ "files" } } )
1512 $data_in = Maasha::Common::read_open( $file );
1514 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1516 foreach $line ( @{ $toc } )
1518 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1519 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1520 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1522 Maasha::Biopieces::put_record( $record, $out );
1524 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1534 close $data_in if $data_in;
1538 sub script_read_solexa
1540 # Martin A. Hansen, March 2008.
1542 # Read Solexa sequence reads from file.
1544 my ( $in, # handle to in stream
1545 $out, # handle to out stream
1546 $options, # options hash
1551 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1553 $options->{ "format" } ||= "octal";
1554 $options->{ "quality" } ||= 20;
1556 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1557 Maasha::Biopieces::put_record( $record, $out );
1562 foreach $file ( @{ $options->{ "files" } } )
1564 $data_in = Maasha::Common::read_open( $file );
1566 if ( $options->{ "format" } eq "octal" )
1568 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1570 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1572 Maasha::Biopieces::put_record( $record, $out );
1574 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1581 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1583 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1585 Maasha::Biopieces::put_record( $record, $out );
1587 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1598 close $data_in if $data_in;
1602 sub script_read_solid
1604 # Martin A. Hansen, April 2008.
1606 # Read Solid sequence from file.
1608 my ( $in, # handle to in stream
1609 $out, # handle to out stream
1610 $options, # options hash
1615 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1617 $options->{ "quality" } ||= 15;
1619 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1620 Maasha::Biopieces::put_record( $record, $out );
1625 foreach $file ( @{ $options->{ "files" } } )
1627 $data_in = Maasha::Common::read_open( $file );
1629 while ( $line = <$data_in> )
1633 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1635 @scores = split /,/, $seq_qual;
1636 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1638 for ( $i = 0; $i < @seqs; $i++ ) {
1639 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1643 REC_TYPE => 'SOLID',
1644 SEQ_NAME => $seq_name,
1646 SEQ_QUAL => join( ";", @scores ),
1647 SEQ_LEN => length $seq_cs,
1648 SEQ => join( "", @seqs ),
1649 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1652 Maasha::Biopieces::put_record( $record, $out );
1654 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1664 close $data_in if $data_in;
1668 sub script_read_mysql
1670 # Martin A. Hansen, May 2008.
1672 # Read a MySQL query into stream.
1674 my ( $in, # handle to in stream
1675 $out, # handle to out stream
1676 $options, # options hash
1681 my ( $record, $dbh, $results );
1683 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1684 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1686 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1687 Maasha::Biopieces::put_record( $record, $out );
1690 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1692 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1694 Maasha::SQL::disconnect( $dbh );
1696 map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1700 sub script_read_ucsc_config
1702 # Martin A. Hansen, November 2008.
1704 # Read track entries from UCSC Genome Browser '.ra' files.
1706 my ( $in, # handle to in stream
1707 $out, # handle to out stream
1708 $options, # options hash
1713 my ( $record, $file, $data_in, $entry, $num );
1715 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1716 Maasha::Biopieces::put_record( $record, $out );
1721 foreach $file ( @{ $options->{ "files" } } )
1723 $data_in = Maasha::Common::read_open( $file );
1725 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
1727 $record->{ 'REC_TYPE' } = "UCSC Config";
1729 Maasha::Biopieces::put_record( $record, $out );
1731 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1741 close $data_in if $data_in;
1745 sub script_assemble_tag_contigs
1747 # Martin A. Hansen, November 2008.
1749 # Assemble tags from the stream into
1752 # The current implementation is quite
1753 # slow because of heavy use of temporary
1756 my ( $in, # handle to in stream
1757 $out, # handle to out stream
1758 $options, # options hash
1763 my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
1765 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
1766 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
1767 $cols = 6; # we only need the first 6 BED columns
1769 while ( $record = Maasha::Biopieces::get_record( $in ) )
1771 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
1773 $strand = $record->{ 'STRAND' } || '+';
1775 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
1781 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
1785 foreach $chr ( sort keys %{ $file_hash } )
1787 $bed_file = $file_hash->{ $chr };
1788 $tag_file = "$bed_file.tc";
1790 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
1792 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
1794 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
1796 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
1797 Maasha::Biopieces::put_record( $record, $out );
1809 sub script_length_seq
1811 # Martin A. Hansen, August 2007.
1813 # Determine the length of sequences in stream.
1815 my ( $in, # handle to in stream
1816 $out, # handle to out stream
1817 $options, # options hash
1822 my ( $record, $total );
1824 while ( $record = Maasha::Biopieces::get_record( $in ) )
1826 if ( $record->{ "SEQ" } )
1828 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1829 $total += $record->{ "SEQ_LEN" };
1832 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1835 Maasha::Biopieces::put_record( { TOTAL_SEQ_LEN => $total }, $out );
1839 sub script_analyze_seq
1841 # Martin A. Hansen, August 2007.
1843 # Analyze sequence composition of sequences in stream.
1845 my ( $in, # handle to in stream
1846 $out, # handle to out stream
1851 my ( $record, $analysis );
1853 while ( $record = Maasha::Biopieces::get_record( $in ) )
1855 if ( $record->{ "SEQ" } )
1857 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
1859 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
1862 Maasha::Biopieces::put_record( $record, $out );
1867 sub script_analyze_tags
1869 # Martin A. Hansen, August 2008.
1871 # Analyze sequence tags in stream.
1873 my ( $in, # handle to in stream
1874 $out, # handle to out stream
1879 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
1881 while ( $record = Maasha::Biopieces::get_record( $in ) )
1883 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
1885 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
1889 $len_hash{ length( $record->{ "SEQ" } ) }++;
1890 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
1893 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
1895 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
1899 $len_hash{ $record->{ "BED_LEN" } }++;
1900 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
1905 foreach $key ( sort { $a <=> $b } keys %len_hash )
1907 $tag_record->{ "TAG_LEN" } = $key;
1908 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
1909 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
1911 Maasha::Biopieces::put_record( $tag_record, $out );
1916 sub script_complexity_seq
1918 # Martin A. Hansen, May 2008.
1920 # Generates an index calculated as the most common di-residue over
1921 # the sequence length for all sequences in stream.
1923 my ( $in, # handle to in stream
1924 $out, # handle to out stream
1929 my ( $record, $index );
1931 while ( $record = Maasha::Biopieces::get_record( $in ) )
1933 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1935 Maasha::Biopieces::put_record( $record, $out );
1940 sub script_oligo_freq
1942 # Martin A. Hansen, August 2007.
1944 # Determine the length of sequences in stream.
1946 my ( $in, # handle to in stream
1947 $out, # handle to out stream
1948 $options, # options hash
1953 my ( $record, %oligos, @freq_table );
1955 $options->{ "word_size" } ||= 7;
1957 while ( $record = Maasha::Biopieces::get_record( $in ) )
1959 if ( $record->{ "SEQ" } )
1961 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
1963 if ( not $options->{ "all" } )
1965 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1967 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1973 Maasha::Biopieces::put_record( $record, $out );
1976 if ( $options->{ "all" } )
1978 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1980 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1985 sub script_create_weight_matrix
1987 # Martin A. Hansen, August 2007.
1989 # Creates a weight matrix from an alignmnet.
1991 my ( $in, # handle to in stream
1992 $out, # handle to out stream
1993 $options, # options hash
1998 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2002 while ( $record = Maasha::Biopieces::get_record( $in ) )
2004 if ( $record->{ "SEQ" } )
2006 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2008 $res = substr $record->{ "SEQ" }, $i, 1;
2010 $freq_hash{ $i }{ $res }++;
2011 $res_hash{ $res } = 1;
2018 Maasha::Biopieces::put_record( $record, $out );
2022 foreach $res ( sort keys %res_hash )
2026 $record->{ "V0" } = $res;
2028 for ( $i = 0; $i < keys %freq_hash; $i++ )
2030 $freq = $freq_hash{ $i }{ $res } || 0;
2032 if ( $options->{ "percent" } ) {
2033 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2036 $record->{ "V" . ( $i + 1 ) } = $freq;
2039 Maasha::Biopieces::put_record( $record, $out );
2044 sub script_calc_bit_scores
2046 # Martin A. Hansen, March 2007.
2048 # Calculates the bit scores for each position from an alignmnet in the stream.
2050 my ( $in, # handle to in stream
2051 $out, # handle to out stream
2056 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2060 while ( $record = Maasha::Biopieces::get_record( $in ) )
2062 if ( $record->{ "SEQ" } )
2064 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2066 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2068 $res = substr $record->{ "SEQ" }, $i, 1;
2070 next if $res =~ /-|_|~|\./;
2072 $freq_hash{ $i }{ $res }++;
2079 Maasha::Biopieces::put_record( $record, $out );
2085 if ( $type eq "protein" ) {
2091 for ( $i = 0; $i < keys %freq_hash; $i++ )
2093 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2095 $bit_diff = $bit_max - $bit_height;
2097 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2100 Maasha::Biopieces::put_record( $record, $out );
2104 sub script_calc_fixedstep
2106 # Martin A. Hansen, September 2008.
2108 # Calculates fixedstep entries from data in the stream.
2110 my ( $in, # handle to in stream
2111 $out, # handle to out stream
2112 $options, # options hash
2117 my ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2119 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2120 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2122 while ( $record = Maasha::Biopieces::get_record( $in ) )
2124 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) {
2125 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } );
2131 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP );
2135 foreach $chr ( sort keys %{ $file_hash } )
2137 $bed_file = $file_hash->{ $chr };
2139 $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } );
2141 #$fixedstep_file = "$bed_file.fixedstep";
2143 # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2145 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2147 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2149 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2150 Maasha::Biopieces::put_record( $record, $out );
2157 unlink $fixedstep_file;
2162 sub script_remove_indels
2164 # Martin A. Hansen, August 2007.
2166 # Remove indels from sequences in stream.
2168 my ( $in, # handle to in stream
2169 $out, # handle to out stream
2176 while ( $record = Maasha::Biopieces::get_record( $in ) )
2178 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2180 Maasha::Biopieces::put_record( $record, $out );
2185 sub script_transliterate_seq
2187 # Martin A. Hansen, August 2007.
2189 # Transliterate chars from sequence in record.
2191 my ( $in, # handle to in stream
2192 $out, # handle to out stream
2193 $options, # options hash
2198 my ( $record, $search, $replace, $delete );
2200 $search = $options->{ "search" } || "";
2201 $replace = $options->{ "replace" } || "";
2202 $delete = $options->{ "delete" } || "";
2204 while ( $record = Maasha::Biopieces::get_record( $in ) )
2206 if ( $record->{ "SEQ" } )
2208 if ( $search and $replace ) {
2209 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2210 } elsif ( $delete ) {
2211 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2215 Maasha::Biopieces::put_record( $record, $out );
2220 sub script_transliterate_vals
2222 # Martin A. Hansen, April 2008.
2224 # Transliterate chars from values in record.
2226 my ( $in, # handle to in stream
2227 $out, # handle to out stream
2228 $options, # options hash
2233 my ( $record, $search, $replace, $delete, $key );
2235 $search = $options->{ "search" } || "";
2236 $replace = $options->{ "replace" } || "";
2237 $delete = $options->{ "delete" } || "";
2239 while ( $record = Maasha::Biopieces::get_record( $in ) )
2241 foreach $key ( @{ $options->{ "keys" } } )
2243 if ( exists $record->{ $key } )
2245 if ( $search and $replace ) {
2246 eval "\$record->{ $key } =~ tr/$search/$replace/";
2247 } elsif ( $delete ) {
2248 eval "\$record->{ $key } =~ tr/$delete//d";
2253 Maasha::Biopieces::put_record( $record, $out );
2258 sub script_translate_seq
2260 # Martin A. Hansen, February 2008.
2262 # Translate DNA sequence into protein sequence.
2264 my ( $in, # handle to in stream
2265 $out, # handle to out stream
2266 $options, # options hash
2271 my ( $record, $frame, %new_record );
2273 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2275 while ( $record = Maasha::Biopieces::get_record( $in ) )
2277 if ( $record->{ "SEQ" } )
2279 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2281 foreach $frame ( @{ $options->{ "frames" } } )
2283 %new_record = %{ $record };
2285 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2286 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2287 $new_record{ "FRAME" } = $frame;
2289 Maasha::Biopieces::put_record( \%new_record, $out );
2295 Maasha::Biopieces::put_record( $record, $out );
2301 sub script_get_genome_align
2303 # Martin A. Hansen, April 2008.
2305 # Gets a subalignment from a multiple genome alignment.
2307 my ( $in, # handle to in stream
2308 $out, # handle to out stream
2309 $options, # options hash
2314 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2316 $options->{ "strand" } ||= "+";
2320 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2322 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2324 $beg = $options->{ "beg" } - 1;
2326 if ( $options->{ "end" } ) {
2327 $end = $options->{ "end" };
2328 } elsif ( $options->{ "len" } ) {
2329 $end = $beg + $options->{ "len" };
2332 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2334 foreach $entry ( @{ $align } )
2336 $entry->{ "CHR" } = $record->{ "CHR" };
2337 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2338 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2339 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2340 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2341 $entry->{ "SCORE" } = $record->{ "SCORE" };
2343 Maasha::Biopieces::put_record( $entry, $out );
2347 while ( $record = Maasha::Biopieces::get_record( $in ) )
2349 if ( $record->{ "REC_TYPE" } eq "BED" )
2351 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2353 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
2355 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
2357 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2359 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2361 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2363 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "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 );
2383 sub script_get_genome_phastcons
2385 # Martin A. Hansen, February 2008.
2387 # Get phastcons scores from genome intervals.
2389 my ( $in, # handle to in stream
2390 $out, # handle to out stream
2391 $options, # options hash
2396 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2398 $options->{ "flank" } ||= 0;
2400 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2401 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2403 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
2404 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2406 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2408 $options->{ "beg" } -= 1; # request is 1-based
2409 $options->{ "end" } -= 1; # request is 1-based
2411 if ( $options->{ "len" } ) {
2412 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2415 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2417 $record->{ "CHR" } = $options->{ "chr" };
2418 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2419 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2421 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2422 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2424 Maasha::Biopieces::put_record( $record, $out );
2427 while ( $record = Maasha::Biopieces::get_record( $in ) )
2429 if ( $record->{ "REC_TYPE" } eq "BED" )
2431 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2433 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2435 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2437 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2439 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2442 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2443 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2445 Maasha::Biopieces::put_record( $record, $out );
2448 close $fh_phastcons if $fh_phastcons;
2454 # Martin A. Hansen, December 2007.
2456 # Folds sequences in stream into secondary structures.
2458 my ( $in, # handle to in stream
2459 $out, # handle to out stream
2464 my ( $record, $type, $struct, $index );
2466 while ( $record = Maasha::Biopieces::get_record( $in ) )
2468 if ( $record->{ "SEQ" } )
2471 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2474 if ( $type ne "protein" )
2476 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2477 $record->{ "SEC_STRUCT" } = $struct;
2478 $record->{ "FREE_ENERGY" } = $index;
2479 $record->{ "SCORE" } = abs int $index;
2480 $record->{ "SIZE" } = length $struct;
2481 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2485 Maasha::Biopieces::put_record( $record, $out );
2490 sub script_split_seq
2492 # Martin A. Hansen, August 2007.
2494 # Split a sequence in stream into words.
2496 my ( $in, # handle to in stream
2497 $out, # handle to out stream
2498 $options, # options hash
2503 my ( $record, $new_record, $i, $subseq, %lookup );
2505 $options->{ "word_size" } ||= 7;
2507 while ( $record = Maasha::Biopieces::get_record( $in ) )
2509 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2511 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2513 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2515 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2517 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2518 $new_record->{ "SEQ" } = $subseq;
2520 Maasha::Biopieces::put_record( $new_record, $out );
2522 $lookup{ $subseq } = 1;
2526 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2527 $new_record->{ "SEQ" } = $subseq;
2529 Maasha::Biopieces::put_record( $new_record, $out );
2535 Maasha::Biopieces::put_record( $record, $out );
2541 sub script_split_bed
2543 # Martin A. Hansen, June 2008.
2545 # Split a BED record into overlapping windows.
2547 my ( $in, # handle to in stream
2548 $out, # handle to out stream
2549 $options, # options hash
2554 my ( $record, $new_record, $i );
2556 $options->{ "window_size" } ||= 20;
2557 $options->{ "step_size" } ||= 1;
2559 while ( $record = Maasha::Biopieces::get_record( $in ) )
2561 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2563 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2565 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2567 $new_record->{ "REC_TYPE" } = "BED";
2568 $new_record->{ "CHR" } = $record->{ "CHR" };
2569 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
2570 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2571 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
2572 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
2573 $new_record->{ "SCORE" } = $record->{ "SCORE" };
2574 $new_record->{ "STRAND" } = $record->{ "STRAND" };
2576 Maasha::Biopieces::put_record( $new_record, $out );
2581 Maasha::Biopieces::put_record( $record, $out );
2587 sub script_align_seq
2589 # Martin A. Hansen, August 2007.
2591 # Align sequences in stream.
2593 my ( $in, # handle to in stream
2594 $out, # handle to out stream
2599 my ( $record, @entries, $entry );
2601 while ( $record = Maasha::Biopieces::get_record( $in ) )
2603 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2604 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2605 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
2606 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
2608 Maasha::Biopieces::put_record( $record, $out );
2612 @entries = Maasha::Align::align( \@entries );
2614 foreach $entry ( @entries )
2616 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
2619 SEQ_NAME => $entry->[ SEQ_NAME ],
2620 SEQ => $entry->[ SEQ ],
2623 Maasha::Biopieces::put_record( $record, $out );
2631 # Martin A. Hansen, February 2008.
2633 # Using the first sequence in stream as reference, tile
2634 # all subsequent sequences based on pairwise alignments.
2636 my ( $in, # handle to in stream
2637 $out, # handle to out stream
2638 $options, # options hash
2643 my ( $record, $first, $ref_entry, @entries );
2647 while ( $record = Maasha::Biopieces::get_record( $in ) )
2649 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2653 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2659 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2664 Maasha::Biopieces::put_record( $record, $out );
2668 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2670 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2674 sub script_invert_align
2676 # Martin A. Hansen, February 2008.
2678 # Inverts an alignment showing only non-mathing residues
2679 # using the first sequence as reference.
2681 my ( $in, # handle to in stream
2682 $out, # handle to out stream
2683 $options, # options hash
2688 my ( $record, @entries );
2690 while ( $record = Maasha::Biopieces::get_record( $in ) )
2692 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2694 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2698 Maasha::Biopieces::put_record( $record, $out );
2702 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
2704 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2708 sub script_patscan_seq
2710 # Martin A. Hansen, August 2007.
2712 # Locates patterns in sequences using scan_for_matches.
2714 my ( $in, # handle to in stream
2715 $out, # handle to out stream
2716 $options, # options hash
2721 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
2723 if ( $options->{ "patterns" } ) {
2724 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
2725 } elsif ( -f $options->{ "patterns_in" } ) {
2726 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
2729 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
2731 push @args, "-c" if $options->{ "comp" };
2732 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
2733 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
2735 $seq_file = "$BP_TMP/patscan.seq";
2736 $pat_file = "$BP_TMP/patscan.pat";
2737 $out_file = "$BP_TMP/patscan.out";
2739 $fh_out = Maasha::Common::write_open( $seq_file );
2743 while ( $record = Maasha::Biopieces::get_record( $in ) )
2745 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
2747 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2749 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
2751 $head_hash{ $i } = $record->{ "SEQ_NAME" };
2759 $arg = join " ", @args;
2760 $arg .= " -p" if $type eq "protein";
2762 foreach $pattern ( @{ $patterns } )
2764 $fh_out = Maasha::Common::write_open( $pat_file );
2766 print $fh_out "$pattern\n";
2770 if ( $options->{ 'genome' } ) {
2771 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
2772 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
2774 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
2775 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
2778 $fh_in = Maasha::Common::read_open( $out_file );
2780 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
2782 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
2784 if ( $options->{ 'genome' } )
2786 $result->{ "CHR" } = $result->{ "S_ID" };
2787 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
2788 $result->{ "CHR_END" } = $result->{ "S_END" };
2790 delete $result->{ "S_ID" };
2791 delete $result->{ "S_BEG" };
2792 delete $result->{ "S_END" };
2796 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
2799 Maasha::Biopieces::put_record( $result, $out );
2813 # Martin A. Hansen, August 2007.
2815 # BLATs sequences in stream against a given genome.
2817 my ( $in, # handle to in stream
2818 $out, # handle to out stream
2819 $options, # options hash
2824 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
2826 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2828 $options->{ 'tile_size' } ||= 11;
2829 $options->{ 'one_off' } ||= 0;
2830 $options->{ 'min_identity' } ||= 90;
2831 $options->{ 'min_score' } ||= 0;
2832 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
2834 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
2835 $blat_args .= " -oneOff=$options->{ 'one_off' }";
2836 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
2837 $blat_args .= " -minScore=$options->{ 'min_score' }";
2838 $blat_args .= " -stepSize=$options->{ 'step_size' }";
2839 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
2841 $query_file = "$BP_TMP/blat.seq";
2843 $fh_out = Maasha::Common::write_open( $query_file );
2845 while ( $record = Maasha::Biopieces::get_record( $in ) )
2847 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2849 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
2850 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
2853 Maasha::Biopieces::put_record( $record, $out );
2858 $blat_args .= " -t=dnax" if $type eq "protein";
2859 $blat_args .= " -q=$type";
2861 $result_file = "$BP_TMP/blat.psl";
2863 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
2867 $entries = Maasha::UCSC::psl_get_entries( $result_file );
2869 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $entries };
2871 unlink $result_file;
2877 # Martin A. Hansen, July 2008.
2879 # soap sequences in stream against a given file or genome.
2881 my ( $in, # handle to in stream
2882 $out, # handle to out stream
2883 $options, # options hash
2888 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
2890 $options->{ "seed_size" } ||= 10;
2891 $options->{ "mismatches" } ||= 2;
2892 $options->{ "gap_size" } ||= 0;
2893 $options->{ "cpus" } ||= 1;
2895 if ( $options->{ "genome" } ) {
2896 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2899 $tmp_in = "$BP_TMP/soap_query.seq";
2900 $tmp_out = "$BP_TMP/soap.result";
2902 $fh_out = Maasha::Common::write_open( $tmp_in );
2906 while ( $record = Maasha::Biopieces::get_record( $in ) )
2908 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2910 Maasha::Fasta::put_entry( $entry, $fh_out );
2915 Maasha::Biopieces::put_record( $record, $out );
2923 "-s $options->{ 'seed_size' }",
2926 "-v $options->{ 'mismatches' }",
2927 "-g $options->{ 'gap_size' }",
2928 "-p $options->{ 'cpus' }",
2929 "-d $options->{ 'in_file' }",
2933 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
2935 Maasha::Common::run( "soap", $args, 1 );
2939 $fh_out = Maasha::Common::read_open( $tmp_out );
2943 while ( $line = <$fh_out> )
2947 @fields = split /\t/, $line;
2949 $record->{ "REC_TYPE" } = "SOAP";
2950 $record->{ "Q_ID" } = $fields[ 0 ];
2951 $record->{ "SCORE" } = $fields[ 3 ];
2952 $record->{ "STRAND" } = $fields[ 6 ];
2953 $record->{ "S_ID" } = $fields[ 7 ];
2954 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
2955 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
2957 Maasha::Biopieces::put_record( $record, $out );
2967 sub script_match_seq
2969 # Martin A. Hansen, August 2007.
2971 # BLATs sequences in stream against a given genome.
2973 my ( $in, # handle to in stream
2974 $out, # handle to out stream
2975 $options, # options hash
2980 my ( $record, @entries, $results );
2982 $options->{ "word_size" } ||= 20;
2983 $options->{ "direction" } ||= "both";
2985 while ( $record = Maasha::Biopieces::get_record( $in ) )
2987 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2988 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2991 Maasha::Biopieces::put_record( $record, $out );
2994 if ( @entries == 1 )
2996 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
2998 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3000 elsif ( @entries == 2 )
3002 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3004 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3009 sub script_create_vmatch_index
3011 # Martin A. Hansen, January 2008.
3013 # Create a vmatch index from sequences in the stream.
3015 my ( $in, # handle to in stream
3016 $out, # handle to out stream
3017 $options, # options hash
3022 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3024 if ( $options->{ "index_name" } )
3026 $file_tmp = $options->{ 'index_name' };
3027 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3030 while ( $record = Maasha::Biopieces::get_record( $in ) )
3032 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3034 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3036 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3039 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3042 if ( $options->{ "index_name" } )
3046 if ( $type eq "protein" ) {
3047 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3049 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3057 sub script_write_align
3059 # Martin A. Hansen, August 2007.
3061 # Write pretty alignments aligned sequences in stream.
3063 my ( $in, # handle to in stream
3064 $out, # handle to out stream
3065 $options, # options hash
3070 my ( $fh, $record, @entries );
3072 $fh = write_stream( $options->{ "data_out" } ) ;
3074 while ( $record = Maasha::Biopieces::get_record( $in ) )
3076 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3077 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3080 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3083 if ( scalar( @entries ) == 2 ) {
3084 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3085 } elsif ( scalar ( @entries ) > 2 ) {
3086 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3093 sub script_write_bed
3095 # Martin A. Hansen, August 2007.
3097 # Write BED format for the UCSC genome browser using records in stream.
3099 my ( $in, # handle to in stream
3100 $out, # handle to out stream
3101 $options, # options hash
3106 my ( $cols, $fh, $record, $bed_entry, $new_record );
3108 $cols = $options->{ 'cols' }->[ 0 ];
3110 $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
3112 while ( $record = Maasha::Biopieces::get_record( $in ) )
3114 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
3116 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
3117 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
3120 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3127 sub script_write_psl
3129 # Martin A. Hansen, August 2007.
3131 # Write PSL output from stream.
3133 my ( $in, # handle to in stream
3134 $out, # handle to out stream
3135 $options, # options hash
3140 my ( $fh, $record, @output, $first );
3144 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3146 while ( $record = Maasha::Biopieces::get_record( $in ) )
3148 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3150 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3152 Maasha::UCSC::psl_put_header( $fh ) if $first;
3153 Maasha::UCSC::psl_put_entry( $record, $fh );
3162 sub script_write_fixedstep
3164 # Martin A. Hansen, Juli 2008.
3166 # Write fixedStep entries from recrods in the stream.
3168 my ( $in, # handle to in stream
3169 $out, # handle to out stream
3170 $options, # options hash
3175 my ( $fh, $record, $entry );
3177 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3179 while ( $record = Maasha::Biopieces::get_record( $in ) )
3181 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
3182 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
3185 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3192 sub script_write_2bit
3194 # Martin A. Hansen, March 2008.
3196 # Write sequence entries from stream in 2bit format.
3198 my ( $in, # handle to in stream
3199 $out, # handle to out stream
3200 $options, # options hash
3205 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
3207 $mask = 1 if not $options->{ "no_mask" };
3209 $tmp_file = "$BP_TMP/write_2bit.fna";
3210 $fh_tmp = Maasha::Common::write_open( $tmp_file );
3212 $fh_out = write_stream( $options->{ "data_out" } );
3214 while ( $record = Maasha::Biopieces::get_record( $in ) )
3216 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
3217 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3220 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3225 $fh_in = Maasha::Common::read_open( $tmp_file );
3227 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3236 sub script_write_solid
3238 # Martin A. Hansen, April 2008.
3240 # Write di-base encoded Solid sequence from entries in stream.
3242 my ( $in, # handle to in stream
3243 $out, # handle to out stream
3244 $options, # options hash
3249 my ( $record, $fh, $entry );
3251 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3253 while ( $record = Maasha::Biopieces::get_record( $in ) )
3255 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3257 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
3259 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3262 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3269 sub script_write_ucsc_config
3271 # Martin A. Hansen, November 2008.
3273 # Write UCSC Genome Broser configuration (.ra file type) from
3274 # records in the stream.
3276 my ( $in, # handle to in stream
3277 $out, # handle to out stream
3278 $options, # options hash
3283 my ( $record, $fh );
3285 $fh = write_stream( $options->{ "data_out" } );
3287 while ( $record = Maasha::Biopieces::get_record( $in ) )
3289 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
3291 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3298 sub script_plot_seqlogo
3300 # Martin A. Hansen, August 2007.
3302 # Calculates and writes a sequence logo for alignments.
3304 my ( $in, # handle to in stream
3305 $out, # handle to out stream
3306 $options, # options hash
3311 my ( $record, @entries, $logo, $fh );
3313 while ( $record = Maasha::Biopieces::get_record( $in ) )
3315 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3316 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3319 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3322 $logo = Maasha::Plot::seq_logo( \@entries );
3324 $fh = write_stream( $options->{ "data_out" } );
3332 sub script_plot_phastcons_profiles
3334 # Martin A. Hansen, January 2008.
3336 # Plots PhastCons profiles.
3338 my ( $in, # handle to in stream
3339 $out, # handle to out stream
3340 $options, # options hash
3345 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
3347 $options->{ "title" } ||= "PhastCons Profiles";
3349 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3350 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3352 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3353 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3355 while ( $record = Maasha::Biopieces::get_record( $in ) )
3357 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3359 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
3360 $record->{ "CHR_BEG" },
3361 $record->{ "CHR_END" },
3362 $options->{ "flank" } );
3364 push @{ $AoA }, [ @{ $scores } ];
3367 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3370 Maasha::UCSC::phastcons_normalize( $AoA );
3372 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
3373 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
3375 $AoA = Maasha::Matrix::matrix_flip( $AoA );
3377 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
3379 $fh = write_stream( $options->{ "data_out" } );
3381 print $fh "$_\n" foreach @{ $plot };
3387 sub script_analyze_bed
3389 # Martin A. Hansen, March 2008.
3391 # Analyze BED entries in stream.
3393 my ( $in, # handle to in stream
3394 $out, # handle to out stream
3395 $options, # options hash
3402 while ( $record = Maasha::Biopieces::get_record( $in ) )
3404 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
3406 Maasha::Biopieces::put_record( $record, $out );
3411 sub script_analyze_vals
3413 # Martin A. Hansen, August 2007.
3415 # Analyze values for given keys in stream.
3417 my ( $in, # handle to in stream
3418 $out, # handle to out stream
3419 $options, # options hash
3424 my ( $record, $key, @keys, %key_hash, $analysis, $len );
3426 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
3428 while ( $record = Maasha::Biopieces::get_record( $in ) )
3430 foreach $key ( keys %{ $record } )
3432 next if $options->{ "keys" } and not exists $key_hash{ $key };
3434 $analysis->{ $key }->{ "COUNT" }++;
3436 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
3438 $analysis->{ $key }->{ "TYPE" } = "num";
3439 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
3440 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3441 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
3445 $len = length $record->{ $key };
3447 $analysis->{ $key }->{ "TYPE" } = "alph";
3448 $analysis->{ $key }->{ "SUM" } += $len;
3449 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3450 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
3454 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3457 foreach $key ( keys %{ $analysis } )
3459 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
3460 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
3463 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
3473 if ( $options->{ "keys" } ) {
3474 @keys = @{ $options->{ "keys" } };
3476 @keys = keys %{ $analysis };
3479 foreach $key ( @keys )
3481 $keys .= sprintf "% 15s", $key;
3482 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
3483 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
3484 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
3485 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
3486 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
3487 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
3490 print $out "$keys\n";
3491 print $out "$types\n";
3492 print $out "$counts\n";
3493 print $out "$mins\n";
3494 print $out "$maxs\n";
3495 print $out "$sums\n";
3496 print $out "$means\n";
3500 sub script_head_records
3502 # Martin A. Hansen, August 2007.
3504 # Display the first sequences in stream.
3506 my ( $in, # handle to in stream
3507 $out, # handle to out stream
3508 $options, # options hash
3513 my ( $record, $count );
3515 $options->{ "num" } ||= 10;
3519 while ( $record = Maasha::Biopieces::get_record( $in ) )
3523 Maasha::Biopieces::put_record( $record, $out );
3525 last if $count == $options->{ "num" };
3530 sub script_remove_keys
3532 # Martin A. Hansen, August 2007.
3534 # Remove keys from stream.
3536 my ( $in, # handle to in stream
3537 $out, # handle to out stream
3538 $options, # options hash
3543 my ( $record, $new_record );
3545 while ( $record = Maasha::Biopieces::get_record( $in ) )
3547 if ( $options->{ "keys" } )
3549 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
3551 elsif ( $options->{ "save_keys" } )
3553 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
3555 $record = $new_record;
3558 Maasha::Biopieces::put_record( $record, $out ) if keys %{ $record };
3563 sub script_remove_adaptor
3565 # Martin A. Hansen, August 2008.
3567 # Find and remove adaptor from sequences in the stream.
3569 my ( $in, # handle to in stream
3570 $out, # handle to out stream
3571 $options, # options hash
3576 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
3578 $options->{ "remove" } ||= "after";
3580 $max_mismatch = $options->{ "mismatches" } || 0;
3581 $offset = $options->{ "offset" };
3583 if ( not defined $offset ) {
3589 $adaptor = uc $options->{ "adaptor" };
3590 $adaptor_len = length $adaptor;
3592 while ( $record = Maasha::Biopieces::get_record( $in ) )
3594 if ( $record->{ "SEQ" } )
3596 $seq = uc $record->{ "SEQ" };
3597 $seq_len = length $seq;
3599 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
3601 $record->{ "ADAPTOR_POS" } = $pos;
3603 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
3605 if ( $options->{ "remove" } eq "after" )
3607 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
3608 $record->{ "SEQ_LEN" } = $pos;
3612 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
3613 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3617 Maasha::Biopieces::put_record( $record, $out );
3621 Maasha::Biopieces::put_record( $record, $out );
3627 sub script_remove_mysql_tables
3629 # Martin A. Hansen, November 2008.
3631 # Remove MySQL tables from values in stream.
3633 my ( $in, # handle to in stream
3634 $out, # handle to out stream
3635 $options, # options hash
3640 my ( $record, %table_hash, $dbh, $table );
3642 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
3643 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
3645 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
3647 while ( $record = Maasha::Biopieces::get_record( $in ) )
3649 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3651 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3654 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3656 foreach $table ( sort keys %table_hash )
3658 if ( Maasha::SQL::table_exists( $dbh, $table ) )
3660 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3661 Maasha::SQL::delete_table( $dbh, $table );
3662 print STDERR "done.\n" if $options->{ 'verbose' };
3666 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
3670 Maasha::SQL::disconnect( $dbh );
3674 sub script_remove_ucsc_tracks
3676 # Martin A. Hansen, November 2008.
3678 # Remove track from MySQL tables and config file.
3680 my ( $in, # handle to in stream
3681 $out, # handle to out stream
3682 $options, # options hash
3687 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
3689 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
3690 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
3691 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
3693 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
3695 while ( $record = Maasha::Biopieces::get_record( $in ) )
3697 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3699 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3702 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
3704 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
3705 push @tracks, $track;
3710 foreach $track ( @tracks )
3712 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
3713 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
3715 push @new_tracks, $track;
3719 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
3721 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
3723 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
3727 # ---- locate track in database ----
3729 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3731 foreach $track ( sort keys %track_hash )
3733 if ( Maasha::SQL::table_exists( $dbh, $track ) )
3735 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3736 Maasha::SQL::delete_table( $dbh, $track );
3737 print STDERR "done.\n" if $options->{ 'verbose' };
3741 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
3745 Maasha::SQL::disconnect( $dbh );
3747 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
3751 sub script_rename_keys
3753 # Martin A. Hansen, August 2007.
3755 # Rename keys in stream.
3757 my ( $in, # handle to in stream
3758 $out, # handle to out stream
3759 $options, # options hash
3766 while ( $record = Maasha::Biopieces::get_record( $in ) )
3768 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3770 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
3772 delete $record->{ $options->{ "keys" }->[ 0 ] };
3775 Maasha::Biopieces::put_record( $record, $out );
3780 sub script_uniq_vals
3782 # Martin A. Hansen, August 2007.
3784 # Find unique values in stream.
3786 my ( $in, # handle to in stream
3787 $out, # handle to out stream
3788 $options, # options hash
3793 my ( %hash, $record );
3795 while ( $record = Maasha::Biopieces::get_record( $in ) )
3797 if ( $record->{ $options->{ "key" } } )
3799 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
3801 Maasha::Biopieces::put_record( $record, $out );
3803 $hash{ $record->{ $options->{ "key" } } } = 1;
3805 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
3807 Maasha::Biopieces::put_record( $record, $out );
3811 $hash{ $record->{ $options->{ "key" } } } = 1;
3816 Maasha::Biopieces::put_record( $record, $out );
3822 sub script_merge_vals
3824 # Martin A. Hansen, August 2007.
3826 # Rename keys in stream.
3828 my ( $in, # handle to in stream
3829 $out, # handle to out stream
3830 $options, # options hash
3835 my ( $record, @join, $i );
3837 $options->{ "delimit" } ||= '_';
3839 while ( $record = Maasha::Biopieces::get_record( $in ) )
3841 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3843 @join = $record->{ $options->{ "keys" }->[ 0 ] };
3845 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
3846 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
3849 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
3852 Maasha::Biopieces::put_record( $record, $out );
3857 sub script_merge_records
3859 # Martin A. Hansen, July 2008.
3861 # Merges records in the stream based on identical values of two given keys.
3863 my ( $in, # handle to in stream
3864 $out, # handle to out stream
3865 $options, # options hash
3870 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
3871 $num1, $num2, $num, $cmp, $i );
3873 $merge = $options->{ "merge" } || "AandB";
3875 $file1 = "$BP_TMP/merge_records1.tmp";
3876 $file2 = "$BP_TMP/merge_records2.tmp";
3878 $fh1 = Maasha::Common::write_open( $file1 );
3879 $fh2 = Maasha::Common::write_open( $file2 );
3881 $key1 = $options->{ "keys" }->[ 0 ];
3882 $key2 = $options->{ "keys" }->[ 1 ];
3884 $num = $key2 =~ s/n$//;
3888 while ( $record = Maasha::Biopieces::get_record( $in ) )
3890 if ( exists $record->{ $key1 } )
3893 @vals1 = $record->{ $key1 };
3895 delete $record->{ $key1 };
3897 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
3899 print $fh1 join( "\t", @vals1 ), "\n";
3903 elsif ( exists $record->{ $key2 } )
3906 @vals2 = $record->{ $key2 };
3908 delete $record->{ $key2 };
3910 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
3912 print $fh2 join( "\t", @vals2 ), "\n";
3923 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3924 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3928 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3929 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3932 $fh1 = Maasha::Common::read_open( $file1 );
3933 $fh2 = Maasha::Common::read_open( $file2 );
3935 @vals1 = Maasha::Common::get_fields( $fh1 );
3936 @vals2 = Maasha::Common::get_fields( $fh2 );
3938 while ( $num1 > 0 and $num2 > 0 )
3943 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
3945 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
3950 if ( $merge =~ /^(AorB|AnotB)$/ )
3952 for ( $i = 0; $i < @keys1; $i++ ) {
3953 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3956 Maasha::Biopieces::put_record( $record, $out );
3959 @vals1 = Maasha::Common::get_fields( $fh1 );
3964 if ( $merge =~ /^(BorA|BnotA)$/ )
3966 for ( $i = 0; $i < @keys2; $i++ ) {
3967 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3970 Maasha::Biopieces::put_record( $record, $out );
3973 @vals2 = Maasha::Common::get_fields( $fh2 );
3978 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
3980 for ( $i = 0; $i < @keys1; $i++ ) {
3981 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3984 for ( $i = 1; $i < @keys2; $i++ ) {
3985 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3988 Maasha::Biopieces::put_record( $record, $out );
3991 @vals1 = Maasha::Common::get_fields( $fh1 );
3992 @vals2 = Maasha::Common::get_fields( $fh2 );
4004 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4008 for ( $i = 0; $i < @keys1; $i++ ) {
4009 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4012 Maasha::Biopieces::put_record( $record, $out );
4015 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4019 for ( $i = 0; $i < @keys2; $i++ ) {
4020 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4023 Maasha::Biopieces::put_record( $record, $out );
4030 # Martin A. Hansen, August 2007.
4032 # Evaluate extression for records in stream.
4034 my ( $in, # handle to in stream
4035 $out, # handle to out stream
4036 $options, # options hash
4041 my ( $record, $eval_key, @keys, $eval_val );
4043 while ( $record = Maasha::Biopieces::get_record( $in ) )
4045 if ( $options->{ "eval" } )
4047 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
4054 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
4056 @keys = grep { exists $record->{ $_ } } @keys;
4059 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
4061 $record->{ $eval_key } = eval "$eval_val";
4062 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
4066 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
4070 Maasha::Biopieces::put_record( $record, $out );
4077 # Martin A. Hansen, June 2008.
4081 my ( $in, # handle to in stream
4082 $out, # handle to out stream
4083 $options, # options hash
4088 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4090 while ( $record = Maasha::Biopieces::get_record( $in ) )
4094 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4096 push @rows, $record->{ $key };
4100 push @matrix, [ @rows ];
4105 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4107 foreach $row ( @matrix )
4109 for ( $i = 0; $i < @{ $row }; $i++ ) {
4110 $record->{ "V$i" } = $row->[ $i ];
4113 Maasha::Biopieces::put_record( $record, $out );
4118 sub script_count_records
4120 # Martin A. Hansen, August 2007.
4122 # Count records in stream.
4124 my ( $in, # handle to in stream
4125 $out, # handle to out stream
4126 $options, # options hash
4131 my ( $record, $count, $result, $fh, $line );
4135 if ( $options->{ "no_stream" } )
4137 while ( $line = <$in> )
4141 $count++ if $line eq "---";
4146 while ( $record = Maasha::Biopieces::get_record( $in ) )
4148 Maasha::Biopieces::put_record( $record, $out );
4154 $result = { "RECORDS_COUNT" => $count };
4156 $fh = write_stream( $options->{ "data_out" } );
4158 Maasha::Biopieces::put_record( $result, $fh );
4164 sub script_random_records
4166 # Martin A. Hansen, August 2007.
4168 # Pick a number or random records from stream.
4170 my ( $in, # handle to in stream
4171 $out, # handle to out stream
4172 $options, # options hash
4177 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4179 $options->{ "num" } ||= 10;
4181 $tmp_file = "$BP_TMP/random_records.tmp";
4183 $fh_out = Maasha::Common::write_open( $tmp_file );
4187 while ( $record = Maasha::Biopieces::get_record( $in ) )
4189 Maasha::Biopieces::put_record( $record, $fh_out );
4199 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4201 while ( $i < $options->{ "num" } )
4203 $rand = int( rand( $count ) );
4205 if ( not exists $rand_hash{ $rand } )
4207 $rand_hash{ $rand } = 1;
4209 $max = $rand if $rand > $max;
4215 $fh_in = Maasha::Common::read_open( $tmp_file );
4219 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4221 Maasha::Biopieces::put_record( $record, $out ) if exists $rand_hash{ $count };
4223 last if $count == $max;
4234 sub script_sort_records
4236 # Martin A. Hansen, August 2007.
4238 # Sort to sort records according to keys.
4240 my ( $in, # handle to in stream
4241 $out, # handle to out stream
4242 $options, # options hash
4247 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4249 foreach $key ( @{ $options->{ "keys" } } )
4251 if ( $key =~ s/n$// ) {
4252 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4254 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4258 $sort_str = join " or ", @sort_cmd;
4259 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4261 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
4262 push @records, $record;
4265 @records = sort $sort_sub @records;
4267 if ( $options->{ "reverse" } )
4269 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4270 Maasha::Biopieces::put_record( $records[ $i ], $out );
4275 for ( $i = 0; $i < scalar @records; $i++ ) {
4276 Maasha::Biopieces::put_record( $records[ $i ], $out );
4282 sub script_count_vals
4284 # Martin A. Hansen, August 2007.
4286 # Count records in stream.
4288 my ( $in, # handle to in stream
4289 $out, # handle to out stream
4290 $options, # options hash
4295 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4297 $tmp_file = "$BP_TMP/count_cache.tmp";
4299 $fh_out = Maasha::Common::write_open( $tmp_file );
4304 while ( $record = Maasha::Biopieces::get_record( $in ) )
4306 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4308 push @records, $record;
4310 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4312 map { Maasha::Biopieces::put_record( $_, $fh_out ) } @records;
4319 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4330 $fh_in = Maasha::Common::read_open( $tmp_file );
4332 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4334 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4336 Maasha::Biopieces::put_record( $record, $out );
4338 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4346 foreach $record ( @records )
4348 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4350 Maasha::Biopieces::put_record( $record, $out );
4357 sub script_plot_histogram
4359 # Martin A. Hansen, September 2007.
4361 # Plot a simple histogram for a given key using GNU plot.
4363 my ( $in, # handle to in stream
4364 $out, # handle to out stream
4365 $options, # options hash
4370 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4372 $options->{ "title" } ||= "Histogram";
4373 $options->{ "sort" } ||= "num";
4375 while ( $record = Maasha::Biopieces::get_record( $in ) )
4377 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4379 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4382 if ( $options->{ "sort" } eq "num" ) {
4383 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4385 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4388 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4390 $fh = write_stream( $options->{ "data_out" } );
4392 print $fh "$_\n" foreach @{ $result };
4398 sub script_plot_lendist
4400 # Martin A. Hansen, August 2007.
4402 # Plot length distribution using GNU plot.
4404 my ( $in, # handle to in stream
4405 $out, # handle to out stream
4406 $options, # options hash
4411 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4413 $options->{ "title" } ||= "Length Distribution";
4415 while ( $record = Maasha::Biopieces::get_record( $in ) )
4417 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4419 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4422 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4424 for ( $i = 0; $i < $max; $i++ ) {
4425 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4428 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4430 $fh = write_stream( $options->{ "data_out" } );
4432 print $fh "$_\n" foreach @{ $result };
4438 sub script_plot_chrdist
4440 # Martin A. Hansen, August 2007.
4442 # Plot chromosome distribution using GNU plot.
4444 my ( $in, # handle to in stream
4445 $out, # handle to out stream
4446 $options, # options hash
4451 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
4453 $options->{ "title" } ||= "Chromosome Distribution";
4455 while ( $record = Maasha::Biopieces::get_record( $in ) )
4457 if ( $record->{ "CHR" } ) { # generic
4458 $data_hash{ $record->{ "CHR" } }++;
4459 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
4460 $data_hash{ $record->{ "S_ID" } }++;
4461 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
4462 $data_hash{ $record->{ "S_ID" } }++;
4463 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
4464 $data_hash{ $record->{ "S_ID" } }++;
4467 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4470 foreach $elem ( keys %data_hash )
4474 $sort_key =~ s/chr//i;
4476 $sort_key =~ s/^X(.*)/99$1/;
4477 $sort_key =~ s/^Y(.*)/99$1/;
4478 $sort_key =~ s/^Z(.*)/999$1/;
4479 $sort_key =~ s/^M(.*)/9999$1/;
4480 $sort_key =~ s/^U(.*)/99999$1/;
4482 $count = $sort_key =~ tr/_//;
4484 $sort_key =~ s/_.*/"999999" x $count/ex;
4486 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
4489 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
4491 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
4493 $fh = write_stream( $options->{ "data_out" } );
4495 print $fh "$_\n" foreach @{ $result };
4501 sub script_plot_karyogram
4503 # Martin A. Hansen, August 2007.
4505 # Plot hits on karyogram.
4507 my ( $in, # handle to in stream
4508 $out, # handle to out stream
4509 $options, # options hash
4514 my ( %options, $record, @data, $fh, $result, %data_hash );
4516 $options->{ "genome" } ||= "human";
4517 $options->{ "feat_color" } ||= "black";
4519 while ( $record = Maasha::Biopieces::get_record( $in ) )
4521 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4523 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
4526 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4529 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
4531 $fh = write_stream( $options->{ "data_out" } );
4539 sub script_plot_matches
4541 # Martin A. Hansen, August 2007.
4543 # Plot matches in 2D generating a dotplot.
4545 my ( $in, # handle to in stream
4546 $out, # handle to out stream
4547 $options, # options hash
4552 my ( $record, @data, $fh, $result, %data_hash );
4554 $options->{ "direction" } ||= "both";
4556 while ( $record = Maasha::Biopieces::get_record( $in ) )
4558 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
4559 push @data, $record;
4562 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4565 $options->{ "title" } ||= "plot_matches";
4566 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
4567 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
4569 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
4571 $fh = write_stream( $options->{ "data_out" } );
4573 print $fh "$_\n" foreach @{ $result };
4579 sub script_length_vals
4581 # Martin A. Hansen, August 2007.
4583 # Determine the length of the value for given keys.
4585 my ( $in, # handle to in stream
4586 $out, # handle to out stream
4587 $options, # options hash
4592 my ( $record, $key );
4594 while ( $record = Maasha::Biopieces::get_record( $in ) )
4596 foreach $key ( @{ $options->{ "keys" } } )
4598 if ( $record->{ $key } ) {
4599 $record->{ $key . "_LEN" } = length $record->{ $key };
4603 Maasha::Biopieces::put_record( $record, $out );
4610 # Martin A. Hansen, August 2007.
4612 # Calculates the sums for values of given keys.
4614 my ( $in, # handle to in stream
4615 $out, # handle to out stream
4616 $options, # options hash
4621 my ( $record, $key, %sum_hash, $fh );
4623 while ( $record = Maasha::Biopieces::get_record( $in ) )
4625 foreach $key ( @{ $options->{ "keys" } } )
4627 if ( $record->{ $key } ) {
4628 $sum_hash{ $key } += $record->{ $key };
4632 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4635 $fh = write_stream( $options->{ "data_out" } );
4637 foreach $key ( @{ $options->{ "keys" } } ) {
4638 Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
4645 sub script_mean_vals
4647 # Martin A. Hansen, August 2007.
4649 # Calculate the mean of values of given keys.
4651 my ( $in, # handle to in stream
4652 $out, # handle to out stream
4653 $options, # options hash
4658 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
4660 while ( $record = Maasha::Biopieces::get_record( $in ) )
4662 foreach $key ( @{ $options->{ "keys" } } )
4664 if ( $record->{ $key } )
4666 $sum_hash{ $key } += $record->{ $key };
4667 $count_hash{ $key }++;
4671 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4674 $fh = write_stream( $options->{ "data_out" } );
4676 foreach $key ( @{ $options->{ "keys" } } )
4678 if ( $count_hash{ $key } ) {
4679 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
4684 Maasha::Biopieces::put_record( { $key . "_MEAN" => $mean } , $fh );
4691 sub script_median_vals
4693 # Martin A. Hansen, March 2008.
4695 # Calculate the median values of given keys.
4697 my ( $in, # handle to in stream
4698 $out, # handle to out stream
4699 $options, # options hash
4704 my ( $record, $key, %median_hash, $median, $fh );
4706 while ( $record = Maasha::Biopieces::get_record( $in ) )
4708 foreach $key ( @{ $options->{ "keys" } } ) {
4709 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
4712 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4715 $fh = write_stream( $options->{ "data_out" } );
4717 foreach $key ( @{ $options->{ "keys" } } )
4719 if ( $median_hash{ $key } ) {
4720 $median = Maasha::Calc::median( $median_hash{ $key } );
4725 Maasha::Biopieces::put_record( { $key . "_MEDIAN" => $median } , $fh );
4734 # Martin A. Hansen, February 2008.
4736 # Determine the maximum values of given keys.
4738 my ( $in, # handle to in stream
4739 $out, # handle to out stream
4740 $options, # options hash
4745 my ( $record, $key, $fh, %max_hash, $max_record );
4747 while ( $record = Maasha::Biopieces::get_record( $in ) )
4749 foreach $key ( @{ $options->{ "keys" } } )
4751 if ( $record->{ $key } )
4753 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
4757 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4760 $fh = write_stream( $options->{ "data_out" } );
4762 foreach $key ( @{ $options->{ "keys" } } )
4764 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
4767 Maasha::Biopieces::put_record( $max_record, $fh );
4775 # Martin A. Hansen, February 2008.
4777 # Determine the minimum values of given keys.
4779 my ( $in, # handle to in stream
4780 $out, # handle to out stream
4781 $options, # options hash
4786 my ( $record, $key, $fh, %min_hash, $min_record );
4788 while ( $record = Maasha::Biopieces::get_record( $in ) )
4790 foreach $key ( @{ $options->{ "keys" } } )
4792 if ( defined $record->{ $key } )
4794 if ( exists $min_hash{ $key } ) {
4795 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
4797 $min_hash{ $key } = $record->{ $key };
4802 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4805 $fh = write_stream( $options->{ "data_out" } );
4807 foreach $key ( @{ $options->{ "keys" } } )
4809 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
4812 Maasha::Biopieces::put_record( $min_record, $fh );
4818 sub script_upload_to_ucsc
4820 # Martin A. Hansen, August 2007.
4822 # Calculate the mean of values of given keys.
4824 # This routine has developed into the most ugly hack. Do something!
4826 my ( $in, # handle to in stream
4827 $out, # handle to out stream
4828 $options, # options hash
4833 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
4835 $options->{ "short_label" } ||= $options->{ 'table' };
4836 $options->{ "long_label" } ||= $options->{ 'table' };
4837 $options->{ "group" } ||= $ENV{ "LOGNAME" };
4838 $options->{ "priority" } ||= 1;
4839 $options->{ "visibility" } ||= "pack";
4840 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
4841 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
4843 $file = "$BP_TMP/ucsc_upload.tmp";
4848 $fh_out = Maasha::Common::write_open( $file );
4850 while ( $record = Maasha::Biopieces::get_record( $in ) )
4852 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4854 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
4858 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4859 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
4862 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
4866 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
4867 Maasha::UCSC::psl_put_entry( $record, $fh_out );
4871 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
4873 # chrom chromStart chromEnd name score strand size secStr conf
4877 print $fh_out join ( "\t",
4879 $record->{ "CHR_BEG" },
4880 $record->{ "CHR_END" } + 1,
4881 $record->{ "Q_ID" },
4882 $record->{ "SCORE" },
4883 $record->{ "STRAND" },
4884 $record->{ "SIZE" },
4885 $record->{ "SEC_STRUCT" },
4886 $record->{ "CONF" },
4889 elsif ( $record->{ "REC_TYPE" } eq "BED" )
4892 $columns = $record->{ "BED_COLS" };
4894 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4895 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4898 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
4903 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4904 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4907 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
4912 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
4914 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4915 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4918 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
4923 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4924 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4928 if ( $i == $options->{ "chunk_size" } )
4932 if ( $format eq "BED" ) {
4933 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4934 } elsif ( $format eq "PSL" ) {
4935 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
4944 $fh_out = Maasha::Common::write_open( $file );
4952 if ( exists $options->{ "database" } and $options->{ "table" } )
4954 if ( $format eq "BED" )
4956 $type = "bed $columns";
4958 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4960 elsif ( $format eq "BED_SS" )
4962 $type = "type bed 6 +";
4964 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4966 elsif ( $format eq "PSL" )
4970 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
4972 elsif ( $format eq "WIGGLE" )
4974 $options->{ "visibility" } = "full";
4976 $wig_file = "$options->{ 'table' }.wig";
4977 $wib_file = "$options->{ 'table' }.wib";
4979 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
4981 Maasha::Common::dir_create_if_not_exists( $wib_dir );
4983 if ( $options->{ 'verbose' } ) {
4984 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
4986 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
4989 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
4997 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5002 Maasha::UCSC::ucsc_update_config( $options, $type );
5007 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<