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 );
44 use Maasha::Stockholm;
58 use vars qw( @ISA @EXPORT_OK );
62 @ISA = qw( Exporter );
78 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
81 $SIG{ '__DIE__' } = \&sig_handler;
82 $SIG{ 'INT' } = \&sig_handler;
83 $SIG{ 'TERM' } = \&sig_handler;
86 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
89 my ( $script, $BP_TMP );
91 $script = Maasha::Common::get_scriptname();
92 $BP_TMP = Maasha::Common::get_tmpdir();
95 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
98 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
99 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
101 $log_global->autoflush( 1 );
102 $log_local->autoflush( 1 );
104 &log( $log_global, $script, \@ARGV );
105 &log( $log_local, $script, \@ARGV );
111 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114 my $t0 = gettimeofday();
116 run_script( $script );
118 my $t1 = gettimeofday();
120 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
123 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128 # Martin A. Hansen, January 2008.
130 # Log messages to logfile.
132 my ( $fh, # filehandle to logfile
133 $script, # script name
134 $argv, # reference to @ARGV
139 my ( $time_stamp, $user );
141 $time_stamp = Maasha::Common::time_stamp();
143 $user = $ENV{ 'USER' };
145 $script = "biopieces" if $script eq "-e";
147 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
153 # Martin A. Hansen, August 2007.
155 # Run a specific script.
157 my ( $script, # script name
162 my ( $options, $in, $out );
164 $options = get_options( $script );
166 $options->{ "SCRIPT" } = $script;
168 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
169 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
172 $in = read_stream( $options->{ "stream_in" } );
173 $out = write_stream( $options->{ "stream_out" } );
175 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
176 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
177 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
178 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
179 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
181 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
182 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
183 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
184 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
185 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
186 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
187 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
188 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
189 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
190 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
191 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
192 elsif ( $script eq "read_mysql" ) { script_read_mysql( $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 "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
204 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
205 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
206 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
207 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
208 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
209 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
212 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
213 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
214 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
215 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
216 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
217 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
218 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
219 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
220 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
221 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
222 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
223 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
224 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
225 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
226 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
227 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
228 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
229 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
230 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
231 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
232 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
233 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
234 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
235 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
236 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
237 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
238 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
239 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
240 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
241 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
242 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
243 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
244 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
245 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
246 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
247 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
248 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
249 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
250 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
251 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
252 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
253 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
254 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
255 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
256 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
257 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
258 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
259 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
260 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
261 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
262 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
263 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
264 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
265 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
267 close $in if defined $in;
274 # Martin A. Hansen, February 2008.
276 # Gets options from commandline and checks these vigerously.
278 my ( $script, # name of script
283 my ( %options, @options, $opt, @genomes );
285 if ( $script eq "print_usage" )
291 elsif ( $script eq "read_fasta" )
298 elsif ( $script eq "read_tab" )
309 elsif ( $script eq "read_psl" )
316 elsif ( $script eq "read_bed" )
323 elsif ( $script eq "read_fixedstep" )
330 elsif ( $script eq "read_blast_tab" )
337 elsif ( $script eq "read_embl" )
347 elsif ( $script eq "read_stockholm" )
354 elsif ( $script eq "read_phastcons" )
365 elsif ( $script eq "read_soft" )
373 elsif ( $script eq "read_gff" )
380 elsif ( $script eq "read_2bit" )
388 elsif ( $script eq "read_solexa" )
396 elsif ( $script eq "read_solid" )
404 elsif ( $script eq "read_mysql" )
413 elsif ( $script eq "format_genome" )
422 elsif ( $script eq "length_seq" )
429 elsif ( $script eq "oligo_freq" )
436 elsif ( $script eq "create_weight_matrix" )
442 elsif ( $script eq "transliterate_seq" )
450 elsif ( $script eq "transliterate_vals" )
459 elsif ( $script eq "translate_seq" )
465 elsif ( $script eq "extract_seq" )
473 elsif ( $script eq "get_genome_seq" )
485 elsif ( $script eq "get_genome_align" )
496 elsif ( $script eq "get_genome_phastcons" )
507 elsif ( $script eq "split_seq" )
514 elsif ( $script eq "split_bed" )
521 elsif ( $script eq "tile_seq" )
528 elsif ( $script eq "invert_align" )
534 elsif ( $script eq "patscan_seq" )
545 elsif ( $script eq "create_blast_db" )
552 elsif ( $script eq "blast_seq" )
564 elsif ( $script eq "blat_seq" )
576 elsif ( $script eq "soap_seq" )
587 elsif ( $script eq "match_seq" )
594 elsif ( $script eq "create_vmatch_index" )
602 elsif ( $script eq "vmatch_seq" )
613 elsif ( $script eq "write_fasta" )
622 elsif ( $script eq "write_align" )
632 elsif ( $script eq "write_blast" )
641 elsif ( $script eq "write_tab" )
653 elsif ( $script eq "write_bed" )
661 elsif ( $script eq "write_psl" )
669 elsif ( $script eq "write_fixedstep" )
677 elsif ( $script eq "write_2bit" )
685 elsif ( $script eq "write_solid" )
694 elsif ( $script eq "plot_seqlogo" )
701 elsif ( $script eq "plot_phastcons_profiles" )
716 elsif ( $script eq "analyze_vals" )
723 elsif ( $script eq "head_records" )
729 elsif ( $script eq "remove_keys" )
736 elsif ( $script eq "rename_keys" )
742 elsif ( $script eq "uniq_vals" )
749 elsif ( $script eq "merge_vals" )
756 elsif ( $script eq "merge_records" )
763 elsif ( $script eq "grab" )
778 elsif ( $script eq "compute" )
784 elsif ( $script eq "add_ident" )
791 elsif ( $script eq "count_records" )
798 elsif ( $script eq "random_records" )
804 elsif ( $script eq "sort_records" )
811 elsif ( $script eq "count_vals" )
817 elsif ( $script eq "plot_histogram" )
830 elsif ( $script eq "plot_lendist" )
842 elsif ( $script eq "plot_chrdist" )
853 elsif ( $script eq "plot_karyogram" )
862 elsif ( $script eq "plot_matches" )
874 elsif ( $script eq "length_vals" )
880 elsif ( $script eq "sum_vals" )
888 elsif ( $script eq "mean_vals" )
896 elsif ( $script eq "median_vals" )
904 elsif ( $script eq "max_vals" )
912 elsif ( $script eq "min_vals" )
920 elsif ( $script eq "upload_to_ucsc" )
947 # print STDERR Dumper( \@options );
954 # print STDERR Dumper( \%options );
956 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
957 return wantarray ? %options : \%options;
960 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
961 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
962 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
963 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
964 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
965 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
966 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
967 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
968 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
970 # ---- check arguments ----
972 if ( $options{ 'data_in' } )
974 $options{ "files" } = getopt_files( $options{ 'data_in' } );
976 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
979 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
981 # print STDERR Dumper( \%options );
983 foreach $opt ( keys %options )
985 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
987 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
989 elsif ( $opt =~ /beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|num|skip|cpus|window_size|step_size/ and $options{ $opt } !~ /^\d+$/ )
991 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
993 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
995 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
997 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
999 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1001 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1003 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1005 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1007 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1009 elsif ( $opt eq "genome" and $script ne "format_genome" )
1011 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1012 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1014 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1015 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1018 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1020 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1022 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1024 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1026 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1028 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1032 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1033 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1034 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1035 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1036 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1037 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1038 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1039 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1040 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" };
1041 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1042 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" };
1044 if ( $script eq "upload_to_ucsc" )
1046 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1047 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1050 return wantarray ? %options : \%options;
1054 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1057 sub script_print_usage
1059 # Martin A. Hansen, January 2008.
1061 # Retrieves usage information from file and
1062 # prints this nicely formatted.
1064 my ( $in, # handle to in stream
1065 $out, # handle to out stream
1066 $options, # options hash
1071 my ( $file, $wiki, $lines );
1073 if ( $options->{ 'data_in' } ) {
1074 $file = $options->{ 'data_in' };
1076 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1079 $wiki = Maasha::Gwiki::gwiki_read( $file );
1081 if ( not $options->{ "help" } ) {
1082 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1085 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1087 print STDERR "$_\n" foreach @{ $lines };
1093 sub script_list_biopieces
1095 # Martin A. Hansen, January 2008.
1097 # Prints the synopsis from the usage for each of the biopieces.
1099 my ( $in, # handle to in stream
1100 $out, # handle to out stream
1101 $options, # options hash
1106 my ( @files, $file, $wiki, $program, $synopsis );
1108 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1110 foreach $file ( sort @files )
1112 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1116 $wiki = Maasha::Gwiki::gwiki_read( $file );
1118 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1119 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1121 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1122 $synopsis =~ s/!(\w)/$1/g;
1124 printf( "%-30s%s\n", $program, $synopsis );
1132 sub script_list_genomes
1134 # Martin A. Hansen, January 2008.
1136 # Prints the synopsis from the usage for each of the biopieces.
1138 my ( $in, # handle to in stream
1139 $out, # handle to out stream
1140 $options, # options hash
1145 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1147 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1149 foreach $genome ( @genomes )
1151 next if $genome =~ /\.$/;
1153 @formats = Maasha::Common::ls_dirs( $genome );
1155 foreach $format ( @formats )
1157 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1159 $hash{ $1 }{ $2 } = 1;
1168 map { push @row, $_ } sort keys %found;
1170 print join( "\t", @row ), "\n";
1172 foreach $genome ( sort keys %hash )
1176 foreach $format ( sort keys %found )
1178 if ( exists $hash{ $genome }{ $format } ) {
1185 print join( "\t", @row ), "\n";
1190 sub script_read_fasta
1192 # Martin A. Hansen, August 2007.
1194 # Read sequences from FASTA file.
1196 my ( $in, # handle to in stream
1197 $out, # handle to out stream
1198 $options, # options hash
1203 my ( $record, $file, $data_in, $entry, $num );
1205 while ( $record = get_record( $in ) ) {
1206 put_record( $record, $out );
1211 foreach $file ( @{ $options->{ "files" } } )
1213 $data_in = Maasha::Common::read_open( $file );
1215 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1217 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1220 SEQ_NAME => $entry->[ SEQ_NAME ],
1221 SEQ => $entry->[ SEQ ],
1222 SEQ_LEN => length $entry->[ SEQ ],
1225 put_record( $record, $out );
1228 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1238 close $data_in if $data_in;
1244 # Martin A. Hansen, August 2007.
1246 # Read table or table columns from stream or file.
1248 my ( $in, # handle to in stream
1249 $out, # handle to out stream
1250 $options, # options hash
1255 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1257 $options->{ 'delimit' } ||= '\s+';
1259 while ( $record = get_record( $in ) ) {
1260 put_record( $record, $out );
1263 $skip = $options->{ 'skip' } ||= 0;
1266 foreach $file ( @{ $options->{ "files" } } )
1268 $data_in = Maasha::Common::read_open( $file );
1270 while ( $line = <$data_in> )
1278 next if $line =~ /^#|^$/;
1285 @fields = split /$options->{'delimit'}/, $line;
1287 if ( $options->{ "cols" } ) {
1288 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1293 for ( $i = 0; $i < @fields2; $i++ )
1295 if ( $options->{ "keys" }->[ $i ] ) {
1296 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1298 $record->{ "V" . $i } = $fields2[ $i ];
1302 put_record( $record, $out );
1304 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1314 close $data_in if $data_in;
1320 # Martin A. Hansen, August 2007.
1322 # Read psl table from stream or file.
1324 my ( $in, # handle to in stream
1325 $out, # handle to out stream
1326 $options, # options hash
1331 my ( $record, @files, $file, $entries, $entry, $num );
1333 while ( $record = get_record( $in ) ) {
1334 put_record( $record, $out );
1339 foreach $file ( @{ $options->{ "files" } } )
1341 $entries = Maasha::UCSC::psl_get_entries( $file );
1343 foreach $entry ( @{ $entries } )
1345 put_record( $entry, $out );
1347 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1359 # Martin A. Hansen, August 2007.
1361 # Read bed table from stream or file.
1363 my ( $in, # handle to in stream
1364 $out, # handle to out stream
1365 $options, # options hash
1370 my ( $file, $record, $entry, $data_in, $num );
1372 while ( $record = get_record( $in ) ) {
1373 put_record( $record, $out );
1378 foreach $file ( @{ $options->{ "files" } } )
1380 $data_in = Maasha::Common::read_open( $file );
1382 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1384 put_record( $entry, $out );
1386 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1396 close $data_in if $data_in;
1400 sub script_read_fixedstep
1402 # Martin A. Hansen, Juli 2008.
1404 # Read fixedStep wiggle format from stream or file.
1406 my ( $in, # handle to in stream
1407 $out, # handle to out stream
1408 $options, # options hash
1413 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $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 ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1427 $head = shift @{ $entry };
1429 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1431 $record->{ "CHR" } = $1;
1432 $record->{ "CHR_BEG" } = $2;
1433 $record->{ "STEP" } = $3;
1434 $record->{ "VALS" } = join ",", @{ $entry };
1437 put_record( $record, $out );
1439 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1449 close $data_in if $data_in;
1453 sub script_read_blast_tab
1455 # Martin A. Hansen, September 2007.
1457 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1459 my ( $in, # handle to in stream
1460 $out, # handle to out stream
1461 $options, # options hash
1466 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1468 while ( $record = get_record( $in ) ) {
1469 put_record( $record, $out );
1474 foreach $file ( @{ $options->{ "files" } } )
1476 $data_in = Maasha::Common::read_open( $file );
1478 while ( $line = <$data_in> )
1482 next if $line =~ /^#/;
1484 @fields = split /\t/, $line;
1486 $record->{ "REC_TYPE" } = "BLAST";
1487 $record->{ "Q_ID" } = $fields[ 0 ];
1488 $record->{ "S_ID" } = $fields[ 1 ];
1489 $record->{ "IDENT" } = $fields[ 2 ];
1490 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1491 $record->{ "MISMATCHES" } = $fields[ 4 ];
1492 $record->{ "GAPS" } = $fields[ 5 ];
1493 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1494 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1495 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1496 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1497 $record->{ "E_VAL" } = $fields[ 10 ];
1498 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1500 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1502 $record->{ "STRAND" } = '-';
1504 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1508 $record->{ "STRAND" } = '+';
1511 put_record( $record, $out );
1513 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1523 close $data_in if $data_in;
1527 sub script_read_embl
1529 # Martin A. Hansen, August 2007.
1533 my ( $in, # handle to in stream
1534 $out, # handle to out stream
1535 $options, # options hash
1540 my ( %options2, $file, $data_in, $num, $entry, $record );
1542 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1543 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1544 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
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 ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1558 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1560 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1562 $record_copy = dclone $record;
1564 delete $record_copy->{ "FT" };
1566 put_record( $record_copy, $out );
1568 delete $record_copy->{ "SEQ" };
1570 foreach $feat ( keys %{ $record->{ "FT" } } )
1572 $record_copy->{ "FEAT_TYPE" } = $feat;
1574 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1576 foreach $qual ( keys %{ $feat2 } )
1578 $qual_val = join "; ", @{ $feat2->{ $qual } };
1583 $record_copy->{ $qual } = $qual_val;
1586 put_record( $record_copy, $out );
1590 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1600 close $data_in if $data_in;
1604 sub script_read_stockholm
1606 # Martin A. Hansen, August 2007.
1608 # Read Stockholm format.
1610 my ( $in, # handle to in stream
1611 $out, # handle to out stream
1612 $options, # options hash
1617 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1619 while ( $record = get_record( $in ) ) {
1620 put_record( $record, $out );
1625 foreach $file ( @{ $options->{ "files" } } )
1627 $data_in = Maasha::Common::read_open( $file );
1629 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1631 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1635 foreach $key ( keys %{ $record->{ "GF" } } ) {
1636 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1639 $record_anno->{ "ALIGN" } = $num;
1641 put_record( $record_anno, $out );
1643 foreach $seq ( @{ $record->{ "ALIGN" } } )
1645 undef $record_align;
1648 SEQ_NAME => $seq->[ 0 ],
1652 put_record( $record_align, $out );
1655 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1665 close $data_in if $data_in;
1669 sub script_read_phastcons
1671 # Martin A. Hansen, December 2007.
1673 # Read PhastCons format.
1675 my ( $in, # handle to in stream
1676 $out, # handle to out stream
1677 $options, # options hash
1682 my ( $data_in, $file, $num, $entry, @records, $record );
1684 $options->{ "min" } ||= 10;
1685 $options->{ "dist" } ||= 25;
1686 $options->{ "threshold" } ||= 0.8;
1687 $options->{ "gap" } ||= 5;
1689 while ( $record = get_record( $in ) ) {
1690 put_record( $record, $out );
1695 foreach $file ( @{ $options->{ "files" } } )
1697 $data_in = Maasha::Common::read_open( $file );
1699 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1701 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1703 foreach $record ( @records )
1705 $record->{ "REC_TYPE" } = "BED";
1706 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1708 put_record( $record, $out );
1710 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1721 close $data_in if $data_in;
1725 sub script_read_soft
1727 # Martin A. Hansen, December 2007.
1730 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1732 my ( $in, # handle to in stream
1733 $out, # handle to out stream
1734 $options, # options hash
1739 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1741 while ( $record = get_record( $in ) ) {
1742 put_record( $record, $out );
1747 foreach $file ( @{ $options->{ "files" } } )
1749 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1751 $soft_index = Maasha::NCBI::soft_index_file( $file );
1753 $fh = Maasha::Common::read_open( $file );
1755 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1757 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1759 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1761 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1763 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1765 foreach $sample ( @samples )
1768 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1770 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1772 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1774 foreach $record ( @{ $records } )
1776 put_record( $record, $out );
1778 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1783 $old_end = $sample->{ "LINE_END" };
1791 close $data_in if $data_in;
1798 # Martin A. Hansen, February 2008.
1801 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1803 my ( $in, # handle to in stream
1804 $out, # handle to out stream
1805 $options, # options hash
1810 my ( $data_in, $file, $fh, $num, $record, $entry );
1812 while ( $record = get_record( $in ) ) {
1813 put_record( $record, $out );
1818 foreach $file ( @{ $options->{ "files" } } )
1820 $fh = Maasha::Common::read_open( $file );
1822 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1824 put_record( $entry, $out );
1826 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1836 close $data_in if $data_in;
1840 sub script_read_2bit
1842 # Martin A. Hansen, March 2008.
1844 # Read sequences from 2bit file.
1846 my ( $in, # handle to in stream
1847 $out, # handle to out stream
1848 $options, # options hash
1853 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1855 $mask = 1 if not $options->{ "no_mask" };
1857 while ( $record = get_record( $in ) ) {
1858 put_record( $record, $out );
1863 foreach $file ( @{ $options->{ "files" } } )
1865 $data_in = Maasha::Common::read_open( $file );
1867 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1869 foreach $line ( @{ $toc } )
1871 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1872 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1873 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1875 put_record( $record, $out );
1877 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1887 close $data_in if $data_in;
1891 sub script_read_solexa
1893 # Martin A. Hansen, March 2008.
1895 # Read Solexa sequence reads from file.
1897 my ( $in, # handle to in stream
1898 $out, # handle to out stream
1899 $options, # options hash
1904 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1906 $options->{ "quality" } ||= 20;
1908 while ( $record = get_record( $in ) ) {
1909 put_record( $record, $out );
1914 foreach $file ( @{ $options->{ "files" } } )
1916 $data_in = Maasha::Common::read_open( $file );
1918 while ( $entry = Maasha::Solexa::solexa_get_entry( $data_in ) )
1920 @seqs = split //, $entry->[ SEQ ];
1921 @scores = split /:/, $entry->[ SCORE ];
1923 for ( $i = 0; $i < @scores; $i++ ) {
1924 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1927 $record->{ "SEQ_NAME" } = $entry->[ SEQ_NAME ];
1928 $record->{ "SEQ" } = join "", @seqs;
1929 $record->{ "SEQ_LEN" } = scalar @seqs;
1930 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1932 put_record( $record, $out );
1934 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1944 close $data_in if $data_in;
1948 sub script_read_solid
1950 # Martin A. Hansen, April 2008.
1952 # Read Solid sequence from file.
1954 my ( $in, # handle to in stream
1955 $out, # handle to out stream
1956 $options, # options hash
1961 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1963 $options->{ "quality" } ||= 15;
1965 while ( $record = get_record( $in ) ) {
1966 put_record( $record, $out );
1971 foreach $file ( @{ $options->{ "files" } } )
1973 $data_in = Maasha::Common::read_open( $file );
1975 while ( $line = <$data_in> )
1979 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1981 @scores = split /,/, $seq_qual;
1982 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1984 for ( $i = 0; $i < @seqs; $i++ ) {
1985 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1989 SEQ_NAME => $seq_name,
1991 SEQ_QUAL => $seq_qual,
1992 SEQ_LEN => length $seq_cs,
1993 SEQ => join( "", @seqs ),
1994 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1997 put_record( $record, $out );
1999 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2009 close $data_in if $data_in;
2013 sub script_read_mysql
2015 # Martin A. Hansen, May 2008.
2017 # Read a MySQL query into stream.
2019 my ( $in, # handle to in stream
2020 $out, # handle to out stream
2021 $options, # options hash
2026 my ( $record, $dbh, $results );
2028 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2029 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2031 while ( $record = get_record( $in ) ) {
2032 put_record( $record, $out );
2035 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2037 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2039 Maasha::SQL::disconnect( $dbh );
2041 map { put_record( $_ ) } @{ $results };
2045 sub script_format_genome
2047 # Martin A. Hansen, Juli 2008.
2049 # Format a genome to speficed formats.
2051 my ( $in, # handle to in stream
2052 $out, # handle to out stream
2053 $options, # options hash
2058 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2060 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2061 $genome = $options->{ 'genome' };
2063 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2064 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2065 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2067 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2069 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2071 $fasta_dir = "$dir/genomes/$genome/fasta";
2075 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2077 $fasta_dir = "$dir/genomes/$genome/fasta";
2079 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2082 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2084 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2086 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2088 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2091 while ( $record = get_record( $in ) )
2093 if ( $fh_out and $entry = record2fasta( $record ) )
2095 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2097 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2099 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2101 $vals = $record->{ 'VALS' };
2105 print $fh_out "$vals\n";
2108 put_record( $record, $out ) if not $options->{ "no_stream" };
2111 foreach $format ( @{ $options->{ 'formats' } } )
2113 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2114 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2115 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2116 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2117 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2120 close $fh_out if $fh_out;
2124 sub script_length_seq
2126 # Martin A. Hansen, August 2007.
2128 # Determine the length of sequences in stream.
2130 my ( $in, # handle to in stream
2131 $out, # handle to out stream
2132 $options, # options hash
2137 my ( $record, $total );
2139 while ( $record = get_record( $in ) )
2141 if ( $record->{ "SEQ" } )
2143 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2144 $total += $record->{ "SEQ_LEN" };
2147 put_record( $record, $out ) if not $options->{ "no_stream" };
2150 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2154 sub script_uppercase_seq
2156 # Martin A. Hansen, August 2007.
2158 # Uppercases sequences in stream.
2160 my ( $in, # handle to in stream
2161 $out, # handle to out stream
2168 while ( $record = get_record( $in ) )
2170 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2172 put_record( $record, $out );
2177 sub script_shuffle_seq
2179 # Martin A. Hansen, December 2007.
2181 # Shuffle sequences in stream.
2183 my ( $in, # handle to in stream
2184 $out, # handle to out stream
2191 while ( $record = get_record( $in ) )
2193 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2195 put_record( $record, $out );
2200 sub script_analyze_seq
2202 # Martin A. Hansen, August 2007.
2204 # Analyze sequence composition of sequences in stream.
2206 my ( $in, # handle to in stream
2207 $out, # handle to out stream
2212 my ( $record, $analysis );
2214 while ( $record = get_record( $in ) )
2216 if ( $record->{ "SEQ" } )
2218 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2220 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2223 put_record( $record, $out );
2228 sub script_analyze_tags
2230 # Martin A. Hansen, August 2008.
2232 # Analyze sequence tags in stream.
2234 my ( $in, # handle to in stream
2235 $out, # handle to out stream
2240 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2242 while ( $record = get_record( $in ) )
2244 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2246 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2250 $len_hash{ length( $record->{ "SEQ" } ) }++;
2251 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2254 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2256 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2260 $len_hash{ $record->{ "BED_LEN" } }++;
2261 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2266 foreach $key ( sort { $a <=> $b } keys %len_hash )
2268 $tag_record->{ "TAG_LEN" } = $key;
2269 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2270 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2272 put_record( $tag_record, $out );
2277 sub script_complexity_seq
2279 # Martin A. Hansen, May 2008.
2281 # Generates an index calculated as the most common di-residue over
2282 # the sequence length for all sequences in stream.
2284 my ( $in, # handle to in stream
2285 $out, # handle to out stream
2290 my ( $record, $index );
2292 while ( $record = get_record( $in ) )
2294 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2296 put_record( $record, $out );
2301 sub script_oligo_freq
2303 # Martin A. Hansen, August 2007.
2305 # Determine the length of sequences in stream.
2307 my ( $in, # handle to in stream
2308 $out, # handle to out stream
2309 $options, # options hash
2314 my ( $record, %oligos, @freq_table );
2316 $options->{ "word_size" } ||= 7;
2318 while ( $record = get_record( $in ) )
2320 if ( $record->{ "SEQ" } )
2322 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2324 if ( not $options->{ "all" } )
2326 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2328 map { put_record( $_, $out ) } @freq_table;
2334 put_record( $record, $out );
2337 if ( $options->{ "all" } )
2339 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2341 map { put_record( $_, $out ) } @freq_table;
2346 sub script_create_weight_matrix
2348 # Martin A. Hansen, August 2007.
2350 # Creates a weight matrix from an alignmnet.
2352 my ( $in, # handle to in stream
2353 $out, # handle to out stream
2354 $options, # options hash
2359 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2363 while ( $record = get_record( $in ) )
2365 if ( $record->{ "SEQ" } )
2367 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2369 $res = substr $record->{ "SEQ" }, $i, 1;
2371 $freq_hash{ $i }{ $res }++;
2372 $res_hash{ $res } = 1;
2379 put_record( $record, $out );
2383 foreach $res ( sort keys %res_hash )
2387 $record->{ "V0" } = $res;
2389 for ( $i = 0; $i < keys %freq_hash; $i++ )
2391 $freq = $freq_hash{ $i }{ $res } || 0;
2393 if ( $options->{ "percent" } ) {
2394 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2397 $record->{ "V" . ( $i + 1 ) } = $freq;
2400 put_record( $record, $out );
2405 sub script_calc_bit_scores
2407 # Martin A. Hansen, March 2007.
2409 # Calculates the bit scores for each position from an alignmnet in the stream.
2411 my ( $in, # handle to in stream
2412 $out, # handle to out stream
2417 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2421 while ( $record = get_record( $in ) )
2423 if ( $record->{ "SEQ" } )
2425 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2427 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2429 $res = substr $record->{ "SEQ" }, $i, 1;
2431 next if $res =~ /-|_|~|\./;
2433 $freq_hash{ $i }{ $res }++;
2440 put_record( $record, $out );
2446 if ( $type eq "protein" ) {
2452 for ( $i = 0; $i < keys %freq_hash; $i++ )
2454 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2456 $bit_diff = $bit_max - $bit_height;
2458 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2461 put_record( $record, $out );
2465 sub script_reverse_seq
2467 # Martin A. Hansen, August 2007.
2469 # Reverse sequence in record.
2471 my ( $in, # handle to in stream
2472 $out, # handle to out stream
2479 while ( $record = get_record( $in ) )
2481 if ( $record->{ "SEQ" } ) {
2482 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2485 put_record( $record, $out );
2490 sub script_complement_seq
2492 # Martin A. Hansen, August 2007.
2494 # Complement sequence in record.
2496 my ( $in, # handle to in stream
2497 $out, # handle to out stream
2502 my ( $record, $type );
2504 while ( $record = get_record( $in ) )
2506 if ( $record->{ "SEQ" } )
2509 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2512 if ( $type eq "rna" ) {
2513 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2514 } elsif ( $type eq "dna" ) {
2515 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2519 put_record( $record, $out );
2524 sub script_remove_indels
2526 # Martin A. Hansen, August 2007.
2528 # Remove indels from sequences in stream.
2530 my ( $in, # handle to in stream
2531 $out, # handle to out stream
2538 while ( $record = get_record( $in ) )
2540 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2542 put_record( $record, $out );
2547 sub script_transliterate_seq
2549 # Martin A. Hansen, August 2007.
2551 # Transliterate chars from sequence in record.
2553 my ( $in, # handle to in stream
2554 $out, # handle to out stream
2555 $options, # options hash
2560 my ( $record, $search, $replace, $delete );
2562 $search = $options->{ "search" } || "";
2563 $replace = $options->{ "replace" } || "";
2564 $delete = $options->{ "delete" } || "";
2566 while ( $record = get_record( $in ) )
2568 if ( $record->{ "SEQ" } )
2570 if ( $search and $replace ) {
2571 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2572 } elsif ( $delete ) {
2573 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2577 put_record( $record, $out );
2582 sub script_transliterate_vals
2584 # Martin A. Hansen, April 2008.
2586 # Transliterate chars from values in record.
2588 my ( $in, # handle to in stream
2589 $out, # handle to out stream
2590 $options, # options hash
2595 my ( $record, $search, $replace, $delete, $key );
2597 $search = $options->{ "search" } || "";
2598 $replace = $options->{ "replace" } || "";
2599 $delete = $options->{ "delete" } || "";
2601 while ( $record = get_record( $in ) )
2603 foreach $key ( @{ $options->{ "keys" } } )
2605 if ( exists $record->{ $key } )
2607 if ( $search and $replace ) {
2608 eval "\$record->{ $key } =~ tr/$search/$replace/";
2609 } elsif ( $delete ) {
2610 eval "\$record->{ $key } =~ tr/$delete//d";
2615 put_record( $record, $out );
2620 sub script_translate_seq
2622 # Martin A. Hansen, February 2008.
2624 # Translate DNA sequence into protein sequence.
2626 my ( $in, # handle to in stream
2627 $out, # handle to out stream
2628 $options, # options hash
2633 my ( $record, $frame, %new_record );
2635 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2637 while ( $record = get_record( $in ) )
2639 if ( $record->{ "SEQ" } )
2641 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2643 foreach $frame ( @{ $options->{ "frames" } } )
2645 %new_record = %{ $record };
2647 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2648 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2649 $new_record{ "FRAME" } = $frame;
2651 put_record( \%new_record, $out );
2657 put_record( $record, $out );
2663 sub script_extract_seq
2665 # Martin A. Hansen, August 2007.
2667 # Extract subsequences from sequences in record.
2669 my ( $in, # handle to in stream
2670 $out, # handle to out stream
2671 $options, # options hash
2676 my ( $beg, $end, $len, $record );
2678 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2681 $beg = $options->{ "beg" } - 1; # correcting for start offset
2684 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2686 } elsif ( defined $options->{ "end" } ) {
2687 $end = $options->{ "end" } - 1; # correcting for start offset
2690 $len = $options->{ "len" };
2692 # print "beg->$beg, end->$end, len->$len\n";
2694 while ( $record = get_record( $in ) )
2696 if ( $record->{ "SEQ" } )
2698 if ( defined $beg and defined $end )
2700 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2701 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2703 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2706 elsif ( defined $beg and defined $len )
2708 if ( $len > length $record->{ "SEQ" } ) {
2709 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2711 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2714 elsif ( defined $beg )
2716 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2720 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2722 put_record( $record, $out );
2727 sub script_get_genome_seq
2729 # Martin A. Hansen, December 2007.
2731 # Gets a subsequence from a genome.
2733 my ( $in, # handle to in stream
2734 $out, # handle to out stream
2735 $options, # options hash
2740 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2742 $options->{ "flank" } ||= 0;
2744 if ( $options->{ "genome" } )
2746 $genome = $options->{ "genome" };
2748 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2749 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2751 $fh = Maasha::Common::read_open( $genome_file );
2752 $index = Maasha::Fasta::index_retrieve( $index_file );
2754 shift @{ $index }; # Get rid of the file size info
2756 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2758 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2760 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2762 $beg = $index_beg + $options->{ "beg" } - 1;
2764 if ( $options->{ "len" } ) {
2765 $len = $options->{ "len" };
2766 } elsif ( $options->{ "end" } ) {
2767 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2770 $beg -= $options->{ "flank" };
2771 $len += 2 * $options->{ "flank" };
2773 if ( $beg <= $index_beg )
2775 $len -= $index_beg - $beg;
2779 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2781 next if $beg > $index_beg + $index_len;
2783 $record->{ "CHR" } = $options->{ "chr" };
2784 $record->{ "CHR_BEG" } = $beg - $index_beg;
2785 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2787 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2788 $record->{ "SEQ_LEN" } = $len;
2790 put_record( $record, $out );
2794 while ( $record = get_record( $in ) )
2796 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2798 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2800 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2802 $beg = $record->{ "CHR_BEG" } + $index_beg;
2803 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2805 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2807 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2809 $beg = $record->{ "S_BEG" } + $index_beg;
2810 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2812 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2814 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2816 $beg = $record->{ "S_BEG" } + $index_beg;
2817 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2820 $beg -= $options->{ "flank" };
2821 $len += 2 * $options->{ "flank" };
2823 if ( $beg <= $index_beg )
2825 $len -= $index_beg - $beg;
2829 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2831 next if $beg > $index_beg + $index_len;
2833 $record->{ "CHR_BEG" } = $beg - $index_beg;
2834 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2836 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2838 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2840 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2841 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2844 if ( $options->{ "mask" } )
2846 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2848 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2850 @begs = split ",", $record->{ "Q_BEGS" };
2851 @lens = split ",", $record->{ "BLOCKSIZES" };
2853 for ( $i = 0; $i < @begs; $i++ ) {
2854 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2860 put_record( $record, $out );
2867 sub script_get_genome_align
2869 # Martin A. Hansen, April 2008.
2871 # Gets a subalignment from a multiple genome alignment.
2873 my ( $in, # handle to in stream
2874 $out, # handle to out stream
2875 $options, # options hash
2880 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2882 $options->{ "strand" } ||= "+";
2886 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2888 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2890 $beg = $options->{ "beg" } - 1;
2892 if ( $options->{ "end" } ) {
2893 $end = $options->{ "end" };
2894 } elsif ( $options->{ "len" } ) {
2895 $end = $beg + $options->{ "len" };
2898 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2900 foreach $entry ( @{ $align } )
2902 $entry->{ "CHR" } = $record->{ "CHR" };
2903 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2904 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2905 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2906 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2907 $entry->{ "SCORE" } = $record->{ "SCORE" };
2909 put_record( $entry, $out );
2913 while ( $record = get_record( $in ) )
2915 if ( $record->{ "REC_TYPE" } eq "BED" )
2917 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2919 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2921 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2923 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2925 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2928 foreach $entry ( @{ $align } )
2930 $entry->{ "CHR" } = $record->{ "CHR" };
2931 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2932 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2933 $entry->{ "STRAND" } = $record->{ "STRAND" };
2934 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2935 $entry->{ "SCORE" } = $record->{ "SCORE" };
2937 put_record( $entry, $out );
2945 sub script_get_genome_phastcons
2947 # Martin A. Hansen, February 2008.
2949 # Get phastcons scores from genome intervals.
2951 my ( $in, # handle to in stream
2952 $out, # handle to out stream
2953 $options, # options hash
2958 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2960 $options->{ "flank" } ||= 0;
2962 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2963 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2965 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2966 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2968 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2970 $options->{ "beg" } -= 1; # request is 1-based
2971 $options->{ "end" } -= 1; # request is 1-based
2973 if ( $options->{ "len" } ) {
2974 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2977 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2979 $record->{ "CHR" } = $options->{ "chr" };
2980 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2981 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2983 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2984 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2986 put_record( $record, $out );
2989 while ( $record = get_record( $in ) )
2991 if ( $record->{ "REC_TYPE" } eq "BED" )
2993 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2995 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2997 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2999 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3001 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3004 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3005 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3007 put_record( $record, $out );
3010 close $fh_phastcons if $fh_phastcons;
3016 # Martin A. Hansen, December 2007.
3018 # Folds sequences in stream into secondary structures.
3020 my ( $in, # handle to in stream
3021 $out, # handle to out stream
3026 my ( $record, $type, $struct, $index );
3028 while ( $record = get_record( $in ) )
3030 if ( $record->{ "SEQ" } )
3033 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3036 if ( $type ne "protein" )
3038 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3039 $record->{ "SEC_STRUCT" } = $struct;
3040 $record->{ "FREE_ENERGY" } = $index;
3041 $record->{ "SCORE" } = abs int $index;
3042 $record->{ "SIZE" } = length $struct;
3043 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3047 put_record( $record, $out );
3052 sub script_split_seq
3054 # Martin A. Hansen, August 2007.
3056 # Split a sequence in stream into words.
3058 my ( $in, # handle to in stream
3059 $out, # handle to out stream
3060 $options, # options hash
3065 my ( $record, $new_record, $i, $subseq, %lookup );
3067 $options->{ "word_size" } ||= 7;
3069 while ( $record = get_record( $in ) )
3071 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3073 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3075 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3077 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3079 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3080 $new_record->{ "SEQ" } = $subseq;
3082 put_record( $new_record, $out );
3084 $lookup{ $subseq } = 1;
3088 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3089 $new_record->{ "SEQ" } = $subseq;
3091 put_record( $new_record, $out );
3097 put_record( $record, $out );
3103 sub script_split_bed
3105 # Martin A. Hansen, June 2008.
3107 # Split a BED record into overlapping windows.
3109 my ( $in, # handle to in stream
3110 $out, # handle to out stream
3111 $options, # options hash
3116 my ( $record, $new_record, $i );
3118 $options->{ "window_size" } ||= 20;
3119 $options->{ "step_size" } ||= 1;
3121 while ( $record = get_record( $in ) )
3123 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3125 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3127 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3129 $new_record->{ "REC_TYPE" } = "BED";
3130 $new_record->{ "CHR" } = $record->{ "CHR" };
3131 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3132 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3133 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3134 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3135 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3136 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3138 put_record( $new_record, $out );
3143 put_record( $record, $out );
3149 sub script_align_seq
3151 # Martin A. Hansen, August 2007.
3153 # Align sequences in stream.
3155 my ( $in, # handle to in stream
3156 $out, # handle to out stream
3161 my ( $record, @entries, $entry );
3163 while ( $record = get_record( $in ) )
3165 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3166 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3167 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3168 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3170 put_record( $record, $out );
3174 @entries = Maasha::Align::align( \@entries );
3176 foreach $entry ( @entries )
3178 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3181 SEQ_NAME => $entry->[ SEQ_NAME ],
3182 SEQ => $entry->[ SEQ ],
3185 put_record( $record, $out );
3193 # Martin A. Hansen, February 2008.
3195 # Using the first sequence in stream as reference, tile
3196 # all subsequent sequences based on pairwise alignments.
3198 my ( $in, # handle to in stream
3199 $out, # handle to out stream
3200 $options, # options hash
3205 my ( $record, $first, $ref_entry, @entries );
3209 while ( $record = get_record( $in ) )
3211 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3215 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3221 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3226 put_record( $record, $out );
3230 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3232 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3236 sub script_invert_align
3238 # Martin A. Hansen, February 2008.
3240 # Inverts an alignment showing only non-mathing residues
3241 # using the first sequence as reference.
3243 my ( $in, # handle to in stream
3244 $out, # handle to out stream
3245 $options, # options hash
3250 my ( $record, @entries );
3252 while ( $record = get_record( $in ) )
3254 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3256 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3260 put_record( $record, $out );
3264 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3266 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3270 sub script_patscan_seq
3272 # Martin A. Hansen, August 2007.
3274 # Locates patterns in sequences using scan_for_matches.
3276 my ( $in, # handle to in stream
3277 $out, # handle to out stream
3278 $options, # options hash
3283 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3285 if ( $options->{ "patterns" } ) {
3286 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3287 } elsif ( -f $options->{ "patterns_in" } ) {
3288 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3291 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3293 push @args, "-c" if $options->{ "comp" };
3294 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3295 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3297 $seq_file = "$BP_TMP/patscan.seq";
3298 $pat_file = "$BP_TMP/patscan.pat";
3299 $out_file = "$BP_TMP/patscan.out";
3301 $fh_out = Maasha::Common::write_open( $seq_file );
3305 while ( $record = get_record( $in ) )
3307 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3309 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3311 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3313 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3321 $arg = join " ", @args;
3322 $arg .= " -p" if $type eq "protein";
3324 foreach $pattern ( @{ $patterns } )
3326 $fh_out = Maasha::Common::write_open( $pat_file );
3328 print $fh_out "$pattern\n";
3332 if ( $options->{ 'genome' } ) {
3333 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3334 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3336 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3337 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3340 $fh_in = Maasha::Common::read_open( $out_file );
3342 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3344 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3346 if ( $options->{ 'genome' } )
3348 $result->{ "CHR" } = $result->{ "S_ID" };
3349 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3350 $result->{ "CHR_END" } = $result->{ "S_END" };
3352 delete $result->{ "S_ID" };
3353 delete $result->{ "S_BEG" };
3354 delete $result->{ "S_END" };
3358 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3361 put_record( $result, $out );
3373 sub script_create_blast_db
3375 # Martin A. Hansen, September 2007.
3377 # Creates a NCBI BLAST database with formatdb
3379 my ( $in, # handle to in stream
3380 $out, # handle to out stream
3381 $options, # options hash
3386 my ( $fh, $seq_type, $path, $record, $entry );
3388 $path = $options->{ "database" };
3390 $fh = Maasha::Common::write_open( $path );
3392 while ( $record = get_record( $in ) )
3394 put_record( $record, $out ) if not $options->{ "no_stream" };
3396 if ( $entry = record2fasta( $record ) )
3398 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3400 Maasha::Fasta::put_entry( $entry, $fh );
3406 if ( $seq_type eq "protein" ) {
3407 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3409 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3416 sub script_blast_seq
3418 # Martin A. Hansen, September 2007.
3420 # BLASTs sequences in stream against a given database.
3422 my ( $in, # handle to in stream
3423 $out, # handle to out stream
3424 $options, # options hash
3429 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3431 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3432 $options->{ "filter" } = "F";
3433 $options->{ "filter" } = "T" if $options->{ "filter" };
3434 $options->{ "cpus" } ||= 1;
3436 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3438 $tmp_in = "$BP_TMP/blast_query.seq";
3439 $tmp_out = "$BP_TMP/blast.result";
3441 $fh_out = Maasha::Common::write_open( $tmp_in );
3443 while ( $record = get_record( $in ) )
3445 if ( $entry = record2fasta( $record ) )
3447 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3449 Maasha::Fasta::put_entry( $entry, $fh_out );
3452 put_record( $record, $out );
3457 if ( -f $options->{ 'database' } . ".phr" ) {
3458 $s_type = "protein";
3460 $s_type = "nucleotide";
3463 if ( not $options->{ 'program' } )
3465 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3466 $options->{ 'program' } = "blastn";
3467 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3468 $options->{ 'program' } = "blastp";
3469 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3470 $options->{ 'program' } = "blastx";
3471 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3472 $options->{ 'program' } = "tblastn";
3476 Maasha::Common::run( "blastall", "-p $options->{ 'program' } -e $options->{ 'e_val' } -a $options->{ 'cpus' } -m 8 -i $tmp_in -d $options->{ 'database' } -F $options->{ 'filter' } -o $tmp_out > /dev/null 2>&1", 1 );
3480 $fh_out = Maasha::Common::read_open( $tmp_out );
3484 while ( $line = <$fh_out> )
3488 next if $line =~ /^#/;
3490 @fields = split /\s+/, $line;
3492 $record->{ "REC_TYPE" } = "BLAST";
3493 $record->{ "Q_ID" } = $fields[ 0 ];
3494 $record->{ "S_ID" } = $fields[ 1 ];
3495 $record->{ "IDENT" } = $fields[ 2 ];
3496 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3497 $record->{ "MISMATCHES" } = $fields[ 4 ];
3498 $record->{ "GAPS" } = $fields[ 5 ];
3499 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3500 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3501 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3502 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3503 $record->{ "E_VAL" } = $fields[ 10 ];
3504 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3506 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3508 $record->{ "STRAND" } = '-';
3510 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3514 $record->{ "STRAND" } = '+';
3517 put_record( $record, $out );
3528 # Martin A. Hansen, August 2007.
3530 # BLATs sequences in stream against a given genome.
3532 my ( $in, # handle to in stream
3533 $out, # handle to out stream
3534 $options, # options hash
3539 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3541 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3543 $options->{ 'tile_size' } ||= 11;
3544 $options->{ 'one_off' } ||= 0;
3545 $options->{ 'min_identity' } ||= 90;
3546 $options->{ 'min_score' } ||= 0;
3547 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3549 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3550 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3551 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3552 $blat_args .= " -minScore=$options->{ 'min_score' }";
3553 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3554 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3556 $query_file = "$BP_TMP/blat.seq";
3558 $fh_out = Maasha::Common::write_open( $query_file );
3560 while ( $record = get_record( $in ) )
3562 if ( $entry = record2fasta( $record ) )
3564 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3565 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3568 put_record( $record, $out );
3573 $blat_args .= " -t=dnax" if $type eq "protein";
3574 $blat_args .= " -q=$type";
3576 $result_file = "$BP_TMP/blat.psl";
3578 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3582 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3584 map { put_record( $_, $out ) } @{ $entries };
3586 unlink $result_file;
3592 # Martin A. Hansen, July 2008.
3594 # soap sequences in stream against a given file or genome.
3596 my ( $in, # handle to in stream
3597 $out, # handle to out stream
3598 $options, # options hash
3603 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3605 $options->{ "seed_size" } ||= 10;
3606 $options->{ "mismatches" } ||= 2;
3607 $options->{ "gap_size" } ||= 0;
3608 $options->{ "cpus" } ||= 1;
3610 if ( $options->{ "genome" } ) {
3611 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3614 $tmp_in = "$BP_TMP/soap_query.seq";
3615 $tmp_out = "$BP_TMP/soap.result";
3617 $fh_out = Maasha::Common::write_open( $tmp_in );
3621 while ( $record = get_record( $in ) )
3623 if ( $entry = record2fasta( $record ) )
3625 Maasha::Fasta::put_entry( $entry, $fh_out );
3630 put_record( $record, $out );
3638 "-s $options->{ 'seed_size' }",
3641 "-v $options->{ 'mismatches' }",
3642 "-g $options->{ 'gap_size' }",
3643 "-p $options->{ 'cpus' }",
3644 "-d $options->{ 'in_file' }",
3648 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3650 Maasha::Common::run( "soap", $args, 1 );
3654 $fh_out = Maasha::Common::read_open( $tmp_out );
3658 while ( $line = <$fh_out> )
3662 @fields = split /\t/, $line;
3664 $record->{ "REC_TYPE" } = "SOAP";
3665 $record->{ "Q_ID" } = $fields[ 0 ];
3666 $record->{ "SCORE" } = $fields[ 3 ];
3667 $record->{ "STRAND" } = $fields[ 6 ];
3668 $record->{ "S_ID" } = $fields[ 7 ];
3669 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3670 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3672 put_record( $record, $out );
3682 sub script_match_seq
3684 # Martin A. Hansen, August 2007.
3686 # BLATs sequences in stream against a given genome.
3688 my ( $in, # handle to in stream
3689 $out, # handle to out stream
3690 $options, # options hash
3695 my ( $record, @entries, $results );
3697 $options->{ "word_size" } ||= 20;
3698 $options->{ "direction" } ||= "both";
3700 while ( $record = get_record( $in ) )
3702 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3703 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3706 put_record( $record, $out );
3709 if ( @entries == 1 )
3711 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3713 map { put_record( $_, $out ) } @{ $results };
3715 elsif ( @entries == 2 )
3717 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3719 map { put_record( $_, $out ) } @{ $results };
3724 sub script_create_vmatch_index
3726 # Martin A. Hansen, January 2008.
3728 # Create a vmatch index from sequences in the stream.
3730 my ( $in, # handle to in stream
3731 $out, # handle to out stream
3732 $options, # options hash
3737 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3739 if ( $options->{ "index_name" } )
3741 $file_tmp = $options->{ 'index_name' };
3742 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3745 while ( $record = get_record( $in ) )
3747 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3749 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3751 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3754 put_record( $record, $out ) if not $options->{ "no_stream" };
3757 if ( $options->{ "index_name" } )
3761 if ( $type eq "protein" ) {
3762 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3764 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3772 sub script_vmatch_seq
3774 # Martin A. Hansen, August 2007.
3776 # Vmatches sequences in stream against a given genome.
3778 my ( $in, # handle to in stream
3779 $out, # handle to out stream
3780 $options, # options hash
3785 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3787 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3789 if ( $options->{ "index_name" } )
3791 @index_files = $options->{ "index_name" };
3795 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3797 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3799 @index_files = sort keys %hash;
3802 while ( $record = get_record( $in ) )
3804 push @records, $record;
3806 put_record( $record, $out );
3809 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3813 $fh_in = Maasha::Common::read_open( $result_file );
3815 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3816 put_record( $record, $out );
3821 unlink $result_file;
3825 sub script_write_fasta
3827 # Martin A. Hansen, August 2007.
3829 # Write FASTA entries from sequences in stream.
3831 my ( $in, # handle to in stream
3832 $out, # handle to out stream
3833 $options, # options hash
3838 my ( $record, $fh, $entry );
3840 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3842 while ( $record = get_record( $in ) )
3844 if ( $entry = record2fasta( $record ) ) {
3845 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3848 put_record( $record, $out ) if not $options->{ "no_stream" };
3855 sub script_write_align
3857 # Martin A. Hansen, August 2007.
3859 # Write pretty alignments aligned sequences in stream.
3861 my ( $in, # handle to in stream
3862 $out, # handle to out stream
3863 $options, # options hash
3868 my ( $fh, $record, @entries );
3870 $fh = write_stream( $options->{ "data_out" } ) ;
3872 while ( $record = get_record( $in ) )
3874 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3875 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3878 put_record( $record, $out ) if not $options->{ "no_stream" };
3881 if ( scalar( @entries ) == 2 ) {
3882 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3883 } elsif ( scalar ( @entries ) > 2 ) {
3884 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3891 sub script_write_blast
3893 # Martin A. Hansen, November 2007.
3895 # Write data in blast table format (-m8 and 9).
3897 my ( $in, # handle to in stream
3898 $out, # handle to out stream
3899 $options, # options hash
3904 my ( $fh, $record, $first );
3906 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3910 while ( $record = get_record( $in ) )
3912 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3914 if ( $options->{ "comment" } and $first )
3916 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";
3921 if ( $record->{ "STRAND" } eq "-" ) {
3922 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3925 print $fh join( "\t",
3926 $record->{ "Q_ID" },
3927 $record->{ "S_ID" },
3928 $record->{ "IDENT" },
3929 $record->{ "ALIGN_LEN" },
3930 $record->{ "MISMATCHES" },
3931 $record->{ "GAPS" },
3932 $record->{ "Q_BEG" } + 1,
3933 $record->{ "Q_END" } + 1,
3934 $record->{ "S_BEG" } + 1,
3935 $record->{ "S_END" } + 1,
3936 $record->{ "E_VAL" },
3937 $record->{ "BIT_SCORE" }
3941 put_record( $record, $out ) if not $options->{ "no_stream" };
3948 sub script_write_tab
3950 # Martin A. Hansen, August 2007.
3952 # Write data as table.
3954 my ( $in, # handle to in stream
3955 $out, # handle to out stream
3956 $options, # options hash
3961 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3963 $options->{ "delimit" } ||= "\t";
3965 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3967 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3969 while ( $record = get_record( $in ) )
3974 if ( $options->{ "keys" } )
3976 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3980 foreach $key ( @{ $options->{ "keys" } } )
3982 if ( exists $record->{ $key } )
3984 push @keys, $key if $options->{ "comment" };
3985 push @vals, $record->{ $key };
3992 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3994 next if exists $no_keys{ $key };
3996 push @keys, $key if $options->{ "comment" };
3997 push @vals, $record->{ $key };
4001 if ( @keys and $options->{ "comment" } )
4003 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4005 delete $options->{ "comment" };
4008 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4010 put_record( $record, $out ) if not $options->{ "no_stream" };
4017 sub script_write_bed
4019 # Martin A. Hansen, August 2007.
4021 # Write BED format for the UCSC genome browser using records in stream.
4023 my ( $in, # handle to in stream
4024 $out, # handle to out stream
4025 $options, # options hash
4030 my ( $fh, $record, $new_record );
4032 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4034 while ( $record = get_record( $in ) )
4036 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4038 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4040 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4042 $new_record->{ "CHR" } = $record->{ "S_ID" };
4043 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4044 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4045 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4046 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4047 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4049 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4051 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4053 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4055 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4057 $new_record->{ "CHR" } = $record->{ "S_ID" };
4058 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4059 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4060 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4061 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4062 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4064 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4066 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4068 $new_record->{ "CHR" } = $record->{ "S_ID" };
4069 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4070 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4071 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4072 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4073 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4075 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4077 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4079 $new_record->{ "CHR" } = $record->{ "S_ID" };
4080 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4081 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4082 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4083 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4084 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4086 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4088 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4090 Maasha::UCSC::bed_put_entry( $record, $fh );
4093 put_record( $record, $out ) if not $options->{ "no_stream" };
4100 sub script_write_psl
4102 # Martin A. Hansen, August 2007.
4104 # Write PSL output from stream.
4106 my ( $in, # handle to in stream
4107 $out, # handle to out stream
4108 $options, # options hash
4113 my ( $fh, $record, @output, $first );
4117 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4119 while ( $record = get_record( $in ) )
4121 put_record( $record, $out ) if not $options->{ "no_stream" };
4123 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4125 Maasha::UCSC::psl_put_header( $fh ) if $first;
4126 Maasha::UCSC::psl_put_entry( $record, $fh );
4135 sub script_write_fixedstep
4137 # Martin A. Hansen, Juli 2008.
4139 # Write fixedStep entries from recrods in the stream.
4141 my ( $in, # handle to in stream
4142 $out, # handle to out stream
4143 $options, # options hash
4148 my ( $fh, $record, $vals );
4150 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4152 while ( $record = get_record( $in ) )
4154 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4156 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4158 $vals = $record->{ 'VALS' };
4162 print $fh "$vals\n";
4165 put_record( $record, $out ) if not $options->{ "no_stream" };
4172 sub script_write_2bit
4174 # Martin A. Hansen, March 2008.
4176 # Write sequence entries from stream in 2bit format.
4178 my ( $in, # handle to in stream
4179 $out, # handle to out stream
4180 $options, # options hash
4185 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4187 $mask = 1 if not $options->{ "no_mask" };
4189 $tmp_file = "$BP_TMP/write_2bit.fna";
4190 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4192 $fh_out = write_stream( $options->{ "data_out" } );
4194 while ( $record = get_record( $in ) )
4196 if ( $entry = record2fasta( $record ) ) {
4197 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4200 put_record( $record, $out ) if not $options->{ "no_stream" };
4205 $fh_in = Maasha::Common::read_open( $tmp_file );
4207 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4216 sub script_write_solid
4218 # Martin A. Hansen, April 2008.
4220 # Write di-base encoded Solid sequence from entries in stream.
4222 my ( $in, # handle to in stream
4223 $out, # handle to out stream
4224 $options, # options hash
4229 my ( $record, $fh, $entry );
4231 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4233 while ( $record = get_record( $in ) )
4235 if ( $entry = record2fasta( $record ) )
4237 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4239 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4242 put_record( $record, $out ) if not $options->{ "no_stream" };
4249 sub script_plot_seqlogo
4251 # Martin A. Hansen, August 2007.
4253 # Calculates and writes a sequence logo for alignments.
4255 my ( $in, # handle to in stream
4256 $out, # handle to out stream
4257 $options, # options hash
4262 my ( $record, @entries, $logo, $fh );
4264 while ( $record = get_record( $in ) )
4266 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4267 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4270 put_record( $record, $out ) if not $options->{ "no_stream" };
4273 $logo = Maasha::Plot::seq_logo( \@entries );
4275 $fh = write_stream( $options->{ "data_out" } );
4283 sub script_plot_phastcons_profiles
4285 # Martin A. Hansen, January 2008.
4287 # Plots PhastCons profiles.
4289 my ( $in, # handle to in stream
4290 $out, # handle to out stream
4291 $options, # options hash
4296 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4298 $options->{ "title" } ||= "PhastCons Profiles";
4300 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4301 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4303 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4304 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4306 while ( $record = get_record( $in ) )
4308 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4310 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4312 push @{ $AoA }, [ @{ $scores } ];
4315 put_record( $record, $out ) if not $options->{ "no_stream" };
4318 Maasha::UCSC::phastcons_normalize( $AoA );
4320 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4321 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4323 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4325 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4327 $fh = write_stream( $options->{ "data_out" } );
4329 print $fh "$_\n" foreach @{ $plot };
4335 sub script_analyze_bed
4337 # Martin A. Hansen, March 2008.
4339 # Analyze BED entries in stream.
4341 my ( $in, # handle to in stream
4342 $out, # handle to out stream
4343 $options, # options hash
4350 while ( $record = get_record( $in ) )
4352 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4354 put_record( $record, $out );
4359 sub script_analyze_vals
4361 # Martin A. Hansen, August 2007.
4363 # Analyze values for given keys in stream.
4365 my ( $in, # handle to in stream
4366 $out, # handle to out stream
4367 $options, # options hash
4372 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4374 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4376 while ( $record = get_record( $in ) )
4378 foreach $key ( keys %{ $record } )
4380 next if $options->{ "keys" } and not exists $key_hash{ $key };
4382 $analysis->{ $key }->{ "COUNT" }++;
4384 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4386 $analysis->{ $key }->{ "TYPE" } = "num";
4387 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4388 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4389 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4393 $len = length $record->{ $key };
4395 $analysis->{ $key }->{ "TYPE" } = "alph";
4396 $analysis->{ $key }->{ "SUM" } += $len;
4397 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4398 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4402 put_record( $record, $out ) if not $options->{ "no_stream" };
4405 foreach $key ( keys %{ $analysis } )
4407 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4408 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4411 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4421 if ( $options->{ "keys" } ) {
4422 @keys = @{ $options->{ "keys" } };
4424 @keys = keys %{ $analysis };
4427 foreach $key ( @keys )
4429 $keys .= sprintf "% 15s", $key;
4430 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4431 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4432 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4433 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4434 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4435 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4438 print $out "$keys\n";
4439 print $out "$types\n";
4440 print $out "$counts\n";
4441 print $out "$mins\n";
4442 print $out "$maxs\n";
4443 print $out "$sums\n";
4444 print $out "$means\n";
4448 sub script_head_records
4450 # Martin A. Hansen, August 2007.
4452 # Display the first sequences in stream.
4454 my ( $in, # handle to in stream
4455 $out, # handle to out stream
4456 $options, # options hash
4461 my ( $record, $count );
4463 $options->{ "num" } ||= 10;
4467 while ( $record = get_record( $in ) )
4471 put_record( $record, $out );
4473 last if $count == $options->{ "num" };
4478 sub script_remove_keys
4480 # Martin A. Hansen, August 2007.
4482 # Remove keys from stream.
4484 my ( $in, # handle to in stream
4485 $out, # handle to out stream
4486 $options, # options hash
4491 my ( $record, $new_record );
4493 while ( $record = get_record( $in ) )
4495 if ( $options->{ "keys" } )
4497 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4499 elsif ( $options->{ "save_keys" } )
4501 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4503 $record = $new_record;
4506 put_record( $record, $out ) if keys %{ $record };
4511 sub script_rename_keys
4513 # Martin A. Hansen, August 2007.
4515 # Rename keys in stream.
4517 my ( $in, # handle to in stream
4518 $out, # handle to out stream
4519 $options, # options hash
4526 while ( $record = get_record( $in ) )
4528 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4530 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4532 delete $record->{ $options->{ "keys" }->[ 0 ] };
4535 put_record( $record, $out );
4540 sub script_uniq_vals
4542 # Martin A. Hansen, August 2007.
4544 # Find unique values in stream.
4546 my ( $in, # handle to in stream
4547 $out, # handle to out stream
4548 $options, # options hash
4553 my ( %hash, $record );
4555 while ( $record = get_record( $in ) )
4557 if ( $record->{ $options->{ "key" } } )
4559 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4561 put_record( $record, $out );
4563 $hash{ $record->{ $options->{ "key" } } } = 1;
4565 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4567 put_record( $record, $out );
4571 $hash{ $record->{ $options->{ "key" } } } = 1;
4576 put_record( $record, $out );
4582 sub script_merge_vals
4584 # Martin A. Hansen, August 2007.
4586 # Rename keys in stream.
4588 my ( $in, # handle to in stream
4589 $out, # handle to out stream
4590 $options, # options hash
4595 my ( $record, @join, $i );
4597 $options->{ "delimit" } ||= '_';
4599 while ( $record = get_record( $in ) )
4601 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4603 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4605 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4606 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4609 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4612 put_record( $record, $out );
4617 sub script_merge_records
4619 # Martin A. Hansen, July 2008.
4621 # Merges records in the stream based on identical values of two given keys.
4623 my ( $in, # handle to in stream
4624 $out, # handle to out stream
4625 $options, # options hash
4630 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4631 $num1, $num2, $num, $cmp, $i );
4633 $merge = $options->{ "merge" } || "AandB";
4635 $file1 = "$BP_TMP/merge_records1.tmp";
4636 $file2 = "$BP_TMP/merge_records2.tmp";
4638 $fh1 = Maasha::Common::write_open( $file1 );
4639 $fh2 = Maasha::Common::write_open( $file2 );
4641 $key1 = $options->{ "keys" }->[ 0 ];
4642 $key2 = $options->{ "keys" }->[ 1 ];
4644 $num = $key2 =~ s/n$//;
4648 while ( $record = get_record( $in ) )
4650 if ( exists $record->{ $key1 } )
4653 @vals1 = $record->{ $key1 };
4655 delete $record->{ $key1 };
4657 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4659 print $fh1 join( "\t", @vals1 ), "\n";
4663 elsif ( exists $record->{ $key2 } )
4666 @vals2 = $record->{ $key2 };
4668 delete $record->{ $key2 };
4670 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4672 print $fh2 join( "\t", @vals2 ), "\n";
4683 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4684 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4688 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4689 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4692 $fh1 = Maasha::Common::read_open( $file1 );
4693 $fh2 = Maasha::Common::read_open( $file2 );
4695 @vals1 = Maasha::Common::get_fields( $fh1 );
4696 @vals2 = Maasha::Common::get_fields( $fh2 );
4698 while ( $num1 > 0 and $num2 > 0 )
4703 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4705 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4710 if ( $merge =~ /^(AorB|AnotB)$/ )
4712 for ( $i = 0; $i < @keys1; $i++ ) {
4713 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4716 put_record( $record, $out );
4719 @vals1 = Maasha::Common::get_fields( $fh1 );
4724 if ( $merge =~ /^(BorA|BnotA)$/ )
4726 for ( $i = 0; $i < @keys2; $i++ ) {
4727 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4730 put_record( $record, $out );
4733 @vals2 = Maasha::Common::get_fields( $fh2 );
4738 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4740 for ( $i = 0; $i < @keys1; $i++ ) {
4741 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4744 for ( $i = 1; $i < @keys2; $i++ ) {
4745 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4748 put_record( $record, $out );
4751 @vals1 = Maasha::Common::get_fields( $fh1 );
4752 @vals2 = Maasha::Common::get_fields( $fh2 );
4764 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4768 for ( $i = 0; $i < @keys1; $i++ ) {
4769 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4772 put_record( $record, $out );
4775 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4779 for ( $i = 0; $i < @keys2; $i++ ) {
4780 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4783 put_record( $record, $out );
4790 # Martin A. Hansen, August 2007.
4792 # Grab for records in stream.
4794 my ( $in, # handle to in stream
4795 $out, # handle to out stream
4796 $options, # options hash
4801 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4803 if ( $options->{ "patterns" } )
4805 $patterns = [ split ",", $options->{ "patterns" } ];
4807 elsif ( -f $options->{ "patterns_in" } )
4809 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4811 elsif ( -f $options->{ "exact_in" } )
4813 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4815 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4820 if ( $options->{ "eval" } )
4822 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4830 while ( $record = get_record( $in ) )
4836 if ( $options->{ "keys" } )
4838 foreach $key ( @{ $options->{ "keys" } } )
4840 if ( exists $lookup_hash{ $record->{ $key } } )
4849 foreach $key ( keys %{ $record } )
4851 if ( not $options->{ "vals_only" } )
4853 if ( exists $lookup_hash{ $key } )
4860 if ( not $options->{ "keys_only" } )
4862 if ( exists $lookup_hash{ $record->{ $key } } )
4873 foreach $pattern ( @{ $patterns } )
4875 if ( $options->{ "keys" } )
4877 foreach $key ( @{ $options->{ "keys" } } )
4879 $pos = index $record->{ $key }, $pattern;
4881 goto FOUND if $pos >= 0;
4886 foreach $key ( keys %{ $record } )
4888 if ( not $options->{ "vals_only" } )
4890 $pos = index $key, $pattern;
4892 goto FOUND if $pos >= 0;
4895 if ( not $options->{ "keys_only" } )
4897 $pos = index $record->{ $key }, $pattern;
4899 goto FOUND if $pos >= 0;
4905 elsif ( $options->{ "regex" } )
4907 if ( $options->{ "keys" } )
4909 foreach $key ( @{ $options->{ "keys" } } )
4911 if ( $options->{ "case_insensitive" } ) {
4912 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4914 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4917 goto FOUND if $pos >= 0;
4922 foreach $key ( keys %{ $record } )
4924 if ( not $options->{ "vals_only" } )
4926 if ( $options->{ "case_insensitive" } ) {
4927 $pos = 1 if $key =~ /$options->{'regex'}/i;
4929 $pos = 1 if $key =~ /$options->{'regex'}/;
4932 goto FOUND if $pos >= 0;
4935 if ( not $options->{ "keys_only" } )
4937 if ( $options->{ "case_insensitive" } ) {
4938 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4940 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4943 goto FOUND if $pos >= 0;
4948 elsif ( $options->{ "eval" } )
4950 if ( defined $record->{ $key } )
4952 if ( $op eq "<" and $record->{ $key } < $val ) {
4953 $pos = 1 and goto FOUND;
4954 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4955 $pos = 1 and goto FOUND;
4956 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4957 $pos = 1 and goto FOUND;
4958 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4959 $pos = 1 and goto FOUND;
4960 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4961 $pos = 1 and goto FOUND;
4962 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4963 $pos = 1 and goto FOUND;
4964 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4965 $pos = 1 and goto FOUND;
4966 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4967 $pos = 1 and goto FOUND;
4974 if ( $pos >= 0 and not $options->{ "invert" } ) {
4975 put_record( $record, $out );
4976 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4977 put_record( $record, $out );
4985 # Martin A. Hansen, August 2007.
4987 # Evaluate extression for records in stream.
4989 my ( $in, # handle to in stream
4990 $out, # handle to out stream
4991 $options, # options hash
4996 my ( $record, $eval_key, $eval_val, $check, @keys );
4998 while ( $record = get_record( $in ) )
5000 if ( $options->{ "eval" } )
5002 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5010 @keys = split /\W+/, $eval_val;
5011 @keys = grep { ! /^\d+$/ } @keys;
5016 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5018 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5021 put_record( $record, $out );
5028 # Martin A. Hansen, June 2008.
5032 my ( $in, # handle to in stream
5033 $out, # handle to out stream
5034 $options, # options hash
5039 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5041 while ( $record = get_record( $in ) )
5045 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5047 push @rows, $record->{ $key };
5051 push @matrix, [ @rows ];
5056 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5058 foreach $row ( @matrix )
5060 for ( $i = 0; $i < @{ $row }; $i++ ) {
5061 $record->{ "V$i" } = $row->[ $i ];
5064 put_record( $record, $out );
5069 sub script_add_ident
5071 # Martin A. Hansen, May 2008.
5073 # Add a unique identifier to each record in stream.
5075 my ( $in, # handle to in stream
5076 $out, # handle to out stream
5077 $options, # options hash
5082 my ( $record, $key, $prefix, $i );
5084 $key = $options->{ "key" } || "ID";
5085 $prefix = $options->{ "prefix" } || "ID";
5089 while ( $record = get_record( $in ) )
5091 $record->{ $key } = sprintf( "$prefix%08d", $i );
5093 put_record( $record, $out );
5100 sub script_count_records
5102 # Martin A. Hansen, August 2007.
5104 # Count records in stream.
5106 my ( $in, # handle to in stream
5107 $out, # handle to out stream
5108 $options, # options hash
5113 my ( $record, $count, $result, $fh, $line );
5117 if ( $options->{ "no_stream" } )
5119 while ( $line = <$in> )
5123 $count++ if $line eq "---";
5128 while ( $record = get_record( $in ) )
5130 put_record( $record, $out );
5136 $result = { "RECORDS_COUNT" => $count };
5138 $fh = write_stream( $options->{ "data_out" } );
5140 put_record( $result, $fh );
5146 sub script_random_records
5148 # Martin A. Hansen, August 2007.
5150 # Pick a number or random records from stream.
5152 my ( $in, # handle to in stream
5153 $out, # handle to out stream
5154 $options, # options hash
5159 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5161 $options->{ "num" } ||= 10;
5163 $tmp_file = "$BP_TMP/random_records.tmp";
5165 $fh_out = Maasha::Common::write_open( $tmp_file );
5169 while ( $record = get_record( $in ) )
5171 put_record( $record, $fh_out );
5181 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5183 while ( $i < $options->{ "num" } )
5185 $rand = int( rand( $count ) );
5187 if ( not exists $rand_hash{ $rand } )
5189 $rand_hash{ $rand } = 1;
5191 $max = $rand if $rand > $max;
5197 $fh_in = Maasha::Common::read_open( $tmp_file );
5201 while ( $record = get_record( $fh_in ) )
5203 put_record( $record, $out ) if exists $rand_hash{ $count };
5205 last if $count == $max;
5216 sub script_sort_records
5218 # Martin A. Hansen, August 2007.
5220 # Sort to sort records according to keys.
5222 my ( $in, # handle to in stream
5223 $out, # handle to out stream
5224 $options, # options hash
5229 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5231 foreach $key ( @{ $options->{ "keys" } } )
5233 if ( $key =~ s/n$// ) {
5234 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5236 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5240 $sort_str = join " or ", @sort_cmd;
5241 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5243 while ( $record = get_record( $in ) ) {
5244 push @records, $record;
5247 @records = sort $sort_sub @records;
5249 if ( $options->{ "reverse" } )
5251 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5252 put_record( $records[ $i ], $out );
5257 for ( $i = 0; $i < scalar @records; $i++ ) {
5258 put_record( $records[ $i ], $out );
5264 sub script_count_vals
5266 # Martin A. Hansen, August 2007.
5268 # Count records in stream.
5270 my ( $in, # handle to in stream
5271 $out, # handle to out stream
5272 $options, # options hash
5277 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5279 $tmp_file = "$BP_TMP/count_cache.tmp";
5281 $fh_out = Maasha::Common::write_open( $tmp_file );
5286 while ( $record = get_record( $in ) )
5288 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5290 push @records, $record;
5292 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5294 map { put_record( $_, $fh_out ) } @records;
5301 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5312 $fh_in = Maasha::Common::read_open( $tmp_file );
5314 while ( $record = get_record( $fh_in ) )
5316 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5318 put_record( $record, $out );
5320 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5328 foreach $record ( @records )
5330 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5332 put_record( $record, $out );
5339 sub script_plot_histogram
5341 # Martin A. Hansen, September 2007.
5343 # Plot a simple histogram for a given key using GNU plot.
5345 my ( $in, # handle to in stream
5346 $out, # handle to out stream
5347 $options, # options hash
5352 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5354 $options->{ "title" } ||= "Histogram";
5355 $options->{ "sort" } ||= "num";
5357 while ( $record = get_record( $in ) )
5359 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5361 put_record( $record, $out ) if not $options->{ "no_stream" };
5364 if ( $options->{ "sort" } eq "num" ) {
5365 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5367 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5370 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5372 $fh = write_stream( $options->{ "data_out" } );
5374 print $fh "$_\n" foreach @{ $result };
5380 sub script_plot_lendist
5382 # Martin A. Hansen, August 2007.
5384 # Plot length distribution using GNU plot.
5386 my ( $in, # handle to in stream
5387 $out, # handle to out stream
5388 $options, # options hash
5393 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5395 $options->{ "title" } ||= "Length Distribution";
5397 while ( $record = get_record( $in ) )
5399 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5401 put_record( $record, $out ) if not $options->{ "no_stream" };
5404 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5406 for ( $i = 0; $i < $max; $i++ ) {
5407 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5410 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5412 $fh = write_stream( $options->{ "data_out" } );
5414 print $fh "$_\n" foreach @{ $result };
5420 sub script_plot_chrdist
5422 # Martin A. Hansen, August 2007.
5424 # Plot chromosome distribution using GNU plot.
5426 my ( $in, # handle to in stream
5427 $out, # handle to out stream
5428 $options, # options hash
5433 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5435 $options->{ "title" } ||= "Chromosome Distribution";
5437 while ( $record = get_record( $in ) )
5439 if ( $record->{ "CHR" } ) { # generic
5440 $data_hash{ $record->{ "CHR" } }++;
5441 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5442 $data_hash{ $record->{ "S_ID" } }++;
5443 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5444 $data_hash{ $record->{ "S_ID" } }++;
5445 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5446 $data_hash{ $record->{ "S_ID" } }++;
5449 put_record( $record, $out ) if not $options->{ "no_stream" };
5452 foreach $elem ( keys %data_hash )
5456 $sort_key =~ s/chr//i;
5458 $sort_key =~ s/^X(.*)/99$1/;
5459 $sort_key =~ s/^Y(.*)/99$1/;
5460 $sort_key =~ s/^Z(.*)/999$1/;
5461 $sort_key =~ s/^M(.*)/9999$1/;
5462 $sort_key =~ s/^U(.*)/99999$1/;
5464 $count = $sort_key =~ tr/_//;
5466 $sort_key =~ s/_.*/"999999" x $count/ex;
5468 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5471 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5473 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5475 $fh = write_stream( $options->{ "data_out" } );
5477 print $fh "$_\n" foreach @{ $result };
5483 sub script_plot_karyogram
5485 # Martin A. Hansen, August 2007.
5487 # Plot hits on karyogram.
5489 my ( $in, # handle to in stream
5490 $out, # handle to out stream
5491 $options, # options hash
5496 my ( %options, $record, @data, $fh, $result, %data_hash );
5498 $options->{ "genome" } ||= "human";
5499 $options->{ "feat_color" } ||= "black";
5501 while ( $record = get_record( $in ) )
5503 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5505 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5508 put_record( $record, $out ) if not $options->{ "no_stream" };
5511 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5513 $fh = write_stream( $options->{ "data_out" } );
5521 sub script_plot_matches
5523 # Martin A. Hansen, August 2007.
5525 # Plot matches in 2D generating a dotplot.
5527 my ( $in, # handle to in stream
5528 $out, # handle to out stream
5529 $options, # options hash
5534 my ( $record, @data, $fh, $result, %data_hash );
5536 $options->{ "direction" } ||= "both";
5538 while ( $record = get_record( $in ) )
5540 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5541 push @data, $record;
5544 put_record( $record, $out ) if not $options->{ "no_stream" };
5547 $options->{ "title" } ||= "plot_matches";
5548 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5549 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5551 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5553 $fh = write_stream( $options->{ "data_out" } );
5555 print $fh "$_\n" foreach @{ $result };
5561 sub script_length_vals
5563 # Martin A. Hansen, August 2007.
5565 # Determine the length of the value for given keys.
5567 my ( $in, # handle to in stream
5568 $out, # handle to out stream
5569 $options, # options hash
5574 my ( $record, $key );
5576 while ( $record = get_record( $in ) )
5578 foreach $key ( @{ $options->{ "keys" } } )
5580 if ( $record->{ $key } ) {
5581 $record->{ $key . "_LEN" } = length $record->{ $key };
5585 put_record( $record, $out );
5592 # Martin A. Hansen, August 2007.
5594 # Calculates the sums for values of given keys.
5596 my ( $in, # handle to in stream
5597 $out, # handle to out stream
5598 $options, # options hash
5603 my ( $record, $key, %sum_hash, $fh );
5605 while ( $record = get_record( $in ) )
5607 foreach $key ( @{ $options->{ "keys" } } )
5609 if ( $record->{ $key } ) {
5610 $sum_hash{ $key } += $record->{ $key };
5614 put_record( $record, $out ) if not $options->{ "no_stream" };
5617 $fh = write_stream( $options->{ "data_out" } );
5619 foreach $key ( @{ $options->{ "keys" } } ) {
5620 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5627 sub script_mean_vals
5629 # Martin A. Hansen, August 2007.
5631 # Calculate the mean of values of given keys.
5633 my ( $in, # handle to in stream
5634 $out, # handle to out stream
5635 $options, # options hash
5640 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5642 while ( $record = get_record( $in ) )
5644 foreach $key ( @{ $options->{ "keys" } } )
5646 if ( $record->{ $key } )
5648 $sum_hash{ $key } += $record->{ $key };
5649 $count_hash{ $key }++;
5653 put_record( $record, $out ) if not $options->{ "no_stream" };
5656 $fh = write_stream( $options->{ "data_out" } );
5658 foreach $key ( @{ $options->{ "keys" } } )
5660 if ( $count_hash{ $key } ) {
5661 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5666 put_record( { $key . "_MEAN" => $mean } , $fh );
5673 sub script_median_vals
5675 # Martin A. Hansen, March 2008.
5677 # Calculate the median values of given keys.
5679 my ( $in, # handle to in stream
5680 $out, # handle to out stream
5681 $options, # options hash
5686 my ( $record, $key, %median_hash, $median, $fh );
5688 while ( $record = get_record( $in ) )
5690 foreach $key ( @{ $options->{ "keys" } } ) {
5691 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5694 put_record( $record, $out ) if not $options->{ "no_stream" };
5697 $fh = write_stream( $options->{ "data_out" } );
5699 foreach $key ( @{ $options->{ "keys" } } )
5701 if ( $median_hash{ $key } ) {
5702 $median = Maasha::Calc::median( $median_hash{ $key } );
5707 put_record( { $key . "_MEDIAN" => $median } , $fh );
5716 # Martin A. Hansen, February 2008.
5718 # Determine the maximum values of given keys.
5720 my ( $in, # handle to in stream
5721 $out, # handle to out stream
5722 $options, # options hash
5727 my ( $record, $key, $fh, %max_hash, $max_record );
5729 while ( $record = get_record( $in ) )
5731 foreach $key ( @{ $options->{ "keys" } } )
5733 if ( $record->{ $key } )
5735 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5739 put_record( $record, $out ) if not $options->{ "no_stream" };
5742 $fh = write_stream( $options->{ "data_out" } );
5744 foreach $key ( @{ $options->{ "keys" } } )
5746 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5749 put_record( $max_record, $fh );
5757 # Martin A. Hansen, February 2008.
5759 # Determine the minimum values of given keys.
5761 my ( $in, # handle to in stream
5762 $out, # handle to out stream
5763 $options, # options hash
5768 my ( $record, $key, $fh, %min_hash, $min_record );
5770 while ( $record = get_record( $in ) )
5772 foreach $key ( @{ $options->{ "keys" } } )
5774 if ( defined $record->{ $key } )
5776 if ( exists $min_hash{ $key } ) {
5777 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5779 $min_hash{ $key } = $record->{ $key };
5784 put_record( $record, $out ) if not $options->{ "no_stream" };
5787 $fh = write_stream( $options->{ "data_out" } );
5789 foreach $key ( @{ $options->{ "keys" } } )
5791 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5794 put_record( $min_record, $fh );
5800 sub script_upload_to_ucsc
5802 # Martin A. Hansen, August 2007.
5804 # Calculate the mean of values of given keys.
5806 my ( $in, # handle to in stream
5807 $out, # handle to out stream
5808 $options, # options hash
5813 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5814 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5816 $options->{ "short_label" } ||= $options->{ 'table' };
5817 $options->{ "long_label" } ||= $options->{ 'table' };
5818 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5819 $options->{ "priority" } ||= 1;
5820 $options->{ "visibility" } ||= "pack";
5821 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5822 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5824 $file = "$BP_TMP/ucsc_upload.tmp";
5832 if ( $options->{ 'wiggle' } )
5834 $options->{ "visibility" } = "full";
5836 while ( $record = get_record( $in ) )
5838 put_record( $record, $out ) if not $options->{ "no_stream" };
5840 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5841 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5842 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5844 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5846 $fh_out = $fh_hash{ $record->{ "CHR" } };
5848 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5851 map { close $_ } keys %fh_hash;
5853 $fh_out = Maasha::Common::write_open( $file );
5855 foreach $chr ( sort keys %fh_hash )
5857 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5859 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5863 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5865 $chr = $entry->{ 'CHR' };
5866 $beg = $entry->{ 'CHR_BEG' };
5867 $end = $entry->{ 'CHR_END' };
5868 $q_id = $entry->{ 'Q_ID' };
5870 if ( $options->{ "score" } ) {
5871 $clones = $entry->{ 'SCORE' };
5872 } if ( $q_id =~ /_(\d+)$/ ) {
5882 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5887 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5888 $block->[ $i ] += $clones;
5891 $max = Maasha::Calc::max( $max, $end );
5900 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5901 $block->[ $i ] += $clones;
5908 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out, $options->{ "log10" } );
5910 unlink "$BP_TMP/$chr";
5915 $wig_file = "$options->{ 'table' }.wig";
5916 $wib_file = "$options->{ 'table' }.wib";
5918 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5920 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5922 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5924 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5925 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5935 $fh_out = Maasha::Common::write_open( $file );
5937 while ( $record = get_record( $in ) )
5939 put_record( $record, $out ) if not $options->{ "no_stream" };
5941 if ( $record->{ "REC_TYPE" } eq "PSL" )
5943 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5944 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5948 $format = "PSL" if not $format;
5950 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5952 # chrom chromStart chromEnd name score strand size secStr conf
5954 print $fh_out join ( "\t",
5956 $record->{ "CHR_BEG" },
5957 $record->{ "CHR_END" } + 1,
5958 $record->{ "Q_ID" },
5959 $record->{ "SCORE" },
5960 $record->{ "STRAND" },
5961 $record->{ "SIZE" },
5962 $record->{ "SEC_STRUCT" },
5963 $record->{ "CONF" },
5966 $format = "BED_SS" if not $format;
5968 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5970 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5972 $format = "BED" if not $format;
5973 $columns = $record->{ "BED_COLS" } if not $columns;
5975 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5977 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5979 $format = "BED" if not $format;
5980 $columns = 6 if not $columns;
5982 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5984 $record->{ "CHR" } = $record->{ "S_ID" };
5985 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5986 $record->{ "CHR_END" } = $record->{ "S_END" };
5987 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5989 $format = "BED" if not $format;
5990 $columns = 6 if not $columns;
5992 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5994 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5996 $record->{ "CHR" } = $record->{ "S_ID" };
5997 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5998 $record->{ "CHR_END" } = $record->{ "S_END" };
5999 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6000 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6002 $format = "BED" if not $format;
6003 $columns = 6 if not $columns;
6005 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6008 if ( $i == $options->{ "chunk_size" } )
6012 if ( $format eq "BED" ) {
6013 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6014 } elsif ( $format eq "PSL" ) {
6015 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6024 $fh_out = Maasha::Common::write_open( $file );
6033 if ( exists $options->{ "database" } and $options->{ "table" } )
6035 if ( $format eq "BED" )
6037 $type = "bed $columns";
6039 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6041 elsif ( $format eq "BED_SS" )
6043 $options->{ "sec_struct" } = 1;
6045 $type = "sec_struct";
6047 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6049 elsif ( $format eq "PSL" )
6053 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6055 elsif ( $format eq "WIGGLE" )
6059 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6064 Maasha::UCSC::update_my_tracks( $options, $type );
6069 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6074 # Martin A. Hansen, July 2008.
6076 # Given a biopiece record converts it to a FASTA record.
6077 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6078 # tried in that order.
6080 my ( $record, # record
6085 my ( $seq_name, $seq );
6087 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6088 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6090 if ( defined $seq_name and defined $seq ) {
6091 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6100 # Martin A. Hansen, July 2007.
6102 # Opens a stream to STDIN or a file,
6104 my ( $path, # path - OPTIONAL
6107 # Returns filehandle.
6111 if ( not -t STDIN ) {
6112 $fh = Maasha::Common::read_stdin();
6113 } elsif ( not $path ) {
6114 # Maasha::Common::error( qq(no data stream) );
6116 $fh = Maasha::Common::read_open( $path );
6119 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6127 # Martin A. Hansen, August 2007.
6129 # Opens a stream to STDOUT or a file.
6131 my ( $path, # path - OPTIONAL
6132 $gzip, # compress data - OPTIONAL
6135 # Returns filehandle.
6140 $fh = Maasha::Common::write_open( $path, $gzip );
6142 $fh = Maasha::Common::write_stdout();
6151 # Martin A. Hansen, July 2007.
6153 # Reads one record at a time and converts that record
6154 # to a Perl data structure (a hash) which is returned.
6156 my ( $fh, # handle to stream
6161 my ( $block, @lines, $line, $key, $value, %record );
6163 local $/ = "\n---\n";
6169 return if not defined $block;
6171 @lines = split "\n", $block;
6173 foreach $line ( @lines )
6175 ( $key, $value ) = split ": ", $line, 2;
6177 $record{ $key } = $value;
6180 return wantarray ? %record : \%record;
6186 # Martin A. Hansen, July 2007.
6188 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6190 my ( $data, # data structure
6191 $fh, # file handle - OPTIONAL
6196 if ( scalar keys %{ $data } )
6200 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6205 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6216 # Martin A. Hansen, November 2007.
6218 # Extracts files from an explicit GetOpt::Long argument
6219 # allowing for the use of glob. E.g.
6220 # --data_in=test.fna
6221 # --data_in=test.fna,test2.fna
6223 # --data_in=test.fna,/dir/*.fna
6225 my ( $option, # option from GetOpt::Long
6230 my ( $elem, @files );
6232 foreach $elem ( split ",", $option )
6236 } elsif ( $elem =~ /\*/ ) {
6237 push @files, glob( $elem );
6241 return wantarray ? @files : \@files;
6247 # Martin A. Hansen, April 2008.
6249 # Removes temporary directory and exits gracefully.
6250 # This subroutine is meant to be run always as the last
6251 # thing even if a script is dies or is interrupted
6254 my ( $sig, # signal from the %SIG
6257 # print STDERR "signal->$sig<-\n";
6265 if ( $sig =~ /MAASHA_ERROR/ ) {
6266 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6267 } elsif ( $sig eq "INT" ) {
6268 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6269 } elsif ( $sig eq "TERM" ) {
6270 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6272 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6284 # Martin A. Hansen, July 2008.
6286 # Cleans out any unused temporary files and direcotries in BP_TMP.
6290 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6292 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6294 $curr_pid = Maasha::Common::get_processid();
6296 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6298 foreach $dir ( @dirs )
6300 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6306 if ( $user eq Maasha::Common::get_user() )
6308 if ( not Maasha::Common::process_running( $pid ) )
6310 # print STDERR "Removing stale dir: $dir\n";
6311 Maasha::Common::dir_remove( $dir );
6313 elsif ( $pid == $curr_pid )
6315 # print STDERR "Removing current dir: $dir\n";
6316 Maasha::Common::dir_remove( $dir );
6330 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<