1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 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 for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
45 use Maasha::Stockholm;
51 use Maasha::UCSC::BED;
52 use Maasha::UCSC::Wiggle;
61 use vars qw( @ISA @EXPORT_OK );
65 @ISA = qw( Exporter );
80 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
83 $SIG{ '__DIE__' } = \&sig_handler;
84 $SIG{ 'INT' } = \&sig_handler;
85 $SIG{ 'TERM' } = \&sig_handler;
88 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
91 my ( $script, $BP_TMP );
93 $script = Maasha::Common::get_scriptname();
94 $BP_TMP = Maasha::Common::get_tmpdir();
97 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
100 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
101 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
103 $log_global->autoflush( 1 );
104 $log_local->autoflush( 1 );
106 &log( $log_global, $script, \@ARGV );
107 &log( $log_local, $script, \@ARGV );
113 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
116 run_script( $script );
119 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
124 # Martin A. Hansen, January 2008.
126 # Log messages to logfile.
128 my ( $fh, # filehandle to logfile
129 $script, # script name
130 $argv, # reference to @ARGV
135 my ( $time_stamp, $user );
137 $time_stamp = Maasha::Common::time_stamp();
139 $user = $ENV{ 'USER' };
141 $script = "biopieces" if $script eq "-e";
143 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
149 # Martin A. Hansen, August 2007.
151 # Run a specific script.
153 my ( $script, # script name
158 my ( $t0, $t1, $options, $in, $out );
160 $t0 = gettimeofday();
162 $options = get_options( $script );
164 $options->{ "SCRIPT" } = $script;
166 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
167 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
170 $in = read_stream( $options->{ "stream_in" } );
171 $out = write_stream( $options->{ "stream_out" } );
173 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
174 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
175 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
176 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
177 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
178 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
179 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
180 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
181 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
182 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
183 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
184 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
185 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
186 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
187 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
188 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
189 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
190 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
191 elsif ( $script eq "read_ucsc_config" ) { script_read_ucsc_config( $in, $out, $options ) }
192 elsif ( $script eq "assemble_tag_contigs" ) { script_assemble_tag_contigs( $in, $out, $options ) }
193 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
194 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
195 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
196 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
197 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
198 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
199 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
200 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
201 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
202 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
203 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
204 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
205 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
206 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
207 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
208 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
209 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
210 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
212 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
213 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
214 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
215 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
216 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
217 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
218 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
219 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
220 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
221 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
222 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
223 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
224 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
225 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
226 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
227 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
228 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
229 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
230 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
231 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
232 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
233 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
234 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
235 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
236 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
237 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
238 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
239 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
240 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
241 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
242 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
243 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
244 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
245 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
246 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
247 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
248 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
249 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
250 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
251 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
252 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
253 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
254 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
255 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
256 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
257 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
258 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
259 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
260 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
261 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
262 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
263 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
264 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
265 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
266 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
267 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
268 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
269 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
270 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
272 close $in if defined $in;
275 $t1 = gettimeofday();
277 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
283 # Martin A. Hansen, February 2008.
285 # Gets options from commandline and checks these vigerously.
287 my ( $script, # name of script
292 my ( %options, @options, $opt, @genomes, $real );
294 if ( $script eq "print_usage" )
300 elsif ( $script eq "read_fasta" )
307 elsif ( $script eq "read_tab" )
318 elsif ( $script eq "read_psl" )
325 elsif ( $script eq "read_bed" )
334 elsif ( $script eq "read_fixedstep" )
341 elsif ( $script eq "read_blast_tab" )
348 elsif ( $script eq "read_embl" )
358 elsif ( $script eq "read_stockholm" )
365 elsif ( $script eq "read_phastcons" )
376 elsif ( $script eq "read_soft" )
384 elsif ( $script eq "read_gff" )
391 elsif ( $script eq "read_2bit" )
399 elsif ( $script eq "read_solexa" )
408 elsif ( $script eq "read_solid" )
416 elsif ( $script eq "read_mysql" )
425 elsif ( $script eq "read_ucsc_config" )
432 elsif ( $script eq "assemble_tag_contigs" )
438 elsif ( $script eq "calc_fixedstep" )
444 elsif ( $script eq "format_genome" )
453 elsif ( $script eq "length_seq" )
460 elsif ( $script eq "oligo_freq" )
467 elsif ( $script eq "create_weight_matrix" )
473 elsif ( $script eq "transliterate_seq" )
481 elsif ( $script eq "transliterate_vals" )
490 elsif ( $script eq "translate_seq" )
496 elsif ( $script eq "extract_seq" )
504 elsif ( $script eq "get_genome_seq" )
517 elsif ( $script eq "get_genome_align" )
528 elsif ( $script eq "get_genome_phastcons" )
539 elsif ( $script eq "split_seq" )
546 elsif ( $script eq "split_bed" )
553 elsif ( $script eq "tile_seq" )
560 elsif ( $script eq "invert_align" )
566 elsif ( $script eq "patscan_seq" )
577 elsif ( $script eq "create_blast_db" )
584 elsif ( $script eq "blast_seq" )
596 elsif ( $script eq "blat_seq" )
608 elsif ( $script eq "soap_seq" )
619 elsif ( $script eq "match_seq" )
626 elsif ( $script eq "create_vmatch_index" )
634 elsif ( $script eq "vmatch_seq" )
645 elsif ( $script eq "write_fasta" )
654 elsif ( $script eq "write_align" )
664 elsif ( $script eq "write_blast" )
673 elsif ( $script eq "write_tab" )
685 elsif ( $script eq "write_bed" )
695 elsif ( $script eq "write_psl" )
703 elsif ( $script eq "write_fixedstep" )
711 elsif ( $script eq "write_2bit" )
719 elsif ( $script eq "write_solid" )
728 elsif ( $script eq "write_ucsc_config" )
735 elsif ( $script eq "plot_seqlogo" )
742 elsif ( $script eq "plot_phastcons_profiles" )
757 elsif ( $script eq "analyze_vals" )
764 elsif ( $script eq "head_records" )
770 elsif ( $script eq "remove_keys" )
777 elsif ( $script eq "remove_adaptor" )
786 elsif ( $script eq "remove_mysql_tables" )
797 elsif ( $script eq "remove_ucsc_tracks" )
809 elsif ( $script eq "rename_keys" )
815 elsif ( $script eq "uniq_vals" )
822 elsif ( $script eq "merge_vals" )
829 elsif ( $script eq "merge_records" )
836 elsif ( $script eq "grab" )
851 elsif ( $script eq "compute" )
857 elsif ( $script eq "add_ident" )
864 elsif ( $script eq "count_records" )
871 elsif ( $script eq "random_records" )
877 elsif ( $script eq "sort_records" )
884 elsif ( $script eq "count_vals" )
890 elsif ( $script eq "plot_histogram" )
903 elsif ( $script eq "plot_lendist" )
915 elsif ( $script eq "plot_chrdist" )
926 elsif ( $script eq "plot_karyogram" )
935 elsif ( $script eq "plot_matches" )
947 elsif ( $script eq "length_vals" )
953 elsif ( $script eq "sum_vals" )
961 elsif ( $script eq "mean_vals" )
969 elsif ( $script eq "median_vals" )
977 elsif ( $script eq "max_vals" )
985 elsif ( $script eq "min_vals" )
993 elsif ( $script eq "upload_to_ucsc" )
1017 # print STDERR Dumper( \@options );
1024 # print STDERR Dumper( \%options );
1026 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
1027 return wantarray ? %options : \%options;
1030 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
1031 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
1032 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
1033 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
1034 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
1035 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
1036 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
1037 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
1038 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
1039 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
1040 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
1042 # ---- check arguments ----
1044 if ( $options{ 'data_in' } )
1046 $options{ "files" } = getopt_files( $options{ 'data_in' } );
1048 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
1051 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
1053 # print STDERR Dumper( \%options );
1055 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
1057 foreach $opt ( keys %options )
1059 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
1061 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1063 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1065 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1067 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1069 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1071 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1073 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1075 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1077 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1079 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1081 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1083 elsif ( $opt eq "genome" and $script ne "format_genome" )
1085 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1086 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1088 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1089 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1092 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1094 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1096 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1098 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1100 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1102 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1104 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1106 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1108 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
1110 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
1114 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
1115 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1116 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1117 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1118 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1119 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1120 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1121 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1122 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1123 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /format_genome|get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
1124 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1125 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" };
1127 if ( $script eq "upload_to_ucsc" )
1129 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1130 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1133 return wantarray ? %options : \%options;
1137 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1140 sub script_print_usage
1142 # Martin A. Hansen, January 2008.
1144 # Retrieves usage information from file and
1145 # prints this nicely formatted.
1147 my ( $in, # handle to in stream
1148 $out, # handle to out stream
1149 $options, # options hash
1154 my ( $file, $wiki, $lines );
1156 if ( $options->{ 'data_in' } ) {
1157 $file = $options->{ 'data_in' };
1159 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1162 $wiki = Maasha::Gwiki::gwiki_read( $file );
1164 if ( not $options->{ "help" } ) {
1165 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1168 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1170 print STDERR "$_\n" foreach @{ $lines };
1176 sub script_list_biopieces
1178 # Martin A. Hansen, January 2008.
1180 # Prints the synopsis from the usage for each of the biopieces.
1182 my ( $in, # handle to in stream
1183 $out, # handle to out stream
1184 $options, # options hash
1189 my ( @files, $file, $wiki, $program, $synopsis );
1191 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1193 foreach $file ( sort @files )
1195 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1199 $wiki = Maasha::Gwiki::gwiki_read( $file );
1201 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1202 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1204 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1205 $synopsis =~ s/!(\w)/$1/g;
1207 printf( "%-30s%s\n", $program, $synopsis );
1215 sub script_list_genomes
1217 # Martin A. Hansen, January 2008.
1219 # Prints the synopsis from the usage for each of the biopieces.
1221 my ( $in, # handle to in stream
1222 $out, # handle to out stream
1223 $options, # options hash
1228 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1230 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1232 foreach $genome ( @genomes )
1234 next if $genome =~ /\.$/;
1236 @formats = Maasha::Common::ls_dirs( $genome );
1238 foreach $format ( @formats )
1240 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1242 $hash{ $1 }{ $2 } = 1;
1251 map { push @row, $_ } sort keys %found;
1253 print join( "\t", @row ), "\n";
1255 foreach $genome ( sort keys %hash )
1259 foreach $format ( sort keys %found )
1261 if ( exists $hash{ $genome }{ $format } ) {
1268 print join( "\t", @row ), "\n";
1273 sub script_read_fasta
1275 # Martin A. Hansen, August 2007.
1277 # Read sequences from FASTA file.
1279 my ( $in, # handle to in stream
1280 $out, # handle to out stream
1281 $options, # options hash
1286 my ( $record, $file, $data_in, $entry, $num );
1288 while ( $record = get_record( $in ) ) {
1289 put_record( $record, $out );
1294 foreach $file ( @{ $options->{ "files" } } )
1296 $data_in = Maasha::Common::read_open( $file );
1298 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1300 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1303 SEQ_NAME => $entry->[ SEQ_NAME ],
1304 SEQ => $entry->[ SEQ ],
1305 SEQ_LEN => length $entry->[ SEQ ],
1308 put_record( $record, $out );
1311 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1321 close $data_in if $data_in;
1327 # Martin A. Hansen, August 2007.
1329 # Read table or table columns from stream or file.
1331 my ( $in, # handle to in stream
1332 $out, # handle to out stream
1333 $options, # options hash
1338 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1340 $options->{ 'delimit' } ||= '\s+';
1342 while ( $record = get_record( $in ) ) {
1343 put_record( $record, $out );
1346 $skip = $options->{ 'skip' } ||= 0;
1349 foreach $file ( @{ $options->{ "files" } } )
1351 $data_in = Maasha::Common::read_open( $file );
1353 while ( $line = <$data_in> )
1361 next if $line =~ /^#|^$/;
1368 @fields = split /$options->{'delimit'}/, $line;
1370 if ( $options->{ "cols" } ) {
1371 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1376 for ( $i = 0; $i < @fields2; $i++ )
1378 if ( $options->{ "keys" }->[ $i ] ) {
1379 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1381 $record->{ "V" . $i } = $fields2[ $i ];
1385 put_record( $record, $out );
1387 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1397 close $data_in if $data_in;
1403 # Martin A. Hansen, August 2007.
1405 # Read psl table from stream or file.
1407 my ( $in, # handle to in stream
1408 $out, # handle to out stream
1409 $options, # options hash
1414 my ( $record, $file, $data_in, $num );
1416 while ( $record = get_record( $in ) ) {
1417 put_record( $record, $out );
1422 foreach $file ( @{ $options->{ "files" } } )
1424 $data_in = Maasha::Common::read_open( $file );
1426 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1428 put_record( $record, $out );
1430 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1442 # Martin A. Hansen, August 2007.
1444 # Read bed table from stream or file.
1446 my ( $in, # handle to in stream
1447 $out, # handle to out stream
1448 $options, # options hash
1453 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1455 $cols = $options->{ 'cols' }->[ 0 ];
1457 while ( $record = get_record( $in ) ) {
1458 put_record( $record, $out );
1463 foreach $file ( @{ $options->{ "files" } } )
1465 $data_in = Maasha::Common::read_open( $file );
1467 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1469 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1471 put_record( $record, $out );
1473 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1483 close $data_in if $data_in;
1487 sub script_read_fixedstep
1489 # Martin A. Hansen, Juli 2008.
1491 # Read fixedstep wiggle format from stream or file.
1493 my ( $in, # handle to in stream
1494 $out, # handle to out stream
1495 $options, # options hash
1500 my ( $file, $record, $entry, $data_in, $num );
1502 while ( $record = get_record( $in ) ) {
1503 put_record( $record, $out );
1508 foreach $file ( @{ $options->{ "files" } } )
1510 $data_in = Maasha::Common::read_open( $file );
1512 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1514 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1516 put_record( $record, $out );
1518 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1528 close $data_in if $data_in;
1532 sub script_read_blast_tab
1534 # Martin A. Hansen, September 2007.
1536 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1538 my ( $in, # handle to in stream
1539 $out, # handle to out stream
1540 $options, # options hash
1545 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1547 while ( $record = get_record( $in ) ) {
1548 put_record( $record, $out );
1553 foreach $file ( @{ $options->{ "files" } } )
1555 $data_in = Maasha::Common::read_open( $file );
1557 while ( $line = <$data_in> )
1561 next if $line =~ /^#/;
1563 @fields = split /\t/, $line;
1565 $record->{ "REC_TYPE" } = "BLAST";
1566 $record->{ "Q_ID" } = $fields[ 0 ];
1567 $record->{ "S_ID" } = $fields[ 1 ];
1568 $record->{ "IDENT" } = $fields[ 2 ];
1569 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1570 $record->{ "MISMATCHES" } = $fields[ 4 ];
1571 $record->{ "GAPS" } = $fields[ 5 ];
1572 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1573 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1574 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1575 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1576 $record->{ "E_VAL" } = $fields[ 10 ];
1577 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1579 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1581 $record->{ "STRAND" } = '-';
1583 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1587 $record->{ "STRAND" } = '+';
1590 put_record( $record, $out );
1592 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1602 close $data_in if $data_in;
1606 sub script_read_embl
1608 # Martin A. Hansen, August 2007.
1612 my ( $in, # handle to in stream
1613 $out, # handle to out stream
1614 $options, # options hash
1619 my ( %options2, $file, $data_in, $num, $entry, $record );
1621 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1622 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1623 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1625 while ( $record = get_record( $in ) ) {
1626 put_record( $record, $out );
1631 foreach $file ( @{ $options->{ "files" } } )
1633 $data_in = Maasha::Common::read_open( $file );
1635 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1637 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1639 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1641 $record_copy = dclone $record;
1643 delete $record_copy->{ "FT" };
1645 put_record( $record_copy, $out );
1647 delete $record_copy->{ "SEQ" };
1649 foreach $feat ( keys %{ $record->{ "FT" } } )
1651 $record_copy->{ "FEAT_TYPE" } = $feat;
1653 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1655 foreach $qual ( keys %{ $feat2 } )
1657 $qual_val = join "; ", @{ $feat2->{ $qual } };
1662 $record_copy->{ $qual } = $qual_val;
1665 put_record( $record_copy, $out );
1669 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1679 close $data_in if $data_in;
1683 sub script_read_stockholm
1685 # Martin A. Hansen, August 2007.
1687 # Read Stockholm format.
1689 my ( $in, # handle to in stream
1690 $out, # handle to out stream
1691 $options, # options hash
1696 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1698 while ( $record = get_record( $in ) ) {
1699 put_record( $record, $out );
1704 foreach $file ( @{ $options->{ "files" } } )
1706 $data_in = Maasha::Common::read_open( $file );
1708 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1710 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1714 foreach $key ( keys %{ $record->{ "GF" } } ) {
1715 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1718 $record_anno->{ "ALIGN" } = $num;
1720 put_record( $record_anno, $out );
1722 foreach $seq ( @{ $record->{ "ALIGN" } } )
1724 undef $record_align;
1727 SEQ_NAME => $seq->[ 0 ],
1731 put_record( $record_align, $out );
1734 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1744 close $data_in if $data_in;
1748 sub script_read_phastcons
1750 # Martin A. Hansen, December 2007.
1752 # Read PhastCons format.
1754 my ( $in, # handle to in stream
1755 $out, # handle to out stream
1756 $options, # options hash
1761 my ( $data_in, $file, $num, $entry, @records, $record );
1763 $options->{ "min" } ||= 10;
1764 $options->{ "dist" } ||= 25;
1765 $options->{ "threshold" } ||= 0.8;
1766 $options->{ "gap" } ||= 5;
1768 while ( $record = get_record( $in ) ) {
1769 put_record( $record, $out );
1774 foreach $file ( @{ $options->{ "files" } } )
1776 $data_in = Maasha::Common::read_open( $file );
1778 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1780 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1782 foreach $record ( @records )
1784 $record->{ "REC_TYPE" } = "BED";
1785 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1787 put_record( $record, $out );
1789 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1800 close $data_in if $data_in;
1804 sub script_read_soft
1806 # Martin A. Hansen, December 2007.
1809 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1811 my ( $in, # handle to in stream
1812 $out, # handle to out stream
1813 $options, # options hash
1818 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1820 while ( $record = get_record( $in ) ) {
1821 put_record( $record, $out );
1826 foreach $file ( @{ $options->{ "files" } } )
1828 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1830 $soft_index = Maasha::NCBI::soft_index_file( $file );
1832 $fh = Maasha::Common::read_open( $file );
1834 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1836 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1838 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1840 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1842 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1844 foreach $sample ( @samples )
1847 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1849 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1851 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1853 foreach $record ( @{ $records } )
1855 put_record( $record, $out );
1857 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1862 $old_end = $sample->{ "LINE_END" };
1870 close $data_in if $data_in;
1877 # Martin A. Hansen, February 2008.
1880 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1882 my ( $in, # handle to in stream
1883 $out, # handle to out stream
1884 $options, # options hash
1889 my ( $data_in, $file, $fh, $num, $record, $entry );
1891 while ( $record = get_record( $in ) ) {
1892 put_record( $record, $out );
1897 foreach $file ( @{ $options->{ "files" } } )
1899 $fh = Maasha::Common::read_open( $file );
1901 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1903 put_record( $entry, $out );
1905 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1915 close $data_in if $data_in;
1919 sub script_read_2bit
1921 # Martin A. Hansen, March 2008.
1923 # Read sequences from 2bit file.
1925 my ( $in, # handle to in stream
1926 $out, # handle to out stream
1927 $options, # options hash
1932 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1934 $mask = 1 if not $options->{ "no_mask" };
1936 while ( $record = get_record( $in ) ) {
1937 put_record( $record, $out );
1942 foreach $file ( @{ $options->{ "files" } } )
1944 $data_in = Maasha::Common::read_open( $file );
1946 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1948 foreach $line ( @{ $toc } )
1950 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1951 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1952 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1954 put_record( $record, $out );
1956 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1966 close $data_in if $data_in;
1970 sub script_read_solexa
1972 # Martin A. Hansen, March 2008.
1974 # Read Solexa sequence reads from file.
1976 my ( $in, # handle to in stream
1977 $out, # handle to out stream
1978 $options, # options hash
1983 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1985 $options->{ "format" } ||= "octal";
1986 $options->{ "quality" } ||= 20;
1988 while ( $record = get_record( $in ) ) {
1989 put_record( $record, $out );
1994 foreach $file ( @{ $options->{ "files" } } )
1996 $data_in = Maasha::Common::read_open( $file );
1998 if ( $options->{ "format" } eq "octal" )
2000 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
2002 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2004 put_record( $record, $out );
2006 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2013 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
2015 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2017 put_record( $record, $out );
2019 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2030 close $data_in if $data_in;
2034 sub script_read_solid
2036 # Martin A. Hansen, April 2008.
2038 # Read Solid sequence from file.
2040 my ( $in, # handle to in stream
2041 $out, # handle to out stream
2042 $options, # options hash
2047 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
2049 $options->{ "quality" } ||= 15;
2051 while ( $record = get_record( $in ) ) {
2052 put_record( $record, $out );
2057 foreach $file ( @{ $options->{ "files" } } )
2059 $data_in = Maasha::Common::read_open( $file );
2061 while ( $line = <$data_in> )
2065 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2067 @scores = split /,/, $seq_qual;
2068 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2070 for ( $i = 0; $i < @seqs; $i++ ) {
2071 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2075 REC_TYPE => 'SOLID',
2076 SEQ_NAME => $seq_name,
2078 SEQ_QUAL => join( ";", @scores ),
2079 SEQ_LEN => length $seq_cs,
2080 SEQ => join( "", @seqs ),
2081 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2084 put_record( $record, $out );
2086 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2096 close $data_in if $data_in;
2100 sub script_read_mysql
2102 # Martin A. Hansen, May 2008.
2104 # Read a MySQL query into stream.
2106 my ( $in, # handle to in stream
2107 $out, # handle to out stream
2108 $options, # options hash
2113 my ( $record, $dbh, $results );
2115 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2116 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2118 while ( $record = get_record( $in ) ) {
2119 put_record( $record, $out );
2122 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2124 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2126 Maasha::SQL::disconnect( $dbh );
2128 map { put_record( $_ ) } @{ $results };
2132 sub script_read_ucsc_config
2134 # Martin A. Hansen, November 2008.
2136 # Read track entries from UCSC Genome Browser '.ra' files.
2138 my ( $in, # handle to in stream
2139 $out, # handle to out stream
2140 $options, # options hash
2145 my ( $record, $file, $data_in, $entry, $num );
2147 while ( $record = get_record( $in ) ) {
2148 put_record( $record, $out );
2153 foreach $file ( @{ $options->{ "files" } } )
2155 $data_in = Maasha::Common::read_open( $file );
2157 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
2159 $record->{ 'REC_TYPE' } = "UCSC Config";
2161 put_record( $record, $out );
2163 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2173 close $data_in if $data_in;
2177 sub script_assemble_tag_contigs
2179 # Martin A. Hansen, November 2008.
2181 # Assemble tags from the stream into
2184 # The current implementation is quite
2185 # slow because of heavy use of temporary
2188 my ( $in, # handle to in stream
2189 $out, # handle to out stream
2190 $options, # options hash
2195 my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
2197 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
2198 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2199 $cols = 6; # we only need the first 6 BED columns
2201 while ( $record = get_record( $in ) )
2203 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
2205 $strand = $record->{ 'STRAND' } || '+';
2207 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
2213 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
2217 foreach $chr ( sort keys %{ $file_hash } )
2219 $bed_file = $file_hash->{ $chr };
2220 $tag_file = "$bed_file.tc";
2222 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
2224 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
2226 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
2228 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
2229 put_record( $record, $out );
2241 sub script_format_genome
2243 # Martin A. Hansen, Juli 2008.
2245 # Format a genome to speficed formats.
2247 my ( $in, # handle to in stream
2248 $out, # handle to out stream
2249 $options, # options hash
2254 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2256 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2257 $genome = $options->{ 'genome' };
2259 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2260 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2261 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2263 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2265 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2267 $fasta_dir = "$dir/genomes/$genome/fasta";
2271 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2273 $fasta_dir = "$dir/genomes/$genome/fasta";
2275 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2278 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2280 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2282 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2284 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2287 while ( $record = get_record( $in ) )
2289 if ( $fh_out and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2291 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2293 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2295 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2297 $vals = $record->{ 'VALS' };
2301 print $fh_out "$vals\n";
2304 put_record( $record, $out ) if not $options->{ "no_stream" };
2307 foreach $format ( @{ $options->{ 'formats' } } )
2309 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2310 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2311 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2312 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2313 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2316 close $fh_out if $fh_out;
2320 sub script_length_seq
2322 # Martin A. Hansen, August 2007.
2324 # Determine the length of sequences in stream.
2326 my ( $in, # handle to in stream
2327 $out, # handle to out stream
2328 $options, # options hash
2333 my ( $record, $total );
2335 while ( $record = get_record( $in ) )
2337 if ( $record->{ "SEQ" } )
2339 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2340 $total += $record->{ "SEQ_LEN" };
2343 put_record( $record, $out ) if not $options->{ "no_stream" };
2346 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2350 sub script_uppercase_seq
2352 # Martin A. Hansen, August 2007.
2354 # Uppercases sequences in stream.
2356 my ( $in, # handle to in stream
2357 $out, # handle to out stream
2364 while ( $record = get_record( $in ) )
2366 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2368 put_record( $record, $out );
2373 sub script_shuffle_seq
2375 # Martin A. Hansen, December 2007.
2377 # Shuffle sequences in stream.
2379 my ( $in, # handle to in stream
2380 $out, # handle to out stream
2387 while ( $record = get_record( $in ) )
2389 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2391 put_record( $record, $out );
2396 sub script_analyze_seq
2398 # Martin A. Hansen, August 2007.
2400 # Analyze sequence composition of sequences in stream.
2402 my ( $in, # handle to in stream
2403 $out, # handle to out stream
2408 my ( $record, $analysis );
2410 while ( $record = get_record( $in ) )
2412 if ( $record->{ "SEQ" } )
2414 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2416 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2419 put_record( $record, $out );
2424 sub script_analyze_tags
2426 # Martin A. Hansen, August 2008.
2428 # Analyze sequence tags in stream.
2430 my ( $in, # handle to in stream
2431 $out, # handle to out stream
2436 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2438 while ( $record = get_record( $in ) )
2440 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2442 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2446 $len_hash{ length( $record->{ "SEQ" } ) }++;
2447 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2450 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2452 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2456 $len_hash{ $record->{ "BED_LEN" } }++;
2457 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2462 foreach $key ( sort { $a <=> $b } keys %len_hash )
2464 $tag_record->{ "TAG_LEN" } = $key;
2465 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2466 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2468 put_record( $tag_record, $out );
2473 sub script_complexity_seq
2475 # Martin A. Hansen, May 2008.
2477 # Generates an index calculated as the most common di-residue over
2478 # the sequence length for all sequences in stream.
2480 my ( $in, # handle to in stream
2481 $out, # handle to out stream
2486 my ( $record, $index );
2488 while ( $record = get_record( $in ) )
2490 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2492 put_record( $record, $out );
2497 sub script_oligo_freq
2499 # Martin A. Hansen, August 2007.
2501 # Determine the length of sequences in stream.
2503 my ( $in, # handle to in stream
2504 $out, # handle to out stream
2505 $options, # options hash
2510 my ( $record, %oligos, @freq_table );
2512 $options->{ "word_size" } ||= 7;
2514 while ( $record = get_record( $in ) )
2516 if ( $record->{ "SEQ" } )
2518 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2520 if ( not $options->{ "all" } )
2522 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2524 map { put_record( $_, $out ) } @freq_table;
2530 put_record( $record, $out );
2533 if ( $options->{ "all" } )
2535 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2537 map { put_record( $_, $out ) } @freq_table;
2542 sub script_create_weight_matrix
2544 # Martin A. Hansen, August 2007.
2546 # Creates a weight matrix from an alignmnet.
2548 my ( $in, # handle to in stream
2549 $out, # handle to out stream
2550 $options, # options hash
2555 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2559 while ( $record = get_record( $in ) )
2561 if ( $record->{ "SEQ" } )
2563 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2565 $res = substr $record->{ "SEQ" }, $i, 1;
2567 $freq_hash{ $i }{ $res }++;
2568 $res_hash{ $res } = 1;
2575 put_record( $record, $out );
2579 foreach $res ( sort keys %res_hash )
2583 $record->{ "V0" } = $res;
2585 for ( $i = 0; $i < keys %freq_hash; $i++ )
2587 $freq = $freq_hash{ $i }{ $res } || 0;
2589 if ( $options->{ "percent" } ) {
2590 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2593 $record->{ "V" . ( $i + 1 ) } = $freq;
2596 put_record( $record, $out );
2601 sub script_calc_bit_scores
2603 # Martin A. Hansen, March 2007.
2605 # Calculates the bit scores for each position from an alignmnet in the stream.
2607 my ( $in, # handle to in stream
2608 $out, # handle to out stream
2613 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2617 while ( $record = get_record( $in ) )
2619 if ( $record->{ "SEQ" } )
2621 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2623 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2625 $res = substr $record->{ "SEQ" }, $i, 1;
2627 next if $res =~ /-|_|~|\./;
2629 $freq_hash{ $i }{ $res }++;
2636 put_record( $record, $out );
2642 if ( $type eq "protein" ) {
2648 for ( $i = 0; $i < keys %freq_hash; $i++ )
2650 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2652 $bit_diff = $bit_max - $bit_height;
2654 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2657 put_record( $record, $out );
2661 sub script_calc_fixedstep
2663 # Martin A. Hansen, September 2008.
2665 # Calculates fixedstep entries from data in the stream.
2667 my ( $in, # handle to in stream
2668 $out, # handle to out stream
2669 $options, # options hash
2674 my ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2676 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2677 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2679 while ( $record = get_record( $in ) )
2681 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) {
2682 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } );
2688 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP );
2692 foreach $chr ( sort keys %{ $file_hash } )
2694 $bed_file = $file_hash->{ $chr };
2696 $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } );
2698 #$fixedstep_file = "$bed_file.fixedstep";
2700 # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2703 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2705 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2707 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2708 put_record( $record, $out );
2715 unlink $fixedstep_file;
2720 sub script_reverse_seq
2722 # Martin A. Hansen, August 2007.
2724 # Reverse sequence in record.
2726 my ( $in, # handle to in stream
2727 $out, # handle to out stream
2734 while ( $record = get_record( $in ) )
2736 if ( $record->{ "SEQ" } ) {
2737 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2740 put_record( $record, $out );
2745 sub script_complement_seq
2747 # Martin A. Hansen, August 2007.
2749 # Complement sequence in record.
2751 my ( $in, # handle to in stream
2752 $out, # handle to out stream
2757 my ( $record, $type );
2759 while ( $record = get_record( $in ) )
2761 if ( $record->{ "SEQ" } )
2764 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2767 if ( $type eq "rna" ) {
2768 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2769 } elsif ( $type eq "dna" ) {
2770 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2774 put_record( $record, $out );
2779 sub script_remove_indels
2781 # Martin A. Hansen, August 2007.
2783 # Remove indels from sequences in stream.
2785 my ( $in, # handle to in stream
2786 $out, # handle to out stream
2793 while ( $record = get_record( $in ) )
2795 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2797 put_record( $record, $out );
2802 sub script_transliterate_seq
2804 # Martin A. Hansen, August 2007.
2806 # Transliterate chars from sequence in record.
2808 my ( $in, # handle to in stream
2809 $out, # handle to out stream
2810 $options, # options hash
2815 my ( $record, $search, $replace, $delete );
2817 $search = $options->{ "search" } || "";
2818 $replace = $options->{ "replace" } || "";
2819 $delete = $options->{ "delete" } || "";
2821 while ( $record = get_record( $in ) )
2823 if ( $record->{ "SEQ" } )
2825 if ( $search and $replace ) {
2826 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2827 } elsif ( $delete ) {
2828 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2832 put_record( $record, $out );
2837 sub script_transliterate_vals
2839 # Martin A. Hansen, April 2008.
2841 # Transliterate chars from values in record.
2843 my ( $in, # handle to in stream
2844 $out, # handle to out stream
2845 $options, # options hash
2850 my ( $record, $search, $replace, $delete, $key );
2852 $search = $options->{ "search" } || "";
2853 $replace = $options->{ "replace" } || "";
2854 $delete = $options->{ "delete" } || "";
2856 while ( $record = get_record( $in ) )
2858 foreach $key ( @{ $options->{ "keys" } } )
2860 if ( exists $record->{ $key } )
2862 if ( $search and $replace ) {
2863 eval "\$record->{ $key } =~ tr/$search/$replace/";
2864 } elsif ( $delete ) {
2865 eval "\$record->{ $key } =~ tr/$delete//d";
2870 put_record( $record, $out );
2875 sub script_translate_seq
2877 # Martin A. Hansen, February 2008.
2879 # Translate DNA sequence into protein sequence.
2881 my ( $in, # handle to in stream
2882 $out, # handle to out stream
2883 $options, # options hash
2888 my ( $record, $frame, %new_record );
2890 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2892 while ( $record = get_record( $in ) )
2894 if ( $record->{ "SEQ" } )
2896 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2898 foreach $frame ( @{ $options->{ "frames" } } )
2900 %new_record = %{ $record };
2902 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2903 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2904 $new_record{ "FRAME" } = $frame;
2906 put_record( \%new_record, $out );
2912 put_record( $record, $out );
2918 sub script_extract_seq
2920 # Martin A. Hansen, August 2007.
2922 # Extract subsequences from sequences in record.
2924 my ( $in, # handle to in stream
2925 $out, # handle to out stream
2926 $options, # options hash
2931 my ( $beg, $end, $len, $record );
2933 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2936 $beg = $options->{ "beg" } - 1; # correcting for start offset
2939 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2941 } elsif ( defined $options->{ "end" } ) {
2942 $end = $options->{ "end" } - 1; # correcting for start offset
2945 $len = $options->{ "len" };
2947 # print "beg->$beg, end->$end, len->$len\n";
2949 while ( $record = get_record( $in ) )
2951 if ( $record->{ "SEQ" } )
2953 if ( defined $beg and defined $end )
2955 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2956 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2958 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2961 elsif ( defined $beg and defined $len )
2963 if ( $len > length $record->{ "SEQ" } ) {
2964 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2966 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2969 elsif ( defined $beg )
2971 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2975 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2977 put_record( $record, $out );
2982 sub script_get_genome_seq
2984 # Martin A. Hansen, December 2007.
2986 # Gets a subsequence from a genome.
2988 my ( $in, # handle to in stream
2989 $out, # handle to out stream
2990 $options, # options hash
2995 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i, $seq );
2997 $options->{ "flank" } ||= 0;
2999 if ( $options->{ "genome" } )
3001 $genome = $options->{ "genome" };
3003 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
3004 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
3006 $fh = Maasha::Common::read_open( $genome_file );
3007 $index = Maasha::Fasta::index_retrieve( $index_file );
3009 shift @{ $index }; # Get rid of the file size info
3011 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
3013 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3015 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
3017 $beg = $index_beg + $options->{ "beg" } - 1;
3019 if ( $options->{ "len" } ) {
3020 $len = $options->{ "len" };
3021 } elsif ( $options->{ "end" } ) {
3022 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
3025 $beg -= $options->{ "flank" };
3026 $len += 2 * $options->{ "flank" };
3028 if ( $beg <= $index_beg )
3030 $len -= $index_beg - $beg;
3034 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3036 next if $beg > $index_beg + $index_len;
3038 $record->{ "CHR" } = $options->{ "chr" };
3039 $record->{ "CHR_BEG" } = $beg - $index_beg;
3040 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3042 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3043 $record->{ "SEQ_LEN" } = $len;
3045 put_record( $record, $out );
3049 while ( $record = get_record( $in ) )
3051 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
3053 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
3055 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
3057 $beg = $record->{ "CHR_BEG" } + $index_beg;
3058 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3060 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
3062 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3064 $beg = $record->{ "S_BEG" } + $index_beg;
3065 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3067 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
3069 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3071 $beg = $record->{ "S_BEG" } + $index_beg;
3072 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3075 $beg -= $options->{ "flank" };
3076 $len += 2 * $options->{ "flank" };
3078 if ( $beg <= $index_beg )
3080 $len -= $index_beg - $beg;
3084 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3086 next if $beg > $index_beg + $index_len;
3088 $record->{ "CHR_BEG" } = $beg - $index_beg;
3089 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3091 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3093 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
3095 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
3096 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
3099 if ( $options->{ "mask" } )
3101 if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
3103 $record->{ "SEQ" } = lc $record->{ "SEQ" };
3105 @begs = split ",", $record->{ "Q_BEGS" };
3106 @lens = split ",", $record->{ "BLOCK_LENS" };
3108 for ( $i = 0; $i < @begs; $i++ ) {
3109 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3113 elsif ( $options->{ "splice" } )
3115 if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
3118 @begs = split ",", $record->{ "Q_BEGS" };
3119 @lens = split ",", $record->{ "BLOCK_LENS" };
3121 for ( $i = 0; $i < @begs; $i++ ) {
3122 $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3125 $record->{ "SEQ" } = $seq;
3129 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3132 put_record( $record, $out );
3139 sub script_get_genome_align
3141 # Martin A. Hansen, April 2008.
3143 # Gets a subalignment from a multiple genome alignment.
3145 my ( $in, # handle to in stream
3146 $out, # handle to out stream
3147 $options, # options hash
3152 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3154 $options->{ "strand" } ||= "+";
3158 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3160 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3162 $beg = $options->{ "beg" } - 1;
3164 if ( $options->{ "end" } ) {
3165 $end = $options->{ "end" };
3166 } elsif ( $options->{ "len" } ) {
3167 $end = $beg + $options->{ "len" };
3170 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3172 foreach $entry ( @{ $align } )
3174 $entry->{ "CHR" } = $record->{ "CHR" };
3175 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3176 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3177 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3178 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3179 $entry->{ "SCORE" } = $record->{ "SCORE" };
3181 put_record( $entry, $out );
3185 while ( $record = get_record( $in ) )
3187 if ( $record->{ "REC_TYPE" } eq "BED" )
3189 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3191 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
3193 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
3195 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3197 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3199 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3201 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3204 foreach $entry ( @{ $align } )
3206 $entry->{ "CHR" } = $record->{ "CHR" };
3207 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3208 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3209 $entry->{ "STRAND" } = $record->{ "STRAND" };
3210 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3211 $entry->{ "SCORE" } = $record->{ "SCORE" };
3213 put_record( $entry, $out );
3221 sub script_get_genome_phastcons
3223 # Martin A. Hansen, February 2008.
3225 # Get phastcons scores from genome intervals.
3227 my ( $in, # handle to in stream
3228 $out, # handle to out stream
3229 $options, # options hash
3234 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3236 $options->{ "flank" } ||= 0;
3238 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3239 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3241 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3242 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3244 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3246 $options->{ "beg" } -= 1; # request is 1-based
3247 $options->{ "end" } -= 1; # request is 1-based
3249 if ( $options->{ "len" } ) {
3250 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3253 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3255 $record->{ "CHR" } = $options->{ "chr" };
3256 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3257 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3259 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3260 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3262 put_record( $record, $out );
3265 while ( $record = get_record( $in ) )
3267 if ( $record->{ "REC_TYPE" } eq "BED" )
3269 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3271 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3273 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3275 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3277 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3280 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3281 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3283 put_record( $record, $out );
3286 close $fh_phastcons if $fh_phastcons;
3292 # Martin A. Hansen, December 2007.
3294 # Folds sequences in stream into secondary structures.
3296 my ( $in, # handle to in stream
3297 $out, # handle to out stream
3302 my ( $record, $type, $struct, $index );
3304 while ( $record = get_record( $in ) )
3306 if ( $record->{ "SEQ" } )
3309 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3312 if ( $type ne "protein" )
3314 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3315 $record->{ "SEC_STRUCT" } = $struct;
3316 $record->{ "FREE_ENERGY" } = $index;
3317 $record->{ "SCORE" } = abs int $index;
3318 $record->{ "SIZE" } = length $struct;
3319 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3323 put_record( $record, $out );
3328 sub script_split_seq
3330 # Martin A. Hansen, August 2007.
3332 # Split a sequence in stream into words.
3334 my ( $in, # handle to in stream
3335 $out, # handle to out stream
3336 $options, # options hash
3341 my ( $record, $new_record, $i, $subseq, %lookup );
3343 $options->{ "word_size" } ||= 7;
3345 while ( $record = get_record( $in ) )
3347 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3349 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3351 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3353 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3355 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3356 $new_record->{ "SEQ" } = $subseq;
3358 put_record( $new_record, $out );
3360 $lookup{ $subseq } = 1;
3364 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3365 $new_record->{ "SEQ" } = $subseq;
3367 put_record( $new_record, $out );
3373 put_record( $record, $out );
3379 sub script_split_bed
3381 # Martin A. Hansen, June 2008.
3383 # Split a BED record into overlapping windows.
3385 my ( $in, # handle to in stream
3386 $out, # handle to out stream
3387 $options, # options hash
3392 my ( $record, $new_record, $i );
3394 $options->{ "window_size" } ||= 20;
3395 $options->{ "step_size" } ||= 1;
3397 while ( $record = get_record( $in ) )
3399 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3401 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3403 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3405 $new_record->{ "REC_TYPE" } = "BED";
3406 $new_record->{ "CHR" } = $record->{ "CHR" };
3407 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3408 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3409 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3410 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3411 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3412 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3414 put_record( $new_record, $out );
3419 put_record( $record, $out );
3425 sub script_align_seq
3427 # Martin A. Hansen, August 2007.
3429 # Align sequences in stream.
3431 my ( $in, # handle to in stream
3432 $out, # handle to out stream
3437 my ( $record, @entries, $entry );
3439 while ( $record = get_record( $in ) )
3441 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3442 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3443 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3444 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3446 put_record( $record, $out );
3450 @entries = Maasha::Align::align( \@entries );
3452 foreach $entry ( @entries )
3454 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3457 SEQ_NAME => $entry->[ SEQ_NAME ],
3458 SEQ => $entry->[ SEQ ],
3461 put_record( $record, $out );
3469 # Martin A. Hansen, February 2008.
3471 # Using the first sequence in stream as reference, tile
3472 # all subsequent sequences based on pairwise alignments.
3474 my ( $in, # handle to in stream
3475 $out, # handle to out stream
3476 $options, # options hash
3481 my ( $record, $first, $ref_entry, @entries );
3485 while ( $record = get_record( $in ) )
3487 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3491 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3497 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3502 put_record( $record, $out );
3506 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3508 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3512 sub script_invert_align
3514 # Martin A. Hansen, February 2008.
3516 # Inverts an alignment showing only non-mathing residues
3517 # using the first sequence as reference.
3519 my ( $in, # handle to in stream
3520 $out, # handle to out stream
3521 $options, # options hash
3526 my ( $record, @entries );
3528 while ( $record = get_record( $in ) )
3530 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3532 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3536 put_record( $record, $out );
3540 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3542 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3546 sub script_patscan_seq
3548 # Martin A. Hansen, August 2007.
3550 # Locates patterns in sequences using scan_for_matches.
3552 my ( $in, # handle to in stream
3553 $out, # handle to out stream
3554 $options, # options hash
3559 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3561 if ( $options->{ "patterns" } ) {
3562 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3563 } elsif ( -f $options->{ "patterns_in" } ) {
3564 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3567 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3569 push @args, "-c" if $options->{ "comp" };
3570 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3571 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3573 $seq_file = "$BP_TMP/patscan.seq";
3574 $pat_file = "$BP_TMP/patscan.pat";
3575 $out_file = "$BP_TMP/patscan.out";
3577 $fh_out = Maasha::Common::write_open( $seq_file );
3581 while ( $record = get_record( $in ) )
3583 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3585 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3587 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3589 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3597 $arg = join " ", @args;
3598 $arg .= " -p" if $type eq "protein";
3600 foreach $pattern ( @{ $patterns } )
3602 $fh_out = Maasha::Common::write_open( $pat_file );
3604 print $fh_out "$pattern\n";
3608 if ( $options->{ 'genome' } ) {
3609 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3610 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3612 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3613 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3616 $fh_in = Maasha::Common::read_open( $out_file );
3618 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3620 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3622 if ( $options->{ 'genome' } )
3624 $result->{ "CHR" } = $result->{ "S_ID" };
3625 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3626 $result->{ "CHR_END" } = $result->{ "S_END" };
3628 delete $result->{ "S_ID" };
3629 delete $result->{ "S_BEG" };
3630 delete $result->{ "S_END" };
3634 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3637 put_record( $result, $out );
3649 sub script_create_blast_db
3651 # Martin A. Hansen, September 2007.
3653 # Creates a NCBI BLAST database with formatdb
3655 my ( $in, # handle to in stream
3656 $out, # handle to out stream
3657 $options, # options hash
3662 my ( $fh, $seq_type, $path, $record, $entry );
3664 $path = $options->{ "database" };
3666 $fh = Maasha::Common::write_open( $path );
3668 while ( $record = get_record( $in ) )
3670 put_record( $record, $out ) if not $options->{ "no_stream" };
3672 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3674 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3676 Maasha::Fasta::put_entry( $entry, $fh );
3682 if ( $seq_type eq "protein" ) {
3683 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3685 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3692 sub script_blast_seq
3694 # Martin A. Hansen, September 2007.
3696 # BLASTs sequences in stream against a given database.
3698 my ( $in, # handle to in stream
3699 $out, # handle to out stream
3700 $options, # options hash
3705 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3707 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3708 $options->{ "filter" } = "F";
3709 $options->{ "filter" } = "T" if $options->{ "filter" };
3710 $options->{ "cpus" } ||= 1;
3712 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3714 $tmp_in = "$BP_TMP/blast_query.seq";
3715 $tmp_out = "$BP_TMP/blast.result";
3717 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
3719 while ( $record = get_record( $in ) )
3721 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3723 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3725 Maasha::Fasta::put_entry( $entry, $fh_out );
3728 put_record( $record, $out );
3733 if ( -f $options->{ 'database' } . ".phr" ) {
3734 $s_type = "protein";
3736 $s_type = "nucleotide";
3739 if ( not $options->{ 'program' } )
3741 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3742 $options->{ 'program' } = "blastn";
3743 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3744 $options->{ 'program' } = "blastp";
3745 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3746 $options->{ 'program' } = "blastx";
3747 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3748 $options->{ 'program' } = "tblastn";
3752 if ( $options->{ 'verbose' } )
3754 Maasha::Common::run(
3757 "-p $options->{ 'program' }",
3758 "-e $options->{ 'e_val' }",
3759 "-a $options->{ 'cpus' }",
3762 "-d $options->{ 'database' }",
3763 "-F $options->{ 'filter' }",
3771 Maasha::Common::run(
3774 "-p $options->{ 'program' }",
3775 "-e $options->{ 'e_val' }",
3776 "-a $options->{ 'cpus' }",
3779 "-d $options->{ 'database' }",
3780 "-F $options->{ 'filter' }",
3790 $fh_out = Maasha::Filesys::file_read_open( $tmp_out );
3794 while ( $line = <$fh_out> )
3798 next if $line =~ /^#/;
3800 @fields = split /\s+/, $line;
3802 $record->{ "REC_TYPE" } = "BLAST";
3803 $record->{ "Q_ID" } = $fields[ 0 ];
3804 $record->{ "S_ID" } = $fields[ 1 ];
3805 $record->{ "IDENT" } = $fields[ 2 ];
3806 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3807 $record->{ "MISMATCHES" } = $fields[ 4 ];
3808 $record->{ "GAPS" } = $fields[ 5 ];
3809 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3810 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3811 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3812 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3813 $record->{ "E_VAL" } = $fields[ 10 ];
3814 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3816 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3818 $record->{ "STRAND" } = '-';
3820 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3824 $record->{ "STRAND" } = '+';
3827 put_record( $record, $out );
3838 # Martin A. Hansen, August 2007.
3840 # BLATs sequences in stream against a given genome.
3842 my ( $in, # handle to in stream
3843 $out, # handle to out stream
3844 $options, # options hash
3849 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3851 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3853 $options->{ 'tile_size' } ||= 11;
3854 $options->{ 'one_off' } ||= 0;
3855 $options->{ 'min_identity' } ||= 90;
3856 $options->{ 'min_score' } ||= 0;
3857 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3859 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3860 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3861 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3862 $blat_args .= " -minScore=$options->{ 'min_score' }";
3863 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3864 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3866 $query_file = "$BP_TMP/blat.seq";
3868 $fh_out = Maasha::Common::write_open( $query_file );
3870 while ( $record = get_record( $in ) )
3872 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3874 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3875 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3878 put_record( $record, $out );
3883 $blat_args .= " -t=dnax" if $type eq "protein";
3884 $blat_args .= " -q=$type";
3886 $result_file = "$BP_TMP/blat.psl";
3888 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3892 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3894 map { put_record( $_, $out ) } @{ $entries };
3896 unlink $result_file;
3902 # Martin A. Hansen, July 2008.
3904 # soap sequences in stream against a given file or genome.
3906 my ( $in, # handle to in stream
3907 $out, # handle to out stream
3908 $options, # options hash
3913 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3915 $options->{ "seed_size" } ||= 10;
3916 $options->{ "mismatches" } ||= 2;
3917 $options->{ "gap_size" } ||= 0;
3918 $options->{ "cpus" } ||= 1;
3920 if ( $options->{ "genome" } ) {
3921 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3924 $tmp_in = "$BP_TMP/soap_query.seq";
3925 $tmp_out = "$BP_TMP/soap.result";
3927 $fh_out = Maasha::Common::write_open( $tmp_in );
3931 while ( $record = get_record( $in ) )
3933 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3935 Maasha::Fasta::put_entry( $entry, $fh_out );
3940 put_record( $record, $out );
3948 "-s $options->{ 'seed_size' }",
3951 "-v $options->{ 'mismatches' }",
3952 "-g $options->{ 'gap_size' }",
3953 "-p $options->{ 'cpus' }",
3954 "-d $options->{ 'in_file' }",
3958 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3960 Maasha::Common::run( "soap", $args, 1 );
3964 $fh_out = Maasha::Common::read_open( $tmp_out );
3968 while ( $line = <$fh_out> )
3972 @fields = split /\t/, $line;
3974 $record->{ "REC_TYPE" } = "SOAP";
3975 $record->{ "Q_ID" } = $fields[ 0 ];
3976 $record->{ "SCORE" } = $fields[ 3 ];
3977 $record->{ "STRAND" } = $fields[ 6 ];
3978 $record->{ "S_ID" } = $fields[ 7 ];
3979 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3980 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3982 put_record( $record, $out );
3992 sub script_match_seq
3994 # Martin A. Hansen, August 2007.
3996 # BLATs sequences in stream against a given genome.
3998 my ( $in, # handle to in stream
3999 $out, # handle to out stream
4000 $options, # options hash
4005 my ( $record, @entries, $results );
4007 $options->{ "word_size" } ||= 20;
4008 $options->{ "direction" } ||= "both";
4010 while ( $record = get_record( $in ) )
4012 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4013 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4016 put_record( $record, $out );
4019 if ( @entries == 1 )
4021 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
4023 map { put_record( $_, $out ) } @{ $results };
4025 elsif ( @entries == 2 )
4027 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
4029 map { put_record( $_, $out ) } @{ $results };
4034 sub script_create_vmatch_index
4036 # Martin A. Hansen, January 2008.
4038 # Create a vmatch index from sequences in the stream.
4040 my ( $in, # handle to in stream
4041 $out, # handle to out stream
4042 $options, # options hash
4047 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
4049 if ( $options->{ "index_name" } )
4051 $file_tmp = $options->{ 'index_name' };
4052 $fh_tmp = Maasha::Common::write_open( $file_tmp );
4055 while ( $record = get_record( $in ) )
4057 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4059 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4061 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
4064 put_record( $record, $out ) if not $options->{ "no_stream" };
4067 if ( $options->{ "index_name" } )
4071 if ( $type eq "protein" ) {
4072 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4074 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4082 sub script_vmatch_seq
4084 # Martin A. Hansen, August 2007.
4086 # Vmatches sequences in stream against a given genome.
4088 my ( $in, # handle to in stream
4089 $out, # handle to out stream
4090 $options, # options hash
4095 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
4097 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
4099 if ( $options->{ "index_name" } )
4101 @index_files = $options->{ "index_name" };
4105 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
4107 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
4109 @index_files = sort keys %hash;
4112 while ( $record = get_record( $in ) )
4114 push @records, $record;
4116 put_record( $record, $out );
4119 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
4123 $fh_in = Maasha::Common::read_open( $result_file );
4125 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
4126 put_record( $record, $out );
4131 unlink $result_file;
4135 sub script_write_fasta
4137 # Martin A. Hansen, August 2007.
4139 # Write FASTA entries from sequences in stream.
4141 my ( $in, # handle to in stream
4142 $out, # handle to out stream
4143 $options, # options hash
4148 my ( $record, $fh, $entry );
4150 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4152 while ( $record = get_record( $in ) )
4154 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4155 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4158 put_record( $record, $out ) if not $options->{ "no_stream" };
4165 sub script_write_align
4167 # Martin A. Hansen, August 2007.
4169 # Write pretty alignments aligned sequences in stream.
4171 my ( $in, # handle to in stream
4172 $out, # handle to out stream
4173 $options, # options hash
4178 my ( $fh, $record, @entries );
4180 $fh = write_stream( $options->{ "data_out" } ) ;
4182 while ( $record = get_record( $in ) )
4184 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4185 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4188 put_record( $record, $out ) if not $options->{ "no_stream" };
4191 if ( scalar( @entries ) == 2 ) {
4192 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4193 } elsif ( scalar ( @entries ) > 2 ) {
4194 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4201 sub script_write_blast
4203 # Martin A. Hansen, November 2007.
4205 # Write data in blast table format (-m8 and 9).
4207 my ( $in, # handle to in stream
4208 $out, # handle to out stream
4209 $options, # options hash
4214 my ( $fh, $record, $first );
4216 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4220 while ( $record = get_record( $in ) )
4222 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4224 if ( $options->{ "comment" } and $first )
4226 print "# Fields: Query id, Subject id, % identity, alignment length, mismatches, gap openings, q. start, q. end, s. start, s. end, e-value, bit score\n";
4231 if ( $record->{ "STRAND" } eq "-" ) {
4232 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4235 print $fh join( "\t",
4236 $record->{ "Q_ID" },
4237 $record->{ "S_ID" },
4238 $record->{ "IDENT" },
4239 $record->{ "ALIGN_LEN" },
4240 $record->{ "MISMATCHES" },
4241 $record->{ "GAPS" },
4242 $record->{ "Q_BEG" } + 1,
4243 $record->{ "Q_END" } + 1,
4244 $record->{ "S_BEG" } + 1,
4245 $record->{ "S_END" } + 1,
4246 $record->{ "E_VAL" },
4247 $record->{ "BIT_SCORE" }
4251 put_record( $record, $out ) if not $options->{ "no_stream" };
4258 sub script_write_tab
4260 # Martin A. Hansen, August 2007.
4262 # Write data as table.
4264 my ( $in, # handle to in stream
4265 $out, # handle to out stream
4266 $options, # options hash
4271 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4273 $options->{ "delimit" } ||= "\t";
4275 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4277 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4279 while ( $record = get_record( $in ) )
4284 if ( $options->{ "keys" } )
4286 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4290 foreach $key ( @{ $options->{ "keys" } } )
4292 if ( exists $record->{ $key } )
4294 push @keys, $key if $options->{ "comment" };
4295 push @vals, $record->{ $key };
4302 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4304 next if exists $no_keys{ $key };
4306 push @keys, $key if $options->{ "comment" };
4307 push @vals, $record->{ $key };
4311 if ( @keys and $options->{ "comment" } )
4313 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4315 delete $options->{ "comment" };
4318 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4320 put_record( $record, $out ) if not $options->{ "no_stream" };
4327 sub script_write_bed
4329 # Martin A. Hansen, August 2007.
4331 # Write BED format for the UCSC genome browser using records in stream.
4333 my ( $in, # handle to in stream
4334 $out, # handle to out stream
4335 $options, # options hash
4340 my ( $cols, $fh, $record, $bed_entry, $new_record );
4342 $cols = $options->{ 'cols' }->[ 0 ];
4344 $fh = write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
4346 while ( $record = get_record( $in ) )
4348 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
4350 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
4351 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
4354 put_record( $record, $out ) if not $options->{ 'no_stream' };
4361 sub script_write_psl
4363 # Martin A. Hansen, August 2007.
4365 # Write PSL output from stream.
4367 my ( $in, # handle to in stream
4368 $out, # handle to out stream
4369 $options, # options hash
4374 my ( $fh, $record, @output, $first );
4378 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4380 while ( $record = get_record( $in ) )
4382 put_record( $record, $out ) if not $options->{ "no_stream" };
4384 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4386 Maasha::UCSC::psl_put_header( $fh ) if $first;
4387 Maasha::UCSC::psl_put_entry( $record, $fh );
4396 sub script_write_fixedstep
4398 # Martin A. Hansen, Juli 2008.
4400 # Write fixedStep entries from recrods in the stream.
4402 my ( $in, # handle to in stream
4403 $out, # handle to out stream
4404 $options, # options hash
4409 my ( $fh, $record, $entry );
4411 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4413 while ( $record = get_record( $in ) )
4415 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4416 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
4419 put_record( $record, $out ) if not $options->{ "no_stream" };
4426 sub script_write_2bit
4428 # Martin A. Hansen, March 2008.
4430 # Write sequence entries from stream in 2bit format.
4432 my ( $in, # handle to in stream
4433 $out, # handle to out stream
4434 $options, # options hash
4439 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4441 $mask = 1 if not $options->{ "no_mask" };
4443 $tmp_file = "$BP_TMP/write_2bit.fna";
4444 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4446 $fh_out = write_stream( $options->{ "data_out" } );
4448 while ( $record = get_record( $in ) )
4450 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4451 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4454 put_record( $record, $out ) if not $options->{ "no_stream" };
4459 $fh_in = Maasha::Common::read_open( $tmp_file );
4461 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4470 sub script_write_solid
4472 # Martin A. Hansen, April 2008.
4474 # Write di-base encoded Solid sequence from entries in stream.
4476 my ( $in, # handle to in stream
4477 $out, # handle to out stream
4478 $options, # options hash
4483 my ( $record, $fh, $entry );
4485 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4487 while ( $record = get_record( $in ) )
4489 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4491 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4493 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4496 put_record( $record, $out ) if not $options->{ "no_stream" };
4503 sub script_write_ucsc_config
4505 # Martin A. Hansen, November 2008.
4507 # Write UCSC Genome Broser configuration (.ra file type) from
4508 # records in the stream.
4510 my ( $in, # handle to in stream
4511 $out, # handle to out stream
4512 $options, # options hash
4517 my ( $record, $fh );
4519 $fh = write_stream( $options->{ "data_out" } );
4521 while ( $record = get_record( $in ) )
4523 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4525 put_record( $record, $out ) if not $options->{ "no_stream" };
4532 sub script_plot_seqlogo
4534 # Martin A. Hansen, August 2007.
4536 # Calculates and writes a sequence logo for alignments.
4538 my ( $in, # handle to in stream
4539 $out, # handle to out stream
4540 $options, # options hash
4545 my ( $record, @entries, $logo, $fh );
4547 while ( $record = get_record( $in ) )
4549 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4550 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4553 put_record( $record, $out ) if not $options->{ "no_stream" };
4556 $logo = Maasha::Plot::seq_logo( \@entries );
4558 $fh = write_stream( $options->{ "data_out" } );
4566 sub script_plot_phastcons_profiles
4568 # Martin A. Hansen, January 2008.
4570 # Plots PhastCons profiles.
4572 my ( $in, # handle to in stream
4573 $out, # handle to out stream
4574 $options, # options hash
4579 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4581 $options->{ "title" } ||= "PhastCons Profiles";
4583 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4584 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4586 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4587 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4589 while ( $record = get_record( $in ) )
4591 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4593 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4594 $record->{ "CHR_BEG" },
4595 $record->{ "CHR_END" },
4596 $options->{ "flank" } );
4598 push @{ $AoA }, [ @{ $scores } ];
4601 put_record( $record, $out ) if not $options->{ "no_stream" };
4604 Maasha::UCSC::phastcons_normalize( $AoA );
4606 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4607 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4609 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4611 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4613 $fh = write_stream( $options->{ "data_out" } );
4615 print $fh "$_\n" foreach @{ $plot };
4621 sub script_analyze_bed
4623 # Martin A. Hansen, March 2008.
4625 # Analyze BED entries in stream.
4627 my ( $in, # handle to in stream
4628 $out, # handle to out stream
4629 $options, # options hash
4636 while ( $record = get_record( $in ) )
4638 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4640 put_record( $record, $out );
4645 sub script_analyze_vals
4647 # Martin A. Hansen, August 2007.
4649 # Analyze values for given keys in stream.
4651 my ( $in, # handle to in stream
4652 $out, # handle to out stream
4653 $options, # options hash
4658 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4660 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4662 while ( $record = get_record( $in ) )
4664 foreach $key ( keys %{ $record } )
4666 next if $options->{ "keys" } and not exists $key_hash{ $key };
4668 $analysis->{ $key }->{ "COUNT" }++;
4670 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4672 $analysis->{ $key }->{ "TYPE" } = "num";
4673 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4674 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4675 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4679 $len = length $record->{ $key };
4681 $analysis->{ $key }->{ "TYPE" } = "alph";
4682 $analysis->{ $key }->{ "SUM" } += $len;
4683 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4684 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4688 put_record( $record, $out ) if not $options->{ "no_stream" };
4691 foreach $key ( keys %{ $analysis } )
4693 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4694 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4697 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4707 if ( $options->{ "keys" } ) {
4708 @keys = @{ $options->{ "keys" } };
4710 @keys = keys %{ $analysis };
4713 foreach $key ( @keys )
4715 $keys .= sprintf "% 15s", $key;
4716 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4717 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4718 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4719 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4720 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4721 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4724 print $out "$keys\n";
4725 print $out "$types\n";
4726 print $out "$counts\n";
4727 print $out "$mins\n";
4728 print $out "$maxs\n";
4729 print $out "$sums\n";
4730 print $out "$means\n";
4734 sub script_head_records
4736 # Martin A. Hansen, August 2007.
4738 # Display the first sequences in stream.
4740 my ( $in, # handle to in stream
4741 $out, # handle to out stream
4742 $options, # options hash
4747 my ( $record, $count );
4749 $options->{ "num" } ||= 10;
4753 while ( $record = get_record( $in ) )
4757 put_record( $record, $out );
4759 last if $count == $options->{ "num" };
4764 sub script_remove_keys
4766 # Martin A. Hansen, August 2007.
4768 # Remove keys from stream.
4770 my ( $in, # handle to in stream
4771 $out, # handle to out stream
4772 $options, # options hash
4777 my ( $record, $new_record );
4779 while ( $record = get_record( $in ) )
4781 if ( $options->{ "keys" } )
4783 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4785 elsif ( $options->{ "save_keys" } )
4787 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4789 $record = $new_record;
4792 put_record( $record, $out ) if keys %{ $record };
4797 sub script_remove_adaptor
4799 # Martin A. Hansen, August 2008.
4801 # Find and remove adaptor from sequences in the stream.
4803 my ( $in, # handle to in stream
4804 $out, # handle to out stream
4805 $options, # options hash
4810 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4812 $options->{ "remove" } ||= "after";
4814 $max_mismatch = $options->{ "mismatches" } || 0;
4815 $offset = $options->{ "offset" };
4817 if ( not defined $offset ) {
4823 $adaptor = uc $options->{ "adaptor" };
4824 $adaptor_len = length $adaptor;
4826 while ( $record = get_record( $in ) )
4828 if ( $record->{ "SEQ" } )
4830 $seq = uc $record->{ "SEQ" };
4831 $seq_len = length $seq;
4833 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4835 $record->{ "ADAPTOR_POS" } = $pos;
4837 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
4839 if ( $options->{ "remove" } eq "after" )
4841 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4842 $record->{ "SEQ_LEN" } = $pos;
4846 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
4847 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
4851 put_record( $record, $out );
4855 put_record( $record, $out );
4861 sub script_remove_mysql_tables
4863 # Martin A. Hansen, November 2008.
4865 # Remove MySQL tables from values in stream.
4867 my ( $in, # handle to in stream
4868 $out, # handle to out stream
4869 $options, # options hash
4874 my ( $record, %table_hash, $dbh, $table );
4876 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
4877 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
4879 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
4881 while ( $record = get_record( $in ) )
4883 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4885 put_record( $record, $out ) if not $options->{ 'no_stream' };
4888 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4890 foreach $table ( sort keys %table_hash )
4892 if ( Maasha::SQL::table_exists( $dbh, $table ) )
4894 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4895 Maasha::SQL::delete_table( $dbh, $table );
4896 print STDERR "done.\n" if $options->{ 'verbose' };
4900 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
4904 Maasha::SQL::disconnect( $dbh );
4908 sub script_remove_ucsc_tracks
4910 # Martin A. Hansen, November 2008.
4912 # Remove track from MySQL tables and config file.
4914 my ( $in, # handle to in stream
4915 $out, # handle to out stream
4916 $options, # options hash
4921 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
4923 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
4924 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
4925 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
4927 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
4929 while ( $record = get_record( $in ) )
4931 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4933 put_record( $record, $out ) if not $options->{ 'no_stream' };
4936 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
4938 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
4939 push @tracks, $track;
4944 foreach $track ( @tracks )
4946 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
4947 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
4949 push @new_tracks, $track;
4953 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
4955 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
4957 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
4961 # ---- locate track in database ----
4963 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4965 foreach $track ( sort keys %track_hash )
4967 if ( Maasha::SQL::table_exists( $dbh, $track ) )
4969 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4970 Maasha::SQL::delete_table( $dbh, $track );
4971 print STDERR "done.\n" if $options->{ 'verbose' };
4975 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
4979 Maasha::SQL::disconnect( $dbh );
4981 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
4985 sub script_rename_keys
4987 # Martin A. Hansen, August 2007.
4989 # Rename keys in stream.
4991 my ( $in, # handle to in stream
4992 $out, # handle to out stream
4993 $options, # options hash
5000 while ( $record = get_record( $in ) )
5002 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5004 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
5006 delete $record->{ $options->{ "keys" }->[ 0 ] };
5009 put_record( $record, $out );
5014 sub script_uniq_vals
5016 # Martin A. Hansen, August 2007.
5018 # Find unique values in stream.
5020 my ( $in, # handle to in stream
5021 $out, # handle to out stream
5022 $options, # options hash
5027 my ( %hash, $record );
5029 while ( $record = get_record( $in ) )
5031 if ( $record->{ $options->{ "key" } } )
5033 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
5035 put_record( $record, $out );
5037 $hash{ $record->{ $options->{ "key" } } } = 1;
5039 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
5041 put_record( $record, $out );
5045 $hash{ $record->{ $options->{ "key" } } } = 1;
5050 put_record( $record, $out );
5056 sub script_merge_vals
5058 # Martin A. Hansen, August 2007.
5060 # Rename keys in stream.
5062 my ( $in, # handle to in stream
5063 $out, # handle to out stream
5064 $options, # options hash
5069 my ( $record, @join, $i );
5071 $options->{ "delimit" } ||= '_';
5073 while ( $record = get_record( $in ) )
5075 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5077 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5079 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5080 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5083 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5086 put_record( $record, $out );
5091 sub script_merge_records
5093 # Martin A. Hansen, July 2008.
5095 # Merges records in the stream based on identical values of two given keys.
5097 my ( $in, # handle to in stream
5098 $out, # handle to out stream
5099 $options, # options hash
5104 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5105 $num1, $num2, $num, $cmp, $i );
5107 $merge = $options->{ "merge" } || "AandB";
5109 $file1 = "$BP_TMP/merge_records1.tmp";
5110 $file2 = "$BP_TMP/merge_records2.tmp";
5112 $fh1 = Maasha::Common::write_open( $file1 );
5113 $fh2 = Maasha::Common::write_open( $file2 );
5115 $key1 = $options->{ "keys" }->[ 0 ];
5116 $key2 = $options->{ "keys" }->[ 1 ];
5118 $num = $key2 =~ s/n$//;
5122 while ( $record = get_record( $in ) )
5124 if ( exists $record->{ $key1 } )
5127 @vals1 = $record->{ $key1 };
5129 delete $record->{ $key1 };
5131 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5133 print $fh1 join( "\t", @vals1 ), "\n";
5137 elsif ( exists $record->{ $key2 } )
5140 @vals2 = $record->{ $key2 };
5142 delete $record->{ $key2 };
5144 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5146 print $fh2 join( "\t", @vals2 ), "\n";
5157 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5158 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5162 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5163 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5166 $fh1 = Maasha::Common::read_open( $file1 );
5167 $fh2 = Maasha::Common::read_open( $file2 );
5169 @vals1 = Maasha::Common::get_fields( $fh1 );
5170 @vals2 = Maasha::Common::get_fields( $fh2 );
5172 while ( $num1 > 0 and $num2 > 0 )
5177 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5179 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5184 if ( $merge =~ /^(AorB|AnotB)$/ )
5186 for ( $i = 0; $i < @keys1; $i++ ) {
5187 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5190 put_record( $record, $out );
5193 @vals1 = Maasha::Common::get_fields( $fh1 );
5198 if ( $merge =~ /^(BorA|BnotA)$/ )
5200 for ( $i = 0; $i < @keys2; $i++ ) {
5201 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5204 put_record( $record, $out );
5207 @vals2 = Maasha::Common::get_fields( $fh2 );
5212 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5214 for ( $i = 0; $i < @keys1; $i++ ) {
5215 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5218 for ( $i = 1; $i < @keys2; $i++ ) {
5219 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5222 put_record( $record, $out );
5225 @vals1 = Maasha::Common::get_fields( $fh1 );
5226 @vals2 = Maasha::Common::get_fields( $fh2 );
5238 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5242 for ( $i = 0; $i < @keys1; $i++ ) {
5243 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5246 put_record( $record, $out );
5249 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5253 for ( $i = 0; $i < @keys2; $i++ ) {
5254 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5257 put_record( $record, $out );
5264 # Martin A. Hansen, August 2007.
5266 # Grab for records in stream.
5268 my ( $in, # handle to in stream
5269 $out, # handle to out stream
5270 $options, # options hash
5275 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5277 $keys = $options->{ 'keys' };
5278 $vals_only = $options->{ 'vals_only' };
5279 $keys_only = $options->{ 'keys_only' };
5280 $invert = $options->{ 'invert' };
5282 if ( $options->{ 'patterns' } )
5284 $patterns = [ split ",", $options->{ 'patterns' } ];
5286 elsif ( -f $options->{ 'patterns_in' } )
5288 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5290 elsif ( $options->{ 'regex' } )
5292 if ( $options->{ 'case_insensitive' } ) {
5293 $regex = qr/$options->{ 'regex' }/i;
5295 $regex = qr/$options->{ 'regex' }/;
5298 elsif ( -f $options->{ 'exact_in' } )
5300 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5302 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5306 elsif ( $options->{ 'eval' } )
5308 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5316 while ( $record = get_record( $in ) )
5320 if ( %lookup_hash ) {
5321 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5322 } elsif ( $patterns ) {
5323 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5324 } elsif ( $regex ) {
5325 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5327 $found = grab_eval( $key, $op, $val, $record );
5330 if ( $found and not $invert ) {
5331 put_record( $record, $out );
5332 } elsif ( not $found and $invert ) {
5333 put_record( $record, $out );
5341 # Martin A. Hansen, August 2007.
5343 # Evaluate extression for records in stream.
5345 my ( $in, # handle to in stream
5346 $out, # handle to out stream
5347 $options, # options hash
5352 my ( $record, $eval_key, @keys, $eval_val );
5354 while ( $record = get_record( $in ) )
5356 if ( $options->{ "eval" } )
5358 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
5365 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5367 @keys = grep { exists $record->{ $_ } } @keys;
5370 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5372 $record->{ $eval_key } = eval "$eval_val";
5373 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
5377 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
5381 put_record( $record, $out );
5388 # Martin A. Hansen, June 2008.
5392 my ( $in, # handle to in stream
5393 $out, # handle to out stream
5394 $options, # options hash
5399 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5401 while ( $record = get_record( $in ) )
5405 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5407 push @rows, $record->{ $key };
5411 push @matrix, [ @rows ];
5416 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5418 foreach $row ( @matrix )
5420 for ( $i = 0; $i < @{ $row }; $i++ ) {
5421 $record->{ "V$i" } = $row->[ $i ];
5424 put_record( $record, $out );
5429 sub script_add_ident
5431 # Martin A. Hansen, May 2008.
5433 # Add a unique identifier to each record in stream.
5435 my ( $in, # handle to in stream
5436 $out, # handle to out stream
5437 $options, # options hash
5442 my ( $record, $key, $prefix, $i );
5444 $key = $options->{ "key" } || "ID";
5445 $prefix = $options->{ "prefix" } || "ID";
5449 while ( $record = get_record( $in ) )
5451 $record->{ $key } = sprintf( "$prefix%08d", $i );
5453 put_record( $record, $out );
5460 sub script_count_records
5462 # Martin A. Hansen, August 2007.
5464 # Count records in stream.
5466 my ( $in, # handle to in stream
5467 $out, # handle to out stream
5468 $options, # options hash
5473 my ( $record, $count, $result, $fh, $line );
5477 if ( $options->{ "no_stream" } )
5479 while ( $line = <$in> )
5483 $count++ if $line eq "---";
5488 while ( $record = get_record( $in ) )
5490 put_record( $record, $out );
5496 $result = { "RECORDS_COUNT" => $count };
5498 $fh = write_stream( $options->{ "data_out" } );
5500 put_record( $result, $fh );
5506 sub script_random_records
5508 # Martin A. Hansen, August 2007.
5510 # Pick a number or random records from stream.
5512 my ( $in, # handle to in stream
5513 $out, # handle to out stream
5514 $options, # options hash
5519 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5521 $options->{ "num" } ||= 10;
5523 $tmp_file = "$BP_TMP/random_records.tmp";
5525 $fh_out = Maasha::Common::write_open( $tmp_file );
5529 while ( $record = get_record( $in ) )
5531 put_record( $record, $fh_out );
5541 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5543 while ( $i < $options->{ "num" } )
5545 $rand = int( rand( $count ) );
5547 if ( not exists $rand_hash{ $rand } )
5549 $rand_hash{ $rand } = 1;
5551 $max = $rand if $rand > $max;
5557 $fh_in = Maasha::Common::read_open( $tmp_file );
5561 while ( $record = get_record( $fh_in ) )
5563 put_record( $record, $out ) if exists $rand_hash{ $count };
5565 last if $count == $max;
5576 sub script_sort_records
5578 # Martin A. Hansen, August 2007.
5580 # Sort to sort records according to keys.
5582 my ( $in, # handle to in stream
5583 $out, # handle to out stream
5584 $options, # options hash
5589 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5591 foreach $key ( @{ $options->{ "keys" } } )
5593 if ( $key =~ s/n$// ) {
5594 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5596 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5600 $sort_str = join " or ", @sort_cmd;
5601 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5603 while ( $record = get_record( $in ) ) {
5604 push @records, $record;
5607 @records = sort $sort_sub @records;
5609 if ( $options->{ "reverse" } )
5611 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5612 put_record( $records[ $i ], $out );
5617 for ( $i = 0; $i < scalar @records; $i++ ) {
5618 put_record( $records[ $i ], $out );
5624 sub script_count_vals
5626 # Martin A. Hansen, August 2007.
5628 # Count records in stream.
5630 my ( $in, # handle to in stream
5631 $out, # handle to out stream
5632 $options, # options hash
5637 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5639 $tmp_file = "$BP_TMP/count_cache.tmp";
5641 $fh_out = Maasha::Common::write_open( $tmp_file );
5646 while ( $record = get_record( $in ) )
5648 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5650 push @records, $record;
5652 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5654 map { put_record( $_, $fh_out ) } @records;
5661 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5672 $fh_in = Maasha::Common::read_open( $tmp_file );
5674 while ( $record = get_record( $fh_in ) )
5676 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5678 put_record( $record, $out );
5680 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5688 foreach $record ( @records )
5690 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5692 put_record( $record, $out );
5699 sub script_plot_histogram
5701 # Martin A. Hansen, September 2007.
5703 # Plot a simple histogram for a given key using GNU plot.
5705 my ( $in, # handle to in stream
5706 $out, # handle to out stream
5707 $options, # options hash
5712 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5714 $options->{ "title" } ||= "Histogram";
5715 $options->{ "sort" } ||= "num";
5717 while ( $record = get_record( $in ) )
5719 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5721 put_record( $record, $out ) if not $options->{ "no_stream" };
5724 if ( $options->{ "sort" } eq "num" ) {
5725 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5727 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5730 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5732 $fh = write_stream( $options->{ "data_out" } );
5734 print $fh "$_\n" foreach @{ $result };
5740 sub script_plot_lendist
5742 # Martin A. Hansen, August 2007.
5744 # Plot length distribution using GNU plot.
5746 my ( $in, # handle to in stream
5747 $out, # handle to out stream
5748 $options, # options hash
5753 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5755 $options->{ "title" } ||= "Length Distribution";
5757 while ( $record = get_record( $in ) )
5759 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5761 put_record( $record, $out ) if not $options->{ "no_stream" };
5764 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5766 for ( $i = 0; $i < $max; $i++ ) {
5767 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5770 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5772 $fh = write_stream( $options->{ "data_out" } );
5774 print $fh "$_\n" foreach @{ $result };
5780 sub script_plot_chrdist
5782 # Martin A. Hansen, August 2007.
5784 # Plot chromosome distribution using GNU plot.
5786 my ( $in, # handle to in stream
5787 $out, # handle to out stream
5788 $options, # options hash
5793 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5795 $options->{ "title" } ||= "Chromosome Distribution";
5797 while ( $record = get_record( $in ) )
5799 if ( $record->{ "CHR" } ) { # generic
5800 $data_hash{ $record->{ "CHR" } }++;
5801 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5802 $data_hash{ $record->{ "S_ID" } }++;
5803 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5804 $data_hash{ $record->{ "S_ID" } }++;
5805 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5806 $data_hash{ $record->{ "S_ID" } }++;
5809 put_record( $record, $out ) if not $options->{ "no_stream" };
5812 foreach $elem ( keys %data_hash )
5816 $sort_key =~ s/chr//i;
5818 $sort_key =~ s/^X(.*)/99$1/;
5819 $sort_key =~ s/^Y(.*)/99$1/;
5820 $sort_key =~ s/^Z(.*)/999$1/;
5821 $sort_key =~ s/^M(.*)/9999$1/;
5822 $sort_key =~ s/^U(.*)/99999$1/;
5824 $count = $sort_key =~ tr/_//;
5826 $sort_key =~ s/_.*/"999999" x $count/ex;
5828 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5831 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5833 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5835 $fh = write_stream( $options->{ "data_out" } );
5837 print $fh "$_\n" foreach @{ $result };
5843 sub script_plot_karyogram
5845 # Martin A. Hansen, August 2007.
5847 # Plot hits on karyogram.
5849 my ( $in, # handle to in stream
5850 $out, # handle to out stream
5851 $options, # options hash
5856 my ( %options, $record, @data, $fh, $result, %data_hash );
5858 $options->{ "genome" } ||= "human";
5859 $options->{ "feat_color" } ||= "black";
5861 while ( $record = get_record( $in ) )
5863 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5865 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5868 put_record( $record, $out ) if not $options->{ "no_stream" };
5871 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5873 $fh = write_stream( $options->{ "data_out" } );
5881 sub script_plot_matches
5883 # Martin A. Hansen, August 2007.
5885 # Plot matches in 2D generating a dotplot.
5887 my ( $in, # handle to in stream
5888 $out, # handle to out stream
5889 $options, # options hash
5894 my ( $record, @data, $fh, $result, %data_hash );
5896 $options->{ "direction" } ||= "both";
5898 while ( $record = get_record( $in ) )
5900 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5901 push @data, $record;
5904 put_record( $record, $out ) if not $options->{ "no_stream" };
5907 $options->{ "title" } ||= "plot_matches";
5908 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5909 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5911 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5913 $fh = write_stream( $options->{ "data_out" } );
5915 print $fh "$_\n" foreach @{ $result };
5921 sub script_length_vals
5923 # Martin A. Hansen, August 2007.
5925 # Determine the length of the value for given keys.
5927 my ( $in, # handle to in stream
5928 $out, # handle to out stream
5929 $options, # options hash
5934 my ( $record, $key );
5936 while ( $record = get_record( $in ) )
5938 foreach $key ( @{ $options->{ "keys" } } )
5940 if ( $record->{ $key } ) {
5941 $record->{ $key . "_LEN" } = length $record->{ $key };
5945 put_record( $record, $out );
5952 # Martin A. Hansen, August 2007.
5954 # Calculates the sums for values of given keys.
5956 my ( $in, # handle to in stream
5957 $out, # handle to out stream
5958 $options, # options hash
5963 my ( $record, $key, %sum_hash, $fh );
5965 while ( $record = get_record( $in ) )
5967 foreach $key ( @{ $options->{ "keys" } } )
5969 if ( $record->{ $key } ) {
5970 $sum_hash{ $key } += $record->{ $key };
5974 put_record( $record, $out ) if not $options->{ "no_stream" };
5977 $fh = write_stream( $options->{ "data_out" } );
5979 foreach $key ( @{ $options->{ "keys" } } ) {
5980 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5987 sub script_mean_vals
5989 # Martin A. Hansen, August 2007.
5991 # Calculate the mean of values of given keys.
5993 my ( $in, # handle to in stream
5994 $out, # handle to out stream
5995 $options, # options hash
6000 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
6002 while ( $record = get_record( $in ) )
6004 foreach $key ( @{ $options->{ "keys" } } )
6006 if ( $record->{ $key } )
6008 $sum_hash{ $key } += $record->{ $key };
6009 $count_hash{ $key }++;
6013 put_record( $record, $out ) if not $options->{ "no_stream" };
6016 $fh = write_stream( $options->{ "data_out" } );
6018 foreach $key ( @{ $options->{ "keys" } } )
6020 if ( $count_hash{ $key } ) {
6021 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
6026 put_record( { $key . "_MEAN" => $mean } , $fh );
6033 sub script_median_vals
6035 # Martin A. Hansen, March 2008.
6037 # Calculate the median values of given keys.
6039 my ( $in, # handle to in stream
6040 $out, # handle to out stream
6041 $options, # options hash
6046 my ( $record, $key, %median_hash, $median, $fh );
6048 while ( $record = get_record( $in ) )
6050 foreach $key ( @{ $options->{ "keys" } } ) {
6051 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6054 put_record( $record, $out ) if not $options->{ "no_stream" };
6057 $fh = write_stream( $options->{ "data_out" } );
6059 foreach $key ( @{ $options->{ "keys" } } )
6061 if ( $median_hash{ $key } ) {
6062 $median = Maasha::Calc::median( $median_hash{ $key } );
6067 put_record( { $key . "_MEDIAN" => $median } , $fh );
6076 # Martin A. Hansen, February 2008.
6078 # Determine the maximum values of given keys.
6080 my ( $in, # handle to in stream
6081 $out, # handle to out stream
6082 $options, # options hash
6087 my ( $record, $key, $fh, %max_hash, $max_record );
6089 while ( $record = get_record( $in ) )
6091 foreach $key ( @{ $options->{ "keys" } } )
6093 if ( $record->{ $key } )
6095 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6099 put_record( $record, $out ) if not $options->{ "no_stream" };
6102 $fh = write_stream( $options->{ "data_out" } );
6104 foreach $key ( @{ $options->{ "keys" } } )
6106 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6109 put_record( $max_record, $fh );
6117 # Martin A. Hansen, February 2008.
6119 # Determine the minimum values of given keys.
6121 my ( $in, # handle to in stream
6122 $out, # handle to out stream
6123 $options, # options hash
6128 my ( $record, $key, $fh, %min_hash, $min_record );
6130 while ( $record = get_record( $in ) )
6132 foreach $key ( @{ $options->{ "keys" } } )
6134 if ( defined $record->{ $key } )
6136 if ( exists $min_hash{ $key } ) {
6137 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6139 $min_hash{ $key } = $record->{ $key };
6144 put_record( $record, $out ) if not $options->{ "no_stream" };
6147 $fh = write_stream( $options->{ "data_out" } );
6149 foreach $key ( @{ $options->{ "keys" } } )
6151 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6154 put_record( $min_record, $fh );
6160 sub script_upload_to_ucsc
6162 # Martin A. Hansen, August 2007.
6164 # Calculate the mean of values of given keys.
6166 # This routine has developed into the most ugly hack. Do something!
6168 my ( $in, # handle to in stream
6169 $out, # handle to out stream
6170 $options, # options hash
6175 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
6177 $options->{ "short_label" } ||= $options->{ 'table' };
6178 $options->{ "long_label" } ||= $options->{ 'table' };
6179 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6180 $options->{ "priority" } ||= 1;
6181 $options->{ "visibility" } ||= "pack";
6182 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6183 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6185 $file = "$BP_TMP/ucsc_upload.tmp";
6190 $fh_out = Maasha::Common::write_open( $file );
6192 while ( $record = get_record( $in ) )
6194 put_record( $record, $out ) if not $options->{ "no_stream" };
6196 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6200 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
6201 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
6204 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6208 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6209 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6213 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6215 # chrom chromStart chromEnd name score strand size secStr conf
6219 print $fh_out join ( "\t",
6221 $record->{ "CHR_BEG" },
6222 $record->{ "CHR_END" } + 1,
6223 $record->{ "Q_ID" },
6224 $record->{ "SCORE" },
6225 $record->{ "STRAND" },
6226 $record->{ "SIZE" },
6227 $record->{ "SEC_STRUCT" },
6228 $record->{ "CONF" },
6231 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6234 $columns = $record->{ "BED_COLS" };
6236 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6237 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6240 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6245 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6246 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6249 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6254 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6256 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6257 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6260 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6265 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6266 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6270 if ( $i == $options->{ "chunk_size" } )
6274 if ( $format eq "BED" ) {
6275 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6276 } elsif ( $format eq "PSL" ) {
6277 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6286 $fh_out = Maasha::Common::write_open( $file );
6294 if ( exists $options->{ "database" } and $options->{ "table" } )
6296 if ( $format eq "BED" )
6298 $type = "bed $columns";
6300 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6302 elsif ( $format eq "BED_SS" )
6304 $type = "type bed 6 +";
6306 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6308 elsif ( $format eq "PSL" )
6312 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6314 elsif ( $format eq "WIGGLE" )
6316 $options->{ "visibility" } = "full";
6318 $wig_file = "$options->{ 'table' }.wig";
6319 $wib_file = "$options->{ 'table' }.wib";
6321 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6323 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6325 if ( $options->{ 'verbose' } ) {
6326 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6328 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6331 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6339 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6344 Maasha::UCSC::ucsc_update_config( $options, $type );
6349 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6354 # Martin A. Hansen, November 2009.
6356 # Uses keys from a lookup hash to search records. Optionally, a list of
6357 # keys can be given so the lookup is limited to these, also, flags
6358 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6359 # succeeded, else 0.
6361 my ( $lookup_hash, # hashref with patterns
6363 $keys, # list of keys - OPTIONAL
6364 $vals_only, # only vals flag - OPTIONAL
6365 $keys_only, # only keys flag - OPTIONAL
6372 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6376 if ( not $vals_only ) {
6377 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6380 if ( not $keys_only ) {
6381 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6391 # Martin A. Hansen, November 2009.
6393 # Uses patterns to match records containing the pattern as a substring.
6394 # Returns 1 if the record is matched, else 0.
6396 my ( $patterns, # list of patterns
6398 $keys, # list of keys - OPTIONAL
6399 $vals_only, # only vals flag - OPTIONAL
6400 $keys_only, # only keys flag - OPTIONAL
6407 foreach $pattern ( @{ $patterns } )
6411 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6415 if ( not $vals_only ) {
6416 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6419 if ( not $keys_only ) {
6420 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6431 # Martin A. Hansen, November 2009.
6433 # Uses regex to match records.
6434 # Returns 1 if the record is matched, else 0.
6436 my ( $regex, # regex to match
6438 $keys, # list of keys - OPTIONAL
6439 $vals_only, # only vals flag - OPTIONAL
6440 $keys_only, # only keys flag - OPTIONAL
6447 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6451 if ( not $vals_only ) {
6452 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6455 if ( not $keys_only ) {
6456 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6466 # Martin A. Hansen, November 2009.
6468 # Test if the value of a given record key evaluates according
6469 # to a given operator. Returns 1 if eval is OK, else 0.
6471 my ( $key, # record key
6479 if ( defined $record->{ $key } )
6481 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6482 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6483 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6484 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6485 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6486 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6487 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6488 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6497 # Martin A. Hansen, July 2007.
6499 # Opens a stream to STDIN or a file,
6501 my ( $path, # path - OPTIONAL
6504 # Returns filehandle.
6508 if ( not -t STDIN ) {
6509 $fh = Maasha::Common::read_stdin();
6510 } elsif ( not $path ) {
6511 # Maasha::Common::error( qq(no data stream) );
6513 $fh = Maasha::Common::read_open( $path );
6516 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6524 # Martin A. Hansen, August 2007.
6526 # Opens a stream to STDOUT or a file.
6528 my ( $path, # path - OPTIONAL
6529 $gzip, # compress data - OPTIONAL
6532 # Returns filehandle.
6537 $fh = Maasha::Common::write_open( $path, $gzip );
6539 $fh = Maasha::Common::write_stdout();
6548 # Martin A. Hansen, July 2007.
6550 # Reads one record at a time and converts that record
6551 # to a Perl data structure (a hash) which is returned.
6553 my ( $fh, # handle to stream
6558 my ( $block, @lines, $line, $key, $value, %record );
6560 local $/ = "\n---\n";
6566 return if not defined $block;
6568 @lines = split "\n", $block;
6570 foreach $line ( @lines )
6572 ( $key, $value ) = split ": ", $line, 2;
6574 $record{ $key } = $value;
6577 return wantarray ? %record : \%record;
6583 # Martin A. Hansen, July 2007.
6585 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6587 my ( $data, # data structure
6588 $fh, # file handle - OPTIONAL
6593 if ( scalar keys %{ $data } )
6597 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6602 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6613 # Martin A. Hansen, November 2007.
6615 # Extracts files from an explicit GetOpt::Long argument
6616 # allowing for the use of glob. E.g.
6617 # --data_in=test.fna
6618 # --data_in=test.fna,test2.fna
6620 # --data_in=test.fna,/dir/*.fna
6622 my ( $option, # option from GetOpt::Long
6627 my ( $elem, @files );
6629 foreach $elem ( split ",", $option )
6633 } elsif ( $elem =~ /\*/ ) {
6634 push @files, glob( $elem );
6638 return wantarray ? @files : \@files;
6644 # Martin A. Hansen, April 2008.
6646 # Removes temporary directory and exits gracefully.
6647 # This subroutine is meant to be run always as the last
6648 # thing even if a script is dies or is interrupted
6651 my ( $sig, # signal from the %SIG
6654 # print STDERR "signal->$sig<-\n";
6662 if ( $sig =~ /MAASHA_ERROR/ ) {
6663 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6664 } elsif ( $sig eq "INT" ) {
6665 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6666 } elsif ( $sig eq "TERM" ) {
6667 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6669 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6681 # Martin A. Hansen, July 2008.
6683 # Cleans out any unused temporary files and directories in BP_TMP.
6687 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6689 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6691 $curr_pid = Maasha::Common::get_processid();
6693 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6695 foreach $dir ( @dirs )
6697 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6703 next if $user eq "m.hansen";
6705 if ( $user eq Maasha::Common::get_user() )
6707 if ( not Maasha::Common::process_running( $pid ) )
6709 # print STDERR "Removing stale dir: $dir\n";
6710 Maasha::Common::dir_remove( $dir );
6712 elsif ( $pid == $curr_pid )
6714 # print STDERR "Removing current dir: $dir\n";
6715 Maasha::Common::dir_remove( $dir );
6729 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<