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 if ( $options->{ 'verbose' } )
3478 Maasha::Common::run(
3481 "-p $options->{ 'program' }",
3482 "-e $options->{ 'e_val' }",
3483 "-a $options->{ 'cpus' }",
3486 "-d $options->{ 'database' }",
3487 "-F $options->{ 'filter' }",
3495 Maasha::Common::run(
3498 "-p $options->{ 'program' }",
3499 "-e $options->{ 'e_val' }",
3500 "-a $options->{ 'cpus' }",
3503 "-d $options->{ 'database' }",
3504 "-F $options->{ 'filter' }",
3514 $fh_out = Maasha::Common::read_open( $tmp_out );
3518 while ( $line = <$fh_out> )
3522 next if $line =~ /^#/;
3524 @fields = split /\s+/, $line;
3526 $record->{ "REC_TYPE" } = "BLAST";
3527 $record->{ "Q_ID" } = $fields[ 0 ];
3528 $record->{ "S_ID" } = $fields[ 1 ];
3529 $record->{ "IDENT" } = $fields[ 2 ];
3530 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3531 $record->{ "MISMATCHES" } = $fields[ 4 ];
3532 $record->{ "GAPS" } = $fields[ 5 ];
3533 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3534 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3535 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3536 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3537 $record->{ "E_VAL" } = $fields[ 10 ];
3538 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3540 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3542 $record->{ "STRAND" } = '-';
3544 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3548 $record->{ "STRAND" } = '+';
3551 put_record( $record, $out );
3562 # Martin A. Hansen, August 2007.
3564 # BLATs sequences in stream against a given genome.
3566 my ( $in, # handle to in stream
3567 $out, # handle to out stream
3568 $options, # options hash
3573 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3575 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3577 $options->{ 'tile_size' } ||= 11;
3578 $options->{ 'one_off' } ||= 0;
3579 $options->{ 'min_identity' } ||= 90;
3580 $options->{ 'min_score' } ||= 0;
3581 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3583 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3584 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3585 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3586 $blat_args .= " -minScore=$options->{ 'min_score' }";
3587 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3588 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3590 $query_file = "$BP_TMP/blat.seq";
3592 $fh_out = Maasha::Common::write_open( $query_file );
3594 while ( $record = get_record( $in ) )
3596 if ( $entry = record2fasta( $record ) )
3598 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3599 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3602 put_record( $record, $out );
3607 $blat_args .= " -t=dnax" if $type eq "protein";
3608 $blat_args .= " -q=$type";
3610 $result_file = "$BP_TMP/blat.psl";
3612 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3616 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3618 map { put_record( $_, $out ) } @{ $entries };
3620 unlink $result_file;
3626 # Martin A. Hansen, July 2008.
3628 # soap sequences in stream against a given file or genome.
3630 my ( $in, # handle to in stream
3631 $out, # handle to out stream
3632 $options, # options hash
3637 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3639 $options->{ "seed_size" } ||= 10;
3640 $options->{ "mismatches" } ||= 2;
3641 $options->{ "gap_size" } ||= 0;
3642 $options->{ "cpus" } ||= 1;
3644 if ( $options->{ "genome" } ) {
3645 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3648 $tmp_in = "$BP_TMP/soap_query.seq";
3649 $tmp_out = "$BP_TMP/soap.result";
3651 $fh_out = Maasha::Common::write_open( $tmp_in );
3655 while ( $record = get_record( $in ) )
3657 if ( $entry = record2fasta( $record ) )
3659 Maasha::Fasta::put_entry( $entry, $fh_out );
3664 put_record( $record, $out );
3672 "-s $options->{ 'seed_size' }",
3675 "-v $options->{ 'mismatches' }",
3676 "-g $options->{ 'gap_size' }",
3677 "-p $options->{ 'cpus' }",
3678 "-d $options->{ 'in_file' }",
3682 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3684 Maasha::Common::run( "soap", $args, 1 );
3688 $fh_out = Maasha::Common::read_open( $tmp_out );
3692 while ( $line = <$fh_out> )
3696 @fields = split /\t/, $line;
3698 $record->{ "REC_TYPE" } = "SOAP";
3699 $record->{ "Q_ID" } = $fields[ 0 ];
3700 $record->{ "SCORE" } = $fields[ 3 ];
3701 $record->{ "STRAND" } = $fields[ 6 ];
3702 $record->{ "S_ID" } = $fields[ 7 ];
3703 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3704 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3706 put_record( $record, $out );
3716 sub script_match_seq
3718 # Martin A. Hansen, August 2007.
3720 # BLATs sequences in stream against a given genome.
3722 my ( $in, # handle to in stream
3723 $out, # handle to out stream
3724 $options, # options hash
3729 my ( $record, @entries, $results );
3731 $options->{ "word_size" } ||= 20;
3732 $options->{ "direction" } ||= "both";
3734 while ( $record = get_record( $in ) )
3736 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3737 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3740 put_record( $record, $out );
3743 if ( @entries == 1 )
3745 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3747 map { put_record( $_, $out ) } @{ $results };
3749 elsif ( @entries == 2 )
3751 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3753 map { put_record( $_, $out ) } @{ $results };
3758 sub script_create_vmatch_index
3760 # Martin A. Hansen, January 2008.
3762 # Create a vmatch index from sequences in the stream.
3764 my ( $in, # handle to in stream
3765 $out, # handle to out stream
3766 $options, # options hash
3771 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3773 if ( $options->{ "index_name" } )
3775 $file_tmp = $options->{ 'index_name' };
3776 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3779 while ( $record = get_record( $in ) )
3781 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3783 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3785 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3788 put_record( $record, $out ) if not $options->{ "no_stream" };
3791 if ( $options->{ "index_name" } )
3795 if ( $type eq "protein" ) {
3796 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3798 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3806 sub script_vmatch_seq
3808 # Martin A. Hansen, August 2007.
3810 # Vmatches sequences in stream against a given genome.
3812 my ( $in, # handle to in stream
3813 $out, # handle to out stream
3814 $options, # options hash
3819 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3821 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3823 if ( $options->{ "index_name" } )
3825 @index_files = $options->{ "index_name" };
3829 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3831 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3833 @index_files = sort keys %hash;
3836 while ( $record = get_record( $in ) )
3838 push @records, $record;
3840 put_record( $record, $out );
3843 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3847 $fh_in = Maasha::Common::read_open( $result_file );
3849 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3850 put_record( $record, $out );
3855 unlink $result_file;
3859 sub script_write_fasta
3861 # Martin A. Hansen, August 2007.
3863 # Write FASTA entries from sequences in stream.
3865 my ( $in, # handle to in stream
3866 $out, # handle to out stream
3867 $options, # options hash
3872 my ( $record, $fh, $entry );
3874 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3876 while ( $record = get_record( $in ) )
3878 if ( $entry = record2fasta( $record ) ) {
3879 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3882 put_record( $record, $out ) if not $options->{ "no_stream" };
3889 sub script_write_align
3891 # Martin A. Hansen, August 2007.
3893 # Write pretty alignments aligned sequences in stream.
3895 my ( $in, # handle to in stream
3896 $out, # handle to out stream
3897 $options, # options hash
3902 my ( $fh, $record, @entries );
3904 $fh = write_stream( $options->{ "data_out" } ) ;
3906 while ( $record = get_record( $in ) )
3908 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3909 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3912 put_record( $record, $out ) if not $options->{ "no_stream" };
3915 if ( scalar( @entries ) == 2 ) {
3916 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3917 } elsif ( scalar ( @entries ) > 2 ) {
3918 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3925 sub script_write_blast
3927 # Martin A. Hansen, November 2007.
3929 # Write data in blast table format (-m8 and 9).
3931 my ( $in, # handle to in stream
3932 $out, # handle to out stream
3933 $options, # options hash
3938 my ( $fh, $record, $first );
3940 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3944 while ( $record = get_record( $in ) )
3946 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3948 if ( $options->{ "comment" } and $first )
3950 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";
3955 if ( $record->{ "STRAND" } eq "-" ) {
3956 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3959 print $fh join( "\t",
3960 $record->{ "Q_ID" },
3961 $record->{ "S_ID" },
3962 $record->{ "IDENT" },
3963 $record->{ "ALIGN_LEN" },
3964 $record->{ "MISMATCHES" },
3965 $record->{ "GAPS" },
3966 $record->{ "Q_BEG" } + 1,
3967 $record->{ "Q_END" } + 1,
3968 $record->{ "S_BEG" } + 1,
3969 $record->{ "S_END" } + 1,
3970 $record->{ "E_VAL" },
3971 $record->{ "BIT_SCORE" }
3975 put_record( $record, $out ) if not $options->{ "no_stream" };
3982 sub script_write_tab
3984 # Martin A. Hansen, August 2007.
3986 # Write data as table.
3988 my ( $in, # handle to in stream
3989 $out, # handle to out stream
3990 $options, # options hash
3995 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3997 $options->{ "delimit" } ||= "\t";
3999 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4001 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4003 while ( $record = get_record( $in ) )
4008 if ( $options->{ "keys" } )
4010 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4014 foreach $key ( @{ $options->{ "keys" } } )
4016 if ( exists $record->{ $key } )
4018 push @keys, $key if $options->{ "comment" };
4019 push @vals, $record->{ $key };
4026 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4028 next if exists $no_keys{ $key };
4030 push @keys, $key if $options->{ "comment" };
4031 push @vals, $record->{ $key };
4035 if ( @keys and $options->{ "comment" } )
4037 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4039 delete $options->{ "comment" };
4042 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4044 put_record( $record, $out ) if not $options->{ "no_stream" };
4051 sub script_write_bed
4053 # Martin A. Hansen, August 2007.
4055 # Write BED format for the UCSC genome browser using records in stream.
4057 my ( $in, # handle to in stream
4058 $out, # handle to out stream
4059 $options, # options hash
4064 my ( $fh, $record, $new_record );
4066 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4068 while ( $record = get_record( $in ) )
4070 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4072 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4074 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4076 $new_record->{ "CHR" } = $record->{ "S_ID" };
4077 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4078 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4079 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4080 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4081 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4083 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4085 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4087 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4089 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4091 $new_record->{ "CHR" } = $record->{ "S_ID" };
4092 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4093 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4094 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4095 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4096 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4098 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4100 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4102 $new_record->{ "CHR" } = $record->{ "S_ID" };
4103 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4104 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4105 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4106 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4107 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4109 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4111 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4113 $new_record->{ "CHR" } = $record->{ "S_ID" };
4114 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4115 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4116 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4117 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4118 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4120 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4122 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4124 Maasha::UCSC::bed_put_entry( $record, $fh );
4127 put_record( $record, $out ) if not $options->{ "no_stream" };
4134 sub script_write_psl
4136 # Martin A. Hansen, August 2007.
4138 # Write PSL output from stream.
4140 my ( $in, # handle to in stream
4141 $out, # handle to out stream
4142 $options, # options hash
4147 my ( $fh, $record, @output, $first );
4151 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4153 while ( $record = get_record( $in ) )
4155 put_record( $record, $out ) if not $options->{ "no_stream" };
4157 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4159 Maasha::UCSC::psl_put_header( $fh ) if $first;
4160 Maasha::UCSC::psl_put_entry( $record, $fh );
4169 sub script_write_fixedstep
4171 # Martin A. Hansen, Juli 2008.
4173 # Write fixedStep entries from recrods in the stream.
4175 my ( $in, # handle to in stream
4176 $out, # handle to out stream
4177 $options, # options hash
4182 my ( $fh, $record, $vals );
4184 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4186 while ( $record = get_record( $in ) )
4188 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4190 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4192 $vals = $record->{ 'VALS' };
4196 print $fh "$vals\n";
4199 put_record( $record, $out ) if not $options->{ "no_stream" };
4206 sub script_write_2bit
4208 # Martin A. Hansen, March 2008.
4210 # Write sequence entries from stream in 2bit format.
4212 my ( $in, # handle to in stream
4213 $out, # handle to out stream
4214 $options, # options hash
4219 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4221 $mask = 1 if not $options->{ "no_mask" };
4223 $tmp_file = "$BP_TMP/write_2bit.fna";
4224 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4226 $fh_out = write_stream( $options->{ "data_out" } );
4228 while ( $record = get_record( $in ) )
4230 if ( $entry = record2fasta( $record ) ) {
4231 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4234 put_record( $record, $out ) if not $options->{ "no_stream" };
4239 $fh_in = Maasha::Common::read_open( $tmp_file );
4241 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4250 sub script_write_solid
4252 # Martin A. Hansen, April 2008.
4254 # Write di-base encoded Solid sequence from entries in stream.
4256 my ( $in, # handle to in stream
4257 $out, # handle to out stream
4258 $options, # options hash
4263 my ( $record, $fh, $entry );
4265 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4267 while ( $record = get_record( $in ) )
4269 if ( $entry = record2fasta( $record ) )
4271 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4273 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4276 put_record( $record, $out ) if not $options->{ "no_stream" };
4283 sub script_plot_seqlogo
4285 # Martin A. Hansen, August 2007.
4287 # Calculates and writes a sequence logo for alignments.
4289 my ( $in, # handle to in stream
4290 $out, # handle to out stream
4291 $options, # options hash
4296 my ( $record, @entries, $logo, $fh );
4298 while ( $record = get_record( $in ) )
4300 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4301 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4304 put_record( $record, $out ) if not $options->{ "no_stream" };
4307 $logo = Maasha::Plot::seq_logo( \@entries );
4309 $fh = write_stream( $options->{ "data_out" } );
4317 sub script_plot_phastcons_profiles
4319 # Martin A. Hansen, January 2008.
4321 # Plots PhastCons profiles.
4323 my ( $in, # handle to in stream
4324 $out, # handle to out stream
4325 $options, # options hash
4330 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4332 $options->{ "title" } ||= "PhastCons Profiles";
4334 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4335 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4337 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4338 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4340 while ( $record = get_record( $in ) )
4342 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4344 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4346 push @{ $AoA }, [ @{ $scores } ];
4349 put_record( $record, $out ) if not $options->{ "no_stream" };
4352 Maasha::UCSC::phastcons_normalize( $AoA );
4354 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4355 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4357 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4359 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4361 $fh = write_stream( $options->{ "data_out" } );
4363 print $fh "$_\n" foreach @{ $plot };
4369 sub script_analyze_bed
4371 # Martin A. Hansen, March 2008.
4373 # Analyze BED entries in stream.
4375 my ( $in, # handle to in stream
4376 $out, # handle to out stream
4377 $options, # options hash
4384 while ( $record = get_record( $in ) )
4386 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4388 put_record( $record, $out );
4393 sub script_analyze_vals
4395 # Martin A. Hansen, August 2007.
4397 # Analyze values for given keys in stream.
4399 my ( $in, # handle to in stream
4400 $out, # handle to out stream
4401 $options, # options hash
4406 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4408 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4410 while ( $record = get_record( $in ) )
4412 foreach $key ( keys %{ $record } )
4414 next if $options->{ "keys" } and not exists $key_hash{ $key };
4416 $analysis->{ $key }->{ "COUNT" }++;
4418 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4420 $analysis->{ $key }->{ "TYPE" } = "num";
4421 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4422 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4423 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4427 $len = length $record->{ $key };
4429 $analysis->{ $key }->{ "TYPE" } = "alph";
4430 $analysis->{ $key }->{ "SUM" } += $len;
4431 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4432 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4436 put_record( $record, $out ) if not $options->{ "no_stream" };
4439 foreach $key ( keys %{ $analysis } )
4441 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4442 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4445 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4455 if ( $options->{ "keys" } ) {
4456 @keys = @{ $options->{ "keys" } };
4458 @keys = keys %{ $analysis };
4461 foreach $key ( @keys )
4463 $keys .= sprintf "% 15s", $key;
4464 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4465 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4466 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4467 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4468 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4469 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4472 print $out "$keys\n";
4473 print $out "$types\n";
4474 print $out "$counts\n";
4475 print $out "$mins\n";
4476 print $out "$maxs\n";
4477 print $out "$sums\n";
4478 print $out "$means\n";
4482 sub script_head_records
4484 # Martin A. Hansen, August 2007.
4486 # Display the first sequences in stream.
4488 my ( $in, # handle to in stream
4489 $out, # handle to out stream
4490 $options, # options hash
4495 my ( $record, $count );
4497 $options->{ "num" } ||= 10;
4501 while ( $record = get_record( $in ) )
4505 put_record( $record, $out );
4507 last if $count == $options->{ "num" };
4512 sub script_remove_keys
4514 # Martin A. Hansen, August 2007.
4516 # Remove keys from stream.
4518 my ( $in, # handle to in stream
4519 $out, # handle to out stream
4520 $options, # options hash
4525 my ( $record, $new_record );
4527 while ( $record = get_record( $in ) )
4529 if ( $options->{ "keys" } )
4531 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4533 elsif ( $options->{ "save_keys" } )
4535 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4537 $record = $new_record;
4540 put_record( $record, $out ) if keys %{ $record };
4545 sub script_rename_keys
4547 # Martin A. Hansen, August 2007.
4549 # Rename keys in stream.
4551 my ( $in, # handle to in stream
4552 $out, # handle to out stream
4553 $options, # options hash
4560 while ( $record = get_record( $in ) )
4562 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4564 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4566 delete $record->{ $options->{ "keys" }->[ 0 ] };
4569 put_record( $record, $out );
4574 sub script_uniq_vals
4576 # Martin A. Hansen, August 2007.
4578 # Find unique values in stream.
4580 my ( $in, # handle to in stream
4581 $out, # handle to out stream
4582 $options, # options hash
4587 my ( %hash, $record );
4589 while ( $record = get_record( $in ) )
4591 if ( $record->{ $options->{ "key" } } )
4593 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4595 put_record( $record, $out );
4597 $hash{ $record->{ $options->{ "key" } } } = 1;
4599 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4601 put_record( $record, $out );
4605 $hash{ $record->{ $options->{ "key" } } } = 1;
4610 put_record( $record, $out );
4616 sub script_merge_vals
4618 # Martin A. Hansen, August 2007.
4620 # Rename keys in stream.
4622 my ( $in, # handle to in stream
4623 $out, # handle to out stream
4624 $options, # options hash
4629 my ( $record, @join, $i );
4631 $options->{ "delimit" } ||= '_';
4633 while ( $record = get_record( $in ) )
4635 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4637 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4639 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4640 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4643 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4646 put_record( $record, $out );
4651 sub script_merge_records
4653 # Martin A. Hansen, July 2008.
4655 # Merges records in the stream based on identical values of two given keys.
4657 my ( $in, # handle to in stream
4658 $out, # handle to out stream
4659 $options, # options hash
4664 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4665 $num1, $num2, $num, $cmp, $i );
4667 $merge = $options->{ "merge" } || "AandB";
4669 $file1 = "$BP_TMP/merge_records1.tmp";
4670 $file2 = "$BP_TMP/merge_records2.tmp";
4672 $fh1 = Maasha::Common::write_open( $file1 );
4673 $fh2 = Maasha::Common::write_open( $file2 );
4675 $key1 = $options->{ "keys" }->[ 0 ];
4676 $key2 = $options->{ "keys" }->[ 1 ];
4678 $num = $key2 =~ s/n$//;
4682 while ( $record = get_record( $in ) )
4684 if ( exists $record->{ $key1 } )
4687 @vals1 = $record->{ $key1 };
4689 delete $record->{ $key1 };
4691 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4693 print $fh1 join( "\t", @vals1 ), "\n";
4697 elsif ( exists $record->{ $key2 } )
4700 @vals2 = $record->{ $key2 };
4702 delete $record->{ $key2 };
4704 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4706 print $fh2 join( "\t", @vals2 ), "\n";
4717 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4718 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4722 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4723 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4726 $fh1 = Maasha::Common::read_open( $file1 );
4727 $fh2 = Maasha::Common::read_open( $file2 );
4729 @vals1 = Maasha::Common::get_fields( $fh1 );
4730 @vals2 = Maasha::Common::get_fields( $fh2 );
4732 while ( $num1 > 0 and $num2 > 0 )
4737 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4739 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4744 if ( $merge =~ /^(AorB|AnotB)$/ )
4746 for ( $i = 0; $i < @keys1; $i++ ) {
4747 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4750 put_record( $record, $out );
4753 @vals1 = Maasha::Common::get_fields( $fh1 );
4758 if ( $merge =~ /^(BorA|BnotA)$/ )
4760 for ( $i = 0; $i < @keys2; $i++ ) {
4761 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4764 put_record( $record, $out );
4767 @vals2 = Maasha::Common::get_fields( $fh2 );
4772 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4774 for ( $i = 0; $i < @keys1; $i++ ) {
4775 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4778 for ( $i = 1; $i < @keys2; $i++ ) {
4779 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4782 put_record( $record, $out );
4785 @vals1 = Maasha::Common::get_fields( $fh1 );
4786 @vals2 = Maasha::Common::get_fields( $fh2 );
4798 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4802 for ( $i = 0; $i < @keys1; $i++ ) {
4803 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4806 put_record( $record, $out );
4809 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4813 for ( $i = 0; $i < @keys2; $i++ ) {
4814 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4817 put_record( $record, $out );
4824 # Martin A. Hansen, August 2007.
4826 # Grab for records in stream.
4828 my ( $in, # handle to in stream
4829 $out, # handle to out stream
4830 $options, # options hash
4835 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4837 if ( $options->{ "patterns" } )
4839 $patterns = [ split ",", $options->{ "patterns" } ];
4841 elsif ( -f $options->{ "patterns_in" } )
4843 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4845 elsif ( -f $options->{ "exact_in" } )
4847 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4849 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4854 if ( $options->{ "eval" } )
4856 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4864 while ( $record = get_record( $in ) )
4870 if ( $options->{ "keys" } )
4872 foreach $key ( @{ $options->{ "keys" } } )
4874 if ( exists $lookup_hash{ $record->{ $key } } )
4883 foreach $key ( keys %{ $record } )
4885 if ( not $options->{ "vals_only" } )
4887 if ( exists $lookup_hash{ $key } )
4894 if ( not $options->{ "keys_only" } )
4896 if ( exists $lookup_hash{ $record->{ $key } } )
4907 foreach $pattern ( @{ $patterns } )
4909 if ( $options->{ "keys" } )
4911 foreach $key ( @{ $options->{ "keys" } } )
4913 $pos = index $record->{ $key }, $pattern;
4915 goto FOUND if $pos >= 0;
4920 foreach $key ( keys %{ $record } )
4922 if ( not $options->{ "vals_only" } )
4924 $pos = index $key, $pattern;
4926 goto FOUND if $pos >= 0;
4929 if ( not $options->{ "keys_only" } )
4931 $pos = index $record->{ $key }, $pattern;
4933 goto FOUND if $pos >= 0;
4939 elsif ( $options->{ "regex" } )
4941 if ( $options->{ "keys" } )
4943 foreach $key ( @{ $options->{ "keys" } } )
4945 if ( $options->{ "case_insensitive" } ) {
4946 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4948 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4951 goto FOUND if $pos >= 0;
4956 foreach $key ( keys %{ $record } )
4958 if ( not $options->{ "vals_only" } )
4960 if ( $options->{ "case_insensitive" } ) {
4961 $pos = 1 if $key =~ /$options->{'regex'}/i;
4963 $pos = 1 if $key =~ /$options->{'regex'}/;
4966 goto FOUND if $pos >= 0;
4969 if ( not $options->{ "keys_only" } )
4971 if ( $options->{ "case_insensitive" } ) {
4972 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4974 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4977 goto FOUND if $pos >= 0;
4982 elsif ( $options->{ "eval" } )
4984 if ( defined $record->{ $key } )
4986 if ( $op eq "<" and $record->{ $key } < $val ) {
4987 $pos = 1 and goto FOUND;
4988 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4989 $pos = 1 and goto FOUND;
4990 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4991 $pos = 1 and goto FOUND;
4992 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4993 $pos = 1 and goto FOUND;
4994 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4995 $pos = 1 and goto FOUND;
4996 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4997 $pos = 1 and goto FOUND;
4998 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4999 $pos = 1 and goto FOUND;
5000 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
5001 $pos = 1 and goto FOUND;
5008 if ( $pos >= 0 and not $options->{ "invert" } ) {
5009 put_record( $record, $out );
5010 } elsif ( $pos < 0 and $options->{ "invert" } ) {
5011 put_record( $record, $out );
5019 # Martin A. Hansen, August 2007.
5021 # Evaluate extression for records in stream.
5023 my ( $in, # handle to in stream
5024 $out, # handle to out stream
5025 $options, # options hash
5030 my ( $record, $eval_key, $eval_val, $check, @keys );
5032 while ( $record = get_record( $in ) )
5034 if ( $options->{ "eval" } )
5036 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5044 @keys = split /\W+/, $eval_val;
5045 @keys = grep { ! /^\d+$/ } @keys;
5050 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5052 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5055 put_record( $record, $out );
5062 # Martin A. Hansen, June 2008.
5066 my ( $in, # handle to in stream
5067 $out, # handle to out stream
5068 $options, # options hash
5073 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5075 while ( $record = get_record( $in ) )
5079 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5081 push @rows, $record->{ $key };
5085 push @matrix, [ @rows ];
5090 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5092 foreach $row ( @matrix )
5094 for ( $i = 0; $i < @{ $row }; $i++ ) {
5095 $record->{ "V$i" } = $row->[ $i ];
5098 put_record( $record, $out );
5103 sub script_add_ident
5105 # Martin A. Hansen, May 2008.
5107 # Add a unique identifier to each record in stream.
5109 my ( $in, # handle to in stream
5110 $out, # handle to out stream
5111 $options, # options hash
5116 my ( $record, $key, $prefix, $i );
5118 $key = $options->{ "key" } || "ID";
5119 $prefix = $options->{ "prefix" } || "ID";
5123 while ( $record = get_record( $in ) )
5125 $record->{ $key } = sprintf( "$prefix%08d", $i );
5127 put_record( $record, $out );
5134 sub script_count_records
5136 # Martin A. Hansen, August 2007.
5138 # Count records in stream.
5140 my ( $in, # handle to in stream
5141 $out, # handle to out stream
5142 $options, # options hash
5147 my ( $record, $count, $result, $fh, $line );
5151 if ( $options->{ "no_stream" } )
5153 while ( $line = <$in> )
5157 $count++ if $line eq "---";
5162 while ( $record = get_record( $in ) )
5164 put_record( $record, $out );
5170 $result = { "RECORDS_COUNT" => $count };
5172 $fh = write_stream( $options->{ "data_out" } );
5174 put_record( $result, $fh );
5180 sub script_random_records
5182 # Martin A. Hansen, August 2007.
5184 # Pick a number or random records from stream.
5186 my ( $in, # handle to in stream
5187 $out, # handle to out stream
5188 $options, # options hash
5193 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5195 $options->{ "num" } ||= 10;
5197 $tmp_file = "$BP_TMP/random_records.tmp";
5199 $fh_out = Maasha::Common::write_open( $tmp_file );
5203 while ( $record = get_record( $in ) )
5205 put_record( $record, $fh_out );
5215 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5217 while ( $i < $options->{ "num" } )
5219 $rand = int( rand( $count ) );
5221 if ( not exists $rand_hash{ $rand } )
5223 $rand_hash{ $rand } = 1;
5225 $max = $rand if $rand > $max;
5231 $fh_in = Maasha::Common::read_open( $tmp_file );
5235 while ( $record = get_record( $fh_in ) )
5237 put_record( $record, $out ) if exists $rand_hash{ $count };
5239 last if $count == $max;
5250 sub script_sort_records
5252 # Martin A. Hansen, August 2007.
5254 # Sort to sort records according to keys.
5256 my ( $in, # handle to in stream
5257 $out, # handle to out stream
5258 $options, # options hash
5263 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5265 foreach $key ( @{ $options->{ "keys" } } )
5267 if ( $key =~ s/n$// ) {
5268 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5270 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5274 $sort_str = join " or ", @sort_cmd;
5275 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5277 while ( $record = get_record( $in ) ) {
5278 push @records, $record;
5281 @records = sort $sort_sub @records;
5283 if ( $options->{ "reverse" } )
5285 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5286 put_record( $records[ $i ], $out );
5291 for ( $i = 0; $i < scalar @records; $i++ ) {
5292 put_record( $records[ $i ], $out );
5298 sub script_count_vals
5300 # Martin A. Hansen, August 2007.
5302 # Count records in stream.
5304 my ( $in, # handle to in stream
5305 $out, # handle to out stream
5306 $options, # options hash
5311 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5313 $tmp_file = "$BP_TMP/count_cache.tmp";
5315 $fh_out = Maasha::Common::write_open( $tmp_file );
5320 while ( $record = get_record( $in ) )
5322 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5324 push @records, $record;
5326 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5328 map { put_record( $_, $fh_out ) } @records;
5335 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5346 $fh_in = Maasha::Common::read_open( $tmp_file );
5348 while ( $record = get_record( $fh_in ) )
5350 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5352 put_record( $record, $out );
5354 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5362 foreach $record ( @records )
5364 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5366 put_record( $record, $out );
5373 sub script_plot_histogram
5375 # Martin A. Hansen, September 2007.
5377 # Plot a simple histogram for a given key using GNU plot.
5379 my ( $in, # handle to in stream
5380 $out, # handle to out stream
5381 $options, # options hash
5386 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5388 $options->{ "title" } ||= "Histogram";
5389 $options->{ "sort" } ||= "num";
5391 while ( $record = get_record( $in ) )
5393 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5395 put_record( $record, $out ) if not $options->{ "no_stream" };
5398 if ( $options->{ "sort" } eq "num" ) {
5399 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5401 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5404 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5406 $fh = write_stream( $options->{ "data_out" } );
5408 print $fh "$_\n" foreach @{ $result };
5414 sub script_plot_lendist
5416 # Martin A. Hansen, August 2007.
5418 # Plot length distribution using GNU plot.
5420 my ( $in, # handle to in stream
5421 $out, # handle to out stream
5422 $options, # options hash
5427 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5429 $options->{ "title" } ||= "Length Distribution";
5431 while ( $record = get_record( $in ) )
5433 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5435 put_record( $record, $out ) if not $options->{ "no_stream" };
5438 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5440 for ( $i = 0; $i < $max; $i++ ) {
5441 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5444 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5446 $fh = write_stream( $options->{ "data_out" } );
5448 print $fh "$_\n" foreach @{ $result };
5454 sub script_plot_chrdist
5456 # Martin A. Hansen, August 2007.
5458 # Plot chromosome distribution using GNU plot.
5460 my ( $in, # handle to in stream
5461 $out, # handle to out stream
5462 $options, # options hash
5467 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5469 $options->{ "title" } ||= "Chromosome Distribution";
5471 while ( $record = get_record( $in ) )
5473 if ( $record->{ "CHR" } ) { # generic
5474 $data_hash{ $record->{ "CHR" } }++;
5475 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5476 $data_hash{ $record->{ "S_ID" } }++;
5477 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5478 $data_hash{ $record->{ "S_ID" } }++;
5479 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5480 $data_hash{ $record->{ "S_ID" } }++;
5483 put_record( $record, $out ) if not $options->{ "no_stream" };
5486 foreach $elem ( keys %data_hash )
5490 $sort_key =~ s/chr//i;
5492 $sort_key =~ s/^X(.*)/99$1/;
5493 $sort_key =~ s/^Y(.*)/99$1/;
5494 $sort_key =~ s/^Z(.*)/999$1/;
5495 $sort_key =~ s/^M(.*)/9999$1/;
5496 $sort_key =~ s/^U(.*)/99999$1/;
5498 $count = $sort_key =~ tr/_//;
5500 $sort_key =~ s/_.*/"999999" x $count/ex;
5502 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5505 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5507 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5509 $fh = write_stream( $options->{ "data_out" } );
5511 print $fh "$_\n" foreach @{ $result };
5517 sub script_plot_karyogram
5519 # Martin A. Hansen, August 2007.
5521 # Plot hits on karyogram.
5523 my ( $in, # handle to in stream
5524 $out, # handle to out stream
5525 $options, # options hash
5530 my ( %options, $record, @data, $fh, $result, %data_hash );
5532 $options->{ "genome" } ||= "human";
5533 $options->{ "feat_color" } ||= "black";
5535 while ( $record = get_record( $in ) )
5537 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5539 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5542 put_record( $record, $out ) if not $options->{ "no_stream" };
5545 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5547 $fh = write_stream( $options->{ "data_out" } );
5555 sub script_plot_matches
5557 # Martin A. Hansen, August 2007.
5559 # Plot matches in 2D generating a dotplot.
5561 my ( $in, # handle to in stream
5562 $out, # handle to out stream
5563 $options, # options hash
5568 my ( $record, @data, $fh, $result, %data_hash );
5570 $options->{ "direction" } ||= "both";
5572 while ( $record = get_record( $in ) )
5574 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5575 push @data, $record;
5578 put_record( $record, $out ) if not $options->{ "no_stream" };
5581 $options->{ "title" } ||= "plot_matches";
5582 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5583 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5585 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5587 $fh = write_stream( $options->{ "data_out" } );
5589 print $fh "$_\n" foreach @{ $result };
5595 sub script_length_vals
5597 # Martin A. Hansen, August 2007.
5599 # Determine the length of the value for given keys.
5601 my ( $in, # handle to in stream
5602 $out, # handle to out stream
5603 $options, # options hash
5608 my ( $record, $key );
5610 while ( $record = get_record( $in ) )
5612 foreach $key ( @{ $options->{ "keys" } } )
5614 if ( $record->{ $key } ) {
5615 $record->{ $key . "_LEN" } = length $record->{ $key };
5619 put_record( $record, $out );
5626 # Martin A. Hansen, August 2007.
5628 # Calculates the sums for values of given keys.
5630 my ( $in, # handle to in stream
5631 $out, # handle to out stream
5632 $options, # options hash
5637 my ( $record, $key, %sum_hash, $fh );
5639 while ( $record = get_record( $in ) )
5641 foreach $key ( @{ $options->{ "keys" } } )
5643 if ( $record->{ $key } ) {
5644 $sum_hash{ $key } += $record->{ $key };
5648 put_record( $record, $out ) if not $options->{ "no_stream" };
5651 $fh = write_stream( $options->{ "data_out" } );
5653 foreach $key ( @{ $options->{ "keys" } } ) {
5654 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5661 sub script_mean_vals
5663 # Martin A. Hansen, August 2007.
5665 # Calculate the mean of values of given keys.
5667 my ( $in, # handle to in stream
5668 $out, # handle to out stream
5669 $options, # options hash
5674 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5676 while ( $record = get_record( $in ) )
5678 foreach $key ( @{ $options->{ "keys" } } )
5680 if ( $record->{ $key } )
5682 $sum_hash{ $key } += $record->{ $key };
5683 $count_hash{ $key }++;
5687 put_record( $record, $out ) if not $options->{ "no_stream" };
5690 $fh = write_stream( $options->{ "data_out" } );
5692 foreach $key ( @{ $options->{ "keys" } } )
5694 if ( $count_hash{ $key } ) {
5695 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5700 put_record( { $key . "_MEAN" => $mean } , $fh );
5707 sub script_median_vals
5709 # Martin A. Hansen, March 2008.
5711 # Calculate the median values of given keys.
5713 my ( $in, # handle to in stream
5714 $out, # handle to out stream
5715 $options, # options hash
5720 my ( $record, $key, %median_hash, $median, $fh );
5722 while ( $record = get_record( $in ) )
5724 foreach $key ( @{ $options->{ "keys" } } ) {
5725 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5728 put_record( $record, $out ) if not $options->{ "no_stream" };
5731 $fh = write_stream( $options->{ "data_out" } );
5733 foreach $key ( @{ $options->{ "keys" } } )
5735 if ( $median_hash{ $key } ) {
5736 $median = Maasha::Calc::median( $median_hash{ $key } );
5741 put_record( { $key . "_MEDIAN" => $median } , $fh );
5750 # Martin A. Hansen, February 2008.
5752 # Determine the maximum values of given keys.
5754 my ( $in, # handle to in stream
5755 $out, # handle to out stream
5756 $options, # options hash
5761 my ( $record, $key, $fh, %max_hash, $max_record );
5763 while ( $record = get_record( $in ) )
5765 foreach $key ( @{ $options->{ "keys" } } )
5767 if ( $record->{ $key } )
5769 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5773 put_record( $record, $out ) if not $options->{ "no_stream" };
5776 $fh = write_stream( $options->{ "data_out" } );
5778 foreach $key ( @{ $options->{ "keys" } } )
5780 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5783 put_record( $max_record, $fh );
5791 # Martin A. Hansen, February 2008.
5793 # Determine the minimum values of given keys.
5795 my ( $in, # handle to in stream
5796 $out, # handle to out stream
5797 $options, # options hash
5802 my ( $record, $key, $fh, %min_hash, $min_record );
5804 while ( $record = get_record( $in ) )
5806 foreach $key ( @{ $options->{ "keys" } } )
5808 if ( defined $record->{ $key } )
5810 if ( exists $min_hash{ $key } ) {
5811 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5813 $min_hash{ $key } = $record->{ $key };
5818 put_record( $record, $out ) if not $options->{ "no_stream" };
5821 $fh = write_stream( $options->{ "data_out" } );
5823 foreach $key ( @{ $options->{ "keys" } } )
5825 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5828 put_record( $min_record, $fh );
5834 sub script_upload_to_ucsc
5836 # Martin A. Hansen, August 2007.
5838 # Calculate the mean of values of given keys.
5840 my ( $in, # handle to in stream
5841 $out, # handle to out stream
5842 $options, # options hash
5847 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5848 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5850 $options->{ "short_label" } ||= $options->{ 'table' };
5851 $options->{ "long_label" } ||= $options->{ 'table' };
5852 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5853 $options->{ "priority" } ||= 1;
5854 $options->{ "visibility" } ||= "pack";
5855 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5856 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5858 $file = "$BP_TMP/ucsc_upload.tmp";
5866 if ( $options->{ 'wiggle' } )
5868 $options->{ "visibility" } = "full";
5870 while ( $record = get_record( $in ) )
5872 put_record( $record, $out ) if not $options->{ "no_stream" };
5874 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5875 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5876 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5878 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5880 $fh_out = $fh_hash{ $record->{ "CHR" } };
5882 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5885 map { close $_ } keys %fh_hash;
5887 $fh_out = Maasha::Common::write_open( $file );
5889 foreach $chr ( sort keys %fh_hash )
5891 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5893 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5897 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5899 $chr = $entry->{ 'CHR' };
5900 $beg = $entry->{ 'CHR_BEG' };
5901 $end = $entry->{ 'CHR_END' };
5902 $q_id = $entry->{ 'Q_ID' };
5904 if ( $options->{ "score" } ) {
5905 $clones = $entry->{ 'SCORE' };
5906 } if ( $q_id =~ /_(\d+)$/ ) {
5916 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5921 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5922 $block->[ $i ] += $clones;
5925 $max = Maasha::Calc::max( $max, $end );
5934 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5935 $block->[ $i ] += $clones;
5942 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out, $options->{ "log10" } );
5944 unlink "$BP_TMP/$chr";
5949 $wig_file = "$options->{ 'table' }.wig";
5950 $wib_file = "$options->{ 'table' }.wib";
5952 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5954 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5956 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5958 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5959 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5969 $fh_out = Maasha::Common::write_open( $file );
5971 while ( $record = get_record( $in ) )
5973 put_record( $record, $out ) if not $options->{ "no_stream" };
5975 if ( $record->{ "REC_TYPE" } eq "PSL" )
5977 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5978 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5982 $format = "PSL" if not $format;
5984 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5986 # chrom chromStart chromEnd name score strand size secStr conf
5988 print $fh_out join ( "\t",
5990 $record->{ "CHR_BEG" },
5991 $record->{ "CHR_END" } + 1,
5992 $record->{ "Q_ID" },
5993 $record->{ "SCORE" },
5994 $record->{ "STRAND" },
5995 $record->{ "SIZE" },
5996 $record->{ "SEC_STRUCT" },
5997 $record->{ "CONF" },
6000 $format = "BED_SS" if not $format;
6002 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6004 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6006 $format = "BED" if not $format;
6007 $columns = $record->{ "BED_COLS" } if not $columns;
6009 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6011 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6013 $format = "BED" if not $format;
6014 $columns = 6 if not $columns;
6016 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6018 $record->{ "CHR" } = $record->{ "S_ID" };
6019 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6020 $record->{ "CHR_END" } = $record->{ "S_END" };
6021 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6023 $format = "BED" if not $format;
6024 $columns = 6 if not $columns;
6026 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6028 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6030 $record->{ "CHR" } = $record->{ "S_ID" };
6031 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6032 $record->{ "CHR_END" } = $record->{ "S_END" };
6033 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6034 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6036 $format = "BED" if not $format;
6037 $columns = 6 if not $columns;
6039 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6042 if ( $i == $options->{ "chunk_size" } )
6046 if ( $format eq "BED" ) {
6047 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6048 } elsif ( $format eq "PSL" ) {
6049 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6058 $fh_out = Maasha::Common::write_open( $file );
6067 if ( exists $options->{ "database" } and $options->{ "table" } )
6069 if ( $format eq "BED" )
6071 $type = "bed $columns";
6073 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6075 elsif ( $format eq "BED_SS" )
6077 $options->{ "sec_struct" } = 1;
6079 $type = "sec_struct";
6081 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6083 elsif ( $format eq "PSL" )
6087 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6089 elsif ( $format eq "WIGGLE" )
6093 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6098 Maasha::UCSC::update_my_tracks( $options, $type );
6103 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6108 # Martin A. Hansen, July 2008.
6110 # Given a biopiece record converts it to a FASTA record.
6111 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6112 # tried in that order.
6114 my ( $record, # record
6119 my ( $seq_name, $seq );
6121 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6122 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6124 if ( defined $seq_name and defined $seq ) {
6125 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6134 # Martin A. Hansen, July 2007.
6136 # Opens a stream to STDIN or a file,
6138 my ( $path, # path - OPTIONAL
6141 # Returns filehandle.
6145 if ( not -t STDIN ) {
6146 $fh = Maasha::Common::read_stdin();
6147 } elsif ( not $path ) {
6148 # Maasha::Common::error( qq(no data stream) );
6150 $fh = Maasha::Common::read_open( $path );
6153 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6161 # Martin A. Hansen, August 2007.
6163 # Opens a stream to STDOUT or a file.
6165 my ( $path, # path - OPTIONAL
6166 $gzip, # compress data - OPTIONAL
6169 # Returns filehandle.
6174 $fh = Maasha::Common::write_open( $path, $gzip );
6176 $fh = Maasha::Common::write_stdout();
6185 # Martin A. Hansen, July 2007.
6187 # Reads one record at a time and converts that record
6188 # to a Perl data structure (a hash) which is returned.
6190 my ( $fh, # handle to stream
6195 my ( $block, @lines, $line, $key, $value, %record );
6197 local $/ = "\n---\n";
6203 return if not defined $block;
6205 @lines = split "\n", $block;
6207 foreach $line ( @lines )
6209 ( $key, $value ) = split ": ", $line, 2;
6211 $record{ $key } = $value;
6214 return wantarray ? %record : \%record;
6220 # Martin A. Hansen, July 2007.
6222 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6224 my ( $data, # data structure
6225 $fh, # file handle - OPTIONAL
6230 if ( scalar keys %{ $data } )
6234 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6239 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6250 # Martin A. Hansen, November 2007.
6252 # Extracts files from an explicit GetOpt::Long argument
6253 # allowing for the use of glob. E.g.
6254 # --data_in=test.fna
6255 # --data_in=test.fna,test2.fna
6257 # --data_in=test.fna,/dir/*.fna
6259 my ( $option, # option from GetOpt::Long
6264 my ( $elem, @files );
6266 foreach $elem ( split ",", $option )
6270 } elsif ( $elem =~ /\*/ ) {
6271 push @files, glob( $elem );
6275 return wantarray ? @files : \@files;
6281 # Martin A. Hansen, April 2008.
6283 # Removes temporary directory and exits gracefully.
6284 # This subroutine is meant to be run always as the last
6285 # thing even if a script is dies or is interrupted
6288 my ( $sig, # signal from the %SIG
6291 # print STDERR "signal->$sig<-\n";
6299 if ( $sig =~ /MAASHA_ERROR/ ) {
6300 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6301 } elsif ( $sig eq "INT" ) {
6302 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6303 } elsif ( $sig eq "TERM" ) {
6304 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6306 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6318 # Martin A. Hansen, July 2008.
6320 # Cleans out any unused temporary files and direcotries in BP_TMP.
6324 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6326 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6328 $curr_pid = Maasha::Common::get_processid();
6330 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6332 foreach $dir ( @dirs )
6334 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6340 if ( $user eq Maasha::Common::get_user() )
6342 if ( not Maasha::Common::process_running( $pid ) )
6344 # print STDERR "Removing stale dir: $dir\n";
6345 Maasha::Common::dir_remove( $dir );
6347 elsif ( $pid == $curr_pid )
6349 # print STDERR "Removing current dir: $dir\n";
6350 Maasha::Common::dir_remove( $dir );
6364 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<