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, $cols, $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 );
2678 $cols = 5; # we only need the first 5 BED columns
2680 while ( $record = get_record( $in ) )
2682 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
2683 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
2689 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
2693 foreach $chr ( sort keys %{ $file_hash } )
2695 $bed_file = $file_hash->{ $chr };
2696 $fixedstep_file = "$bed_file.fixedstep";
2698 Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2700 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2702 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2704 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2705 put_record( $record, $out );
2712 unlink $fixedstep_file;
2717 sub script_reverse_seq
2719 # Martin A. Hansen, August 2007.
2721 # Reverse sequence in record.
2723 my ( $in, # handle to in stream
2724 $out, # handle to out stream
2731 while ( $record = get_record( $in ) )
2733 if ( $record->{ "SEQ" } ) {
2734 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2737 put_record( $record, $out );
2742 sub script_complement_seq
2744 # Martin A. Hansen, August 2007.
2746 # Complement sequence in record.
2748 my ( $in, # handle to in stream
2749 $out, # handle to out stream
2754 my ( $record, $type );
2756 while ( $record = get_record( $in ) )
2758 if ( $record->{ "SEQ" } )
2761 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2764 if ( $type eq "rna" ) {
2765 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2766 } elsif ( $type eq "dna" ) {
2767 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2771 put_record( $record, $out );
2776 sub script_remove_indels
2778 # Martin A. Hansen, August 2007.
2780 # Remove indels from sequences in stream.
2782 my ( $in, # handle to in stream
2783 $out, # handle to out stream
2790 while ( $record = get_record( $in ) )
2792 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2794 put_record( $record, $out );
2799 sub script_transliterate_seq
2801 # Martin A. Hansen, August 2007.
2803 # Transliterate chars from sequence in record.
2805 my ( $in, # handle to in stream
2806 $out, # handle to out stream
2807 $options, # options hash
2812 my ( $record, $search, $replace, $delete );
2814 $search = $options->{ "search" } || "";
2815 $replace = $options->{ "replace" } || "";
2816 $delete = $options->{ "delete" } || "";
2818 while ( $record = get_record( $in ) )
2820 if ( $record->{ "SEQ" } )
2822 if ( $search and $replace ) {
2823 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2824 } elsif ( $delete ) {
2825 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2829 put_record( $record, $out );
2834 sub script_transliterate_vals
2836 # Martin A. Hansen, April 2008.
2838 # Transliterate chars from values in record.
2840 my ( $in, # handle to in stream
2841 $out, # handle to out stream
2842 $options, # options hash
2847 my ( $record, $search, $replace, $delete, $key );
2849 $search = $options->{ "search" } || "";
2850 $replace = $options->{ "replace" } || "";
2851 $delete = $options->{ "delete" } || "";
2853 while ( $record = get_record( $in ) )
2855 foreach $key ( @{ $options->{ "keys" } } )
2857 if ( exists $record->{ $key } )
2859 if ( $search and $replace ) {
2860 eval "\$record->{ $key } =~ tr/$search/$replace/";
2861 } elsif ( $delete ) {
2862 eval "\$record->{ $key } =~ tr/$delete//d";
2867 put_record( $record, $out );
2872 sub script_translate_seq
2874 # Martin A. Hansen, February 2008.
2876 # Translate DNA sequence into protein sequence.
2878 my ( $in, # handle to in stream
2879 $out, # handle to out stream
2880 $options, # options hash
2885 my ( $record, $frame, %new_record );
2887 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2889 while ( $record = get_record( $in ) )
2891 if ( $record->{ "SEQ" } )
2893 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2895 foreach $frame ( @{ $options->{ "frames" } } )
2897 %new_record = %{ $record };
2899 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2900 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2901 $new_record{ "FRAME" } = $frame;
2903 put_record( \%new_record, $out );
2909 put_record( $record, $out );
2915 sub script_extract_seq
2917 # Martin A. Hansen, August 2007.
2919 # Extract subsequences from sequences in record.
2921 my ( $in, # handle to in stream
2922 $out, # handle to out stream
2923 $options, # options hash
2928 my ( $beg, $end, $len, $record );
2930 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2933 $beg = $options->{ "beg" } - 1; # correcting for start offset
2936 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2938 } elsif ( defined $options->{ "end" } ) {
2939 $end = $options->{ "end" } - 1; # correcting for start offset
2942 $len = $options->{ "len" };
2944 # print "beg->$beg, end->$end, len->$len\n";
2946 while ( $record = get_record( $in ) )
2948 if ( $record->{ "SEQ" } )
2950 if ( defined $beg and defined $end )
2952 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2953 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2955 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2958 elsif ( defined $beg and defined $len )
2960 if ( $len > length $record->{ "SEQ" } ) {
2961 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2963 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2966 elsif ( defined $beg )
2968 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2972 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2974 put_record( $record, $out );
2979 sub script_get_genome_seq
2981 # Martin A. Hansen, December 2007.
2983 # Gets a subsequence from a genome.
2985 my ( $in, # handle to in stream
2986 $out, # handle to out stream
2987 $options, # options hash
2992 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i, $seq );
2994 $options->{ "flank" } ||= 0;
2996 if ( $options->{ "genome" } )
2998 $genome = $options->{ "genome" };
3000 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
3001 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
3003 $fh = Maasha::Common::read_open( $genome_file );
3004 $index = Maasha::Fasta::index_retrieve( $index_file );
3006 shift @{ $index }; # Get rid of the file size info
3008 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
3010 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3012 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
3014 $beg = $index_beg + $options->{ "beg" } - 1;
3016 if ( $options->{ "len" } ) {
3017 $len = $options->{ "len" };
3018 } elsif ( $options->{ "end" } ) {
3019 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
3022 $beg -= $options->{ "flank" };
3023 $len += 2 * $options->{ "flank" };
3025 if ( $beg <= $index_beg )
3027 $len -= $index_beg - $beg;
3031 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3033 next if $beg > $index_beg + $index_len;
3035 $record->{ "CHR" } = $options->{ "chr" };
3036 $record->{ "CHR_BEG" } = $beg - $index_beg;
3037 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3039 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3040 $record->{ "SEQ_LEN" } = $len;
3042 put_record( $record, $out );
3046 while ( $record = get_record( $in ) )
3048 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
3050 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
3052 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
3054 $beg = $record->{ "CHR_BEG" } + $index_beg;
3055 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3057 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
3059 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3061 $beg = $record->{ "S_BEG" } + $index_beg;
3062 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3064 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
3066 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3068 $beg = $record->{ "S_BEG" } + $index_beg;
3069 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3072 $beg -= $options->{ "flank" };
3073 $len += 2 * $options->{ "flank" };
3075 if ( $beg <= $index_beg )
3077 $len -= $index_beg - $beg;
3081 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3083 next if $beg > $index_beg + $index_len;
3085 $record->{ "CHR_BEG" } = $beg - $index_beg;
3086 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3088 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3090 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
3092 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
3093 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
3096 if ( $options->{ "mask" } )
3098 if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
3100 $record->{ "SEQ" } = lc $record->{ "SEQ" };
3102 @begs = split ",", $record->{ "Q_BEGS" };
3103 @lens = split ",", $record->{ "BLOCK_LENS" };
3105 for ( $i = 0; $i < @begs; $i++ ) {
3106 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3110 elsif ( $options->{ "splice" } )
3112 if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
3115 @begs = split ",", $record->{ "Q_BEGS" };
3116 @lens = split ",", $record->{ "BLOCK_LENS" };
3118 for ( $i = 0; $i < @begs; $i++ ) {
3119 $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3125 put_record( $record, $out );
3132 sub script_get_genome_align
3134 # Martin A. Hansen, April 2008.
3136 # Gets a subalignment from a multiple genome alignment.
3138 my ( $in, # handle to in stream
3139 $out, # handle to out stream
3140 $options, # options hash
3145 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3147 $options->{ "strand" } ||= "+";
3151 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3153 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3155 $beg = $options->{ "beg" } - 1;
3157 if ( $options->{ "end" } ) {
3158 $end = $options->{ "end" };
3159 } elsif ( $options->{ "len" } ) {
3160 $end = $beg + $options->{ "len" };
3163 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3165 foreach $entry ( @{ $align } )
3167 $entry->{ "CHR" } = $record->{ "CHR" };
3168 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3169 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3170 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3171 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3172 $entry->{ "SCORE" } = $record->{ "SCORE" };
3174 put_record( $entry, $out );
3178 while ( $record = get_record( $in ) )
3180 if ( $record->{ "REC_TYPE" } eq "BED" )
3182 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3184 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
3186 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
3188 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3190 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3192 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3194 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3197 foreach $entry ( @{ $align } )
3199 $entry->{ "CHR" } = $record->{ "CHR" };
3200 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3201 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3202 $entry->{ "STRAND" } = $record->{ "STRAND" };
3203 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3204 $entry->{ "SCORE" } = $record->{ "SCORE" };
3206 put_record( $entry, $out );
3214 sub script_get_genome_phastcons
3216 # Martin A. Hansen, February 2008.
3218 # Get phastcons scores from genome intervals.
3220 my ( $in, # handle to in stream
3221 $out, # handle to out stream
3222 $options, # options hash
3227 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3229 $options->{ "flank" } ||= 0;
3231 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3232 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3234 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3235 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3237 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3239 $options->{ "beg" } -= 1; # request is 1-based
3240 $options->{ "end" } -= 1; # request is 1-based
3242 if ( $options->{ "len" } ) {
3243 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3246 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3248 $record->{ "CHR" } = $options->{ "chr" };
3249 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3250 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3252 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3253 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3255 put_record( $record, $out );
3258 while ( $record = get_record( $in ) )
3260 if ( $record->{ "REC_TYPE" } eq "BED" )
3262 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3264 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3266 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3268 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3270 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3273 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3274 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3276 put_record( $record, $out );
3279 close $fh_phastcons if $fh_phastcons;
3285 # Martin A. Hansen, December 2007.
3287 # Folds sequences in stream into secondary structures.
3289 my ( $in, # handle to in stream
3290 $out, # handle to out stream
3295 my ( $record, $type, $struct, $index );
3297 while ( $record = get_record( $in ) )
3299 if ( $record->{ "SEQ" } )
3302 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3305 if ( $type ne "protein" )
3307 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3308 $record->{ "SEC_STRUCT" } = $struct;
3309 $record->{ "FREE_ENERGY" } = $index;
3310 $record->{ "SCORE" } = abs int $index;
3311 $record->{ "SIZE" } = length $struct;
3312 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3316 put_record( $record, $out );
3321 sub script_split_seq
3323 # Martin A. Hansen, August 2007.
3325 # Split a sequence in stream into words.
3327 my ( $in, # handle to in stream
3328 $out, # handle to out stream
3329 $options, # options hash
3334 my ( $record, $new_record, $i, $subseq, %lookup );
3336 $options->{ "word_size" } ||= 7;
3338 while ( $record = get_record( $in ) )
3340 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3342 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3344 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3346 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3348 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3349 $new_record->{ "SEQ" } = $subseq;
3351 put_record( $new_record, $out );
3353 $lookup{ $subseq } = 1;
3357 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3358 $new_record->{ "SEQ" } = $subseq;
3360 put_record( $new_record, $out );
3366 put_record( $record, $out );
3372 sub script_split_bed
3374 # Martin A. Hansen, June 2008.
3376 # Split a BED record into overlapping windows.
3378 my ( $in, # handle to in stream
3379 $out, # handle to out stream
3380 $options, # options hash
3385 my ( $record, $new_record, $i );
3387 $options->{ "window_size" } ||= 20;
3388 $options->{ "step_size" } ||= 1;
3390 while ( $record = get_record( $in ) )
3392 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3394 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3396 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3398 $new_record->{ "REC_TYPE" } = "BED";
3399 $new_record->{ "CHR" } = $record->{ "CHR" };
3400 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3401 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3402 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3403 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3404 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3405 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3407 put_record( $new_record, $out );
3412 put_record( $record, $out );
3418 sub script_align_seq
3420 # Martin A. Hansen, August 2007.
3422 # Align sequences in stream.
3424 my ( $in, # handle to in stream
3425 $out, # handle to out stream
3430 my ( $record, @entries, $entry );
3432 while ( $record = get_record( $in ) )
3434 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3435 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3436 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3437 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3439 put_record( $record, $out );
3443 @entries = Maasha::Align::align( \@entries );
3445 foreach $entry ( @entries )
3447 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3450 SEQ_NAME => $entry->[ SEQ_NAME ],
3451 SEQ => $entry->[ SEQ ],
3454 put_record( $record, $out );
3462 # Martin A. Hansen, February 2008.
3464 # Using the first sequence in stream as reference, tile
3465 # all subsequent sequences based on pairwise alignments.
3467 my ( $in, # handle to in stream
3468 $out, # handle to out stream
3469 $options, # options hash
3474 my ( $record, $first, $ref_entry, @entries );
3478 while ( $record = get_record( $in ) )
3480 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3484 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3490 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3495 put_record( $record, $out );
3499 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3501 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3505 sub script_invert_align
3507 # Martin A. Hansen, February 2008.
3509 # Inverts an alignment showing only non-mathing residues
3510 # using the first sequence as reference.
3512 my ( $in, # handle to in stream
3513 $out, # handle to out stream
3514 $options, # options hash
3519 my ( $record, @entries );
3521 while ( $record = get_record( $in ) )
3523 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3525 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3529 put_record( $record, $out );
3533 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3535 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3539 sub script_patscan_seq
3541 # Martin A. Hansen, August 2007.
3543 # Locates patterns in sequences using scan_for_matches.
3545 my ( $in, # handle to in stream
3546 $out, # handle to out stream
3547 $options, # options hash
3552 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3554 if ( $options->{ "patterns" } ) {
3555 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3556 } elsif ( -f $options->{ "patterns_in" } ) {
3557 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3560 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3562 push @args, "-c" if $options->{ "comp" };
3563 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3564 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3566 $seq_file = "$BP_TMP/patscan.seq";
3567 $pat_file = "$BP_TMP/patscan.pat";
3568 $out_file = "$BP_TMP/patscan.out";
3570 $fh_out = Maasha::Common::write_open( $seq_file );
3574 while ( $record = get_record( $in ) )
3576 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3578 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3580 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3582 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3590 $arg = join " ", @args;
3591 $arg .= " -p" if $type eq "protein";
3593 foreach $pattern ( @{ $patterns } )
3595 $fh_out = Maasha::Common::write_open( $pat_file );
3597 print $fh_out "$pattern\n";
3601 if ( $options->{ 'genome' } ) {
3602 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3603 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3605 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3606 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3609 $fh_in = Maasha::Common::read_open( $out_file );
3611 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3613 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3615 if ( $options->{ 'genome' } )
3617 $result->{ "CHR" } = $result->{ "S_ID" };
3618 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3619 $result->{ "CHR_END" } = $result->{ "S_END" };
3621 delete $result->{ "S_ID" };
3622 delete $result->{ "S_BEG" };
3623 delete $result->{ "S_END" };
3627 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3630 put_record( $result, $out );
3642 sub script_create_blast_db
3644 # Martin A. Hansen, September 2007.
3646 # Creates a NCBI BLAST database with formatdb
3648 my ( $in, # handle to in stream
3649 $out, # handle to out stream
3650 $options, # options hash
3655 my ( $fh, $seq_type, $path, $record, $entry );
3657 $path = $options->{ "database" };
3659 $fh = Maasha::Common::write_open( $path );
3661 while ( $record = get_record( $in ) )
3663 put_record( $record, $out ) if not $options->{ "no_stream" };
3665 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3667 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3669 Maasha::Fasta::put_entry( $entry, $fh );
3675 if ( $seq_type eq "protein" ) {
3676 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3678 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3685 sub script_blast_seq
3687 # Martin A. Hansen, September 2007.
3689 # BLASTs sequences in stream against a given database.
3691 my ( $in, # handle to in stream
3692 $out, # handle to out stream
3693 $options, # options hash
3698 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3700 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3701 $options->{ "filter" } = "F";
3702 $options->{ "filter" } = "T" if $options->{ "filter" };
3703 $options->{ "cpus" } ||= 1;
3705 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3707 $tmp_in = "$BP_TMP/blast_query.seq";
3708 $tmp_out = "$BP_TMP/blast.result";
3710 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
3712 while ( $record = get_record( $in ) )
3714 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3716 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3718 Maasha::Fasta::put_entry( $entry, $fh_out );
3721 put_record( $record, $out );
3726 if ( -f $options->{ 'database' } . ".phr" ) {
3727 $s_type = "protein";
3729 $s_type = "nucleotide";
3732 if ( not $options->{ 'program' } )
3734 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3735 $options->{ 'program' } = "blastn";
3736 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3737 $options->{ 'program' } = "blastp";
3738 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3739 $options->{ 'program' } = "blastx";
3740 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3741 $options->{ 'program' } = "tblastn";
3745 if ( $options->{ 'verbose' } )
3747 Maasha::Common::run(
3750 "-p $options->{ 'program' }",
3751 "-e $options->{ 'e_val' }",
3752 "-a $options->{ 'cpus' }",
3755 "-d $options->{ 'database' }",
3756 "-F $options->{ 'filter' }",
3764 Maasha::Common::run(
3767 "-p $options->{ 'program' }",
3768 "-e $options->{ 'e_val' }",
3769 "-a $options->{ 'cpus' }",
3772 "-d $options->{ 'database' }",
3773 "-F $options->{ 'filter' }",
3783 $fh_out = Maasha::Filesys::file_read_open( $tmp_out );
3787 while ( $line = <$fh_out> )
3791 next if $line =~ /^#/;
3793 @fields = split /\s+/, $line;
3795 $record->{ "REC_TYPE" } = "BLAST";
3796 $record->{ "Q_ID" } = $fields[ 0 ];
3797 $record->{ "S_ID" } = $fields[ 1 ];
3798 $record->{ "IDENT" } = $fields[ 2 ];
3799 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3800 $record->{ "MISMATCHES" } = $fields[ 4 ];
3801 $record->{ "GAPS" } = $fields[ 5 ];
3802 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3803 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3804 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3805 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3806 $record->{ "E_VAL" } = $fields[ 10 ];
3807 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3809 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3811 $record->{ "STRAND" } = '-';
3813 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3817 $record->{ "STRAND" } = '+';
3820 put_record( $record, $out );
3831 # Martin A. Hansen, August 2007.
3833 # BLATs sequences in stream against a given genome.
3835 my ( $in, # handle to in stream
3836 $out, # handle to out stream
3837 $options, # options hash
3842 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3844 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3846 $options->{ 'tile_size' } ||= 11;
3847 $options->{ 'one_off' } ||= 0;
3848 $options->{ 'min_identity' } ||= 90;
3849 $options->{ 'min_score' } ||= 0;
3850 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3852 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3853 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3854 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3855 $blat_args .= " -minScore=$options->{ 'min_score' }";
3856 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3857 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3859 $query_file = "$BP_TMP/blat.seq";
3861 $fh_out = Maasha::Common::write_open( $query_file );
3863 while ( $record = get_record( $in ) )
3865 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3867 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3868 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3871 put_record( $record, $out );
3876 $blat_args .= " -t=dnax" if $type eq "protein";
3877 $blat_args .= " -q=$type";
3879 $result_file = "$BP_TMP/blat.psl";
3881 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3885 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3887 map { put_record( $_, $out ) } @{ $entries };
3889 unlink $result_file;
3895 # Martin A. Hansen, July 2008.
3897 # soap sequences in stream against a given file or genome.
3899 my ( $in, # handle to in stream
3900 $out, # handle to out stream
3901 $options, # options hash
3906 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3908 $options->{ "seed_size" } ||= 10;
3909 $options->{ "mismatches" } ||= 2;
3910 $options->{ "gap_size" } ||= 0;
3911 $options->{ "cpus" } ||= 1;
3913 if ( $options->{ "genome" } ) {
3914 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3917 $tmp_in = "$BP_TMP/soap_query.seq";
3918 $tmp_out = "$BP_TMP/soap.result";
3920 $fh_out = Maasha::Common::write_open( $tmp_in );
3924 while ( $record = get_record( $in ) )
3926 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3928 Maasha::Fasta::put_entry( $entry, $fh_out );
3933 put_record( $record, $out );
3941 "-s $options->{ 'seed_size' }",
3944 "-v $options->{ 'mismatches' }",
3945 "-g $options->{ 'gap_size' }",
3946 "-p $options->{ 'cpus' }",
3947 "-d $options->{ 'in_file' }",
3951 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3953 Maasha::Common::run( "soap", $args, 1 );
3957 $fh_out = Maasha::Common::read_open( $tmp_out );
3961 while ( $line = <$fh_out> )
3965 @fields = split /\t/, $line;
3967 $record->{ "REC_TYPE" } = "SOAP";
3968 $record->{ "Q_ID" } = $fields[ 0 ];
3969 $record->{ "SCORE" } = $fields[ 3 ];
3970 $record->{ "STRAND" } = $fields[ 6 ];
3971 $record->{ "S_ID" } = $fields[ 7 ];
3972 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3973 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3975 put_record( $record, $out );
3985 sub script_match_seq
3987 # Martin A. Hansen, August 2007.
3989 # BLATs sequences in stream against a given genome.
3991 my ( $in, # handle to in stream
3992 $out, # handle to out stream
3993 $options, # options hash
3998 my ( $record, @entries, $results );
4000 $options->{ "word_size" } ||= 20;
4001 $options->{ "direction" } ||= "both";
4003 while ( $record = get_record( $in ) )
4005 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4006 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4009 put_record( $record, $out );
4012 if ( @entries == 1 )
4014 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
4016 map { put_record( $_, $out ) } @{ $results };
4018 elsif ( @entries == 2 )
4020 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
4022 map { put_record( $_, $out ) } @{ $results };
4027 sub script_create_vmatch_index
4029 # Martin A. Hansen, January 2008.
4031 # Create a vmatch index from sequences in the stream.
4033 my ( $in, # handle to in stream
4034 $out, # handle to out stream
4035 $options, # options hash
4040 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
4042 if ( $options->{ "index_name" } )
4044 $file_tmp = $options->{ 'index_name' };
4045 $fh_tmp = Maasha::Common::write_open( $file_tmp );
4048 while ( $record = get_record( $in ) )
4050 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4052 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4054 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
4057 put_record( $record, $out ) if not $options->{ "no_stream" };
4060 if ( $options->{ "index_name" } )
4064 if ( $type eq "protein" ) {
4065 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4067 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4075 sub script_vmatch_seq
4077 # Martin A. Hansen, August 2007.
4079 # Vmatches sequences in stream against a given genome.
4081 my ( $in, # handle to in stream
4082 $out, # handle to out stream
4083 $options, # options hash
4088 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
4090 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
4092 if ( $options->{ "index_name" } )
4094 @index_files = $options->{ "index_name" };
4098 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
4100 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
4102 @index_files = sort keys %hash;
4105 while ( $record = get_record( $in ) )
4107 push @records, $record;
4109 put_record( $record, $out );
4112 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
4116 $fh_in = Maasha::Common::read_open( $result_file );
4118 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
4119 put_record( $record, $out );
4124 unlink $result_file;
4128 sub script_write_fasta
4130 # Martin A. Hansen, August 2007.
4132 # Write FASTA entries from sequences in stream.
4134 my ( $in, # handle to in stream
4135 $out, # handle to out stream
4136 $options, # options hash
4141 my ( $record, $fh, $entry );
4143 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4145 while ( $record = get_record( $in ) )
4147 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4148 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4151 put_record( $record, $out ) if not $options->{ "no_stream" };
4158 sub script_write_align
4160 # Martin A. Hansen, August 2007.
4162 # Write pretty alignments aligned sequences in stream.
4164 my ( $in, # handle to in stream
4165 $out, # handle to out stream
4166 $options, # options hash
4171 my ( $fh, $record, @entries );
4173 $fh = write_stream( $options->{ "data_out" } ) ;
4175 while ( $record = get_record( $in ) )
4177 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4178 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4181 put_record( $record, $out ) if not $options->{ "no_stream" };
4184 if ( scalar( @entries ) == 2 ) {
4185 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4186 } elsif ( scalar ( @entries ) > 2 ) {
4187 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4194 sub script_write_blast
4196 # Martin A. Hansen, November 2007.
4198 # Write data in blast table format (-m8 and 9).
4200 my ( $in, # handle to in stream
4201 $out, # handle to out stream
4202 $options, # options hash
4207 my ( $fh, $record, $first );
4209 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4213 while ( $record = get_record( $in ) )
4215 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4217 if ( $options->{ "comment" } and $first )
4219 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";
4224 if ( $record->{ "STRAND" } eq "-" ) {
4225 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4228 print $fh join( "\t",
4229 $record->{ "Q_ID" },
4230 $record->{ "S_ID" },
4231 $record->{ "IDENT" },
4232 $record->{ "ALIGN_LEN" },
4233 $record->{ "MISMATCHES" },
4234 $record->{ "GAPS" },
4235 $record->{ "Q_BEG" } + 1,
4236 $record->{ "Q_END" } + 1,
4237 $record->{ "S_BEG" } + 1,
4238 $record->{ "S_END" } + 1,
4239 $record->{ "E_VAL" },
4240 $record->{ "BIT_SCORE" }
4244 put_record( $record, $out ) if not $options->{ "no_stream" };
4251 sub script_write_tab
4253 # Martin A. Hansen, August 2007.
4255 # Write data as table.
4257 my ( $in, # handle to in stream
4258 $out, # handle to out stream
4259 $options, # options hash
4264 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4266 $options->{ "delimit" } ||= "\t";
4268 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4270 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4272 while ( $record = get_record( $in ) )
4277 if ( $options->{ "keys" } )
4279 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4283 foreach $key ( @{ $options->{ "keys" } } )
4285 if ( exists $record->{ $key } )
4287 push @keys, $key if $options->{ "comment" };
4288 push @vals, $record->{ $key };
4295 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4297 next if exists $no_keys{ $key };
4299 push @keys, $key if $options->{ "comment" };
4300 push @vals, $record->{ $key };
4304 if ( @keys and $options->{ "comment" } )
4306 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4308 delete $options->{ "comment" };
4311 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4313 put_record( $record, $out ) if not $options->{ "no_stream" };
4320 sub script_write_bed
4322 # Martin A. Hansen, August 2007.
4324 # Write BED format for the UCSC genome browser using records in stream.
4326 my ( $in, # handle to in stream
4327 $out, # handle to out stream
4328 $options, # options hash
4333 my ( $cols, $fh, $record, $bed_entry, $new_record );
4335 $cols = $options->{ 'cols' }->[ 0 ];
4337 $fh = write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
4339 while ( $record = get_record( $in ) )
4341 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
4343 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
4344 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
4347 put_record( $record, $out ) if not $options->{ 'no_stream' };
4354 sub script_write_psl
4356 # Martin A. Hansen, August 2007.
4358 # Write PSL output from stream.
4360 my ( $in, # handle to in stream
4361 $out, # handle to out stream
4362 $options, # options hash
4367 my ( $fh, $record, @output, $first );
4371 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4373 while ( $record = get_record( $in ) )
4375 put_record( $record, $out ) if not $options->{ "no_stream" };
4377 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4379 Maasha::UCSC::psl_put_header( $fh ) if $first;
4380 Maasha::UCSC::psl_put_entry( $record, $fh );
4389 sub script_write_fixedstep
4391 # Martin A. Hansen, Juli 2008.
4393 # Write fixedStep entries from recrods in the stream.
4395 my ( $in, # handle to in stream
4396 $out, # handle to out stream
4397 $options, # options hash
4402 my ( $fh, $record, $entry );
4404 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4406 while ( $record = get_record( $in ) )
4408 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4409 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
4412 put_record( $record, $out ) if not $options->{ "no_stream" };
4419 sub script_write_2bit
4421 # Martin A. Hansen, March 2008.
4423 # Write sequence entries from stream in 2bit format.
4425 my ( $in, # handle to in stream
4426 $out, # handle to out stream
4427 $options, # options hash
4432 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4434 $mask = 1 if not $options->{ "no_mask" };
4436 $tmp_file = "$BP_TMP/write_2bit.fna";
4437 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4439 $fh_out = write_stream( $options->{ "data_out" } );
4441 while ( $record = get_record( $in ) )
4443 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4444 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4447 put_record( $record, $out ) if not $options->{ "no_stream" };
4452 $fh_in = Maasha::Common::read_open( $tmp_file );
4454 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4463 sub script_write_solid
4465 # Martin A. Hansen, April 2008.
4467 # Write di-base encoded Solid sequence from entries in stream.
4469 my ( $in, # handle to in stream
4470 $out, # handle to out stream
4471 $options, # options hash
4476 my ( $record, $fh, $entry );
4478 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4480 while ( $record = get_record( $in ) )
4482 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4484 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4486 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4489 put_record( $record, $out ) if not $options->{ "no_stream" };
4496 sub script_write_ucsc_config
4498 # Martin A. Hansen, November 2008.
4500 # Write UCSC Genome Broser configuration (.ra file type) from
4501 # records in the stream.
4503 my ( $in, # handle to in stream
4504 $out, # handle to out stream
4505 $options, # options hash
4510 my ( $record, $fh );
4512 $fh = write_stream( $options->{ "data_out" } );
4514 while ( $record = get_record( $in ) )
4516 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4518 put_record( $record, $out ) if not $options->{ "no_stream" };
4525 sub script_plot_seqlogo
4527 # Martin A. Hansen, August 2007.
4529 # Calculates and writes a sequence logo for alignments.
4531 my ( $in, # handle to in stream
4532 $out, # handle to out stream
4533 $options, # options hash
4538 my ( $record, @entries, $logo, $fh );
4540 while ( $record = get_record( $in ) )
4542 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4543 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4546 put_record( $record, $out ) if not $options->{ "no_stream" };
4549 $logo = Maasha::Plot::seq_logo( \@entries );
4551 $fh = write_stream( $options->{ "data_out" } );
4559 sub script_plot_phastcons_profiles
4561 # Martin A. Hansen, January 2008.
4563 # Plots PhastCons profiles.
4565 my ( $in, # handle to in stream
4566 $out, # handle to out stream
4567 $options, # options hash
4572 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4574 $options->{ "title" } ||= "PhastCons Profiles";
4576 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4577 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4579 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4580 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4582 while ( $record = get_record( $in ) )
4584 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4586 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4587 $record->{ "CHR_BEG" },
4588 $record->{ "CHR_END" },
4589 $options->{ "flank" } );
4591 push @{ $AoA }, [ @{ $scores } ];
4594 put_record( $record, $out ) if not $options->{ "no_stream" };
4597 Maasha::UCSC::phastcons_normalize( $AoA );
4599 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4600 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4602 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4604 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4606 $fh = write_stream( $options->{ "data_out" } );
4608 print $fh "$_\n" foreach @{ $plot };
4614 sub script_analyze_bed
4616 # Martin A. Hansen, March 2008.
4618 # Analyze BED entries in stream.
4620 my ( $in, # handle to in stream
4621 $out, # handle to out stream
4622 $options, # options hash
4629 while ( $record = get_record( $in ) )
4631 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4633 put_record( $record, $out );
4638 sub script_analyze_vals
4640 # Martin A. Hansen, August 2007.
4642 # Analyze values for given keys in stream.
4644 my ( $in, # handle to in stream
4645 $out, # handle to out stream
4646 $options, # options hash
4651 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4653 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4655 while ( $record = get_record( $in ) )
4657 foreach $key ( keys %{ $record } )
4659 next if $options->{ "keys" } and not exists $key_hash{ $key };
4661 $analysis->{ $key }->{ "COUNT" }++;
4663 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4665 $analysis->{ $key }->{ "TYPE" } = "num";
4666 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4667 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4668 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4672 $len = length $record->{ $key };
4674 $analysis->{ $key }->{ "TYPE" } = "alph";
4675 $analysis->{ $key }->{ "SUM" } += $len;
4676 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4677 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4681 put_record( $record, $out ) if not $options->{ "no_stream" };
4684 foreach $key ( keys %{ $analysis } )
4686 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4687 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4690 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4700 if ( $options->{ "keys" } ) {
4701 @keys = @{ $options->{ "keys" } };
4703 @keys = keys %{ $analysis };
4706 foreach $key ( @keys )
4708 $keys .= sprintf "% 15s", $key;
4709 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4710 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4711 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4712 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4713 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4714 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4717 print $out "$keys\n";
4718 print $out "$types\n";
4719 print $out "$counts\n";
4720 print $out "$mins\n";
4721 print $out "$maxs\n";
4722 print $out "$sums\n";
4723 print $out "$means\n";
4727 sub script_head_records
4729 # Martin A. Hansen, August 2007.
4731 # Display the first sequences in stream.
4733 my ( $in, # handle to in stream
4734 $out, # handle to out stream
4735 $options, # options hash
4740 my ( $record, $count );
4742 $options->{ "num" } ||= 10;
4746 while ( $record = get_record( $in ) )
4750 put_record( $record, $out );
4752 last if $count == $options->{ "num" };
4757 sub script_remove_keys
4759 # Martin A. Hansen, August 2007.
4761 # Remove keys from stream.
4763 my ( $in, # handle to in stream
4764 $out, # handle to out stream
4765 $options, # options hash
4770 my ( $record, $new_record );
4772 while ( $record = get_record( $in ) )
4774 if ( $options->{ "keys" } )
4776 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4778 elsif ( $options->{ "save_keys" } )
4780 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4782 $record = $new_record;
4785 put_record( $record, $out ) if keys %{ $record };
4790 sub script_remove_adaptor
4792 # Martin A. Hansen, August 2008.
4794 # Find and remove adaptor from sequences in the stream.
4796 my ( $in, # handle to in stream
4797 $out, # handle to out stream
4798 $options, # options hash
4803 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4805 $options->{ "remove" } ||= "after";
4807 $max_mismatch = $options->{ "mismatches" } || 0;
4808 $offset = $options->{ "offset" };
4810 if ( not defined $offset ) {
4816 $adaptor = uc $options->{ "adaptor" };
4817 $adaptor_len = length $adaptor;
4819 while ( $record = get_record( $in ) )
4821 if ( $record->{ "SEQ" } )
4823 $seq = uc $record->{ "SEQ" };
4824 $seq_len = length $seq;
4826 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4828 $record->{ "ADAPTOR_POS" } = $pos;
4830 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
4832 if ( $options->{ "remove" } eq "after" )
4834 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4835 $record->{ "SEQ_LEN" } = $pos;
4839 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
4840 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
4844 put_record( $record, $out );
4848 put_record( $record, $out );
4854 sub script_remove_mysql_tables
4856 # Martin A. Hansen, November 2008.
4858 # Remove MySQL tables from values in stream.
4860 my ( $in, # handle to in stream
4861 $out, # handle to out stream
4862 $options, # options hash
4867 my ( $record, %table_hash, $dbh, $table );
4869 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
4870 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
4872 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
4874 while ( $record = get_record( $in ) )
4876 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4878 put_record( $record, $out ) if not $options->{ 'no_stream' };
4881 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4883 foreach $table ( sort keys %table_hash )
4885 if ( Maasha::SQL::table_exists( $dbh, $table ) )
4887 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4888 Maasha::SQL::delete_table( $dbh, $table );
4889 print STDERR "done.\n" if $options->{ 'verbose' };
4893 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
4897 Maasha::SQL::disconnect( $dbh );
4901 sub script_remove_ucsc_tracks
4903 # Martin A. Hansen, November 2008.
4905 # Remove track from MySQL tables and config file.
4907 my ( $in, # handle to in stream
4908 $out, # handle to out stream
4909 $options, # options hash
4914 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
4916 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
4917 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
4918 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
4920 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
4922 while ( $record = get_record( $in ) )
4924 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4926 put_record( $record, $out ) if not $options->{ 'no_stream' };
4929 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
4931 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
4932 push @tracks, $track;
4937 foreach $track ( @tracks )
4939 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
4940 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
4942 push @new_tracks, $track;
4946 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
4948 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
4950 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
4954 # ---- locate track in database ----
4956 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4958 foreach $track ( sort keys %track_hash )
4960 if ( Maasha::SQL::table_exists( $dbh, $track ) )
4962 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4963 Maasha::SQL::delete_table( $dbh, $track );
4964 print STDERR "done.\n" if $options->{ 'verbose' };
4968 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
4972 Maasha::SQL::disconnect( $dbh );
4974 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
4978 sub script_rename_keys
4980 # Martin A. Hansen, August 2007.
4982 # Rename keys in stream.
4984 my ( $in, # handle to in stream
4985 $out, # handle to out stream
4986 $options, # options hash
4993 while ( $record = get_record( $in ) )
4995 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4997 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4999 delete $record->{ $options->{ "keys" }->[ 0 ] };
5002 put_record( $record, $out );
5007 sub script_uniq_vals
5009 # Martin A. Hansen, August 2007.
5011 # Find unique values in stream.
5013 my ( $in, # handle to in stream
5014 $out, # handle to out stream
5015 $options, # options hash
5020 my ( %hash, $record );
5022 while ( $record = get_record( $in ) )
5024 if ( $record->{ $options->{ "key" } } )
5026 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
5028 put_record( $record, $out );
5030 $hash{ $record->{ $options->{ "key" } } } = 1;
5032 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
5034 put_record( $record, $out );
5038 $hash{ $record->{ $options->{ "key" } } } = 1;
5043 put_record( $record, $out );
5049 sub script_merge_vals
5051 # Martin A. Hansen, August 2007.
5053 # Rename keys in stream.
5055 my ( $in, # handle to in stream
5056 $out, # handle to out stream
5057 $options, # options hash
5062 my ( $record, @join, $i );
5064 $options->{ "delimit" } ||= '_';
5066 while ( $record = get_record( $in ) )
5068 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5070 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5072 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5073 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5076 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5079 put_record( $record, $out );
5084 sub script_merge_records
5086 # Martin A. Hansen, July 2008.
5088 # Merges records in the stream based on identical values of two given keys.
5090 my ( $in, # handle to in stream
5091 $out, # handle to out stream
5092 $options, # options hash
5097 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5098 $num1, $num2, $num, $cmp, $i );
5100 $merge = $options->{ "merge" } || "AandB";
5102 $file1 = "$BP_TMP/merge_records1.tmp";
5103 $file2 = "$BP_TMP/merge_records2.tmp";
5105 $fh1 = Maasha::Common::write_open( $file1 );
5106 $fh2 = Maasha::Common::write_open( $file2 );
5108 $key1 = $options->{ "keys" }->[ 0 ];
5109 $key2 = $options->{ "keys" }->[ 1 ];
5111 $num = $key2 =~ s/n$//;
5115 while ( $record = get_record( $in ) )
5117 if ( exists $record->{ $key1 } )
5120 @vals1 = $record->{ $key1 };
5122 delete $record->{ $key1 };
5124 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5126 print $fh1 join( "\t", @vals1 ), "\n";
5130 elsif ( exists $record->{ $key2 } )
5133 @vals2 = $record->{ $key2 };
5135 delete $record->{ $key2 };
5137 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5139 print $fh2 join( "\t", @vals2 ), "\n";
5150 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5151 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5155 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5156 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5159 $fh1 = Maasha::Common::read_open( $file1 );
5160 $fh2 = Maasha::Common::read_open( $file2 );
5162 @vals1 = Maasha::Common::get_fields( $fh1 );
5163 @vals2 = Maasha::Common::get_fields( $fh2 );
5165 while ( $num1 > 0 and $num2 > 0 )
5170 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5172 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5177 if ( $merge =~ /^(AorB|AnotB)$/ )
5179 for ( $i = 0; $i < @keys1; $i++ ) {
5180 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5183 put_record( $record, $out );
5186 @vals1 = Maasha::Common::get_fields( $fh1 );
5191 if ( $merge =~ /^(BorA|BnotA)$/ )
5193 for ( $i = 0; $i < @keys2; $i++ ) {
5194 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5197 put_record( $record, $out );
5200 @vals2 = Maasha::Common::get_fields( $fh2 );
5205 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5207 for ( $i = 0; $i < @keys1; $i++ ) {
5208 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5211 for ( $i = 1; $i < @keys2; $i++ ) {
5212 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5215 put_record( $record, $out );
5218 @vals1 = Maasha::Common::get_fields( $fh1 );
5219 @vals2 = Maasha::Common::get_fields( $fh2 );
5231 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5235 for ( $i = 0; $i < @keys1; $i++ ) {
5236 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5239 put_record( $record, $out );
5242 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5246 for ( $i = 0; $i < @keys2; $i++ ) {
5247 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5250 put_record( $record, $out );
5257 # Martin A. Hansen, August 2007.
5259 # Grab for records in stream.
5261 my ( $in, # handle to in stream
5262 $out, # handle to out stream
5263 $options, # options hash
5268 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5270 $keys = $options->{ 'keys' };
5271 $vals_only = $options->{ 'vals_only' };
5272 $keys_only = $options->{ 'keys_only' };
5273 $invert = $options->{ 'invert' };
5275 if ( $options->{ 'patterns' } )
5277 $patterns = [ split ",", $options->{ 'patterns' } ];
5279 elsif ( -f $options->{ 'patterns_in' } )
5281 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5283 elsif ( $options->{ 'regex' } )
5285 if ( $options->{ 'case_insensitive' } ) {
5286 $regex = qr/$options->{ 'regex' }/i;
5288 $regex = qr/$options->{ 'regex' }/;
5291 elsif ( -f $options->{ 'exact_in' } )
5293 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5295 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5299 elsif ( $options->{ 'eval' } )
5301 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5309 while ( $record = get_record( $in ) )
5313 if ( %lookup_hash ) {
5314 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5315 } elsif ( $patterns ) {
5316 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5317 } elsif ( $regex ) {
5318 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5320 $found = grab_eval( $key, $op, $val, $record );
5323 if ( $found and not $invert ) {
5324 put_record( $record, $out );
5325 } elsif ( not $found and $invert ) {
5326 put_record( $record, $out );
5334 # Martin A. Hansen, August 2007.
5336 # Evaluate extression for records in stream.
5338 my ( $in, # handle to in stream
5339 $out, # handle to out stream
5340 $options, # options hash
5345 my ( $record, $eval_key, @keys, $eval_val );
5347 while ( $record = get_record( $in ) )
5349 if ( $options->{ "eval" } )
5351 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
5358 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5360 @keys = grep { exists $record->{ $_ } } @keys;
5363 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5365 $record->{ $eval_key } = eval "$eval_val";
5366 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
5370 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
5374 put_record( $record, $out );
5381 # Martin A. Hansen, June 2008.
5385 my ( $in, # handle to in stream
5386 $out, # handle to out stream
5387 $options, # options hash
5392 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5394 while ( $record = get_record( $in ) )
5398 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5400 push @rows, $record->{ $key };
5404 push @matrix, [ @rows ];
5409 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5411 foreach $row ( @matrix )
5413 for ( $i = 0; $i < @{ $row }; $i++ ) {
5414 $record->{ "V$i" } = $row->[ $i ];
5417 put_record( $record, $out );
5422 sub script_add_ident
5424 # Martin A. Hansen, May 2008.
5426 # Add a unique identifier to each record in stream.
5428 my ( $in, # handle to in stream
5429 $out, # handle to out stream
5430 $options, # options hash
5435 my ( $record, $key, $prefix, $i );
5437 $key = $options->{ "key" } || "ID";
5438 $prefix = $options->{ "prefix" } || "ID";
5442 while ( $record = get_record( $in ) )
5444 $record->{ $key } = sprintf( "$prefix%08d", $i );
5446 put_record( $record, $out );
5453 sub script_count_records
5455 # Martin A. Hansen, August 2007.
5457 # Count records in stream.
5459 my ( $in, # handle to in stream
5460 $out, # handle to out stream
5461 $options, # options hash
5466 my ( $record, $count, $result, $fh, $line );
5470 if ( $options->{ "no_stream" } )
5472 while ( $line = <$in> )
5476 $count++ if $line eq "---";
5481 while ( $record = get_record( $in ) )
5483 put_record( $record, $out );
5489 $result = { "RECORDS_COUNT" => $count };
5491 $fh = write_stream( $options->{ "data_out" } );
5493 put_record( $result, $fh );
5499 sub script_random_records
5501 # Martin A. Hansen, August 2007.
5503 # Pick a number or random records from stream.
5505 my ( $in, # handle to in stream
5506 $out, # handle to out stream
5507 $options, # options hash
5512 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5514 $options->{ "num" } ||= 10;
5516 $tmp_file = "$BP_TMP/random_records.tmp";
5518 $fh_out = Maasha::Common::write_open( $tmp_file );
5522 while ( $record = get_record( $in ) )
5524 put_record( $record, $fh_out );
5534 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5536 while ( $i < $options->{ "num" } )
5538 $rand = int( rand( $count ) );
5540 if ( not exists $rand_hash{ $rand } )
5542 $rand_hash{ $rand } = 1;
5544 $max = $rand if $rand > $max;
5550 $fh_in = Maasha::Common::read_open( $tmp_file );
5554 while ( $record = get_record( $fh_in ) )
5556 put_record( $record, $out ) if exists $rand_hash{ $count };
5558 last if $count == $max;
5569 sub script_sort_records
5571 # Martin A. Hansen, August 2007.
5573 # Sort to sort records according to keys.
5575 my ( $in, # handle to in stream
5576 $out, # handle to out stream
5577 $options, # options hash
5582 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5584 foreach $key ( @{ $options->{ "keys" } } )
5586 if ( $key =~ s/n$// ) {
5587 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5589 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5593 $sort_str = join " or ", @sort_cmd;
5594 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5596 while ( $record = get_record( $in ) ) {
5597 push @records, $record;
5600 @records = sort $sort_sub @records;
5602 if ( $options->{ "reverse" } )
5604 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5605 put_record( $records[ $i ], $out );
5610 for ( $i = 0; $i < scalar @records; $i++ ) {
5611 put_record( $records[ $i ], $out );
5617 sub script_count_vals
5619 # Martin A. Hansen, August 2007.
5621 # Count records in stream.
5623 my ( $in, # handle to in stream
5624 $out, # handle to out stream
5625 $options, # options hash
5630 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5632 $tmp_file = "$BP_TMP/count_cache.tmp";
5634 $fh_out = Maasha::Common::write_open( $tmp_file );
5639 while ( $record = get_record( $in ) )
5641 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5643 push @records, $record;
5645 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5647 map { put_record( $_, $fh_out ) } @records;
5654 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5665 $fh_in = Maasha::Common::read_open( $tmp_file );
5667 while ( $record = get_record( $fh_in ) )
5669 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5671 put_record( $record, $out );
5673 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5681 foreach $record ( @records )
5683 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5685 put_record( $record, $out );
5692 sub script_plot_histogram
5694 # Martin A. Hansen, September 2007.
5696 # Plot a simple histogram for a given key using GNU plot.
5698 my ( $in, # handle to in stream
5699 $out, # handle to out stream
5700 $options, # options hash
5705 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5707 $options->{ "title" } ||= "Histogram";
5708 $options->{ "sort" } ||= "num";
5710 while ( $record = get_record( $in ) )
5712 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5714 put_record( $record, $out ) if not $options->{ "no_stream" };
5717 if ( $options->{ "sort" } eq "num" ) {
5718 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5720 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5723 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5725 $fh = write_stream( $options->{ "data_out" } );
5727 print $fh "$_\n" foreach @{ $result };
5733 sub script_plot_lendist
5735 # Martin A. Hansen, August 2007.
5737 # Plot length distribution using GNU plot.
5739 my ( $in, # handle to in stream
5740 $out, # handle to out stream
5741 $options, # options hash
5746 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5748 $options->{ "title" } ||= "Length Distribution";
5750 while ( $record = get_record( $in ) )
5752 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5754 put_record( $record, $out ) if not $options->{ "no_stream" };
5757 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5759 for ( $i = 0; $i < $max; $i++ ) {
5760 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5763 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5765 $fh = write_stream( $options->{ "data_out" } );
5767 print $fh "$_\n" foreach @{ $result };
5773 sub script_plot_chrdist
5775 # Martin A. Hansen, August 2007.
5777 # Plot chromosome distribution using GNU plot.
5779 my ( $in, # handle to in stream
5780 $out, # handle to out stream
5781 $options, # options hash
5786 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5788 $options->{ "title" } ||= "Chromosome Distribution";
5790 while ( $record = get_record( $in ) )
5792 if ( $record->{ "CHR" } ) { # generic
5793 $data_hash{ $record->{ "CHR" } }++;
5794 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5795 $data_hash{ $record->{ "S_ID" } }++;
5796 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5797 $data_hash{ $record->{ "S_ID" } }++;
5798 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5799 $data_hash{ $record->{ "S_ID" } }++;
5802 put_record( $record, $out ) if not $options->{ "no_stream" };
5805 foreach $elem ( keys %data_hash )
5809 $sort_key =~ s/chr//i;
5811 $sort_key =~ s/^X(.*)/99$1/;
5812 $sort_key =~ s/^Y(.*)/99$1/;
5813 $sort_key =~ s/^Z(.*)/999$1/;
5814 $sort_key =~ s/^M(.*)/9999$1/;
5815 $sort_key =~ s/^U(.*)/99999$1/;
5817 $count = $sort_key =~ tr/_//;
5819 $sort_key =~ s/_.*/"999999" x $count/ex;
5821 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5824 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5826 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5828 $fh = write_stream( $options->{ "data_out" } );
5830 print $fh "$_\n" foreach @{ $result };
5836 sub script_plot_karyogram
5838 # Martin A. Hansen, August 2007.
5840 # Plot hits on karyogram.
5842 my ( $in, # handle to in stream
5843 $out, # handle to out stream
5844 $options, # options hash
5849 my ( %options, $record, @data, $fh, $result, %data_hash );
5851 $options->{ "genome" } ||= "human";
5852 $options->{ "feat_color" } ||= "black";
5854 while ( $record = get_record( $in ) )
5856 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5858 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5861 put_record( $record, $out ) if not $options->{ "no_stream" };
5864 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5866 $fh = write_stream( $options->{ "data_out" } );
5874 sub script_plot_matches
5876 # Martin A. Hansen, August 2007.
5878 # Plot matches in 2D generating a dotplot.
5880 my ( $in, # handle to in stream
5881 $out, # handle to out stream
5882 $options, # options hash
5887 my ( $record, @data, $fh, $result, %data_hash );
5889 $options->{ "direction" } ||= "both";
5891 while ( $record = get_record( $in ) )
5893 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5894 push @data, $record;
5897 put_record( $record, $out ) if not $options->{ "no_stream" };
5900 $options->{ "title" } ||= "plot_matches";
5901 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5902 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5904 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5906 $fh = write_stream( $options->{ "data_out" } );
5908 print $fh "$_\n" foreach @{ $result };
5914 sub script_length_vals
5916 # Martin A. Hansen, August 2007.
5918 # Determine the length of the value for given keys.
5920 my ( $in, # handle to in stream
5921 $out, # handle to out stream
5922 $options, # options hash
5927 my ( $record, $key );
5929 while ( $record = get_record( $in ) )
5931 foreach $key ( @{ $options->{ "keys" } } )
5933 if ( $record->{ $key } ) {
5934 $record->{ $key . "_LEN" } = length $record->{ $key };
5938 put_record( $record, $out );
5945 # Martin A. Hansen, August 2007.
5947 # Calculates the sums for values of given keys.
5949 my ( $in, # handle to in stream
5950 $out, # handle to out stream
5951 $options, # options hash
5956 my ( $record, $key, %sum_hash, $fh );
5958 while ( $record = get_record( $in ) )
5960 foreach $key ( @{ $options->{ "keys" } } )
5962 if ( $record->{ $key } ) {
5963 $sum_hash{ $key } += $record->{ $key };
5967 put_record( $record, $out ) if not $options->{ "no_stream" };
5970 $fh = write_stream( $options->{ "data_out" } );
5972 foreach $key ( @{ $options->{ "keys" } } ) {
5973 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5980 sub script_mean_vals
5982 # Martin A. Hansen, August 2007.
5984 # Calculate the mean of values of given keys.
5986 my ( $in, # handle to in stream
5987 $out, # handle to out stream
5988 $options, # options hash
5993 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5995 while ( $record = get_record( $in ) )
5997 foreach $key ( @{ $options->{ "keys" } } )
5999 if ( $record->{ $key } )
6001 $sum_hash{ $key } += $record->{ $key };
6002 $count_hash{ $key }++;
6006 put_record( $record, $out ) if not $options->{ "no_stream" };
6009 $fh = write_stream( $options->{ "data_out" } );
6011 foreach $key ( @{ $options->{ "keys" } } )
6013 if ( $count_hash{ $key } ) {
6014 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
6019 put_record( { $key . "_MEAN" => $mean } , $fh );
6026 sub script_median_vals
6028 # Martin A. Hansen, March 2008.
6030 # Calculate the median values of given keys.
6032 my ( $in, # handle to in stream
6033 $out, # handle to out stream
6034 $options, # options hash
6039 my ( $record, $key, %median_hash, $median, $fh );
6041 while ( $record = get_record( $in ) )
6043 foreach $key ( @{ $options->{ "keys" } } ) {
6044 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6047 put_record( $record, $out ) if not $options->{ "no_stream" };
6050 $fh = write_stream( $options->{ "data_out" } );
6052 foreach $key ( @{ $options->{ "keys" } } )
6054 if ( $median_hash{ $key } ) {
6055 $median = Maasha::Calc::median( $median_hash{ $key } );
6060 put_record( { $key . "_MEDIAN" => $median } , $fh );
6069 # Martin A. Hansen, February 2008.
6071 # Determine the maximum values of given keys.
6073 my ( $in, # handle to in stream
6074 $out, # handle to out stream
6075 $options, # options hash
6080 my ( $record, $key, $fh, %max_hash, $max_record );
6082 while ( $record = get_record( $in ) )
6084 foreach $key ( @{ $options->{ "keys" } } )
6086 if ( $record->{ $key } )
6088 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6092 put_record( $record, $out ) if not $options->{ "no_stream" };
6095 $fh = write_stream( $options->{ "data_out" } );
6097 foreach $key ( @{ $options->{ "keys" } } )
6099 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6102 put_record( $max_record, $fh );
6110 # Martin A. Hansen, February 2008.
6112 # Determine the minimum values of given keys.
6114 my ( $in, # handle to in stream
6115 $out, # handle to out stream
6116 $options, # options hash
6121 my ( $record, $key, $fh, %min_hash, $min_record );
6123 while ( $record = get_record( $in ) )
6125 foreach $key ( @{ $options->{ "keys" } } )
6127 if ( defined $record->{ $key } )
6129 if ( exists $min_hash{ $key } ) {
6130 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6132 $min_hash{ $key } = $record->{ $key };
6137 put_record( $record, $out ) if not $options->{ "no_stream" };
6140 $fh = write_stream( $options->{ "data_out" } );
6142 foreach $key ( @{ $options->{ "keys" } } )
6144 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6147 put_record( $min_record, $fh );
6153 sub script_upload_to_ucsc
6155 # Martin A. Hansen, August 2007.
6157 # Calculate the mean of values of given keys.
6159 # This routine has developed into the most ugly hack. Do something!
6161 my ( $in, # handle to in stream
6162 $out, # handle to out stream
6163 $options, # options hash
6168 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
6170 $options->{ "short_label" } ||= $options->{ 'table' };
6171 $options->{ "long_label" } ||= $options->{ 'table' };
6172 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6173 $options->{ "priority" } ||= 1;
6174 $options->{ "visibility" } ||= "pack";
6175 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6176 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6178 $file = "$BP_TMP/ucsc_upload.tmp";
6183 $fh_out = Maasha::Common::write_open( $file );
6185 while ( $record = get_record( $in ) )
6187 put_record( $record, $out ) if not $options->{ "no_stream" };
6189 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6193 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
6194 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
6197 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6201 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6202 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6206 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6208 # chrom chromStart chromEnd name score strand size secStr conf
6212 print $fh_out join ( "\t",
6214 $record->{ "CHR_BEG" },
6215 $record->{ "CHR_END" } + 1,
6216 $record->{ "Q_ID" },
6217 $record->{ "SCORE" },
6218 $record->{ "STRAND" },
6219 $record->{ "SIZE" },
6220 $record->{ "SEC_STRUCT" },
6221 $record->{ "CONF" },
6224 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6227 $columns = $record->{ "BED_COLS" };
6229 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6230 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6233 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6238 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6239 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6242 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6247 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6249 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6250 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6253 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6258 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6259 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6263 if ( $i == $options->{ "chunk_size" } )
6267 if ( $format eq "BED" ) {
6268 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6269 } elsif ( $format eq "PSL" ) {
6270 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6279 $fh_out = Maasha::Common::write_open( $file );
6287 if ( exists $options->{ "database" } and $options->{ "table" } )
6289 if ( $format eq "BED" )
6291 $type = "bed $columns";
6293 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6295 elsif ( $format eq "BED_SS" )
6297 $type = "type bed 6 +";
6299 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6301 elsif ( $format eq "PSL" )
6305 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6307 elsif ( $format eq "WIGGLE" )
6309 $options->{ "visibility" } = "full";
6311 $wig_file = "$options->{ 'table' }.wig";
6312 $wib_file = "$options->{ 'table' }.wib";
6314 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6316 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6318 if ( $options->{ 'verbose' } ) {
6319 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6321 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6324 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6332 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6337 Maasha::UCSC::ucsc_update_config( $options, $type );
6342 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6347 # Martin A. Hansen, November 2009.
6349 # Uses keys from a lookup hash to search records. Optionally, a list of
6350 # keys can be given so the lookup is limited to these, also, flags
6351 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6352 # succeeded, else 0.
6354 my ( $lookup_hash, # hashref with patterns
6356 $keys, # list of keys - OPTIONAL
6357 $vals_only, # only vals flag - OPTIONAL
6358 $keys_only, # only keys flag - OPTIONAL
6365 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6369 if ( not $vals_only ) {
6370 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6373 if ( not $keys_only ) {
6374 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6384 # Martin A. Hansen, November 2009.
6386 # Uses patterns to match records containing the pattern as a substring.
6387 # Returns 1 if the record is matched, else 0.
6389 my ( $patterns, # list of patterns
6391 $keys, # list of keys - OPTIONAL
6392 $vals_only, # only vals flag - OPTIONAL
6393 $keys_only, # only keys flag - OPTIONAL
6400 foreach $pattern ( @{ $patterns } )
6404 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6408 if ( not $vals_only ) {
6409 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6412 if ( not $keys_only ) {
6413 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6424 # Martin A. Hansen, November 2009.
6426 # Uses regex to match records.
6427 # Returns 1 if the record is matched, else 0.
6429 my ( $regex, # regex to match
6431 $keys, # list of keys - OPTIONAL
6432 $vals_only, # only vals flag - OPTIONAL
6433 $keys_only, # only keys flag - OPTIONAL
6440 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6444 if ( not $vals_only ) {
6445 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6448 if ( not $keys_only ) {
6449 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6459 # Martin A. Hansen, November 2009.
6461 # Test if the value of a given record key evaluates according
6462 # to a given operator. Returns 1 if eval is OK, else 0.
6464 my ( $key, # record key
6472 if ( defined $record->{ $key } )
6474 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6475 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6476 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6477 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6478 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6479 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6480 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6481 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6490 # Martin A. Hansen, July 2007.
6492 # Opens a stream to STDIN or a file,
6494 my ( $path, # path - OPTIONAL
6497 # Returns filehandle.
6501 if ( not -t STDIN ) {
6502 $fh = Maasha::Common::read_stdin();
6503 } elsif ( not $path ) {
6504 # Maasha::Common::error( qq(no data stream) );
6506 $fh = Maasha::Common::read_open( $path );
6509 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6517 # Martin A. Hansen, August 2007.
6519 # Opens a stream to STDOUT or a file.
6521 my ( $path, # path - OPTIONAL
6522 $gzip, # compress data - OPTIONAL
6525 # Returns filehandle.
6530 $fh = Maasha::Common::write_open( $path, $gzip );
6532 $fh = Maasha::Common::write_stdout();
6541 # Martin A. Hansen, July 2007.
6543 # Reads one record at a time and converts that record
6544 # to a Perl data structure (a hash) which is returned.
6546 my ( $fh, # handle to stream
6551 my ( $block, @lines, $line, $key, $value, %record );
6553 local $/ = "\n---\n";
6559 return if not defined $block;
6561 @lines = split "\n", $block;
6563 foreach $line ( @lines )
6565 ( $key, $value ) = split ": ", $line, 2;
6567 $record{ $key } = $value;
6570 return wantarray ? %record : \%record;
6576 # Martin A. Hansen, July 2007.
6578 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6580 my ( $data, # data structure
6581 $fh, # file handle - OPTIONAL
6586 if ( scalar keys %{ $data } )
6590 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6595 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6606 # Martin A. Hansen, November 2007.
6608 # Extracts files from an explicit GetOpt::Long argument
6609 # allowing for the use of glob. E.g.
6610 # --data_in=test.fna
6611 # --data_in=test.fna,test2.fna
6613 # --data_in=test.fna,/dir/*.fna
6615 my ( $option, # option from GetOpt::Long
6620 my ( $elem, @files );
6622 foreach $elem ( split ",", $option )
6626 } elsif ( $elem =~ /\*/ ) {
6627 push @files, glob( $elem );
6631 return wantarray ? @files : \@files;
6637 # Martin A. Hansen, April 2008.
6639 # Removes temporary directory and exits gracefully.
6640 # This subroutine is meant to be run always as the last
6641 # thing even if a script is dies or is interrupted
6644 my ( $sig, # signal from the %SIG
6647 # print STDERR "signal->$sig<-\n";
6655 if ( $sig =~ /MAASHA_ERROR/ ) {
6656 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6657 } elsif ( $sig eq "INT" ) {
6658 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6659 } elsif ( $sig eq "TERM" ) {
6660 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6662 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6674 # Martin A. Hansen, July 2008.
6676 # Cleans out any unused temporary files and directories in BP_TMP.
6680 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6682 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6684 $curr_pid = Maasha::Common::get_processid();
6686 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6688 foreach $dir ( @dirs )
6690 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6696 next if $user eq "m.hansen";
6698 if ( $user eq Maasha::Common::get_user() )
6700 if ( not Maasha::Common::process_running( $pid ) )
6702 # print STDERR "Removing stale dir: $dir\n";
6703 Maasha::Common::dir_remove( $dir );
6705 elsif ( $pid == $curr_pid )
6707 # print STDERR "Removing current dir: $dir\n";
6708 Maasha::Common::dir_remove( $dir );
6722 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<