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" )
516 elsif ( $script eq "get_genome_align" )
527 elsif ( $script eq "get_genome_phastcons" )
538 elsif ( $script eq "split_seq" )
545 elsif ( $script eq "split_bed" )
552 elsif ( $script eq "tile_seq" )
559 elsif ( $script eq "invert_align" )
565 elsif ( $script eq "patscan_seq" )
576 elsif ( $script eq "create_blast_db" )
583 elsif ( $script eq "blast_seq" )
595 elsif ( $script eq "blat_seq" )
607 elsif ( $script eq "soap_seq" )
618 elsif ( $script eq "match_seq" )
625 elsif ( $script eq "create_vmatch_index" )
633 elsif ( $script eq "vmatch_seq" )
644 elsif ( $script eq "write_fasta" )
653 elsif ( $script eq "write_align" )
663 elsif ( $script eq "write_blast" )
672 elsif ( $script eq "write_tab" )
684 elsif ( $script eq "write_bed" )
694 elsif ( $script eq "write_psl" )
702 elsif ( $script eq "write_fixedstep" )
710 elsif ( $script eq "write_2bit" )
718 elsif ( $script eq "write_solid" )
727 elsif ( $script eq "write_ucsc_config" )
734 elsif ( $script eq "plot_seqlogo" )
741 elsif ( $script eq "plot_phastcons_profiles" )
756 elsif ( $script eq "analyze_vals" )
763 elsif ( $script eq "head_records" )
769 elsif ( $script eq "remove_keys" )
776 elsif ( $script eq "remove_adaptor" )
785 elsif ( $script eq "remove_mysql_tables" )
796 elsif ( $script eq "remove_ucsc_tracks" )
808 elsif ( $script eq "rename_keys" )
814 elsif ( $script eq "uniq_vals" )
821 elsif ( $script eq "merge_vals" )
828 elsif ( $script eq "merge_records" )
835 elsif ( $script eq "grab" )
850 elsif ( $script eq "compute" )
856 elsif ( $script eq "add_ident" )
863 elsif ( $script eq "count_records" )
870 elsif ( $script eq "random_records" )
876 elsif ( $script eq "sort_records" )
883 elsif ( $script eq "count_vals" )
889 elsif ( $script eq "plot_histogram" )
902 elsif ( $script eq "plot_lendist" )
914 elsif ( $script eq "plot_chrdist" )
925 elsif ( $script eq "plot_karyogram" )
934 elsif ( $script eq "plot_matches" )
946 elsif ( $script eq "length_vals" )
952 elsif ( $script eq "sum_vals" )
960 elsif ( $script eq "mean_vals" )
968 elsif ( $script eq "median_vals" )
976 elsif ( $script eq "max_vals" )
984 elsif ( $script eq "min_vals" )
992 elsif ( $script eq "upload_to_ucsc" )
1016 # print STDERR Dumper( \@options );
1023 # print STDERR Dumper( \%options );
1025 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
1026 return wantarray ? %options : \%options;
1029 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
1030 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
1031 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
1032 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
1033 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
1034 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
1035 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
1036 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
1037 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
1038 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
1039 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
1041 # ---- check arguments ----
1043 if ( $options{ 'data_in' } )
1045 $options{ "files" } = getopt_files( $options{ 'data_in' } );
1047 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
1050 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
1052 # print STDERR Dumper( \%options );
1054 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
1056 foreach $opt ( keys %options )
1058 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
1060 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1062 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1064 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1066 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1068 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1070 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1072 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1074 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1076 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1078 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1080 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1082 elsif ( $opt eq "genome" and $script ne "format_genome" )
1084 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1085 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1087 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1088 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1091 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1093 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1095 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1097 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1099 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1101 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1103 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1105 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1107 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
1109 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
1113 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
1114 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1115 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1116 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1117 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1118 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1119 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1120 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1121 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1122 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" };
1123 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1124 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" };
1126 if ( $script eq "upload_to_ucsc" )
1128 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1129 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1132 return wantarray ? %options : \%options;
1136 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1139 sub script_print_usage
1141 # Martin A. Hansen, January 2008.
1143 # Retrieves usage information from file and
1144 # prints this nicely formatted.
1146 my ( $in, # handle to in stream
1147 $out, # handle to out stream
1148 $options, # options hash
1153 my ( $file, $wiki, $lines );
1155 if ( $options->{ 'data_in' } ) {
1156 $file = $options->{ 'data_in' };
1158 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1161 $wiki = Maasha::Gwiki::gwiki_read( $file );
1163 if ( not $options->{ "help" } ) {
1164 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1167 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1169 print STDERR "$_\n" foreach @{ $lines };
1175 sub script_list_biopieces
1177 # Martin A. Hansen, January 2008.
1179 # Prints the synopsis from the usage for each of the biopieces.
1181 my ( $in, # handle to in stream
1182 $out, # handle to out stream
1183 $options, # options hash
1188 my ( @files, $file, $wiki, $program, $synopsis );
1190 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1192 foreach $file ( sort @files )
1194 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1198 $wiki = Maasha::Gwiki::gwiki_read( $file );
1200 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1201 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1203 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1204 $synopsis =~ s/!(\w)/$1/g;
1206 printf( "%-30s%s\n", $program, $synopsis );
1214 sub script_list_genomes
1216 # Martin A. Hansen, January 2008.
1218 # Prints the synopsis from the usage for each of the biopieces.
1220 my ( $in, # handle to in stream
1221 $out, # handle to out stream
1222 $options, # options hash
1227 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1229 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1231 foreach $genome ( @genomes )
1233 next if $genome =~ /\.$/;
1235 @formats = Maasha::Common::ls_dirs( $genome );
1237 foreach $format ( @formats )
1239 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1241 $hash{ $1 }{ $2 } = 1;
1250 map { push @row, $_ } sort keys %found;
1252 print join( "\t", @row ), "\n";
1254 foreach $genome ( sort keys %hash )
1258 foreach $format ( sort keys %found )
1260 if ( exists $hash{ $genome }{ $format } ) {
1267 print join( "\t", @row ), "\n";
1272 sub script_read_fasta
1274 # Martin A. Hansen, August 2007.
1276 # Read sequences from FASTA file.
1278 my ( $in, # handle to in stream
1279 $out, # handle to out stream
1280 $options, # options hash
1285 my ( $record, $file, $data_in, $entry, $num );
1287 while ( $record = get_record( $in ) ) {
1288 put_record( $record, $out );
1293 foreach $file ( @{ $options->{ "files" } } )
1295 $data_in = Maasha::Common::read_open( $file );
1297 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1299 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1302 SEQ_NAME => $entry->[ SEQ_NAME ],
1303 SEQ => $entry->[ SEQ ],
1304 SEQ_LEN => length $entry->[ SEQ ],
1307 put_record( $record, $out );
1310 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1320 close $data_in if $data_in;
1326 # Martin A. Hansen, August 2007.
1328 # Read table or table columns from stream or file.
1330 my ( $in, # handle to in stream
1331 $out, # handle to out stream
1332 $options, # options hash
1337 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1339 $options->{ 'delimit' } ||= '\s+';
1341 while ( $record = get_record( $in ) ) {
1342 put_record( $record, $out );
1345 $skip = $options->{ 'skip' } ||= 0;
1348 foreach $file ( @{ $options->{ "files" } } )
1350 $data_in = Maasha::Common::read_open( $file );
1352 while ( $line = <$data_in> )
1360 next if $line =~ /^#|^$/;
1367 @fields = split /$options->{'delimit'}/, $line;
1369 if ( $options->{ "cols" } ) {
1370 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1375 for ( $i = 0; $i < @fields2; $i++ )
1377 if ( $options->{ "keys" }->[ $i ] ) {
1378 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1380 $record->{ "V" . $i } = $fields2[ $i ];
1384 put_record( $record, $out );
1386 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1396 close $data_in if $data_in;
1402 # Martin A. Hansen, August 2007.
1404 # Read psl table from stream or file.
1406 my ( $in, # handle to in stream
1407 $out, # handle to out stream
1408 $options, # options hash
1413 my ( $record, $file, $data_in, $num );
1415 while ( $record = get_record( $in ) ) {
1416 put_record( $record, $out );
1421 foreach $file ( @{ $options->{ "files" } } )
1423 $data_in = Maasha::Common::read_open( $file );
1425 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1427 put_record( $record, $out );
1429 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1441 # Martin A. Hansen, August 2007.
1443 # Read bed table from stream or file.
1445 my ( $in, # handle to in stream
1446 $out, # handle to out stream
1447 $options, # options hash
1452 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1454 $cols = $options->{ 'cols' }->[ 0 ];
1456 while ( $record = get_record( $in ) ) {
1457 put_record( $record, $out );
1462 foreach $file ( @{ $options->{ "files" } } )
1464 $data_in = Maasha::Common::read_open( $file );
1466 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1468 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1470 put_record( $record, $out );
1472 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1482 close $data_in if $data_in;
1486 sub script_read_fixedstep
1488 # Martin A. Hansen, Juli 2008.
1490 # Read fixedstep wiggle format from stream or file.
1492 my ( $in, # handle to in stream
1493 $out, # handle to out stream
1494 $options, # options hash
1499 my ( $file, $record, $entry, $data_in, $num );
1501 while ( $record = get_record( $in ) ) {
1502 put_record( $record, $out );
1507 foreach $file ( @{ $options->{ "files" } } )
1509 $data_in = Maasha::Common::read_open( $file );
1511 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1513 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1515 put_record( $record, $out );
1517 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1527 close $data_in if $data_in;
1531 sub script_read_blast_tab
1533 # Martin A. Hansen, September 2007.
1535 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1537 my ( $in, # handle to in stream
1538 $out, # handle to out stream
1539 $options, # options hash
1544 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1546 while ( $record = get_record( $in ) ) {
1547 put_record( $record, $out );
1552 foreach $file ( @{ $options->{ "files" } } )
1554 $data_in = Maasha::Common::read_open( $file );
1556 while ( $line = <$data_in> )
1560 next if $line =~ /^#/;
1562 @fields = split /\t/, $line;
1564 $record->{ "REC_TYPE" } = "BLAST";
1565 $record->{ "Q_ID" } = $fields[ 0 ];
1566 $record->{ "S_ID" } = $fields[ 1 ];
1567 $record->{ "IDENT" } = $fields[ 2 ];
1568 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1569 $record->{ "MISMATCHES" } = $fields[ 4 ];
1570 $record->{ "GAPS" } = $fields[ 5 ];
1571 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1572 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1573 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1574 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1575 $record->{ "E_VAL" } = $fields[ 10 ];
1576 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1578 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1580 $record->{ "STRAND" } = '-';
1582 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1586 $record->{ "STRAND" } = '+';
1589 put_record( $record, $out );
1591 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1601 close $data_in if $data_in;
1605 sub script_read_embl
1607 # Martin A. Hansen, August 2007.
1611 my ( $in, # handle to in stream
1612 $out, # handle to out stream
1613 $options, # options hash
1618 my ( %options2, $file, $data_in, $num, $entry, $record );
1620 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1621 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1622 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1624 while ( $record = get_record( $in ) ) {
1625 put_record( $record, $out );
1630 foreach $file ( @{ $options->{ "files" } } )
1632 $data_in = Maasha::Common::read_open( $file );
1634 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1636 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1638 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1640 $record_copy = dclone $record;
1642 delete $record_copy->{ "FT" };
1644 put_record( $record_copy, $out );
1646 delete $record_copy->{ "SEQ" };
1648 foreach $feat ( keys %{ $record->{ "FT" } } )
1650 $record_copy->{ "FEAT_TYPE" } = $feat;
1652 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1654 foreach $qual ( keys %{ $feat2 } )
1656 $qual_val = join "; ", @{ $feat2->{ $qual } };
1661 $record_copy->{ $qual } = $qual_val;
1664 put_record( $record_copy, $out );
1668 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1678 close $data_in if $data_in;
1682 sub script_read_stockholm
1684 # Martin A. Hansen, August 2007.
1686 # Read Stockholm format.
1688 my ( $in, # handle to in stream
1689 $out, # handle to out stream
1690 $options, # options hash
1695 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1697 while ( $record = get_record( $in ) ) {
1698 put_record( $record, $out );
1703 foreach $file ( @{ $options->{ "files" } } )
1705 $data_in = Maasha::Common::read_open( $file );
1707 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1709 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1713 foreach $key ( keys %{ $record->{ "GF" } } ) {
1714 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1717 $record_anno->{ "ALIGN" } = $num;
1719 put_record( $record_anno, $out );
1721 foreach $seq ( @{ $record->{ "ALIGN" } } )
1723 undef $record_align;
1726 SEQ_NAME => $seq->[ 0 ],
1730 put_record( $record_align, $out );
1733 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1743 close $data_in if $data_in;
1747 sub script_read_phastcons
1749 # Martin A. Hansen, December 2007.
1751 # Read PhastCons format.
1753 my ( $in, # handle to in stream
1754 $out, # handle to out stream
1755 $options, # options hash
1760 my ( $data_in, $file, $num, $entry, @records, $record );
1762 $options->{ "min" } ||= 10;
1763 $options->{ "dist" } ||= 25;
1764 $options->{ "threshold" } ||= 0.8;
1765 $options->{ "gap" } ||= 5;
1767 while ( $record = get_record( $in ) ) {
1768 put_record( $record, $out );
1773 foreach $file ( @{ $options->{ "files" } } )
1775 $data_in = Maasha::Common::read_open( $file );
1777 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1779 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1781 foreach $record ( @records )
1783 $record->{ "REC_TYPE" } = "BED";
1784 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1786 put_record( $record, $out );
1788 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1799 close $data_in if $data_in;
1803 sub script_read_soft
1805 # Martin A. Hansen, December 2007.
1808 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1810 my ( $in, # handle to in stream
1811 $out, # handle to out stream
1812 $options, # options hash
1817 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1819 while ( $record = get_record( $in ) ) {
1820 put_record( $record, $out );
1825 foreach $file ( @{ $options->{ "files" } } )
1827 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1829 $soft_index = Maasha::NCBI::soft_index_file( $file );
1831 $fh = Maasha::Common::read_open( $file );
1833 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1835 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1837 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1839 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1841 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1843 foreach $sample ( @samples )
1846 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1848 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1850 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1852 foreach $record ( @{ $records } )
1854 put_record( $record, $out );
1856 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1861 $old_end = $sample->{ "LINE_END" };
1869 close $data_in if $data_in;
1876 # Martin A. Hansen, February 2008.
1879 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1881 my ( $in, # handle to in stream
1882 $out, # handle to out stream
1883 $options, # options hash
1888 my ( $data_in, $file, $fh, $num, $record, $entry );
1890 while ( $record = get_record( $in ) ) {
1891 put_record( $record, $out );
1896 foreach $file ( @{ $options->{ "files" } } )
1898 $fh = Maasha::Common::read_open( $file );
1900 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1902 put_record( $entry, $out );
1904 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1914 close $data_in if $data_in;
1918 sub script_read_2bit
1920 # Martin A. Hansen, March 2008.
1922 # Read sequences from 2bit file.
1924 my ( $in, # handle to in stream
1925 $out, # handle to out stream
1926 $options, # options hash
1931 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1933 $mask = 1 if not $options->{ "no_mask" };
1935 while ( $record = get_record( $in ) ) {
1936 put_record( $record, $out );
1941 foreach $file ( @{ $options->{ "files" } } )
1943 $data_in = Maasha::Common::read_open( $file );
1945 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1947 foreach $line ( @{ $toc } )
1949 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1950 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1951 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1953 put_record( $record, $out );
1955 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1965 close $data_in if $data_in;
1969 sub script_read_solexa
1971 # Martin A. Hansen, March 2008.
1973 # Read Solexa sequence reads from file.
1975 my ( $in, # handle to in stream
1976 $out, # handle to out stream
1977 $options, # options hash
1982 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1984 $options->{ "format" } ||= "octal";
1985 $options->{ "quality" } ||= 20;
1987 while ( $record = get_record( $in ) ) {
1988 put_record( $record, $out );
1993 foreach $file ( @{ $options->{ "files" } } )
1995 $data_in = Maasha::Common::read_open( $file );
1997 if ( $options->{ "format" } eq "octal" )
1999 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
2001 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2003 put_record( $record, $out );
2005 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2012 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
2014 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2016 put_record( $record, $out );
2018 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2029 close $data_in if $data_in;
2033 sub script_read_solid
2035 # Martin A. Hansen, April 2008.
2037 # Read Solid sequence from file.
2039 my ( $in, # handle to in stream
2040 $out, # handle to out stream
2041 $options, # options hash
2046 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
2048 $options->{ "quality" } ||= 15;
2050 while ( $record = get_record( $in ) ) {
2051 put_record( $record, $out );
2056 foreach $file ( @{ $options->{ "files" } } )
2058 $data_in = Maasha::Common::read_open( $file );
2060 while ( $line = <$data_in> )
2064 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2066 @scores = split /,/, $seq_qual;
2067 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2069 for ( $i = 0; $i < @seqs; $i++ ) {
2070 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2074 REC_TYPE => 'SOLID',
2075 SEQ_NAME => $seq_name,
2077 SEQ_QUAL => join( ";", @scores ),
2078 SEQ_LEN => length $seq_cs,
2079 SEQ => join( "", @seqs ),
2080 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2083 put_record( $record, $out );
2085 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2095 close $data_in if $data_in;
2099 sub script_read_mysql
2101 # Martin A. Hansen, May 2008.
2103 # Read a MySQL query into stream.
2105 my ( $in, # handle to in stream
2106 $out, # handle to out stream
2107 $options, # options hash
2112 my ( $record, $dbh, $results );
2114 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2115 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2117 while ( $record = get_record( $in ) ) {
2118 put_record( $record, $out );
2121 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2123 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2125 Maasha::SQL::disconnect( $dbh );
2127 map { put_record( $_ ) } @{ $results };
2131 sub script_read_ucsc_config
2133 # Martin A. Hansen, November 2008.
2135 # Read track entries from UCSC Genome Browser '.ra' files.
2137 my ( $in, # handle to in stream
2138 $out, # handle to out stream
2139 $options, # options hash
2144 my ( $record, $file, $data_in, $entry, $num );
2146 while ( $record = get_record( $in ) ) {
2147 put_record( $record, $out );
2152 foreach $file ( @{ $options->{ "files" } } )
2154 $data_in = Maasha::Common::read_open( $file );
2156 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
2158 $record->{ 'REC_TYPE' } = "UCSC Config";
2160 put_record( $record, $out );
2162 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2172 close $data_in if $data_in;
2176 sub script_assemble_tag_contigs
2178 # Martin A. Hansen, November 2008.
2180 # Assemble tags from the stream into
2183 # The current implementation is quite
2184 # slow because of heavy use of temporary
2187 my ( $in, # handle to in stream
2188 $out, # handle to out stream
2189 $options, # options hash
2194 my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
2196 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
2197 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2198 $cols = 6; # we only need the first 6 BED columns
2200 while ( $record = get_record( $in ) )
2202 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
2204 $strand = $record->{ 'STRAND' } || '+';
2206 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
2212 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
2216 foreach $chr ( sort keys %{ $file_hash } )
2218 $bed_file = $file_hash->{ $chr };
2219 $tag_file = "$bed_file.tc";
2221 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
2223 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
2225 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
2227 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
2228 put_record( $record, $out );
2240 sub script_format_genome
2242 # Martin A. Hansen, Juli 2008.
2244 # Format a genome to speficed formats.
2246 my ( $in, # handle to in stream
2247 $out, # handle to out stream
2248 $options, # options hash
2253 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2255 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2256 $genome = $options->{ 'genome' };
2258 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2259 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2260 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2262 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2264 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2266 $fasta_dir = "$dir/genomes/$genome/fasta";
2270 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2272 $fasta_dir = "$dir/genomes/$genome/fasta";
2274 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2277 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2279 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2281 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2283 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2286 while ( $record = get_record( $in ) )
2288 if ( $fh_out and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2290 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2292 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2294 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2296 $vals = $record->{ 'VALS' };
2300 print $fh_out "$vals\n";
2303 put_record( $record, $out ) if not $options->{ "no_stream" };
2306 foreach $format ( @{ $options->{ 'formats' } } )
2308 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2309 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2310 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2311 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2312 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2315 close $fh_out if $fh_out;
2319 sub script_length_seq
2321 # Martin A. Hansen, August 2007.
2323 # Determine the length of sequences in stream.
2325 my ( $in, # handle to in stream
2326 $out, # handle to out stream
2327 $options, # options hash
2332 my ( $record, $total );
2334 while ( $record = get_record( $in ) )
2336 if ( $record->{ "SEQ" } )
2338 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2339 $total += $record->{ "SEQ_LEN" };
2342 put_record( $record, $out ) if not $options->{ "no_stream" };
2345 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2349 sub script_uppercase_seq
2351 # Martin A. Hansen, August 2007.
2353 # Uppercases sequences in stream.
2355 my ( $in, # handle to in stream
2356 $out, # handle to out stream
2363 while ( $record = get_record( $in ) )
2365 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2367 put_record( $record, $out );
2372 sub script_shuffle_seq
2374 # Martin A. Hansen, December 2007.
2376 # Shuffle sequences in stream.
2378 my ( $in, # handle to in stream
2379 $out, # handle to out stream
2386 while ( $record = get_record( $in ) )
2388 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2390 put_record( $record, $out );
2395 sub script_analyze_seq
2397 # Martin A. Hansen, August 2007.
2399 # Analyze sequence composition of sequences in stream.
2401 my ( $in, # handle to in stream
2402 $out, # handle to out stream
2407 my ( $record, $analysis );
2409 while ( $record = get_record( $in ) )
2411 if ( $record->{ "SEQ" } )
2413 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2415 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2418 put_record( $record, $out );
2423 sub script_analyze_tags
2425 # Martin A. Hansen, August 2008.
2427 # Analyze sequence tags in stream.
2429 my ( $in, # handle to in stream
2430 $out, # handle to out stream
2435 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2437 while ( $record = get_record( $in ) )
2439 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2441 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2445 $len_hash{ length( $record->{ "SEQ" } ) }++;
2446 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2449 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2451 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2455 $len_hash{ $record->{ "BED_LEN" } }++;
2456 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2461 foreach $key ( sort { $a <=> $b } keys %len_hash )
2463 $tag_record->{ "TAG_LEN" } = $key;
2464 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2465 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2467 put_record( $tag_record, $out );
2472 sub script_complexity_seq
2474 # Martin A. Hansen, May 2008.
2476 # Generates an index calculated as the most common di-residue over
2477 # the sequence length for all sequences in stream.
2479 my ( $in, # handle to in stream
2480 $out, # handle to out stream
2485 my ( $record, $index );
2487 while ( $record = get_record( $in ) )
2489 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2491 put_record( $record, $out );
2496 sub script_oligo_freq
2498 # Martin A. Hansen, August 2007.
2500 # Determine the length of sequences in stream.
2502 my ( $in, # handle to in stream
2503 $out, # handle to out stream
2504 $options, # options hash
2509 my ( $record, %oligos, @freq_table );
2511 $options->{ "word_size" } ||= 7;
2513 while ( $record = get_record( $in ) )
2515 if ( $record->{ "SEQ" } )
2517 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2519 if ( not $options->{ "all" } )
2521 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2523 map { put_record( $_, $out ) } @freq_table;
2529 put_record( $record, $out );
2532 if ( $options->{ "all" } )
2534 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2536 map { put_record( $_, $out ) } @freq_table;
2541 sub script_create_weight_matrix
2543 # Martin A. Hansen, August 2007.
2545 # Creates a weight matrix from an alignmnet.
2547 my ( $in, # handle to in stream
2548 $out, # handle to out stream
2549 $options, # options hash
2554 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2558 while ( $record = get_record( $in ) )
2560 if ( $record->{ "SEQ" } )
2562 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2564 $res = substr $record->{ "SEQ" }, $i, 1;
2566 $freq_hash{ $i }{ $res }++;
2567 $res_hash{ $res } = 1;
2574 put_record( $record, $out );
2578 foreach $res ( sort keys %res_hash )
2582 $record->{ "V0" } = $res;
2584 for ( $i = 0; $i < keys %freq_hash; $i++ )
2586 $freq = $freq_hash{ $i }{ $res } || 0;
2588 if ( $options->{ "percent" } ) {
2589 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2592 $record->{ "V" . ( $i + 1 ) } = $freq;
2595 put_record( $record, $out );
2600 sub script_calc_bit_scores
2602 # Martin A. Hansen, March 2007.
2604 # Calculates the bit scores for each position from an alignmnet in the stream.
2606 my ( $in, # handle to in stream
2607 $out, # handle to out stream
2612 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2616 while ( $record = get_record( $in ) )
2618 if ( $record->{ "SEQ" } )
2620 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2622 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2624 $res = substr $record->{ "SEQ" }, $i, 1;
2626 next if $res =~ /-|_|~|\./;
2628 $freq_hash{ $i }{ $res }++;
2635 put_record( $record, $out );
2641 if ( $type eq "protein" ) {
2647 for ( $i = 0; $i < keys %freq_hash; $i++ )
2649 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2651 $bit_diff = $bit_max - $bit_height;
2653 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2656 put_record( $record, $out );
2660 sub script_calc_fixedstep
2662 # Martin A. Hansen, September 2008.
2664 # Calculates fixedstep entries from data in the stream.
2666 my ( $in, # handle to in stream
2667 $out, # handle to out stream
2668 $options, # options hash
2673 my ( $bed_file, $fh_in, $fh_out, $cols, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2675 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2676 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2677 $cols = 5; # we only need the first 5 BED columns
2679 while ( $record = get_record( $in ) )
2681 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
2682 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
2688 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
2692 foreach $chr ( sort keys %{ $file_hash } )
2694 $bed_file = $file_hash->{ $chr };
2695 $fixedstep_file = "$bed_file.fixedstep";
2697 Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2699 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2701 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2703 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2704 put_record( $record, $out );
2711 unlink $fixedstep_file;
2716 sub script_reverse_seq
2718 # Martin A. Hansen, August 2007.
2720 # Reverse sequence in record.
2722 my ( $in, # handle to in stream
2723 $out, # handle to out stream
2730 while ( $record = get_record( $in ) )
2732 if ( $record->{ "SEQ" } ) {
2733 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2736 put_record( $record, $out );
2741 sub script_complement_seq
2743 # Martin A. Hansen, August 2007.
2745 # Complement sequence in record.
2747 my ( $in, # handle to in stream
2748 $out, # handle to out stream
2753 my ( $record, $type );
2755 while ( $record = get_record( $in ) )
2757 if ( $record->{ "SEQ" } )
2760 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2763 if ( $type eq "rna" ) {
2764 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2765 } elsif ( $type eq "dna" ) {
2766 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2770 put_record( $record, $out );
2775 sub script_remove_indels
2777 # Martin A. Hansen, August 2007.
2779 # Remove indels from sequences in stream.
2781 my ( $in, # handle to in stream
2782 $out, # handle to out stream
2789 while ( $record = get_record( $in ) )
2791 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2793 put_record( $record, $out );
2798 sub script_transliterate_seq
2800 # Martin A. Hansen, August 2007.
2802 # Transliterate chars from sequence in record.
2804 my ( $in, # handle to in stream
2805 $out, # handle to out stream
2806 $options, # options hash
2811 my ( $record, $search, $replace, $delete );
2813 $search = $options->{ "search" } || "";
2814 $replace = $options->{ "replace" } || "";
2815 $delete = $options->{ "delete" } || "";
2817 while ( $record = get_record( $in ) )
2819 if ( $record->{ "SEQ" } )
2821 if ( $search and $replace ) {
2822 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2823 } elsif ( $delete ) {
2824 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2828 put_record( $record, $out );
2833 sub script_transliterate_vals
2835 # Martin A. Hansen, April 2008.
2837 # Transliterate chars from values in record.
2839 my ( $in, # handle to in stream
2840 $out, # handle to out stream
2841 $options, # options hash
2846 my ( $record, $search, $replace, $delete, $key );
2848 $search = $options->{ "search" } || "";
2849 $replace = $options->{ "replace" } || "";
2850 $delete = $options->{ "delete" } || "";
2852 while ( $record = get_record( $in ) )
2854 foreach $key ( @{ $options->{ "keys" } } )
2856 if ( exists $record->{ $key } )
2858 if ( $search and $replace ) {
2859 eval "\$record->{ $key } =~ tr/$search/$replace/";
2860 } elsif ( $delete ) {
2861 eval "\$record->{ $key } =~ tr/$delete//d";
2866 put_record( $record, $out );
2871 sub script_translate_seq
2873 # Martin A. Hansen, February 2008.
2875 # Translate DNA sequence into protein sequence.
2877 my ( $in, # handle to in stream
2878 $out, # handle to out stream
2879 $options, # options hash
2884 my ( $record, $frame, %new_record );
2886 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2888 while ( $record = get_record( $in ) )
2890 if ( $record->{ "SEQ" } )
2892 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2894 foreach $frame ( @{ $options->{ "frames" } } )
2896 %new_record = %{ $record };
2898 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2899 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2900 $new_record{ "FRAME" } = $frame;
2902 put_record( \%new_record, $out );
2908 put_record( $record, $out );
2914 sub script_extract_seq
2916 # Martin A. Hansen, August 2007.
2918 # Extract subsequences from sequences in record.
2920 my ( $in, # handle to in stream
2921 $out, # handle to out stream
2922 $options, # options hash
2927 my ( $beg, $end, $len, $record );
2929 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2932 $beg = $options->{ "beg" } - 1; # correcting for start offset
2935 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2937 } elsif ( defined $options->{ "end" } ) {
2938 $end = $options->{ "end" } - 1; # correcting for start offset
2941 $len = $options->{ "len" };
2943 # print "beg->$beg, end->$end, len->$len\n";
2945 while ( $record = get_record( $in ) )
2947 if ( $record->{ "SEQ" } )
2949 if ( defined $beg and defined $end )
2951 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2952 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2954 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2957 elsif ( defined $beg and defined $len )
2959 if ( $len > length $record->{ "SEQ" } ) {
2960 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2962 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2965 elsif ( defined $beg )
2967 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2971 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2973 put_record( $record, $out );
2978 sub script_get_genome_seq
2980 # Martin A. Hansen, December 2007.
2982 # Gets a subsequence from a genome.
2984 my ( $in, # handle to in stream
2985 $out, # handle to out stream
2986 $options, # options hash
2991 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2993 $options->{ "flank" } ||= 0;
2995 if ( $options->{ "genome" } )
2997 $genome = $options->{ "genome" };
2999 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
3000 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
3002 $fh = Maasha::Common::read_open( $genome_file );
3003 $index = Maasha::Fasta::index_retrieve( $index_file );
3005 shift @{ $index }; # Get rid of the file size info
3007 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
3009 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3011 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
3013 $beg = $index_beg + $options->{ "beg" } - 1;
3015 if ( $options->{ "len" } ) {
3016 $len = $options->{ "len" };
3017 } elsif ( $options->{ "end" } ) {
3018 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
3021 $beg -= $options->{ "flank" };
3022 $len += 2 * $options->{ "flank" };
3024 if ( $beg <= $index_beg )
3026 $len -= $index_beg - $beg;
3030 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3032 next if $beg > $index_beg + $index_len;
3034 $record->{ "CHR" } = $options->{ "chr" };
3035 $record->{ "CHR_BEG" } = $beg - $index_beg;
3036 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3038 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3039 $record->{ "SEQ_LEN" } = $len;
3041 put_record( $record, $out );
3045 while ( $record = get_record( $in ) )
3047 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
3049 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
3051 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
3053 $beg = $record->{ "CHR_BEG" } + $index_beg;
3054 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3056 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
3058 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3060 $beg = $record->{ "S_BEG" } + $index_beg;
3061 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3063 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
3065 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3067 $beg = $record->{ "S_BEG" } + $index_beg;
3068 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3071 $beg -= $options->{ "flank" };
3072 $len += 2 * $options->{ "flank" };
3074 if ( $beg <= $index_beg )
3076 $len -= $index_beg - $beg;
3080 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3082 next if $beg > $index_beg + $index_len;
3084 $record->{ "CHR_BEG" } = $beg - $index_beg;
3085 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3087 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3089 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
3091 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
3092 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
3095 if ( $options->{ "mask" } )
3097 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
3099 $record->{ "SEQ" } = lc $record->{ "SEQ" };
3101 @begs = split ",", $record->{ "Q_BEGS" };
3102 @lens = split ",", $record->{ "BLOCKSIZES" };
3104 for ( $i = 0; $i < @begs; $i++ ) {
3105 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3111 put_record( $record, $out );
3118 sub script_get_genome_align
3120 # Martin A. Hansen, April 2008.
3122 # Gets a subalignment from a multiple genome alignment.
3124 my ( $in, # handle to in stream
3125 $out, # handle to out stream
3126 $options, # options hash
3131 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3133 $options->{ "strand" } ||= "+";
3137 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3139 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3141 $beg = $options->{ "beg" } - 1;
3143 if ( $options->{ "end" } ) {
3144 $end = $options->{ "end" };
3145 } elsif ( $options->{ "len" } ) {
3146 $end = $beg + $options->{ "len" };
3149 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3151 foreach $entry ( @{ $align } )
3153 $entry->{ "CHR" } = $record->{ "CHR" };
3154 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3155 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3156 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3157 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3158 $entry->{ "SCORE" } = $record->{ "SCORE" };
3160 put_record( $entry, $out );
3164 while ( $record = get_record( $in ) )
3166 if ( $record->{ "REC_TYPE" } eq "BED" )
3168 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3170 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
3172 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
3174 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3176 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3178 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3180 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3183 foreach $entry ( @{ $align } )
3185 $entry->{ "CHR" } = $record->{ "CHR" };
3186 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3187 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3188 $entry->{ "STRAND" } = $record->{ "STRAND" };
3189 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3190 $entry->{ "SCORE" } = $record->{ "SCORE" };
3192 put_record( $entry, $out );
3200 sub script_get_genome_phastcons
3202 # Martin A. Hansen, February 2008.
3204 # Get phastcons scores from genome intervals.
3206 my ( $in, # handle to in stream
3207 $out, # handle to out stream
3208 $options, # options hash
3213 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3215 $options->{ "flank" } ||= 0;
3217 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3218 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3220 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3221 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3223 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3225 $options->{ "beg" } -= 1; # request is 1-based
3226 $options->{ "end" } -= 1; # request is 1-based
3228 if ( $options->{ "len" } ) {
3229 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3232 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3234 $record->{ "CHR" } = $options->{ "chr" };
3235 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3236 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3238 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3239 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3241 put_record( $record, $out );
3244 while ( $record = get_record( $in ) )
3246 if ( $record->{ "REC_TYPE" } eq "BED" )
3248 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3250 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3252 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3254 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3256 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3259 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3260 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3262 put_record( $record, $out );
3265 close $fh_phastcons if $fh_phastcons;
3271 # Martin A. Hansen, December 2007.
3273 # Folds sequences in stream into secondary structures.
3275 my ( $in, # handle to in stream
3276 $out, # handle to out stream
3281 my ( $record, $type, $struct, $index );
3283 while ( $record = get_record( $in ) )
3285 if ( $record->{ "SEQ" } )
3288 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3291 if ( $type ne "protein" )
3293 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3294 $record->{ "SEC_STRUCT" } = $struct;
3295 $record->{ "FREE_ENERGY" } = $index;
3296 $record->{ "SCORE" } = abs int $index;
3297 $record->{ "SIZE" } = length $struct;
3298 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3302 put_record( $record, $out );
3307 sub script_split_seq
3309 # Martin A. Hansen, August 2007.
3311 # Split a sequence in stream into words.
3313 my ( $in, # handle to in stream
3314 $out, # handle to out stream
3315 $options, # options hash
3320 my ( $record, $new_record, $i, $subseq, %lookup );
3322 $options->{ "word_size" } ||= 7;
3324 while ( $record = get_record( $in ) )
3326 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3328 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3330 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3332 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3334 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3335 $new_record->{ "SEQ" } = $subseq;
3337 put_record( $new_record, $out );
3339 $lookup{ $subseq } = 1;
3343 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3344 $new_record->{ "SEQ" } = $subseq;
3346 put_record( $new_record, $out );
3352 put_record( $record, $out );
3358 sub script_split_bed
3360 # Martin A. Hansen, June 2008.
3362 # Split a BED record into overlapping windows.
3364 my ( $in, # handle to in stream
3365 $out, # handle to out stream
3366 $options, # options hash
3371 my ( $record, $new_record, $i );
3373 $options->{ "window_size" } ||= 20;
3374 $options->{ "step_size" } ||= 1;
3376 while ( $record = get_record( $in ) )
3378 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3380 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3382 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3384 $new_record->{ "REC_TYPE" } = "BED";
3385 $new_record->{ "CHR" } = $record->{ "CHR" };
3386 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3387 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3388 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3389 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3390 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3391 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3393 put_record( $new_record, $out );
3398 put_record( $record, $out );
3404 sub script_align_seq
3406 # Martin A. Hansen, August 2007.
3408 # Align sequences in stream.
3410 my ( $in, # handle to in stream
3411 $out, # handle to out stream
3416 my ( $record, @entries, $entry );
3418 while ( $record = get_record( $in ) )
3420 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3421 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3422 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3423 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3425 put_record( $record, $out );
3429 @entries = Maasha::Align::align( \@entries );
3431 foreach $entry ( @entries )
3433 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3436 SEQ_NAME => $entry->[ SEQ_NAME ],
3437 SEQ => $entry->[ SEQ ],
3440 put_record( $record, $out );
3448 # Martin A. Hansen, February 2008.
3450 # Using the first sequence in stream as reference, tile
3451 # all subsequent sequences based on pairwise alignments.
3453 my ( $in, # handle to in stream
3454 $out, # handle to out stream
3455 $options, # options hash
3460 my ( $record, $first, $ref_entry, @entries );
3464 while ( $record = get_record( $in ) )
3466 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3470 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3476 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3481 put_record( $record, $out );
3485 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3487 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3491 sub script_invert_align
3493 # Martin A. Hansen, February 2008.
3495 # Inverts an alignment showing only non-mathing residues
3496 # using the first sequence as reference.
3498 my ( $in, # handle to in stream
3499 $out, # handle to out stream
3500 $options, # options hash
3505 my ( $record, @entries );
3507 while ( $record = get_record( $in ) )
3509 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3511 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3515 put_record( $record, $out );
3519 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3521 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3525 sub script_patscan_seq
3527 # Martin A. Hansen, August 2007.
3529 # Locates patterns in sequences using scan_for_matches.
3531 my ( $in, # handle to in stream
3532 $out, # handle to out stream
3533 $options, # options hash
3538 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3540 if ( $options->{ "patterns" } ) {
3541 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3542 } elsif ( -f $options->{ "patterns_in" } ) {
3543 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3546 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3548 push @args, "-c" if $options->{ "comp" };
3549 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3550 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3552 $seq_file = "$BP_TMP/patscan.seq";
3553 $pat_file = "$BP_TMP/patscan.pat";
3554 $out_file = "$BP_TMP/patscan.out";
3556 $fh_out = Maasha::Common::write_open( $seq_file );
3560 while ( $record = get_record( $in ) )
3562 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3564 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3566 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3568 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3576 $arg = join " ", @args;
3577 $arg .= " -p" if $type eq "protein";
3579 foreach $pattern ( @{ $patterns } )
3581 $fh_out = Maasha::Common::write_open( $pat_file );
3583 print $fh_out "$pattern\n";
3587 if ( $options->{ 'genome' } ) {
3588 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3589 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3591 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3592 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3595 $fh_in = Maasha::Common::read_open( $out_file );
3597 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3599 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3601 if ( $options->{ 'genome' } )
3603 $result->{ "CHR" } = $result->{ "S_ID" };
3604 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3605 $result->{ "CHR_END" } = $result->{ "S_END" };
3607 delete $result->{ "S_ID" };
3608 delete $result->{ "S_BEG" };
3609 delete $result->{ "S_END" };
3613 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3616 put_record( $result, $out );
3628 sub script_create_blast_db
3630 # Martin A. Hansen, September 2007.
3632 # Creates a NCBI BLAST database with formatdb
3634 my ( $in, # handle to in stream
3635 $out, # handle to out stream
3636 $options, # options hash
3641 my ( $fh, $seq_type, $path, $record, $entry );
3643 $path = $options->{ "database" };
3645 $fh = Maasha::Common::write_open( $path );
3647 while ( $record = get_record( $in ) )
3649 put_record( $record, $out ) if not $options->{ "no_stream" };
3651 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3653 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3655 Maasha::Fasta::put_entry( $entry, $fh );
3661 if ( $seq_type eq "protein" ) {
3662 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3664 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3671 sub script_blast_seq
3673 # Martin A. Hansen, September 2007.
3675 # BLASTs sequences in stream against a given database.
3677 my ( $in, # handle to in stream
3678 $out, # handle to out stream
3679 $options, # options hash
3684 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3686 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3687 $options->{ "filter" } = "F";
3688 $options->{ "filter" } = "T" if $options->{ "filter" };
3689 $options->{ "cpus" } ||= 1;
3691 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3693 $tmp_in = "$BP_TMP/blast_query.seq";
3694 $tmp_out = "$BP_TMP/blast.result";
3696 $fh_out = Maasha::Filesys::file_write_open( $tmp_in );
3698 while ( $record = get_record( $in ) )
3700 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3702 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3704 Maasha::Fasta::put_entry( $entry, $fh_out );
3707 put_record( $record, $out );
3712 if ( -f $options->{ 'database' } . ".phr" ) {
3713 $s_type = "protein";
3715 $s_type = "nucleotide";
3718 if ( not $options->{ 'program' } )
3720 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3721 $options->{ 'program' } = "blastn";
3722 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3723 $options->{ 'program' } = "blastp";
3724 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3725 $options->{ 'program' } = "blastx";
3726 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3727 $options->{ 'program' } = "tblastn";
3731 if ( $options->{ 'verbose' } )
3733 Maasha::Common::run(
3736 "-p $options->{ 'program' }",
3737 "-e $options->{ 'e_val' }",
3738 "-a $options->{ 'cpus' }",
3741 "-d $options->{ 'database' }",
3742 "-F $options->{ 'filter' }",
3750 Maasha::Common::run(
3753 "-p $options->{ 'program' }",
3754 "-e $options->{ 'e_val' }",
3755 "-a $options->{ 'cpus' }",
3758 "-d $options->{ 'database' }",
3759 "-F $options->{ 'filter' }",
3769 $fh_out = Maasha::Filesys::file_read_open( $tmp_out );
3773 while ( $line = <$fh_out> )
3777 next if $line =~ /^#/;
3779 @fields = split /\s+/, $line;
3781 $record->{ "REC_TYPE" } = "BLAST";
3782 $record->{ "Q_ID" } = $fields[ 0 ];
3783 $record->{ "S_ID" } = $fields[ 1 ];
3784 $record->{ "IDENT" } = $fields[ 2 ];
3785 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3786 $record->{ "MISMATCHES" } = $fields[ 4 ];
3787 $record->{ "GAPS" } = $fields[ 5 ];
3788 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3789 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3790 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3791 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3792 $record->{ "E_VAL" } = $fields[ 10 ];
3793 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3795 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3797 $record->{ "STRAND" } = '-';
3799 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3803 $record->{ "STRAND" } = '+';
3806 put_record( $record, $out );
3817 # Martin A. Hansen, August 2007.
3819 # BLATs sequences in stream against a given genome.
3821 my ( $in, # handle to in stream
3822 $out, # handle to out stream
3823 $options, # options hash
3828 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3830 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3832 $options->{ 'tile_size' } ||= 11;
3833 $options->{ 'one_off' } ||= 0;
3834 $options->{ 'min_identity' } ||= 90;
3835 $options->{ 'min_score' } ||= 0;
3836 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3838 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3839 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3840 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3841 $blat_args .= " -minScore=$options->{ 'min_score' }";
3842 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3843 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3845 $query_file = "$BP_TMP/blat.seq";
3847 $fh_out = Maasha::Common::write_open( $query_file );
3849 while ( $record = get_record( $in ) )
3851 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3853 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3854 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3857 put_record( $record, $out );
3862 $blat_args .= " -t=dnax" if $type eq "protein";
3863 $blat_args .= " -q=$type";
3865 $result_file = "$BP_TMP/blat.psl";
3867 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3871 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3873 map { put_record( $_, $out ) } @{ $entries };
3875 unlink $result_file;
3881 # Martin A. Hansen, July 2008.
3883 # soap sequences in stream against a given file or genome.
3885 my ( $in, # handle to in stream
3886 $out, # handle to out stream
3887 $options, # options hash
3892 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3894 $options->{ "seed_size" } ||= 10;
3895 $options->{ "mismatches" } ||= 2;
3896 $options->{ "gap_size" } ||= 0;
3897 $options->{ "cpus" } ||= 1;
3899 if ( $options->{ "genome" } ) {
3900 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3903 $tmp_in = "$BP_TMP/soap_query.seq";
3904 $tmp_out = "$BP_TMP/soap.result";
3906 $fh_out = Maasha::Common::write_open( $tmp_in );
3910 while ( $record = get_record( $in ) )
3912 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3914 Maasha::Fasta::put_entry( $entry, $fh_out );
3919 put_record( $record, $out );
3927 "-s $options->{ 'seed_size' }",
3930 "-v $options->{ 'mismatches' }",
3931 "-g $options->{ 'gap_size' }",
3932 "-p $options->{ 'cpus' }",
3933 "-d $options->{ 'in_file' }",
3937 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3939 Maasha::Common::run( "soap", $args, 1 );
3943 $fh_out = Maasha::Common::read_open( $tmp_out );
3947 while ( $line = <$fh_out> )
3951 @fields = split /\t/, $line;
3953 $record->{ "REC_TYPE" } = "SOAP";
3954 $record->{ "Q_ID" } = $fields[ 0 ];
3955 $record->{ "SCORE" } = $fields[ 3 ];
3956 $record->{ "STRAND" } = $fields[ 6 ];
3957 $record->{ "S_ID" } = $fields[ 7 ];
3958 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3959 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3961 put_record( $record, $out );
3971 sub script_match_seq
3973 # Martin A. Hansen, August 2007.
3975 # BLATs sequences in stream against a given genome.
3977 my ( $in, # handle to in stream
3978 $out, # handle to out stream
3979 $options, # options hash
3984 my ( $record, @entries, $results );
3986 $options->{ "word_size" } ||= 20;
3987 $options->{ "direction" } ||= "both";
3989 while ( $record = get_record( $in ) )
3991 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3992 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3995 put_record( $record, $out );
3998 if ( @entries == 1 )
4000 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
4002 map { put_record( $_, $out ) } @{ $results };
4004 elsif ( @entries == 2 )
4006 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
4008 map { put_record( $_, $out ) } @{ $results };
4013 sub script_create_vmatch_index
4015 # Martin A. Hansen, January 2008.
4017 # Create a vmatch index from sequences in the stream.
4019 my ( $in, # handle to in stream
4020 $out, # handle to out stream
4021 $options, # options hash
4026 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
4028 if ( $options->{ "index_name" } )
4030 $file_tmp = $options->{ 'index_name' };
4031 $fh_tmp = Maasha::Common::write_open( $file_tmp );
4034 while ( $record = get_record( $in ) )
4036 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4038 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4040 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
4043 put_record( $record, $out ) if not $options->{ "no_stream" };
4046 if ( $options->{ "index_name" } )
4050 if ( $type eq "protein" ) {
4051 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4053 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4061 sub script_vmatch_seq
4063 # Martin A. Hansen, August 2007.
4065 # Vmatches sequences in stream against a given genome.
4067 my ( $in, # handle to in stream
4068 $out, # handle to out stream
4069 $options, # options hash
4074 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
4076 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
4078 if ( $options->{ "index_name" } )
4080 @index_files = $options->{ "index_name" };
4084 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
4086 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
4088 @index_files = sort keys %hash;
4091 while ( $record = get_record( $in ) )
4093 push @records, $record;
4095 put_record( $record, $out );
4098 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
4102 $fh_in = Maasha::Common::read_open( $result_file );
4104 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
4105 put_record( $record, $out );
4110 unlink $result_file;
4114 sub script_write_fasta
4116 # Martin A. Hansen, August 2007.
4118 # Write FASTA entries from sequences in stream.
4120 my ( $in, # handle to in stream
4121 $out, # handle to out stream
4122 $options, # options hash
4127 my ( $record, $fh, $entry );
4129 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4131 while ( $record = get_record( $in ) )
4133 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4134 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4137 put_record( $record, $out ) if not $options->{ "no_stream" };
4144 sub script_write_align
4146 # Martin A. Hansen, August 2007.
4148 # Write pretty alignments aligned sequences in stream.
4150 my ( $in, # handle to in stream
4151 $out, # handle to out stream
4152 $options, # options hash
4157 my ( $fh, $record, @entries );
4159 $fh = write_stream( $options->{ "data_out" } ) ;
4161 while ( $record = get_record( $in ) )
4163 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4164 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4167 put_record( $record, $out ) if not $options->{ "no_stream" };
4170 if ( scalar( @entries ) == 2 ) {
4171 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4172 } elsif ( scalar ( @entries ) > 2 ) {
4173 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4180 sub script_write_blast
4182 # Martin A. Hansen, November 2007.
4184 # Write data in blast table format (-m8 and 9).
4186 my ( $in, # handle to in stream
4187 $out, # handle to out stream
4188 $options, # options hash
4193 my ( $fh, $record, $first );
4195 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4199 while ( $record = get_record( $in ) )
4201 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4203 if ( $options->{ "comment" } and $first )
4205 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";
4210 if ( $record->{ "STRAND" } eq "-" ) {
4211 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4214 print $fh join( "\t",
4215 $record->{ "Q_ID" },
4216 $record->{ "S_ID" },
4217 $record->{ "IDENT" },
4218 $record->{ "ALIGN_LEN" },
4219 $record->{ "MISMATCHES" },
4220 $record->{ "GAPS" },
4221 $record->{ "Q_BEG" } + 1,
4222 $record->{ "Q_END" } + 1,
4223 $record->{ "S_BEG" } + 1,
4224 $record->{ "S_END" } + 1,
4225 $record->{ "E_VAL" },
4226 $record->{ "BIT_SCORE" }
4230 put_record( $record, $out ) if not $options->{ "no_stream" };
4237 sub script_write_tab
4239 # Martin A. Hansen, August 2007.
4241 # Write data as table.
4243 my ( $in, # handle to in stream
4244 $out, # handle to out stream
4245 $options, # options hash
4250 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4252 $options->{ "delimit" } ||= "\t";
4254 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4256 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4258 while ( $record = get_record( $in ) )
4263 if ( $options->{ "keys" } )
4265 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4269 foreach $key ( @{ $options->{ "keys" } } )
4271 if ( exists $record->{ $key } )
4273 push @keys, $key if $options->{ "comment" };
4274 push @vals, $record->{ $key };
4281 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4283 next if exists $no_keys{ $key };
4285 push @keys, $key if $options->{ "comment" };
4286 push @vals, $record->{ $key };
4290 if ( @keys and $options->{ "comment" } )
4292 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4294 delete $options->{ "comment" };
4297 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4299 put_record( $record, $out ) if not $options->{ "no_stream" };
4306 sub script_write_bed
4308 # Martin A. Hansen, August 2007.
4310 # Write BED format for the UCSC genome browser using records in stream.
4312 my ( $in, # handle to in stream
4313 $out, # handle to out stream
4314 $options, # options hash
4319 my ( $cols, $fh, $record, $bed_entry, $new_record );
4321 $cols = $options->{ 'cols' }->[ 0 ];
4323 $fh = write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
4325 while ( $record = get_record( $in ) )
4327 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
4329 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
4330 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
4333 put_record( $record, $out ) if not $options->{ 'no_stream' };
4340 sub script_write_psl
4342 # Martin A. Hansen, August 2007.
4344 # Write PSL output from stream.
4346 my ( $in, # handle to in stream
4347 $out, # handle to out stream
4348 $options, # options hash
4353 my ( $fh, $record, @output, $first );
4357 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4359 while ( $record = get_record( $in ) )
4361 put_record( $record, $out ) if not $options->{ "no_stream" };
4363 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4365 Maasha::UCSC::psl_put_header( $fh ) if $first;
4366 Maasha::UCSC::psl_put_entry( $record, $fh );
4375 sub script_write_fixedstep
4377 # Martin A. Hansen, Juli 2008.
4379 # Write fixedStep entries from recrods in the stream.
4381 my ( $in, # handle to in stream
4382 $out, # handle to out stream
4383 $options, # options hash
4388 my ( $fh, $record, $entry );
4390 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4392 while ( $record = get_record( $in ) )
4394 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4395 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
4398 put_record( $record, $out ) if not $options->{ "no_stream" };
4405 sub script_write_2bit
4407 # Martin A. Hansen, March 2008.
4409 # Write sequence entries from stream in 2bit format.
4411 my ( $in, # handle to in stream
4412 $out, # handle to out stream
4413 $options, # options hash
4418 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4420 $mask = 1 if not $options->{ "no_mask" };
4422 $tmp_file = "$BP_TMP/write_2bit.fna";
4423 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4425 $fh_out = write_stream( $options->{ "data_out" } );
4427 while ( $record = get_record( $in ) )
4429 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4430 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4433 put_record( $record, $out ) if not $options->{ "no_stream" };
4438 $fh_in = Maasha::Common::read_open( $tmp_file );
4440 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4449 sub script_write_solid
4451 # Martin A. Hansen, April 2008.
4453 # Write di-base encoded Solid sequence from entries in stream.
4455 my ( $in, # handle to in stream
4456 $out, # handle to out stream
4457 $options, # options hash
4462 my ( $record, $fh, $entry );
4464 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4466 while ( $record = get_record( $in ) )
4468 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4470 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4472 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4475 put_record( $record, $out ) if not $options->{ "no_stream" };
4482 sub script_write_ucsc_config
4484 # Martin A. Hansen, November 2008.
4486 # Write UCSC Genome Broser configuration (.ra file type) from
4487 # records in the stream.
4489 my ( $in, # handle to in stream
4490 $out, # handle to out stream
4491 $options, # options hash
4496 my ( $record, $fh );
4498 $fh = write_stream( $options->{ "data_out" } );
4500 while ( $record = get_record( $in ) )
4502 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4504 put_record( $record, $out ) if not $options->{ "no_stream" };
4511 sub script_plot_seqlogo
4513 # Martin A. Hansen, August 2007.
4515 # Calculates and writes a sequence logo for alignments.
4517 my ( $in, # handle to in stream
4518 $out, # handle to out stream
4519 $options, # options hash
4524 my ( $record, @entries, $logo, $fh );
4526 while ( $record = get_record( $in ) )
4528 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4529 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4532 put_record( $record, $out ) if not $options->{ "no_stream" };
4535 $logo = Maasha::Plot::seq_logo( \@entries );
4537 $fh = write_stream( $options->{ "data_out" } );
4545 sub script_plot_phastcons_profiles
4547 # Martin A. Hansen, January 2008.
4549 # Plots PhastCons profiles.
4551 my ( $in, # handle to in stream
4552 $out, # handle to out stream
4553 $options, # options hash
4558 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4560 $options->{ "title" } ||= "PhastCons Profiles";
4562 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4563 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4565 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4566 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4568 while ( $record = get_record( $in ) )
4570 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4572 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4573 $record->{ "CHR_BEG" },
4574 $record->{ "CHR_END" },
4575 $options->{ "flank" } );
4577 push @{ $AoA }, [ @{ $scores } ];
4580 put_record( $record, $out ) if not $options->{ "no_stream" };
4583 Maasha::UCSC::phastcons_normalize( $AoA );
4585 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4586 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4588 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4590 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4592 $fh = write_stream( $options->{ "data_out" } );
4594 print $fh "$_\n" foreach @{ $plot };
4600 sub script_analyze_bed
4602 # Martin A. Hansen, March 2008.
4604 # Analyze BED entries in stream.
4606 my ( $in, # handle to in stream
4607 $out, # handle to out stream
4608 $options, # options hash
4615 while ( $record = get_record( $in ) )
4617 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4619 put_record( $record, $out );
4624 sub script_analyze_vals
4626 # Martin A. Hansen, August 2007.
4628 # Analyze values for given keys in stream.
4630 my ( $in, # handle to in stream
4631 $out, # handle to out stream
4632 $options, # options hash
4637 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4639 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4641 while ( $record = get_record( $in ) )
4643 foreach $key ( keys %{ $record } )
4645 next if $options->{ "keys" } and not exists $key_hash{ $key };
4647 $analysis->{ $key }->{ "COUNT" }++;
4649 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4651 $analysis->{ $key }->{ "TYPE" } = "num";
4652 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4653 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4654 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4658 $len = length $record->{ $key };
4660 $analysis->{ $key }->{ "TYPE" } = "alph";
4661 $analysis->{ $key }->{ "SUM" } += $len;
4662 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4663 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4667 put_record( $record, $out ) if not $options->{ "no_stream" };
4670 foreach $key ( keys %{ $analysis } )
4672 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4673 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4676 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4686 if ( $options->{ "keys" } ) {
4687 @keys = @{ $options->{ "keys" } };
4689 @keys = keys %{ $analysis };
4692 foreach $key ( @keys )
4694 $keys .= sprintf "% 15s", $key;
4695 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4696 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4697 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4698 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4699 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4700 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4703 print $out "$keys\n";
4704 print $out "$types\n";
4705 print $out "$counts\n";
4706 print $out "$mins\n";
4707 print $out "$maxs\n";
4708 print $out "$sums\n";
4709 print $out "$means\n";
4713 sub script_head_records
4715 # Martin A. Hansen, August 2007.
4717 # Display the first sequences in stream.
4719 my ( $in, # handle to in stream
4720 $out, # handle to out stream
4721 $options, # options hash
4726 my ( $record, $count );
4728 $options->{ "num" } ||= 10;
4732 while ( $record = get_record( $in ) )
4736 put_record( $record, $out );
4738 last if $count == $options->{ "num" };
4743 sub script_remove_keys
4745 # Martin A. Hansen, August 2007.
4747 # Remove keys from stream.
4749 my ( $in, # handle to in stream
4750 $out, # handle to out stream
4751 $options, # options hash
4756 my ( $record, $new_record );
4758 while ( $record = get_record( $in ) )
4760 if ( $options->{ "keys" } )
4762 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4764 elsif ( $options->{ "save_keys" } )
4766 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4768 $record = $new_record;
4771 put_record( $record, $out ) if keys %{ $record };
4776 sub script_remove_adaptor
4778 # Martin A. Hansen, August 2008.
4780 # Find and remove adaptor from sequences in the stream.
4782 my ( $in, # handle to in stream
4783 $out, # handle to out stream
4784 $options, # options hash
4789 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4791 $options->{ "remove" } ||= "after";
4793 $max_mismatch = $options->{ "mismatches" } || 0;
4794 $offset = $options->{ "offset" };
4796 if ( not defined $offset ) {
4802 $adaptor = uc $options->{ "adaptor" };
4803 $adaptor_len = length $adaptor;
4805 while ( $record = get_record( $in ) )
4807 if ( $record->{ "SEQ" } )
4809 $seq = uc $record->{ "SEQ" };
4810 $seq_len = length $seq;
4812 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4814 $record->{ "ADAPTOR_POS" } = $pos;
4816 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
4818 if ( $options->{ "remove" } eq "after" )
4820 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4821 $record->{ "SEQ_LEN" } = $pos;
4825 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
4826 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
4830 put_record( $record, $out );
4834 put_record( $record, $out );
4840 sub script_remove_mysql_tables
4842 # Martin A. Hansen, November 2008.
4844 # Remove MySQL tables from values in stream.
4846 my ( $in, # handle to in stream
4847 $out, # handle to out stream
4848 $options, # options hash
4853 my ( $record, %table_hash, $dbh, $table );
4855 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
4856 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
4858 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
4860 while ( $record = get_record( $in ) )
4862 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4864 put_record( $record, $out ) if not $options->{ 'no_stream' };
4867 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4869 foreach $table ( sort keys %table_hash )
4871 if ( Maasha::SQL::table_exists( $dbh, $table ) )
4873 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4874 Maasha::SQL::delete_table( $dbh, $table );
4875 print STDERR "done.\n" if $options->{ 'verbose' };
4879 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
4883 Maasha::SQL::disconnect( $dbh );
4887 sub script_remove_ucsc_tracks
4889 # Martin A. Hansen, November 2008.
4891 # Remove track from MySQL tables and config file.
4893 my ( $in, # handle to in stream
4894 $out, # handle to out stream
4895 $options, # options hash
4900 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
4902 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
4903 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
4904 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
4906 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
4908 while ( $record = get_record( $in ) )
4910 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4912 put_record( $record, $out ) if not $options->{ 'no_stream' };
4915 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
4917 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
4918 push @tracks, $track;
4923 foreach $track ( @tracks )
4925 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
4926 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
4928 push @new_tracks, $track;
4932 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
4934 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
4936 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
4940 # ---- locate track in database ----
4942 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4944 foreach $track ( sort keys %track_hash )
4946 if ( Maasha::SQL::table_exists( $dbh, $track ) )
4948 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4949 Maasha::SQL::delete_table( $dbh, $track );
4950 print STDERR "done.\n" if $options->{ 'verbose' };
4954 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
4958 Maasha::SQL::disconnect( $dbh );
4960 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
4964 sub script_rename_keys
4966 # Martin A. Hansen, August 2007.
4968 # Rename keys in stream.
4970 my ( $in, # handle to in stream
4971 $out, # handle to out stream
4972 $options, # options hash
4979 while ( $record = get_record( $in ) )
4981 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4983 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4985 delete $record->{ $options->{ "keys" }->[ 0 ] };
4988 put_record( $record, $out );
4993 sub script_uniq_vals
4995 # Martin A. Hansen, August 2007.
4997 # Find unique values in stream.
4999 my ( $in, # handle to in stream
5000 $out, # handle to out stream
5001 $options, # options hash
5006 my ( %hash, $record );
5008 while ( $record = get_record( $in ) )
5010 if ( $record->{ $options->{ "key" } } )
5012 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
5014 put_record( $record, $out );
5016 $hash{ $record->{ $options->{ "key" } } } = 1;
5018 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
5020 put_record( $record, $out );
5024 $hash{ $record->{ $options->{ "key" } } } = 1;
5029 put_record( $record, $out );
5035 sub script_merge_vals
5037 # Martin A. Hansen, August 2007.
5039 # Rename keys in stream.
5041 my ( $in, # handle to in stream
5042 $out, # handle to out stream
5043 $options, # options hash
5048 my ( $record, @join, $i );
5050 $options->{ "delimit" } ||= '_';
5052 while ( $record = get_record( $in ) )
5054 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5056 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5058 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5059 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5062 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5065 put_record( $record, $out );
5070 sub script_merge_records
5072 # Martin A. Hansen, July 2008.
5074 # Merges records in the stream based on identical values of two given keys.
5076 my ( $in, # handle to in stream
5077 $out, # handle to out stream
5078 $options, # options hash
5083 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5084 $num1, $num2, $num, $cmp, $i );
5086 $merge = $options->{ "merge" } || "AandB";
5088 $file1 = "$BP_TMP/merge_records1.tmp";
5089 $file2 = "$BP_TMP/merge_records2.tmp";
5091 $fh1 = Maasha::Common::write_open( $file1 );
5092 $fh2 = Maasha::Common::write_open( $file2 );
5094 $key1 = $options->{ "keys" }->[ 0 ];
5095 $key2 = $options->{ "keys" }->[ 1 ];
5097 $num = $key2 =~ s/n$//;
5101 while ( $record = get_record( $in ) )
5103 if ( exists $record->{ $key1 } )
5106 @vals1 = $record->{ $key1 };
5108 delete $record->{ $key1 };
5110 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5112 print $fh1 join( "\t", @vals1 ), "\n";
5116 elsif ( exists $record->{ $key2 } )
5119 @vals2 = $record->{ $key2 };
5121 delete $record->{ $key2 };
5123 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5125 print $fh2 join( "\t", @vals2 ), "\n";
5136 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5137 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5141 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5142 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5145 $fh1 = Maasha::Common::read_open( $file1 );
5146 $fh2 = Maasha::Common::read_open( $file2 );
5148 @vals1 = Maasha::Common::get_fields( $fh1 );
5149 @vals2 = Maasha::Common::get_fields( $fh2 );
5151 while ( $num1 > 0 and $num2 > 0 )
5156 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5158 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5163 if ( $merge =~ /^(AorB|AnotB)$/ )
5165 for ( $i = 0; $i < @keys1; $i++ ) {
5166 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5169 put_record( $record, $out );
5172 @vals1 = Maasha::Common::get_fields( $fh1 );
5177 if ( $merge =~ /^(BorA|BnotA)$/ )
5179 for ( $i = 0; $i < @keys2; $i++ ) {
5180 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5183 put_record( $record, $out );
5186 @vals2 = Maasha::Common::get_fields( $fh2 );
5191 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5193 for ( $i = 0; $i < @keys1; $i++ ) {
5194 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5197 for ( $i = 1; $i < @keys2; $i++ ) {
5198 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5201 put_record( $record, $out );
5204 @vals1 = Maasha::Common::get_fields( $fh1 );
5205 @vals2 = Maasha::Common::get_fields( $fh2 );
5217 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5221 for ( $i = 0; $i < @keys1; $i++ ) {
5222 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5225 put_record( $record, $out );
5228 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5232 for ( $i = 0; $i < @keys2; $i++ ) {
5233 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5236 put_record( $record, $out );
5243 # Martin A. Hansen, August 2007.
5245 # Grab for records in stream.
5247 my ( $in, # handle to in stream
5248 $out, # handle to out stream
5249 $options, # options hash
5254 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5256 $keys = $options->{ 'keys' };
5257 $vals_only = $options->{ 'vals_only' };
5258 $keys_only = $options->{ 'keys_only' };
5259 $invert = $options->{ 'invert' };
5261 if ( $options->{ 'patterns' } )
5263 $patterns = [ split ",", $options->{ 'patterns' } ];
5265 elsif ( -f $options->{ 'patterns_in' } )
5267 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5269 elsif ( $options->{ 'regex' } )
5271 if ( $options->{ 'case_insensitive' } ) {
5272 $regex = qr/$options->{ 'regex' }/i;
5274 $regex = qr/$options->{ 'regex' }/;
5277 elsif ( -f $options->{ 'exact_in' } )
5279 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5281 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5285 elsif ( $options->{ 'eval' } )
5287 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5295 while ( $record = get_record( $in ) )
5299 if ( %lookup_hash ) {
5300 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5301 } elsif ( $patterns ) {
5302 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5303 } elsif ( $regex ) {
5304 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5306 $found = grab_eval( $key, $op, $val, $record );
5309 if ( $found and not $invert ) {
5310 put_record( $record, $out );
5311 } elsif ( not $found and $invert ) {
5312 put_record( $record, $out );
5320 # Martin A. Hansen, August 2007.
5322 # Evaluate extression for records in stream.
5324 my ( $in, # handle to in stream
5325 $out, # handle to out stream
5326 $options, # options hash
5331 my ( $record, $eval_key, @keys, $eval_val );
5333 while ( $record = get_record( $in ) )
5335 if ( $options->{ "eval" } )
5337 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
5344 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5346 @keys = grep { exists $record->{ $_ } } @keys;
5349 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5351 $record->{ $eval_key } = eval "$eval_val";
5352 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
5356 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
5360 put_record( $record, $out );
5367 # Martin A. Hansen, June 2008.
5371 my ( $in, # handle to in stream
5372 $out, # handle to out stream
5373 $options, # options hash
5378 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5380 while ( $record = get_record( $in ) )
5384 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5386 push @rows, $record->{ $key };
5390 push @matrix, [ @rows ];
5395 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5397 foreach $row ( @matrix )
5399 for ( $i = 0; $i < @{ $row }; $i++ ) {
5400 $record->{ "V$i" } = $row->[ $i ];
5403 put_record( $record, $out );
5408 sub script_add_ident
5410 # Martin A. Hansen, May 2008.
5412 # Add a unique identifier to each record in stream.
5414 my ( $in, # handle to in stream
5415 $out, # handle to out stream
5416 $options, # options hash
5421 my ( $record, $key, $prefix, $i );
5423 $key = $options->{ "key" } || "ID";
5424 $prefix = $options->{ "prefix" } || "ID";
5428 while ( $record = get_record( $in ) )
5430 $record->{ $key } = sprintf( "$prefix%08d", $i );
5432 put_record( $record, $out );
5439 sub script_count_records
5441 # Martin A. Hansen, August 2007.
5443 # Count records in stream.
5445 my ( $in, # handle to in stream
5446 $out, # handle to out stream
5447 $options, # options hash
5452 my ( $record, $count, $result, $fh, $line );
5456 if ( $options->{ "no_stream" } )
5458 while ( $line = <$in> )
5462 $count++ if $line eq "---";
5467 while ( $record = get_record( $in ) )
5469 put_record( $record, $out );
5475 $result = { "RECORDS_COUNT" => $count };
5477 $fh = write_stream( $options->{ "data_out" } );
5479 put_record( $result, $fh );
5485 sub script_random_records
5487 # Martin A. Hansen, August 2007.
5489 # Pick a number or random records from stream.
5491 my ( $in, # handle to in stream
5492 $out, # handle to out stream
5493 $options, # options hash
5498 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5500 $options->{ "num" } ||= 10;
5502 $tmp_file = "$BP_TMP/random_records.tmp";
5504 $fh_out = Maasha::Common::write_open( $tmp_file );
5508 while ( $record = get_record( $in ) )
5510 put_record( $record, $fh_out );
5520 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5522 while ( $i < $options->{ "num" } )
5524 $rand = int( rand( $count ) );
5526 if ( not exists $rand_hash{ $rand } )
5528 $rand_hash{ $rand } = 1;
5530 $max = $rand if $rand > $max;
5536 $fh_in = Maasha::Common::read_open( $tmp_file );
5540 while ( $record = get_record( $fh_in ) )
5542 put_record( $record, $out ) if exists $rand_hash{ $count };
5544 last if $count == $max;
5555 sub script_sort_records
5557 # Martin A. Hansen, August 2007.
5559 # Sort to sort records according to keys.
5561 my ( $in, # handle to in stream
5562 $out, # handle to out stream
5563 $options, # options hash
5568 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5570 foreach $key ( @{ $options->{ "keys" } } )
5572 if ( $key =~ s/n$// ) {
5573 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5575 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5579 $sort_str = join " or ", @sort_cmd;
5580 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5582 while ( $record = get_record( $in ) ) {
5583 push @records, $record;
5586 @records = sort $sort_sub @records;
5588 if ( $options->{ "reverse" } )
5590 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5591 put_record( $records[ $i ], $out );
5596 for ( $i = 0; $i < scalar @records; $i++ ) {
5597 put_record( $records[ $i ], $out );
5603 sub script_count_vals
5605 # Martin A. Hansen, August 2007.
5607 # Count records in stream.
5609 my ( $in, # handle to in stream
5610 $out, # handle to out stream
5611 $options, # options hash
5616 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5618 $tmp_file = "$BP_TMP/count_cache.tmp";
5620 $fh_out = Maasha::Common::write_open( $tmp_file );
5625 while ( $record = get_record( $in ) )
5627 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5629 push @records, $record;
5631 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5633 map { put_record( $_, $fh_out ) } @records;
5640 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5651 $fh_in = Maasha::Common::read_open( $tmp_file );
5653 while ( $record = get_record( $fh_in ) )
5655 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5657 put_record( $record, $out );
5659 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5667 foreach $record ( @records )
5669 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5671 put_record( $record, $out );
5678 sub script_plot_histogram
5680 # Martin A. Hansen, September 2007.
5682 # Plot a simple histogram for a given key using GNU plot.
5684 my ( $in, # handle to in stream
5685 $out, # handle to out stream
5686 $options, # options hash
5691 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5693 $options->{ "title" } ||= "Histogram";
5694 $options->{ "sort" } ||= "num";
5696 while ( $record = get_record( $in ) )
5698 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5700 put_record( $record, $out ) if not $options->{ "no_stream" };
5703 if ( $options->{ "sort" } eq "num" ) {
5704 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5706 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5709 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5711 $fh = write_stream( $options->{ "data_out" } );
5713 print $fh "$_\n" foreach @{ $result };
5719 sub script_plot_lendist
5721 # Martin A. Hansen, August 2007.
5723 # Plot length distribution using GNU plot.
5725 my ( $in, # handle to in stream
5726 $out, # handle to out stream
5727 $options, # options hash
5732 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5734 $options->{ "title" } ||= "Length Distribution";
5736 while ( $record = get_record( $in ) )
5738 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5740 put_record( $record, $out ) if not $options->{ "no_stream" };
5743 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5745 for ( $i = 0; $i < $max; $i++ ) {
5746 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5749 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5751 $fh = write_stream( $options->{ "data_out" } );
5753 print $fh "$_\n" foreach @{ $result };
5759 sub script_plot_chrdist
5761 # Martin A. Hansen, August 2007.
5763 # Plot chromosome distribution using GNU plot.
5765 my ( $in, # handle to in stream
5766 $out, # handle to out stream
5767 $options, # options hash
5772 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5774 $options->{ "title" } ||= "Chromosome Distribution";
5776 while ( $record = get_record( $in ) )
5778 if ( $record->{ "CHR" } ) { # generic
5779 $data_hash{ $record->{ "CHR" } }++;
5780 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5781 $data_hash{ $record->{ "S_ID" } }++;
5782 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5783 $data_hash{ $record->{ "S_ID" } }++;
5784 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5785 $data_hash{ $record->{ "S_ID" } }++;
5788 put_record( $record, $out ) if not $options->{ "no_stream" };
5791 foreach $elem ( keys %data_hash )
5795 $sort_key =~ s/chr//i;
5797 $sort_key =~ s/^X(.*)/99$1/;
5798 $sort_key =~ s/^Y(.*)/99$1/;
5799 $sort_key =~ s/^Z(.*)/999$1/;
5800 $sort_key =~ s/^M(.*)/9999$1/;
5801 $sort_key =~ s/^U(.*)/99999$1/;
5803 $count = $sort_key =~ tr/_//;
5805 $sort_key =~ s/_.*/"999999" x $count/ex;
5807 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5810 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5812 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5814 $fh = write_stream( $options->{ "data_out" } );
5816 print $fh "$_\n" foreach @{ $result };
5822 sub script_plot_karyogram
5824 # Martin A. Hansen, August 2007.
5826 # Plot hits on karyogram.
5828 my ( $in, # handle to in stream
5829 $out, # handle to out stream
5830 $options, # options hash
5835 my ( %options, $record, @data, $fh, $result, %data_hash );
5837 $options->{ "genome" } ||= "human";
5838 $options->{ "feat_color" } ||= "black";
5840 while ( $record = get_record( $in ) )
5842 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5844 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5847 put_record( $record, $out ) if not $options->{ "no_stream" };
5850 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5852 $fh = write_stream( $options->{ "data_out" } );
5860 sub script_plot_matches
5862 # Martin A. Hansen, August 2007.
5864 # Plot matches in 2D generating a dotplot.
5866 my ( $in, # handle to in stream
5867 $out, # handle to out stream
5868 $options, # options hash
5873 my ( $record, @data, $fh, $result, %data_hash );
5875 $options->{ "direction" } ||= "both";
5877 while ( $record = get_record( $in ) )
5879 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5880 push @data, $record;
5883 put_record( $record, $out ) if not $options->{ "no_stream" };
5886 $options->{ "title" } ||= "plot_matches";
5887 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5888 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5890 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5892 $fh = write_stream( $options->{ "data_out" } );
5894 print $fh "$_\n" foreach @{ $result };
5900 sub script_length_vals
5902 # Martin A. Hansen, August 2007.
5904 # Determine the length of the value for given keys.
5906 my ( $in, # handle to in stream
5907 $out, # handle to out stream
5908 $options, # options hash
5913 my ( $record, $key );
5915 while ( $record = get_record( $in ) )
5917 foreach $key ( @{ $options->{ "keys" } } )
5919 if ( $record->{ $key } ) {
5920 $record->{ $key . "_LEN" } = length $record->{ $key };
5924 put_record( $record, $out );
5931 # Martin A. Hansen, August 2007.
5933 # Calculates the sums for values of given keys.
5935 my ( $in, # handle to in stream
5936 $out, # handle to out stream
5937 $options, # options hash
5942 my ( $record, $key, %sum_hash, $fh );
5944 while ( $record = get_record( $in ) )
5946 foreach $key ( @{ $options->{ "keys" } } )
5948 if ( $record->{ $key } ) {
5949 $sum_hash{ $key } += $record->{ $key };
5953 put_record( $record, $out ) if not $options->{ "no_stream" };
5956 $fh = write_stream( $options->{ "data_out" } );
5958 foreach $key ( @{ $options->{ "keys" } } ) {
5959 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5966 sub script_mean_vals
5968 # Martin A. Hansen, August 2007.
5970 # Calculate the mean of values of given keys.
5972 my ( $in, # handle to in stream
5973 $out, # handle to out stream
5974 $options, # options hash
5979 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5981 while ( $record = get_record( $in ) )
5983 foreach $key ( @{ $options->{ "keys" } } )
5985 if ( $record->{ $key } )
5987 $sum_hash{ $key } += $record->{ $key };
5988 $count_hash{ $key }++;
5992 put_record( $record, $out ) if not $options->{ "no_stream" };
5995 $fh = write_stream( $options->{ "data_out" } );
5997 foreach $key ( @{ $options->{ "keys" } } )
5999 if ( $count_hash{ $key } ) {
6000 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
6005 put_record( { $key . "_MEAN" => $mean } , $fh );
6012 sub script_median_vals
6014 # Martin A. Hansen, March 2008.
6016 # Calculate the median values of given keys.
6018 my ( $in, # handle to in stream
6019 $out, # handle to out stream
6020 $options, # options hash
6025 my ( $record, $key, %median_hash, $median, $fh );
6027 while ( $record = get_record( $in ) )
6029 foreach $key ( @{ $options->{ "keys" } } ) {
6030 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6033 put_record( $record, $out ) if not $options->{ "no_stream" };
6036 $fh = write_stream( $options->{ "data_out" } );
6038 foreach $key ( @{ $options->{ "keys" } } )
6040 if ( $median_hash{ $key } ) {
6041 $median = Maasha::Calc::median( $median_hash{ $key } );
6046 put_record( { $key . "_MEDIAN" => $median } , $fh );
6055 # Martin A. Hansen, February 2008.
6057 # Determine the maximum values of given keys.
6059 my ( $in, # handle to in stream
6060 $out, # handle to out stream
6061 $options, # options hash
6066 my ( $record, $key, $fh, %max_hash, $max_record );
6068 while ( $record = get_record( $in ) )
6070 foreach $key ( @{ $options->{ "keys" } } )
6072 if ( $record->{ $key } )
6074 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6078 put_record( $record, $out ) if not $options->{ "no_stream" };
6081 $fh = write_stream( $options->{ "data_out" } );
6083 foreach $key ( @{ $options->{ "keys" } } )
6085 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6088 put_record( $max_record, $fh );
6096 # Martin A. Hansen, February 2008.
6098 # Determine the minimum values of given keys.
6100 my ( $in, # handle to in stream
6101 $out, # handle to out stream
6102 $options, # options hash
6107 my ( $record, $key, $fh, %min_hash, $min_record );
6109 while ( $record = get_record( $in ) )
6111 foreach $key ( @{ $options->{ "keys" } } )
6113 if ( defined $record->{ $key } )
6115 if ( exists $min_hash{ $key } ) {
6116 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6118 $min_hash{ $key } = $record->{ $key };
6123 put_record( $record, $out ) if not $options->{ "no_stream" };
6126 $fh = write_stream( $options->{ "data_out" } );
6128 foreach $key ( @{ $options->{ "keys" } } )
6130 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6133 put_record( $min_record, $fh );
6139 sub script_upload_to_ucsc
6141 # Martin A. Hansen, August 2007.
6143 # Calculate the mean of values of given keys.
6145 # This routine has developed into the most ugly hack. Do something!
6147 my ( $in, # handle to in stream
6148 $out, # handle to out stream
6149 $options, # options hash
6154 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
6156 $options->{ "short_label" } ||= $options->{ 'table' };
6157 $options->{ "long_label" } ||= $options->{ 'table' };
6158 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6159 $options->{ "priority" } ||= 1;
6160 $options->{ "visibility" } ||= "pack";
6161 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6162 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6164 $file = "$BP_TMP/ucsc_upload.tmp";
6169 $fh_out = Maasha::Common::write_open( $file );
6171 while ( $record = get_record( $in ) )
6173 put_record( $record, $out ) if not $options->{ "no_stream" };
6175 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6179 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
6180 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
6183 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6187 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6188 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6192 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6194 # chrom chromStart chromEnd name score strand size secStr conf
6198 print $fh_out join ( "\t",
6200 $record->{ "CHR_BEG" },
6201 $record->{ "CHR_END" } + 1,
6202 $record->{ "Q_ID" },
6203 $record->{ "SCORE" },
6204 $record->{ "STRAND" },
6205 $record->{ "SIZE" },
6206 $record->{ "SEC_STRUCT" },
6207 $record->{ "CONF" },
6210 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6213 $columns = $record->{ "BED_COLS" };
6215 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6216 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6219 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6224 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6225 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6228 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6233 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6235 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6236 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6239 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6244 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6245 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
6249 if ( $i == $options->{ "chunk_size" } )
6253 if ( $format eq "BED" ) {
6254 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6255 } elsif ( $format eq "PSL" ) {
6256 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6265 $fh_out = Maasha::Common::write_open( $file );
6273 if ( exists $options->{ "database" } and $options->{ "table" } )
6275 if ( $format eq "BED" )
6277 $type = "bed $columns";
6279 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6281 elsif ( $format eq "BED_SS" )
6283 $type = "type bed 6 +";
6285 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6287 elsif ( $format eq "PSL" )
6291 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6293 elsif ( $format eq "WIGGLE" )
6295 $options->{ "visibility" } = "full";
6297 $wig_file = "$options->{ 'table' }.wig";
6298 $wib_file = "$options->{ 'table' }.wib";
6300 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6302 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6304 if ( $options->{ 'verbose' } ) {
6305 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6307 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6310 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6318 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6323 Maasha::UCSC::ucsc_update_config( $options, $type );
6328 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6333 # Martin A. Hansen, November 2009.
6335 # Uses keys from a lookup hash to search records. Optionally, a list of
6336 # keys can be given so the lookup is limited to these, also, flags
6337 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6338 # succeeded, else 0.
6340 my ( $lookup_hash, # hashref with patterns
6342 $keys, # list of keys - OPTIONAL
6343 $vals_only, # only vals flag - OPTIONAL
6344 $keys_only, # only keys flag - OPTIONAL
6351 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6355 if ( not $vals_only ) {
6356 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6359 if ( not $keys_only ) {
6360 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6370 # Martin A. Hansen, November 2009.
6372 # Uses patterns to match records containing the pattern as a substring.
6373 # Returns 1 if the record is matched, else 0.
6375 my ( $patterns, # list of patterns
6377 $keys, # list of keys - OPTIONAL
6378 $vals_only, # only vals flag - OPTIONAL
6379 $keys_only, # only keys flag - OPTIONAL
6386 foreach $pattern ( @{ $patterns } )
6390 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6394 if ( not $vals_only ) {
6395 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6398 if ( not $keys_only ) {
6399 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6410 # Martin A. Hansen, November 2009.
6412 # Uses regex to match records.
6413 # Returns 1 if the record is matched, else 0.
6415 my ( $regex, # regex to match
6417 $keys, # list of keys - OPTIONAL
6418 $vals_only, # only vals flag - OPTIONAL
6419 $keys_only, # only keys flag - OPTIONAL
6426 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6430 if ( not $vals_only ) {
6431 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6434 if ( not $keys_only ) {
6435 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6445 # Martin A. Hansen, November 2009.
6447 # Test if the value of a given record key evaluates according
6448 # to a given operator. Returns 1 if eval is OK, else 0.
6450 my ( $key, # record key
6458 if ( defined $record->{ $key } )
6460 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6461 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6462 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6463 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6464 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6465 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6466 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6467 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6476 # Martin A. Hansen, July 2007.
6478 # Opens a stream to STDIN or a file,
6480 my ( $path, # path - OPTIONAL
6483 # Returns filehandle.
6487 if ( not -t STDIN ) {
6488 $fh = Maasha::Common::read_stdin();
6489 } elsif ( not $path ) {
6490 # Maasha::Common::error( qq(no data stream) );
6492 $fh = Maasha::Common::read_open( $path );
6495 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6503 # Martin A. Hansen, August 2007.
6505 # Opens a stream to STDOUT or a file.
6507 my ( $path, # path - OPTIONAL
6508 $gzip, # compress data - OPTIONAL
6511 # Returns filehandle.
6516 $fh = Maasha::Common::write_open( $path, $gzip );
6518 $fh = Maasha::Common::write_stdout();
6527 # Martin A. Hansen, July 2007.
6529 # Reads one record at a time and converts that record
6530 # to a Perl data structure (a hash) which is returned.
6532 my ( $fh, # handle to stream
6537 my ( $block, @lines, $line, $key, $value, %record );
6539 local $/ = "\n---\n";
6545 return if not defined $block;
6547 @lines = split "\n", $block;
6549 foreach $line ( @lines )
6551 ( $key, $value ) = split ": ", $line, 2;
6553 $record{ $key } = $value;
6556 return wantarray ? %record : \%record;
6562 # Martin A. Hansen, July 2007.
6564 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6566 my ( $data, # data structure
6567 $fh, # file handle - OPTIONAL
6572 if ( scalar keys %{ $data } )
6576 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6581 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6592 # Martin A. Hansen, November 2007.
6594 # Extracts files from an explicit GetOpt::Long argument
6595 # allowing for the use of glob. E.g.
6596 # --data_in=test.fna
6597 # --data_in=test.fna,test2.fna
6599 # --data_in=test.fna,/dir/*.fna
6601 my ( $option, # option from GetOpt::Long
6606 my ( $elem, @files );
6608 foreach $elem ( split ",", $option )
6612 } elsif ( $elem =~ /\*/ ) {
6613 push @files, glob( $elem );
6617 return wantarray ? @files : \@files;
6623 # Martin A. Hansen, April 2008.
6625 # Removes temporary directory and exits gracefully.
6626 # This subroutine is meant to be run always as the last
6627 # thing even if a script is dies or is interrupted
6630 my ( $sig, # signal from the %SIG
6633 # print STDERR "signal->$sig<-\n";
6641 if ( $sig =~ /MAASHA_ERROR/ ) {
6642 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6643 } elsif ( $sig eq "INT" ) {
6644 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6645 } elsif ( $sig eq "TERM" ) {
6646 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6648 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6660 # Martin A. Hansen, July 2008.
6662 # Cleans out any unused temporary files and directories in BP_TMP.
6666 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6668 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6670 $curr_pid = Maasha::Common::get_processid();
6672 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6674 foreach $dir ( @dirs )
6676 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6682 next if $user eq "m.hansen";
6684 if ( $user eq Maasha::Common::get_user() )
6686 if ( not Maasha::Common::process_running( $pid ) )
6688 # print STDERR "Removing stale dir: $dir\n";
6689 Maasha::Common::dir_remove( $dir );
6691 elsif ( $pid == $curr_pid )
6693 # print STDERR "Removing current dir: $dir\n";
6694 Maasha::Common::dir_remove( $dir );
6708 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<