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;
57 use vars qw( @ISA @EXPORT_OK );
61 @ISA = qw( Exporter );
76 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
79 $SIG{ '__DIE__' } = \&sig_handler;
80 $SIG{ 'INT' } = \&sig_handler;
81 $SIG{ 'TERM' } = \&sig_handler;
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
87 my ( $script, $BP_TMP );
89 $script = Maasha::Common::get_scriptname();
90 $BP_TMP = Maasha::Common::get_tmpdir();
93 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
96 my $log_fh = Maasha::Common::append_open( "$ENV{ 'BP_TMP' }/biopieces.log" );
98 $log_fh->autoflush( 1 );
100 &log( $log_fh, $script, \@ARGV );
105 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
108 my $t0 = gettimeofday();
110 run_script( $script );
112 my $t1 = gettimeofday();
114 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
117 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
122 # Martin A. Hansen, January 2008.
124 # Log messages to logfile.
126 my ( $fh, # filehandle to logfile
127 $script, # script name
128 $argv, # reference to @ARGV
133 my ( $time_stamp, $user );
135 $time_stamp = Maasha::Common::time_stamp();
137 $user = $ENV{ 'USER' };
139 $script = "biopieces" if $script eq "-e";
141 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
147 # Martin A. Hansen, August 2007.
149 # Run a specific script.
151 my ( $script, # script name
156 my ( $options, $in, $out );
158 $options = get_options( $script );
160 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
161 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
164 $in = read_stream( $options->{ "stream_in" } );
165 $out = write_stream( $options->{ "stream_out" } );
167 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
168 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
169 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
170 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
171 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
172 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
173 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
174 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
175 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
176 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
177 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
178 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
179 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
180 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
181 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
182 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
183 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
184 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
185 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
186 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
187 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
188 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
189 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
190 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
191 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
192 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
193 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
194 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
195 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
196 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
197 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
198 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
199 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
200 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
201 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
202 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
203 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
204 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
205 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
206 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
207 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
208 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
209 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
210 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
211 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
212 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
213 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
214 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
215 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
216 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
217 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options, $options ) }
218 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
219 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
220 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
221 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
222 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
223 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options, $options ) }
224 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options, $options ) }
225 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
226 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
227 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
228 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
229 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
230 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
231 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
232 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
233 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
234 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
235 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
236 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
237 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
238 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
239 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
240 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
241 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
242 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
243 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
244 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
245 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
246 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
247 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
248 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
249 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
250 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
251 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
252 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
253 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
255 close $in if defined $in;
258 # unset status - missing
259 # write log file - missing
265 # Martin A. Hansen, February 2008.
267 # Gets options from commandline and checks these vigerously.
269 my ( $script, # name of script
274 my ( %options, @options, $opt, @genomes );
276 if ( $script eq "print_usage" )
282 elsif ( $script eq "read_fasta" )
289 elsif ( $script eq "read_tab" )
300 elsif ( $script eq "read_psl" )
307 elsif ( $script eq "read_bed" )
314 elsif ( $script eq "read_blast_tab" )
321 elsif ( $script eq "read_embl" )
331 elsif ( $script eq "read_stockholm" )
338 elsif ( $script eq "read_phastcons" )
349 elsif ( $script eq "read_soft" )
356 elsif ( $script eq "read_gff" )
363 elsif ( $script eq "read_2bit" )
371 elsif ( $script eq "read_solexa" )
379 elsif ( $script eq "read_solid" )
387 elsif ( $script eq "read_mysql" )
396 elsif ( $script eq "format_genome" )
405 elsif ( $script eq "length_seq" )
412 elsif ( $script eq "oligo_freq" )
419 elsif ( $script eq "create_weight_matrix" )
425 elsif ( $script eq "transliterate_seq" )
433 elsif ( $script eq "transliterate_vals" )
442 elsif ( $script eq "translate_seq" )
448 elsif ( $script eq "extract_seq" )
456 elsif ( $script eq "get_genome_seq" )
468 elsif ( $script eq "get_genome_align" )
479 elsif ( $script eq "get_genome_phastcons" )
490 elsif ( $script eq "split_seq" )
497 elsif ( $script eq "split_bed" )
504 elsif ( $script eq "tile_seq" )
511 elsif ( $script eq "invert_align" )
517 elsif ( $script eq "patscan_seq" )
528 elsif ( $script eq "create_blast_db" )
535 elsif ( $script eq "blast_seq" )
547 elsif ( $script eq "blat_seq" )
559 elsif ( $script eq "match_seq" )
566 elsif ( $script eq "create_vmatch_index" )
574 elsif ( $script eq "vmatch_seq" )
585 elsif ( $script eq "write_fasta" )
594 elsif ( $script eq "write_align" )
604 elsif ( $script eq "write_blast" )
613 elsif ( $script eq "write_tab" )
625 elsif ( $script eq "write_bed" )
633 elsif ( $script eq "write_psl" )
641 elsif ( $script eq "write_2bit" )
649 elsif ( $script eq "write_solid" )
658 elsif ( $script eq "plot_seqlogo" )
665 elsif ( $script eq "plot_phastcons_profiles" )
680 elsif ( $script eq "analyze_vals" )
687 elsif ( $script eq "head_records" )
693 elsif ( $script eq "remove_keys" )
700 elsif ( $script eq "rename_keys" )
706 elsif ( $script eq "uniq_vals" )
713 elsif ( $script eq "merge_vals" )
720 elsif ( $script eq "grab" )
735 elsif ( $script eq "compute" )
741 elsif ( $script eq "add_ident" )
748 elsif ( $script eq "count_records" )
755 elsif ( $script eq "random_records" )
761 elsif ( $script eq "sort_records" )
768 elsif ( $script eq "count_vals" )
774 elsif ( $script eq "plot_histogram" )
787 elsif ( $script eq "plot_lendist" )
799 elsif ( $script eq "plot_chrdist" )
810 elsif ( $script eq "plot_karyogram" )
819 elsif ( $script eq "plot_matches" )
831 elsif ( $script eq "length_vals" )
837 elsif ( $script eq "sum_vals" )
845 elsif ( $script eq "mean_vals" )
853 elsif ( $script eq "median_vals" )
861 elsif ( $script eq "max_vals" )
869 elsif ( $script eq "min_vals" )
877 elsif ( $script eq "upload_to_ucsc" )
902 # print STDERR Dumper( \@options );
909 $options{ "script" } = $script;
911 # print STDERR Dumper( \%options );
913 if ( scalar( keys %options ) == 1 or $options{ "help" } ) {
914 return wantarray ? %options : \%options;
917 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
918 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
919 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
920 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
921 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
922 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
923 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
924 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
926 # ---- check arguments ----
928 if ( $options{ 'data_in' } )
930 $options{ "files" } = getopt_files( $options{ 'data_in' } );
932 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
935 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
937 # print STDERR Dumper( \%options );
939 foreach $opt ( keys %options )
941 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
943 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
945 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+$/ )
947 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
949 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
951 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
953 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
955 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
957 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
959 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
961 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
963 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
965 elsif ( $opt eq "genome" and $script ne "format_genome" )
967 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
968 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
970 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
971 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
974 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
976 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
978 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
980 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
984 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
985 Maasha::Common::error( qq(no --index_name specified) ) if $script eq "create_vmatch_index" and not $options{ "index_name" };
986 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
987 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
988 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
989 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
990 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" };
991 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
992 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" };
994 if ( $script eq "upload_to_ucsc" )
996 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
997 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1000 return wantarray ? %options : \%options;
1004 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1007 sub script_print_usage
1009 # Martin A. Hansen, January 2008.
1011 # Retrieves usage information from file and
1012 # prints this nicely formatted.
1014 my ( $in, # handle to in stream
1015 $out, # handle to out stream
1016 $options, # options hash
1021 my ( $file, $wiki, $lines );
1023 if ( $options->{ 'data_in' } ) {
1024 $file = $options->{ 'data_in' };
1026 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
1029 $wiki = Maasha::Gwiki::gwiki_read( $file );
1031 if ( not $options->{ "help" } ) {
1032 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1035 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1037 print STDERR "$_\n" foreach @{ $lines };
1043 sub script_list_biopieces
1045 # Martin A. Hansen, January 2008.
1047 # Prints the synopsis from the usage for each of the biopieces.
1049 my ( $in, # handle to in stream
1050 $out, # handle to out stream
1051 $options, # options hash
1056 my ( @files, $file, $wiki, $program, $synopsis );
1058 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1060 foreach $file ( sort @files )
1062 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1066 $wiki = Maasha::Gwiki::gwiki_read( $file );
1068 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1069 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1071 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1073 printf( "%-30s%s\n", $program, $synopsis );
1081 sub script_list_genomes
1083 # Martin A. Hansen, January 2008.
1085 # Prints the synopsis from the usage for each of the biopieces.
1087 my ( $in, # handle to in stream
1088 $out, # handle to out stream
1089 $options, # options hash
1094 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1096 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1098 foreach $genome ( @genomes )
1100 next if $genome =~ /\.$/;
1102 @formats = Maasha::Common::ls_dirs( $genome );
1104 foreach $format ( @formats )
1106 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1108 $hash{ $1 }{ $2 } = 1;
1117 map { push @row, $_ } sort keys %found;
1119 print join( "\t", @row ), "\n";
1121 foreach $genome ( sort keys %hash )
1125 foreach $format ( sort keys %found )
1127 if ( exists $hash{ $genome }{ $format } ) {
1134 print join( "\t", @row ), "\n";
1139 sub script_read_fasta
1141 # Martin A. Hansen, August 2007.
1143 # Read sequences from FASTA file.
1145 my ( $in, # handle to in stream
1146 $out, # handle to out stream
1147 $options, # options hash
1152 my ( $record, $file, $data_in, $entry, $num );
1154 while ( $record = get_record( $in ) ) {
1155 put_record( $record, $out );
1160 foreach $file ( @{ $options->{ "files" } } )
1162 $data_in = Maasha::Common::read_open( $file );
1164 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1166 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1169 SEQ_NAME => $entry->[ SEQ_NAME ],
1170 SEQ => $entry->[ SEQ ],
1171 SEQ_LEN => length $entry->[ SEQ ],
1174 put_record( $record, $out );
1177 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1187 close $data_in if $data_in;
1193 # Martin A. Hansen, August 2007.
1195 # Read table or table columns from stream or file.
1197 my ( $in, # handle to in stream
1198 $out, # handle to out stream
1199 $options, # options hash
1204 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1206 $options->{ 'delimit' } ||= '\s+';
1208 while ( $record = get_record( $in ) ) {
1209 put_record( $record, $out );
1212 $skip = $options->{ 'skip' } ||= 0;
1215 foreach $file ( @{ $options->{ "files" } } )
1217 $data_in = Maasha::Common::read_open( $file );
1219 while ( $line = <$data_in> )
1227 next if $line =~ /^#|^$/;
1234 @fields = split /$options->{'delimit'}/, $line;
1236 if ( $options->{ "cols" } ) {
1237 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1242 for ( $i = 0; $i < @fields2; $i++ )
1244 if ( $options->{ "keys" }->[ $i ] ) {
1245 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1247 $record->{ "V" . $i } = $fields2[ $i ];
1251 put_record( $record, $out );
1253 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1263 close $data_in if $data_in;
1269 # Martin A. Hansen, August 2007.
1271 # Read psl table from stream or file.
1273 my ( $in, # handle to in stream
1274 $out, # handle to out stream
1275 $options, # options hash
1280 my ( $record, @files, $file, $entries, $entry, $num );
1282 while ( $record = get_record( $in ) ) {
1283 put_record( $record, $out );
1288 foreach $file ( @{ $options->{ "files" } } )
1290 $entries = Maasha::UCSC::psl_get_entries( $file );
1292 foreach $entry ( @{ $entries } )
1294 put_record( $entry, $out );
1296 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1308 # Martin A. Hansen, August 2007.
1310 # Read bed table from stream or file.
1312 my ( $in, # handle to in stream
1313 $out, # handle to out stream
1314 $options, # options hash
1319 my ( $file, $record, $entry, $data_in, $num );
1321 while ( $record = get_record( $in ) ) {
1322 put_record( $record, $out );
1327 foreach $file ( @{ $options->{ "files" } } )
1329 $data_in = Maasha::Common::read_open( $file );
1331 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1333 put_record( $entry, $out );
1335 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1345 close $data_in if $data_in;
1349 sub script_read_blast_tab
1351 # Martin A. Hansen, September 2007.
1353 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1355 my ( $in, # handle to in stream
1356 $out, # handle to out stream
1357 $options, # options hash
1362 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1364 while ( $record = get_record( $in ) ) {
1365 put_record( $record, $out );
1370 foreach $file ( @{ $options->{ "files" } } )
1372 $data_in = Maasha::Common::read_open( $file );
1374 while ( $line = <$data_in> )
1378 next if $line =~ /^#/;
1380 @fields = split /\t/, $line;
1382 $record->{ "REC_TYPE" } = "BLAST";
1383 $record->{ "Q_ID" } = $fields[ 0 ];
1384 $record->{ "S_ID" } = $fields[ 1 ];
1385 $record->{ "IDENT" } = $fields[ 2 ];
1386 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1387 $record->{ "MISMATCHES" } = $fields[ 4 ];
1388 $record->{ "GAPS" } = $fields[ 5 ];
1389 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1390 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1391 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1392 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1393 $record->{ "E_VAL" } = $fields[ 10 ];
1394 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1396 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1398 $record->{ "STRAND" } = '-';
1400 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1404 $record->{ "STRAND" } = '+';
1407 put_record( $record, $out );
1409 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1419 close $data_in if $data_in;
1423 sub script_read_embl
1425 # Martin A. Hansen, August 2007.
1429 my ( $in, # handle to in stream
1430 $out, # handle to out stream
1431 $options, # options hash
1436 my ( %options2, $file, $data_in, $num, $entry, $record );
1438 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1439 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1440 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1442 while ( $record = get_record( $in ) ) {
1443 put_record( $record, $out );
1448 foreach $file ( @{ $options->{ "files" } } )
1450 $data_in = Maasha::Common::read_open( $file );
1452 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1454 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1456 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1458 $record_copy = dclone $record;
1460 delete $record_copy->{ "FT" };
1462 put_record( $record_copy, $out );
1464 delete $record_copy->{ "SEQ" };
1466 foreach $feat ( keys %{ $record->{ "FT" } } )
1468 $record_copy->{ "FEAT_TYPE" } = $feat;
1470 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1472 foreach $qual ( keys %{ $feat2 } )
1474 $qual_val = join "; ", @{ $feat2->{ $qual } };
1479 $record_copy->{ $qual } = $qual_val;
1482 put_record( $record_copy, $out );
1486 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1496 close $data_in if $data_in;
1500 sub script_read_stockholm
1502 # Martin A. Hansen, August 2007.
1504 # Read Stockholm format.
1506 my ( $in, # handle to in stream
1507 $out, # handle to out stream
1508 $options, # options hash
1513 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1515 while ( $record = get_record( $in ) ) {
1516 put_record( $record, $out );
1521 foreach $file ( @{ $options->{ "files" } } )
1523 $data_in = Maasha::Common::read_open( $file );
1525 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1527 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1531 foreach $key ( keys %{ $record->{ "GF" } } ) {
1532 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1535 $record_anno->{ "ALIGN" } = $num;
1537 put_record( $record_anno, $out );
1539 foreach $seq ( @{ $record->{ "ALIGN" } } )
1541 undef $record_align;
1544 SEQ_NAME => $seq->[ 0 ],
1548 put_record( $record_align, $out );
1551 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1561 close $data_in if $data_in;
1565 sub script_read_phastcons
1567 # Martin A. Hansen, December 2007.
1569 # Read PhastCons format.
1571 my ( $in, # handle to in stream
1572 $out, # handle to out stream
1573 $options, # options hash
1578 my ( $data_in, $file, $num, $entry, @records, $record );
1580 $options->{ "min" } ||= 10;
1581 $options->{ "dist" } ||= 25;
1582 $options->{ "threshold" } ||= 0.8;
1583 $options->{ "gap" } ||= 5;
1585 while ( $record = get_record( $in ) ) {
1586 put_record( $record, $out );
1591 foreach $file ( @{ $options->{ "files" } } )
1593 $data_in = Maasha::Common::read_open( $file );
1595 while ( $entry = Maasha::UCSC::phastcons_get_entry( $data_in ) )
1597 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1599 foreach $record ( @records )
1601 $record->{ "REC_TYPE" } = "BED";
1602 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1604 put_record( $record, $out );
1606 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1617 close $data_in if $data_in;
1621 sub script_read_soft
1623 # Martin A. Hansen, December 2007.
1626 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1628 my ( $in, # handle to in stream
1629 $out, # handle to out stream
1630 $options, # options hash
1635 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1637 while ( $record = get_record( $in ) ) {
1638 put_record( $record, $out );
1643 foreach $file ( @{ $options->{ "files" } } )
1645 $soft_index = Maasha::NCBI::soft_index_file( $file );
1647 $fh = Maasha::Common::read_open( $file );
1649 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1651 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1653 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1655 $old_end = $platforms[ -1 ]->[ 2 ];
1657 foreach $sample ( @samples )
1659 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1661 foreach $record ( @{ $records } )
1663 put_record( $record, $out );
1665 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1670 $old_end = $sample->[ 2 ];
1678 close $data_in if $data_in;
1685 # Martin A. Hansen, February 2008.
1688 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1690 my ( $in, # handle to in stream
1691 $out, # handle to out stream
1692 $options, # options hash
1697 my ( $data_in, $file, $fh, $num, $record, $entry );
1699 while ( $record = get_record( $in ) ) {
1700 put_record( $record, $out );
1705 foreach $file ( @{ $options->{ "files" } } )
1707 $fh = Maasha::Common::read_open( $file );
1709 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1711 put_record( $entry, $out );
1713 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1723 close $data_in if $data_in;
1727 sub script_read_2bit
1729 # Martin A. Hansen, March 2008.
1731 # Read sequences from 2bit file.
1733 my ( $in, # handle to in stream
1734 $out, # handle to out stream
1735 $options, # options hash
1740 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1742 $mask = 1 if not $options->{ "no_mask" };
1744 while ( $record = get_record( $in ) ) {
1745 put_record( $record, $out );
1750 foreach $file ( @{ $options->{ "files" } } )
1752 $data_in = Maasha::Common::read_open( $file );
1754 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1756 foreach $line ( @{ $toc } )
1758 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1759 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1760 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1762 put_record( $record, $out );
1764 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1774 close $data_in if $data_in;
1778 sub script_read_solexa
1780 # Martin A. Hansen, March 2008.
1782 # Read Solexa sequence reads from file.
1784 my ( $in, # handle to in stream
1785 $out, # handle to out stream
1786 $options, # options hash
1791 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1793 $options->{ "quality" } ||= 20;
1795 while ( $record = get_record( $in ) ) {
1796 put_record( $record, $out );
1801 foreach $file ( @{ $options->{ "files" } } )
1803 $data_in = Maasha::Common::read_open( $file );
1804 $base_name = Maasha::Common::get_basename( $file );
1805 $base_name =~ s/\..*//;
1809 while ( $line = <$data_in> )
1811 @fields = split /:/, $line;
1812 @seqs = split //, $fields[ 5 ];
1813 @scores = split / /, $fields[ -1 ];
1815 for ( $i = 0; $i < @scores; $i++ ) {
1816 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1819 $seq = join "", @seqs;
1821 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1822 $record->{ "SEQ" } = $seq;
1823 $record->{ "SEQ_LEN" } = length $seq;
1824 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1826 put_record( $record, $out );
1828 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1839 close $data_in if $data_in;
1843 sub script_read_solid
1845 # Martin A. Hansen, April 2008.
1847 # Read Solid sequence from file.
1849 my ( $in, # handle to in stream
1850 $out, # handle to out stream
1851 $options, # options hash
1856 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1858 $options->{ "quality" } ||= 15;
1860 while ( $record = get_record( $in ) ) {
1861 put_record( $record, $out );
1866 foreach $file ( @{ $options->{ "files" } } )
1868 $data_in = Maasha::Common::read_open( $file );
1870 while ( $line = <$data_in> )
1874 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1876 @scores = split /,/, $seq_qual;
1877 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1879 for ( $i = 0; $i < @seqs; $i++ ) {
1880 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1884 SEQ_NAME => $seq_name,
1886 SEQ_QUAL => $seq_qual,
1887 SEQ_LEN => length $seq_cs,
1888 SEQ => join( "", @seqs ),
1889 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1892 put_record( $record, $out );
1894 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1904 close $data_in if $data_in;
1908 sub script_read_mysql
1910 # Martin A. Hansen, May 2008.
1912 # Read a MySQL query into stream.
1914 my ( $in, # handle to in stream
1915 $out, # handle to out stream
1916 $options, # options hash
1921 my ( $record, $dbh, $results );
1923 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1924 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1926 while ( $record = get_record( $in ) ) {
1927 put_record( $record, $out );
1930 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1932 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1934 Maasha::SQL::disconnect( $dbh );
1936 map { put_record( $_ ) } @{ $results };
1940 sub script_format_genome
1942 # Martin A. Hansen, Juli 2008.
1944 # Format a genome to speficed formats.
1946 my ( $in, # handle to in stream
1947 $out, # handle to out stream
1948 $options, # options hash
1953 my ( $dir, $genome, $fasta_dir, $fh_out, $record, $format, $index );
1955 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
1956 $genome = $options->{ 'genome' };
1958 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
1959 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
1960 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
1962 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
1964 $fasta_dir = "$dir/genomes/$genome/fasta";
1966 elsif ( grep { $_ =~ /fasta/i } @{ $options->{ "formats" } } )
1968 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
1970 $fasta_dir = "$dir/genomes/$genome/fasta";
1972 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
1976 $fasta_dir = $BP_TMP;
1978 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
1981 while ( $record = get_record( $in ) )
1983 if ( $fh_out and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
1984 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, $options->{ "wrap" } );
1987 put_record( $record, $out ) if not $options->{ "no_stream" };
1990 foreach $format ( @{ $options->{ 'formats' } } )
1992 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
1993 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
1994 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
1995 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
1998 close $fh_out if $fh_out;
2002 sub script_length_seq
2004 # Martin A. Hansen, August 2007.
2006 # Determine the length of sequences in stream.
2008 my ( $in, # handle to in stream
2009 $out, # handle to out stream
2010 $options, # options hash
2015 my ( $record, $total );
2017 while ( $record = get_record( $in ) )
2019 if ( $record->{ "SEQ" } )
2021 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2022 $total += $record->{ "SEQ_LEN" };
2025 put_record( $record, $out ) if not $options->{ "no_stream" };
2028 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2032 sub script_uppercase_seq
2034 # Martin A. Hansen, August 2007.
2036 # Uppercases sequences in stream.
2038 my ( $in, # handle to in stream
2039 $out, # handle to out stream
2046 while ( $record = get_record( $in ) )
2048 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2050 put_record( $record, $out );
2055 sub script_shuffle_seq
2057 # Martin A. Hansen, December 2007.
2059 # Shuffle sequences in stream.
2061 my ( $in, # handle to in stream
2062 $out, # handle to out stream
2069 while ( $record = get_record( $in ) )
2071 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2073 put_record( $record, $out );
2078 sub script_analyze_seq
2080 # Martin A. Hansen, August 2007.
2082 # Analyze sequence composition of sequences in stream.
2084 my ( $in, # handle to in stream
2085 $out, # handle to out stream
2090 my ( $record, $analysis );
2092 while ( $record = get_record( $in ) )
2094 if ( $record->{ "SEQ" } )
2096 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2098 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2101 put_record( $record, $out );
2106 sub script_analyze_tags
2108 # Martin A. Hansen, August 2008.
2110 # Analyze sequence tags in stream.
2112 my ( $in, # handle to in stream
2113 $out, # handle to out stream
2118 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2120 while ( $record = get_record( $in ) )
2122 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2124 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2128 $len_hash{ length( $record->{ "SEQ" } ) }++;
2129 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2132 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2134 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2138 $len_hash{ $record->{ "BED_LEN" } }++;
2139 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2144 foreach $key ( sort { $a <=> $b } keys %len_hash )
2146 $tag_record->{ "TAG_LEN" } = $key;
2147 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2148 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2150 put_record( $tag_record, $out );
2155 sub script_complexity_seq
2157 # Martin A. Hansen, May 2008.
2159 # Generates an index calculated as the most common di-residue over
2160 # the sequence length for all sequences in stream.
2162 my ( $in, # handle to in stream
2163 $out, # handle to out stream
2168 my ( $record, $index );
2170 while ( $record = get_record( $in ) )
2172 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2174 put_record( $record, $out );
2179 sub script_oligo_freq
2181 # Martin A. Hansen, August 2007.
2183 # Determine the length of sequences in stream.
2185 my ( $in, # handle to in stream
2186 $out, # handle to out stream
2187 $options, # options hash
2192 my ( $record, %oligos, @freq_table );
2194 $options->{ "word_size" } ||= 7;
2196 while ( $record = get_record( $in ) )
2198 if ( $record->{ "SEQ" } )
2200 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2202 if ( not $options->{ "all" } )
2204 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2206 map { put_record( $_, $out ) } @freq_table;
2212 put_record( $record, $out );
2215 if ( $options->{ "all" } )
2217 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2219 map { put_record( $_, $out ) } @freq_table;
2224 sub script_create_weight_matrix
2226 # Martin A. Hansen, August 2007.
2228 # Creates a weight matrix from an alignmnet.
2230 my ( $in, # handle to in stream
2231 $out, # handle to out stream
2232 $options, # options hash
2237 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2241 while ( $record = get_record( $in ) )
2243 if ( $record->{ "SEQ" } )
2245 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2247 $res = substr $record->{ "SEQ" }, $i, 1;
2249 $freq_hash{ $i }{ $res }++;
2250 $res_hash{ $res } = 1;
2257 put_record( $record, $out );
2261 foreach $res ( sort keys %res_hash )
2265 $record->{ "V0" } = $res;
2267 for ( $i = 0; $i < keys %freq_hash; $i++ )
2269 $freq = $freq_hash{ $i }{ $res } || 0;
2271 if ( $options->{ "percent" } ) {
2272 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2275 $record->{ "V" . ( $i + 1 ) } = $freq;
2278 put_record( $record, $out );
2283 sub script_calc_bit_scores
2285 # Martin A. Hansen, March 2007.
2287 # Calculates the bit scores for each position from an alignmnet in the stream.
2289 my ( $in, # handle to in stream
2290 $out, # handle to out stream
2295 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2299 while ( $record = get_record( $in ) )
2301 if ( $record->{ "SEQ" } )
2303 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2305 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2307 $res = substr $record->{ "SEQ" }, $i, 1;
2309 next if $res =~ /-|_|~|\./;
2311 $freq_hash{ $i }{ $res }++;
2318 put_record( $record, $out );
2324 if ( $type eq "protein" ) {
2330 for ( $i = 0; $i < keys %freq_hash; $i++ )
2332 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2334 $bit_diff = $bit_max - $bit_height;
2336 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2339 put_record( $record, $out );
2343 sub script_reverse_seq
2345 # Martin A. Hansen, August 2007.
2347 # Reverse sequence in record.
2349 my ( $in, # handle to in stream
2350 $out, # handle to out stream
2357 while ( $record = get_record( $in ) )
2359 if ( $record->{ "SEQ" } ) {
2360 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2363 put_record( $record, $out );
2368 sub script_complement_seq
2370 # Martin A. Hansen, August 2007.
2372 # Complement sequence in record.
2374 my ( $in, # handle to in stream
2375 $out, # handle to out stream
2380 my ( $record, $type );
2382 while ( $record = get_record( $in ) )
2384 if ( $record->{ "SEQ" } )
2387 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2390 if ( $type eq "rna" ) {
2391 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2392 } elsif ( $type eq "dna" ) {
2393 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2397 put_record( $record, $out );
2402 sub script_remove_indels
2404 # Martin A. Hansen, August 2007.
2406 # Remove indels from sequences in stream.
2408 my ( $in, # handle to in stream
2409 $out, # handle to out stream
2416 while ( $record = get_record( $in ) )
2418 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2420 put_record( $record, $out );
2425 sub script_transliterate_seq
2427 # Martin A. Hansen, August 2007.
2429 # Transliterate chars from sequence in record.
2431 my ( $in, # handle to in stream
2432 $out, # handle to out stream
2433 $options, # options hash
2438 my ( $record, $search, $replace, $delete );
2440 $search = $options->{ "search" } || "";
2441 $replace = $options->{ "replace" } || "";
2442 $delete = $options->{ "delete" } || "";
2444 while ( $record = get_record( $in ) )
2446 if ( $record->{ "SEQ" } )
2448 if ( $search and $replace ) {
2449 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2450 } elsif ( $delete ) {
2451 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2455 put_record( $record, $out );
2460 sub script_transliterate_vals
2462 # Martin A. Hansen, April 2008.
2464 # Transliterate chars from values in record.
2466 my ( $in, # handle to in stream
2467 $out, # handle to out stream
2468 $options, # options hash
2473 my ( $record, $search, $replace, $delete, $key );
2475 $search = $options->{ "search" } || "";
2476 $replace = $options->{ "replace" } || "";
2477 $delete = $options->{ "delete" } || "";
2479 while ( $record = get_record( $in ) )
2481 foreach $key ( @{ $options->{ "keys" } } )
2483 if ( exists $record->{ $key } )
2485 if ( $search and $replace ) {
2486 eval "\$record->{ $key } =~ tr/$search/$replace/";
2487 } elsif ( $delete ) {
2488 eval "\$record->{ $key } =~ tr/$delete//d";
2493 put_record( $record, $out );
2498 sub script_translate_seq
2500 # Martin A. Hansen, February 2008.
2502 # Translate DNA sequence into protein sequence.
2504 my ( $in, # handle to in stream
2505 $out, # handle to out stream
2506 $options, # options hash
2511 my ( $record, $frame, %new_record );
2513 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2515 while ( $record = get_record( $in ) )
2517 if ( $record->{ "SEQ" } )
2519 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2521 foreach $frame ( @{ $options->{ "frames" } } )
2523 %new_record = %{ $record };
2525 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2526 $new_record{ "SEQ_LEN" } = length $record->{ "SEQ" };
2527 $new_record{ "FRAME" } = $frame;
2529 put_record( \%new_record, $out );
2535 put_record( $record, $out );
2541 sub script_extract_seq
2543 # Martin A. Hansen, August 2007.
2545 # Extract subsequences from sequences in record.
2547 my ( $in, # handle to in stream
2548 $out, # handle to out stream
2549 $options, # options hash
2554 my ( $beg, $end, $len, $record );
2556 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2559 $beg = $options->{ "beg" } - 1; # correcting for start offset
2562 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2564 } elsif ( defined $options->{ "end" } ) {
2565 $end = $options->{ "end" } - 1; # correcting for start offset
2568 $len = $options->{ "len" };
2570 # print "beg->$beg, end->$end, len->$len\n";
2572 while ( $record = get_record( $in ) )
2574 if ( $record->{ "SEQ" } )
2576 if ( defined $beg and defined $end )
2578 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2579 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2581 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2584 elsif ( defined $beg and defined $len )
2586 if ( $len > length $record->{ "SEQ" } ) {
2587 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2589 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2592 elsif ( defined $beg )
2594 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2598 put_record( $record, $out );
2603 sub script_get_genome_seq
2605 # Martin A. Hansen, December 2007.
2607 # Gets a subsequence from a genome.
2609 my ( $in, # handle to in stream
2610 $out, # handle to out stream
2611 $options, # options hash
2616 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2618 $options->{ "flank" } ||= 0;
2620 if ( $options->{ "genome" } )
2622 $genome = $options->{ "genome" };
2624 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2625 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2627 $fh = Maasha::Common::read_open( $genome_file );
2628 $index = Maasha::Fasta::index_retrieve( $index_file );
2630 shift @{ $index }; # Get rid of the file size info
2632 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2634 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2636 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2638 $beg = $index_beg + $options->{ "beg" } - 1;
2640 if ( $options->{ "len" } ) {
2641 $len = $options->{ "len" };
2642 } elsif ( $options->{ "end" } ) {
2643 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2646 $beg -= $options->{ "flank" };
2647 $len += 2 * $options->{ "flank" };
2649 if ( $beg <= $index_beg )
2651 $len -= $index_beg - $beg;
2655 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2657 next if $beg > $index_beg + $index_len;
2659 $record->{ "CHR" } = $options->{ "chr" };
2660 $record->{ "CHR_BEG" } = $beg - $index_beg;
2661 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2663 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2664 $record->{ "SEQ_LEN" } = $len;
2666 put_record( $record, $out );
2670 while ( $record = get_record( $in ) )
2672 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2674 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2676 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2678 $beg = $record->{ "CHR_BEG" } + $index_beg;
2679 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2681 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2683 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2685 $beg = $record->{ "S_BEG" } + $index_beg;
2686 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2688 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2690 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2692 $beg = $record->{ "S_BEG" } + $index_beg;
2693 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2696 $beg -= $options->{ "flank" };
2697 $len += 2 * $options->{ "flank" };
2699 if ( $beg <= $index_beg )
2701 $len -= $index_beg - $beg;
2705 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2707 next if $beg > $index_beg + $index_len;
2709 $record->{ "CHR_BEG" } = $beg - $index_beg;
2710 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2712 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2714 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2716 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2717 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2720 if ( $options->{ "mask" } )
2722 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2724 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2726 @begs = split ",", $record->{ "Q_BEGS" };
2727 @lens = split ",", $record->{ "BLOCKSIZES" };
2729 for ( $i = 0; $i < @begs; $i++ ) {
2730 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2736 put_record( $record, $out );
2743 sub script_get_genome_align
2745 # Martin A. Hansen, April 2008.
2747 # Gets a subalignment from a multiple genome alignment.
2749 my ( $in, # handle to in stream
2750 $out, # handle to out stream
2751 $options, # options hash
2756 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2758 $options->{ "strand" } ||= "+";
2762 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2764 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2766 $beg = $options->{ "beg" } - 1;
2768 if ( $options->{ "end" } ) {
2769 $end = $options->{ "end" };
2770 } elsif ( $options->{ "len" } ) {
2771 $end = $beg + $options->{ "len" };
2774 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2776 foreach $entry ( @{ $align } )
2778 $entry->{ "CHR" } = $record->{ "CHR" };
2779 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2780 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2781 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2782 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2783 $entry->{ "SCORE" } = $record->{ "SCORE" };
2785 put_record( $entry, $out );
2789 while ( $record = get_record( $in ) )
2791 if ( $record->{ "REC_TYPE" } eq "BED" )
2793 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2795 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2797 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2799 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2801 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2804 foreach $entry ( @{ $align } )
2806 $entry->{ "CHR" } = $record->{ "CHR" };
2807 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2808 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2809 $entry->{ "STRAND" } = $record->{ "STRAND" };
2810 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2811 $entry->{ "SCORE" } = $record->{ "SCORE" };
2813 put_record( $entry, $out );
2821 sub script_get_genome_phastcons
2823 # Martin A. Hansen, February 2008.
2825 # Get phastcons scores from genome intervals.
2827 my ( $in, # handle to in stream
2828 $out, # handle to out stream
2829 $options, # options hash
2834 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2836 $options->{ "flank" } ||= 0;
2838 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2839 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2841 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2842 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2844 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2846 $options->{ "beg" } -= 1; # request is 1-based
2847 $options->{ "end" } -= 1; # request is 1-based
2849 if ( $options->{ "len" } ) {
2850 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2853 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2855 $record->{ "CHR" } = $options->{ "chr" };
2856 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2857 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2859 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2860 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2862 put_record( $record, $out );
2865 while ( $record = get_record( $in ) )
2867 if ( $record->{ "REC_TYPE" } eq "BED" )
2869 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2871 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2873 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2875 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2877 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2880 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2881 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2883 put_record( $record, $out );
2886 close $fh_phastcons if $fh_phastcons;
2892 # Martin A. Hansen, December 2007.
2894 # Folds sequences in stream into secondary structures.
2896 my ( $in, # handle to in stream
2897 $out, # handle to out stream
2902 my ( $record, $type, $struct, $index );
2904 while ( $record = get_record( $in ) )
2906 if ( $record->{ "SEQ" } )
2909 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2912 if ( $type ne "protein" )
2914 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2915 $record->{ "SEC_STRUCT" } = $struct;
2916 $record->{ "FREE_ENERGY" } = $index;
2917 $record->{ "SCORE" } = abs int $index;
2918 $record->{ "SIZE" } = length $struct;
2919 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2923 put_record( $record, $out );
2928 sub script_split_seq
2930 # Martin A. Hansen, August 2007.
2932 # Split a sequence in stream into words.
2934 my ( $in, # handle to in stream
2935 $out, # handle to out stream
2936 $options, # options hash
2941 my ( $record, $new_record, $i, $subseq, %lookup );
2943 $options->{ "word_size" } ||= 7;
2945 while ( $record = get_record( $in ) )
2947 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2949 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2951 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2953 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2955 $new_record->{ "REC_TYPE" } = "SPLIT";
2956 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2957 $new_record->{ "SEQ" } = $subseq;
2959 put_record( $new_record, $out );
2961 $lookup{ $subseq } = 1;
2965 $new_record->{ "REC_TYPE" } = "SPLIT";
2966 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2967 $new_record->{ "SEQ" } = $subseq;
2969 put_record( $new_record, $out );
2975 put_record( $record, $out );
2981 sub script_split_bed
2983 # Martin A. Hansen, June 2008.
2985 # Split a BED record into overlapping windows.
2987 my ( $in, # handle to in stream
2988 $out, # handle to out stream
2989 $options, # options hash
2994 my ( $record, $new_record, $i );
2996 $options->{ "window_size" } ||= 20;
2997 $options->{ "step_size" } ||= 1;
2999 while ( $record = get_record( $in ) )
3001 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3003 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3005 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3007 $new_record->{ "REC_TYPE" } = "BED";
3008 $new_record->{ "CHR" } = $record->{ "CHR" };
3009 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3010 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3011 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3012 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3013 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3014 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3016 put_record( $new_record, $out );
3021 put_record( $record, $out );
3027 sub script_align_seq
3029 # Martin A. Hansen, August 2007.
3031 # Align sequences in stream.
3033 my ( $in, # handle to in stream
3034 $out, # handle to out stream
3039 my ( $record, @entries, $entry );
3041 while ( $record = get_record( $in ) )
3043 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3044 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3045 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3046 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3048 put_record( $record, $out );
3052 @entries = Maasha::Align::align( \@entries );
3054 foreach $entry ( @entries )
3056 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3059 SEQ_NAME => $entry->[ SEQ_NAME ],
3060 SEQ => $entry->[ SEQ ],
3063 put_record( $record, $out );
3071 # Martin A. Hansen, February 2008.
3073 # Using the first sequence in stream as reference, tile
3074 # all subsequent sequences based on pairwise alignments.
3076 my ( $in, # handle to in stream
3077 $out, # handle to out stream
3078 $options, # options hash
3083 my ( $record, $first, $ref_entry, @entries );
3087 while ( $record = get_record( $in ) )
3089 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3093 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3099 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3104 put_record( $record, $out );
3108 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3110 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3114 sub script_invert_align
3116 # Martin A. Hansen, February 2008.
3118 # Inverts an alignment showing only non-mathing residues
3119 # using the first sequence as reference.
3121 my ( $in, # handle to in stream
3122 $out, # handle to out stream
3123 $options, # options hash
3128 my ( $record, @entries );
3130 while ( $record = get_record( $in ) )
3132 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3134 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3138 put_record( $record, $out );
3142 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3144 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3148 sub script_patscan_seq
3150 # Martin A. Hansen, August 2007.
3152 # Locates patterns in sequences using scan_for_matches.
3154 my ( $in, # handle to in stream
3155 $out, # handle to out stream
3156 $options, # options hash
3161 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3163 if ( $options->{ "patterns" } ) {
3164 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3165 } elsif ( -f $options->{ "patterns_in" } ) {
3166 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3169 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3171 push @args, "-c" if $options->{ "comp" };
3172 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3173 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3175 $seq_file = "$BP_TMP/patscan.seq";
3176 $pat_file = "$BP_TMP/patscan.pat";
3177 $out_file = "$BP_TMP/patscan.out";
3179 $fh_out = Maasha::Common::write_open( $seq_file );
3183 while ( $record = get_record( $in ) )
3185 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3187 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3189 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3191 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3196 # put_record( $record, $out );
3201 $arg = join " ", @args;
3202 $arg .= " -p" if $type eq "protein";
3204 foreach $pattern ( @{ $patterns } )
3206 $fh_out = Maasha::Common::write_open( $pat_file );
3208 print $fh_out "$pattern\n";
3212 if ( $options->{ 'genome' } ) {
3213 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3214 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3216 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3217 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3220 $fh_in = Maasha::Common::read_open( $out_file );
3222 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3224 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3226 if ( $options->{ 'genome' } )
3228 $result->{ "CHR" } = $result->{ "S_ID" };
3229 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3230 $result->{ "CHR_END" } = $result->{ "S_END" };
3232 delete $result->{ "S_ID" };
3233 delete $result->{ "S_BEG" };
3234 delete $result->{ "S_END" };
3238 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3241 put_record( $result, $out );
3253 sub script_create_blast_db
3255 # Martin A. Hansen, September 2007.
3257 # Creates a NCBI BLAST database with formatdb
3259 my ( $in, # handle to in stream
3260 $out, # handle to out stream
3261 $options, # options hash
3266 my ( $fh, $seq_type, $path, $record );
3268 $path = $options->{ "database" };
3270 $fh = Maasha::Common::write_open( $path );
3272 while ( $record = get_record( $in ) )
3274 put_record( $record, $out ) if not $options->{ "no_stream" };
3276 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3278 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3280 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3286 if ( $seq_type eq "protein" ) {
3287 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3289 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3296 sub script_blast_seq
3298 # Martin A. Hansen, September 2007.
3300 # BLASTs sequences in stream against a given database.
3302 my ( $in, # handle to in stream
3303 $out, # handle to out stream
3304 $options, # options hash
3309 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3311 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3312 $options->{ "filter" } = "F";
3313 $options->{ "filter" } = "T" if $options->{ "filter" };
3314 $options->{ "cpus" } ||= 1;
3316 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna"if $options->{ 'genome' };
3318 $tmp_in = "$BP_TMP/blast_query.seq";
3319 $tmp_out = "$BP_TMP/blast.result";
3321 $fh_out = Maasha::Common::write_open( $tmp_in );
3323 while ( $record = get_record( $in ) )
3325 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3327 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3329 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3332 put_record( $record, $out );
3337 if ( -f $options->{ 'database' } . ".phr" ) {
3338 $s_type = "protein";
3340 $s_type = "nucleotide";
3343 if ( not $options->{ 'program' } )
3345 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3346 $options->{ 'program' } = "blastn";
3347 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3348 $options->{ 'program' } = "blastp";
3349 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3350 $options->{ 'program' } = "blastx";
3351 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3352 $options->{ 'program' } = "tblastn";
3356 Maasha::Common::run( "blastall", "-p $options->{ 'program' } -e $options->{ 'e_val' } -a $options->{ 'cpus' } -m 8 -i $tmp_in -d $options->{ 'database' } -F $options->{ 'filter' } -o $tmp_out > /dev/null 2>&1", 1 );
3360 $fh_out = Maasha::Common::read_open( $tmp_out );
3364 while ( $line = <$fh_out> )
3368 next if $line =~ /^#/;
3370 @fields = split /\s+/, $line;
3372 $record->{ "REC_TYPE" } = "BLAST";
3373 $record->{ "Q_ID" } = $fields[ 0 ];
3374 $record->{ "S_ID" } = $fields[ 1 ];
3375 $record->{ "IDENT" } = $fields[ 2 ];
3376 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3377 $record->{ "MISMATCHES" } = $fields[ 4 ];
3378 $record->{ "GAPS" } = $fields[ 5 ];
3379 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3380 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3381 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3382 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3383 $record->{ "E_VAL" } = $fields[ 10 ];
3384 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3386 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3388 $record->{ "STRAND" } = '-';
3390 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3394 $record->{ "STRAND" } = '+';
3397 put_record( $record, $out );
3408 # Martin A. Hansen, August 2007.
3410 # BLATs sequences in stream against a given genome.
3412 my ( $in, # handle to in stream
3413 $out, # handle to out stream
3414 $options, # options hash
3419 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3421 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3423 $options->{ 'tile_size' } ||= 11;
3424 $options->{ 'one_off' } ||= 0;
3425 $options->{ 'min_identity' } ||= 90;
3426 $options->{ 'min_score' } ||= 0;
3427 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3429 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3430 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3431 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3432 $blat_args .= " -minScore=$options->{ 'min_score' }";
3433 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3434 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3436 $query_file = "$BP_TMP/blat.seq";
3438 $fh_out = Maasha::Common::write_open( $query_file );
3440 while ( $record = get_record( $in ) )
3442 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3444 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3445 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3448 put_record( $record, $out );
3453 $blat_args .= " -t=dnax" if $type eq "protein";
3454 $blat_args .= " -q=$type";
3456 $result_file = "$BP_TMP/blat.psl";
3458 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3462 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3464 map { put_record( $_, $out ) } @{ $entries };
3466 unlink $result_file;
3470 sub script_match_seq
3472 # Martin A. Hansen, August 2007.
3474 # BLATs sequences in stream against a given genome.
3476 my ( $in, # handle to in stream
3477 $out, # handle to out stream
3478 $options, # options hash
3483 my ( $record, @entries, $results );
3485 $options->{ "word_size" } ||= 20;
3486 $options->{ "direction" } ||= "both";
3488 while ( $record = get_record( $in ) )
3490 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3491 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3494 put_record( $record, $out );
3497 if ( @entries == 1 )
3499 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3501 map { put_record( $_, $out ) } @{ $results };
3503 elsif ( @entries == 2 )
3505 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3507 map { put_record( $_, $out ) } @{ $results };
3512 sub script_create_vmatch_index
3514 # Martin A. Hansen, January 2008.
3516 # Create a vmatch index from sequences in the stream.
3518 my ( $in, # handle to in stream
3519 $out, # handle to out stream
3520 $options, # options hash
3525 my ( $record, $file_tmp, $fh_tmp, $type );
3527 if ( $options->{ "index_name" } )
3529 $file_tmp = $options->{ 'index_name' };
3530 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3533 while ( $record = get_record( $in ) )
3535 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3537 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3539 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3542 put_record( $record, $out ) if not $options->{ "no_stream" };
3545 if ( $options->{ "index_name" } )
3549 if ( $type eq "protein" ) {
3550 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3552 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3560 sub script_vmatch_seq
3562 # Martin A. Hansen, August 2007.
3564 # Vmatches 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 ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3575 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3577 if ( $options->{ "index_name" } )
3579 @index_files = $options->{ "index_name" };
3583 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3585 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3587 @index_files = sort keys %hash;
3590 while ( $record = get_record( $in ) )
3592 push @records, $record;
3594 put_record( $record, $out );
3597 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3601 $fh_in = Maasha::Common::read_open( $result_file );
3603 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3604 put_record( $record, $out );
3609 unlink $result_file;
3613 sub script_write_fasta
3615 # Martin A. Hansen, August 2007.
3617 # Write FASTA entries from sequences in stream.
3619 my ( $in, # handle to in stream
3620 $out, # handle to out stream
3621 $options, # options hash
3626 my ( $record, $fh );
3628 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3630 while ( $record = get_record( $in ) )
3632 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3633 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3636 put_record( $record, $out ) if not $options->{ "no_stream" };
3643 sub script_write_align
3645 # Martin A. Hansen, August 2007.
3647 # Write pretty alignments aligned sequences in stream.
3649 my ( $in, # handle to in stream
3650 $out, # handle to out stream
3651 $options, # options hash
3656 my ( $fh, $record, @entries );
3658 $fh = write_stream( $options->{ "data_out" } ) ;
3660 while ( $record = get_record( $in ) )
3662 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3663 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3666 put_record( $record, $out ) if not $options->{ "no_stream" };
3669 if ( scalar( @entries ) == 2 ) {
3670 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3671 } elsif ( scalar ( @entries ) > 2 ) {
3672 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3679 sub script_write_blast
3681 # Martin A. Hansen, November 2007.
3683 # Write data in blast table format (-m8 and 9).
3685 my ( $in, # handle to in stream
3686 $out, # handle to out stream
3687 $options, # options hash
3692 my ( $fh, $record, $first );
3694 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3698 while ( $record = get_record( $in ) )
3700 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3702 if ( $options->{ "comment" } and $first )
3704 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";
3709 if ( $record->{ "STRAND" } eq "-" ) {
3710 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3713 print $fh join( "\t",
3714 $record->{ "Q_ID" },
3715 $record->{ "S_ID" },
3716 $record->{ "IDENT" },
3717 $record->{ "ALIGN_LEN" },
3718 $record->{ "MISMATCHES" },
3719 $record->{ "GAPS" },
3720 $record->{ "Q_BEG" } + 1,
3721 $record->{ "Q_END" } + 1,
3722 $record->{ "S_BEG" } + 1,
3723 $record->{ "S_END" } + 1,
3724 $record->{ "E_VAL" },
3725 $record->{ "BIT_SCORE" }
3729 put_record( $record, $out ) if not $options->{ "no_stream" };
3736 sub script_write_tab
3738 # Martin A. Hansen, August 2007.
3740 # Write data as table.
3742 my ( $in, # handle to in stream
3743 $out, # handle to out stream
3744 $options, # options hash
3749 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3751 $options->{ "delimit" } ||= "\t";
3753 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3755 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3757 while ( $record = get_record( $in ) )
3762 if ( $options->{ "keys" } )
3764 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3768 foreach $key ( @{ $options->{ "keys" } } )
3770 if ( exists $record->{ $key } )
3772 push @keys, $key if $options->{ "comment" };
3773 push @vals, $record->{ $key };
3780 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3782 next if exists $no_keys{ $key };
3784 push @keys, $key if $options->{ "comment" };
3785 push @vals, $record->{ $key };
3789 if ( @keys and $options->{ "comment" } )
3791 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3793 delete $options->{ "comment" };
3796 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3798 put_record( $record, $out ) if not $options->{ "no_stream" };
3805 sub script_write_bed
3807 # Martin A. Hansen, August 2007.
3809 # Write BED format for the UCSC genome browser using records in stream.
3811 # Crude - needs lots of work!
3813 my ( $in, # handle to in stream
3814 $out, # handle to out stream
3815 $options, # options hash
3820 my ( $fh, $record, $new_record );
3822 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3824 while ( $record = get_record( $in ) )
3826 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3828 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3830 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3832 $new_record->{ "CHR" } = $record->{ "S_ID" };
3833 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3834 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3835 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3836 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
3837 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3839 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3841 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
3843 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
3845 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
3847 $new_record->{ "CHR" } = $record->{ "S_ID" };
3848 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3849 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3850 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3851 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3852 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3854 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3856 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
3858 $new_record->{ "CHR" } = $record->{ "S_ID" };
3859 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3860 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3861 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3862 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3863 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3865 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3867 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
3869 Maasha::UCSC::bed_put_entry( $record, $fh );
3872 put_record( $record, $out ) if not $options->{ "no_stream" };
3879 sub script_write_psl
3881 # Martin A. Hansen, August 2007.
3883 # Write PSL output from stream.
3885 my ( $in, # handle to in stream
3886 $out, # handle to out stream
3887 $options, # options hash
3892 my ( $fh, $record, @output, $first );
3896 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3898 while ( $record = get_record( $in ) )
3900 put_record( $record, $out ) if not $options->{ "no_stream" };
3902 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3904 Maasha::UCSC::psl_put_header( $fh ) if $first;
3905 Maasha::UCSC::psl_put_entry( $record, $fh );
3914 sub script_write_2bit
3916 # Martin A. Hansen, March 2008.
3918 # Write sequence entries from stream in 2bit format.
3920 my ( $in, # handle to in stream
3921 $out, # handle to out stream
3922 $options, # options hash
3927 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
3929 $mask = 1 if not $options->{ "no_mask" };
3931 $tmp_file = "$BP_TMP/write_2bit.fna";
3932 $fh_tmp = Maasha::Common::write_open( $tmp_file );
3934 $fh_out = write_stream( $options->{ "data_out" } );
3936 while ( $record = get_record( $in ) )
3938 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3939 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3942 put_record( $record, $out ) if not $options->{ "no_stream" };
3947 $fh_in = Maasha::Common::read_open( $tmp_file );
3949 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3958 sub script_write_solid
3960 # Martin A. Hansen, April 2008.
3962 # Write di-base encoded Solid sequence from entries in stream.
3964 my ( $in, # handle to in stream
3965 $out, # handle to out stream
3966 $options, # options hash
3971 my ( $record, $fh, $seq_cs );
3973 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3975 while ( $record = get_record( $in ) )
3977 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3979 $seq_cs = Maasha::Solid::seq2color_space( $record->{ "SEQ" } );
3981 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
3984 put_record( $record, $out ) if not $options->{ "no_stream" };
3991 sub script_plot_seqlogo
3993 # Martin A. Hansen, August 2007.
3995 # Calculates and writes a sequence logo for alignments.
3997 my ( $in, # handle to in stream
3998 $out, # handle to out stream
3999 $options, # options hash
4004 my ( $record, @entries, $logo, $fh );
4006 while ( $record = get_record( $in ) )
4008 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4009 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4012 put_record( $record, $out ) if not $options->{ "no_stream" };
4015 $logo = Maasha::Plot::seq_logo( \@entries );
4017 $fh = write_stream( $options->{ "data_out" } );
4025 sub script_plot_phastcons_profiles
4027 # Martin A. Hansen, January 2008.
4029 # Plots PhastCons profiles.
4031 my ( $in, # handle to in stream
4032 $out, # handle to out stream
4033 $options, # options hash
4038 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4040 $options->{ "title" } ||= "PhastCons Profiles";
4042 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4043 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4045 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4046 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4048 while ( $record = get_record( $in ) )
4050 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4052 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4054 push @{ $AoA }, [ @{ $scores } ];
4057 put_record( $record, $out ) if not $options->{ "no_stream" };
4060 Maasha::UCSC::phastcons_normalize( $AoA );
4062 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4063 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4065 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4067 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4069 $fh = write_stream( $options->{ "data_out" } );
4071 print $fh "$_\n" foreach @{ $plot };
4077 sub script_analyze_bed
4079 # Martin A. Hansen, March 2008.
4081 # Analyze BED entries in stream.
4083 my ( $in, # handle to in stream
4084 $out, # handle to out stream
4085 $options, # options hash
4092 while ( $record = get_record( $in ) )
4094 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4096 put_record( $record, $out );
4101 sub script_analyze_vals
4103 # Martin A. Hansen, August 2007.
4105 # Analyze values for given keys in stream.
4107 my ( $in, # handle to in stream
4108 $out, # handle to out stream
4109 $options, # options hash
4114 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4116 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4118 while ( $record = get_record( $in ) )
4120 foreach $key ( keys %{ $record } )
4122 next if $options->{ "keys" } and not exists $key_hash{ $key };
4124 $analysis->{ $key }->{ "COUNT" }++;
4126 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4128 $analysis->{ $key }->{ "TYPE" } = "num";
4129 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4130 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4131 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4135 $len = length $record->{ $key };
4137 $analysis->{ $key }->{ "TYPE" } = "alph";
4138 $analysis->{ $key }->{ "SUM" } += $len;
4139 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4140 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4144 put_record( $record, $out ) if not $options->{ "no_stream" };
4147 foreach $key ( keys %{ $analysis } )
4149 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4150 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUN" };
4153 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4163 if ( $options->{ "keys" } ) {
4164 @keys = @{ $options->{ "keys" } };
4166 @keys = keys %{ $analysis };
4169 foreach $key ( @keys )
4171 $keys .= sprintf "% 15s", $key;
4172 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4173 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4174 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4175 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4176 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4177 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4180 print $out "$keys\n";
4181 print $out "$types\n";
4182 print $out "$counts\n";
4183 print $out "$mins\n";
4184 print $out "$maxs\n";
4185 print $out "$sums\n";
4186 print $out "$means\n";
4190 sub script_head_records
4192 # Martin A. Hansen, August 2007.
4194 # Display the first sequences in stream.
4196 my ( $in, # handle to in stream
4197 $out, # handle to out stream
4198 $options, # options hash
4203 my ( $record, $count );
4205 $options->{ "num" } ||= 10;
4209 while ( $record = get_record( $in ) )
4213 put_record( $record, $out );
4215 last if $count == $options->{ "num" };
4220 sub script_remove_keys
4222 # Martin A. Hansen, August 2007.
4224 # Remove keys from stream.
4226 my ( $in, # handle to in stream
4227 $out, # handle to out stream
4228 $options, # options hash
4233 my ( $record, $new_record );
4235 while ( $record = get_record( $in ) )
4237 if ( $options->{ "keys" } )
4239 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4241 elsif ( $options->{ "save_keys" } )
4243 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4245 $record = $new_record;
4248 put_record( $record, $out ) if keys %{ $record };
4253 sub script_rename_keys
4255 # Martin A. Hansen, August 2007.
4257 # Rename keys in stream.
4259 my ( $in, # handle to in stream
4260 $out, # handle to out stream
4261 $options, # options hash
4268 while ( $record = get_record( $in ) )
4270 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4272 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4274 delete $record->{ $options->{ "keys" }->[ 0 ] };
4277 put_record( $record, $out );
4282 sub script_uniq_vals
4284 # Martin A. Hansen, August 2007.
4286 # Find unique values in stream.
4288 my ( $in, # handle to in stream
4289 $out, # handle to out stream
4290 $options, # options hash
4295 my ( %hash, $record );
4297 while ( $record = get_record( $in ) )
4299 if ( $record->{ $options->{ "key" } } )
4301 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4303 put_record( $record, $out );
4305 $hash{ $record->{ $options->{ "key" } } } = 1;
4307 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4309 put_record( $record, $out );
4313 $hash{ $record->{ $options->{ "key" } } } = 1;
4318 put_record( $record, $out );
4324 sub script_merge_vals
4326 # Martin A. Hansen, August 2007.
4328 # Rename keys in stream.
4330 my ( $in, # handle to in stream
4331 $out, # handle to out stream
4332 $options, # options hash
4337 my ( $record, @join, $i );
4339 $options->{ "delimit" } ||= '_';
4341 while ( $record = get_record( $in ) )
4343 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4345 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4347 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4348 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4351 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4354 put_record( $record, $out );
4361 # Martin A. Hansen, August 2007.
4363 # Grab for records in stream.
4365 my ( $in, # handle to in stream
4366 $out, # handle to out stream
4367 $options, # options hash
4372 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4374 if ( $options->{ "patterns" } )
4376 $patterns = [ split ",", $options->{ "patterns" } ];
4378 elsif ( -f $options->{ "patterns_in" } )
4380 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4382 elsif ( -f $options->{ "exact_in" } )
4384 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4386 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4391 if ( $options->{ "eval" } )
4393 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4401 while ( $record = get_record( $in ) )
4407 if ( $options->{ "keys" } )
4409 foreach $key ( @{ $options->{ "keys" } } )
4411 if ( exists $lookup_hash{ $record->{ $key } } )
4420 foreach $key ( keys %{ $record } )
4422 if ( not $options->{ "vals_only" } )
4424 if ( exists $lookup_hash{ $key } )
4431 if ( not $options->{ "keys_only" } )
4433 if ( exists $lookup_hash{ $record->{ $key } } )
4444 foreach $pattern ( @{ $patterns } )
4446 if ( $options->{ "keys" } )
4448 foreach $key ( @{ $options->{ "keys" } } )
4450 $pos = index $record->{ $key }, $pattern;
4452 goto FOUND if $pos >= 0;
4457 foreach $key ( keys %{ $record } )
4459 if ( not $options->{ "vals_only" } )
4461 $pos = index $key, $pattern;
4463 goto FOUND if $pos >= 0;
4466 if ( not $options->{ "keys_only" } )
4468 $pos = index $record->{ $key }, $pattern;
4470 goto FOUND if $pos >= 0;
4476 elsif ( $options->{ "regex" } )
4478 if ( $options->{ "keys" } )
4480 foreach $key ( @{ $options->{ "keys" } } )
4482 if ( $options->{ "case_insensitive" } ) {
4483 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4485 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4488 goto FOUND if $pos >= 0;
4493 foreach $key ( keys %{ $record } )
4495 if ( not $options->{ "vals_only" } )
4497 if ( $options->{ "case_insensitive" } ) {
4498 $pos = 1 if $key =~ /$options->{'regex'}/i;
4500 $pos = 1 if $key =~ /$options->{'regex'}/;
4503 goto FOUND if $pos >= 0;
4506 if ( not $options->{ "keys_only" } )
4508 if ( $options->{ "case_insensitive" } ) {
4509 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4511 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4514 goto FOUND if $pos >= 0;
4519 elsif ( $options->{ "eval" } )
4521 if ( defined $record->{ $key } )
4523 if ( $op eq "<" and $record->{ $key } < $val ) {
4524 $pos = 1 and goto FOUND;
4525 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4526 $pos = 1 and goto FOUND;
4527 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4528 $pos = 1 and goto FOUND;
4529 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4530 $pos = 1 and goto FOUND;
4531 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4532 $pos = 1 and goto FOUND;
4533 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4534 $pos = 1 and goto FOUND;
4535 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4536 $pos = 1 and goto FOUND;
4537 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4538 $pos = 1 and goto FOUND;
4545 if ( $pos >= 0 and not $options->{ "invert" } ) {
4546 put_record( $record, $out );
4547 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4548 put_record( $record, $out );
4556 # Martin A. Hansen, August 2007.
4558 # Evaluate extression for records in stream.
4560 my ( $in, # handle to in stream
4561 $out, # handle to out stream
4562 $options, # options hash
4567 my ( $record, $eval_key, $eval_val, $check, @keys );
4569 while ( $record = get_record( $in ) )
4571 if ( $options->{ "eval" } )
4573 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4581 @keys = split /\W+/, $eval_val;
4582 @keys = grep { ! /^\d+$/ } @keys;
4587 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4589 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4592 put_record( $record, $out );
4599 # Martin A. Hansen, June 2008.
4603 my ( $in, # handle to in stream
4604 $out, # handle to out stream
4605 $options, # options hash
4610 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4612 while ( $record = get_record( $in ) )
4616 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4618 push @rows, $record->{ $key };
4622 push @matrix, [ @rows ];
4627 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4629 foreach $row ( @matrix )
4631 for ( $i = 0; $i < @{ $row }; $i++ ) {
4632 $record->{ "V$i" } = $row->[ $i ];
4635 put_record( $record, $out );
4640 sub script_add_ident
4642 # Martin A. Hansen, May 2008.
4644 # Add a unique identifier to each record in stream.
4646 my ( $in, # handle to in stream
4647 $out, # handle to out stream
4648 $options, # options hash
4653 my ( $record, $key, $prefix, $i );
4655 $key = $options->{ "key" } || "ID";
4656 $prefix = $options->{ "prefix" } || "ID";
4660 while ( $record = get_record( $in ) )
4662 $record->{ $key } = sprintf( "$prefix%08d", $i );
4664 put_record( $record, $out );
4671 sub script_count_records
4673 # Martin A. Hansen, August 2007.
4675 # Count records in stream.
4677 my ( $in, # handle to in stream
4678 $out, # handle to out stream
4679 $options, # options hash
4684 my ( $record, $count, $result, $fh, $line );
4688 if ( $options->{ "no_stream" } )
4690 while ( $line = <$in> )
4694 $count++ if $line eq "---";
4699 while ( $record = get_record( $in ) )
4701 put_record( $record, $out );
4707 $result = { "count_records" => $count };
4709 $fh = write_stream( $options->{ "data_out" } );
4711 put_record( $result, $fh );
4717 sub script_random_records
4719 # Martin A. Hansen, August 2007.
4721 # Pick a number or random records from stream.
4723 my ( $in, # handle to in stream
4724 $out, # handle to out stream
4725 $options, # options hash
4730 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4732 $options->{ "num" } ||= 10;
4734 $tmp_file = "$BP_TMP/random_records.tmp";
4736 $fh_out = Maasha::Common::write_open( $tmp_file );
4740 while ( $record = get_record( $in ) )
4742 put_record( $record, $fh_out );
4752 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4754 while ( $i < $options->{ "num" } )
4756 $rand = int( rand( $count ) );
4758 if ( not exists $rand_hash{ $rand } )
4760 $rand_hash{ $rand } = 1;
4762 $max = $rand if $rand > $max;
4768 $fh_in = Maasha::Common::read_open( $tmp_file );
4772 while ( $record = get_record( $fh_in ) )
4774 put_record( $record, $out ) if exists $rand_hash{ $count };
4776 last if $count == $max;
4787 sub script_sort_records
4789 # Martin A. Hansen, August 2007.
4791 # Sort to sort records according to keys.
4793 my ( $in, # handle to in stream
4794 $out, # handle to out stream
4795 $options, # options hash
4800 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4802 foreach $key ( @{ $options->{ "keys" } } )
4804 if ( $key =~ s/n$// ) {
4805 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4807 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4811 $sort_str = join " or ", @sort_cmd;
4812 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4814 while ( $record = get_record( $in ) ) {
4815 push @records, $record;
4818 @records = sort $sort_sub @records;
4820 if ( $options->{ "reverse" } )
4822 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4823 put_record( $records[ $i ], $out );
4828 for ( $i = 0; $i < scalar @records; $i++ ) {
4829 put_record( $records[ $i ], $out );
4835 sub script_count_vals
4837 # Martin A. Hansen, August 2007.
4839 # Count records in stream.
4841 my ( $in, # handle to in stream
4842 $out, # handle to out stream
4843 $options, # options hash
4848 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4850 $tmp_file = "$BP_TMP/count_cache.tmp";
4852 $fh_out = Maasha::Common::write_open( $tmp_file );
4856 while ( $record = get_record( $in ) )
4858 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4860 push @records, $record;
4862 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4864 map { put_record( $_, $fh_out ) } @records;
4871 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4882 $fh_in = Maasha::Common::read_open( $tmp_file );
4884 while ( $record = get_record( $fh_in ) )
4886 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4888 put_record( $record, $out );
4890 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4898 foreach $record ( @records )
4900 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4902 put_record( $record, $out );
4909 sub script_plot_histogram
4911 # Martin A. Hansen, September 2007.
4913 # Plot a simple histogram for a given key using GNU plot.
4915 my ( $in, # handle to in stream
4916 $out, # handle to out stream
4917 $options, # options hash
4922 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4924 $options->{ "title" } ||= "Histogram";
4925 $options->{ "sort" } ||= "num";
4927 while ( $record = get_record( $in ) )
4929 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
4931 put_record( $record, $out ) if not $options->{ "no_stream" };
4934 if ( $options->{ "sort" } eq "num" ) {
4935 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4937 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4940 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4942 $fh = write_stream( $options->{ "data_out" } );
4944 print $fh "$_\n" foreach @{ $result };
4950 sub script_plot_lendist
4952 # Martin A. Hansen, August 2007.
4954 # Plot length distribution using GNU plot.
4956 my ( $in, # handle to in stream
4957 $out, # handle to out stream
4958 $options, # options hash
4963 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4965 $options->{ "title" } ||= "Length Distribution";
4967 while ( $record = get_record( $in ) )
4969 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
4971 put_record( $record, $out ) if not $options->{ "no_stream" };
4974 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4976 for ( $i = 0; $i < $max; $i++ ) {
4977 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4980 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4982 $fh = write_stream( $options->{ "data_out" } );
4984 print $fh "$_\n" foreach @{ $result };
4990 sub script_plot_chrdist
4992 # Martin A. Hansen, August 2007.
4994 # Plot chromosome distribution using GNU plot.
4996 my ( $in, # handle to in stream
4997 $out, # handle to out stream
4998 $options, # options hash
5003 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5005 $options->{ "title" } ||= "Chromosome Distribution";
5007 while ( $record = get_record( $in ) )
5009 if ( $record->{ "CHR" } ) { # generic
5010 $data_hash{ $record->{ "CHR" } }++;
5011 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5012 $data_hash{ $record->{ "S_ID" } }++;
5013 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5014 $data_hash{ $record->{ "S_ID" } }++;
5015 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5016 $data_hash{ $record->{ "S_ID" } }++;
5019 put_record( $record, $out ) if not $options->{ "no_stream" };
5022 foreach $elem ( keys %data_hash )
5026 $sort_key =~ s/chr//i;
5028 $sort_key =~ s/^X(.*)/99$1/;
5029 $sort_key =~ s/^Y(.*)/99$1/;
5030 $sort_key =~ s/^Z(.*)/999$1/;
5031 $sort_key =~ s/^M(.*)/9999$1/;
5032 $sort_key =~ s/^U(.*)/99999$1/;
5034 $count = $sort_key =~ tr/_//;
5036 $sort_key =~ s/_.*/"999999" x $count/ex;
5038 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5041 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5043 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5045 $fh = write_stream( $options->{ "data_out" } );
5047 print $fh "$_\n" foreach @{ $result };
5053 sub script_plot_karyogram
5055 # Martin A. Hansen, August 2007.
5057 # Plot hits on karyogram.
5059 my ( $in, # handle to in stream
5060 $out, # handle to out stream
5061 $options, # options hash
5066 my ( %options, $record, @data, $fh, $result, %data_hash );
5068 $options->{ "genome" } ||= "human";
5069 $options->{ "feat_color" } ||= "black";
5071 while ( $record = get_record( $in ) )
5073 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5075 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5078 put_record( $record, $out ) if not $options->{ "no_stream" };
5081 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5083 $fh = write_stream( $options->{ "data_out" } );
5091 sub script_plot_matches
5093 # Martin A. Hansen, August 2007.
5095 # Plot matches in 2D generating a dotplot.
5097 my ( $in, # handle to in stream
5098 $out, # handle to out stream
5099 $options, # options hash
5104 my ( $record, @data, $fh, $result, %data_hash );
5106 $options->{ "direction" } ||= "both";
5108 while ( $record = get_record( $in ) )
5110 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5111 push @data, $record;
5114 put_record( $record, $out ) if not $options->{ "no_stream" };
5117 $options->{ "title" } ||= "plot_matches";
5118 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5119 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5121 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5123 $fh = write_stream( $options->{ "data_out" } );
5125 print $fh "$_\n" foreach @{ $result };
5131 sub script_length_vals
5133 # Martin A. Hansen, August 2007.
5135 # Determine the length of the value for given keys.
5137 my ( $in, # handle to in stream
5138 $out, # handle to out stream
5139 $options, # options hash
5144 my ( $record, $key );
5146 while ( $record = get_record( $in ) )
5148 foreach $key ( @{ $options->{ "keys" } } )
5150 if ( $record->{ $key } ) {
5151 $record->{ $key . "_LEN" } = length $record->{ $key };
5155 put_record( $record, $out );
5162 # Martin A. Hansen, August 2007.
5164 # Calculates the sums for values of given keys.
5166 my ( $in, # handle to in stream
5167 $out, # handle to out stream
5168 $options, # options hash
5173 my ( $record, $key, %sum_hash, $fh );
5175 while ( $record = get_record( $in ) )
5177 foreach $key ( @{ $options->{ "keys" } } )
5179 if ( $record->{ $key } ) {
5180 $sum_hash{ $key } += $record->{ $key };
5184 put_record( $record, $out ) if not $options->{ "no_stream" };
5187 $fh = write_stream( $options->{ "data_out" } );
5189 foreach $key ( @{ $options->{ "keys" } } ) {
5190 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5197 sub script_mean_vals
5199 # Martin A. Hansen, August 2007.
5201 # Calculate the mean of values of given keys.
5203 my ( $in, # handle to in stream
5204 $out, # handle to out stream
5205 $options, # options hash
5210 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5212 while ( $record = get_record( $in ) )
5214 foreach $key ( @{ $options->{ "keys" } } )
5216 if ( $record->{ $key } )
5218 $sum_hash{ $key } += $record->{ $key };
5219 $count_hash{ $key }++;
5223 put_record( $record, $out ) if not $options->{ "no_stream" };
5226 $fh = write_stream( $options->{ "data_out" } );
5228 foreach $key ( @{ $options->{ "keys" } } )
5230 if ( $count_hash{ $key } ) {
5231 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5236 put_record( { $key . "_MEAN" => $mean } , $fh );
5243 sub script_median_vals
5245 # Martin A. Hansen, March 2008.
5247 # Calculate the median values of given keys.
5249 my ( $in, # handle to in stream
5250 $out, # handle to out stream
5251 $options, # options hash
5256 my ( $record, $key, %median_hash, $median, $fh );
5258 while ( $record = get_record( $in ) )
5260 foreach $key ( @{ $options->{ "keys" } } ) {
5261 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5264 put_record( $record, $out ) if not $options->{ "no_stream" };
5267 $fh = write_stream( $options->{ "data_out" } );
5269 foreach $key ( @{ $options->{ "keys" } } )
5271 if ( $median_hash{ $key } ) {
5272 $median = Maasha::Calc::median( $median_hash{ $key } );
5277 put_record( { $key . "_MEDIAN" => $median } , $fh );
5286 # Martin A. Hansen, February 2008.
5288 # Determine the maximum values of given keys.
5290 my ( $in, # handle to in stream
5291 $out, # handle to out stream
5292 $options, # options hash
5297 my ( $record, $key, $fh, %max_hash, $max_record );
5299 while ( $record = get_record( $in ) )
5301 foreach $key ( @{ $options->{ "keys" } } )
5303 if ( $record->{ $key } )
5305 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5309 put_record( $record, $out ) if not $options->{ "no_stream" };
5312 $fh = write_stream( $options->{ "data_out" } );
5314 foreach $key ( @{ $options->{ "keys" } } )
5316 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5319 put_record( $max_record, $fh );
5327 # Martin A. Hansen, February 2008.
5329 # Determine the minimum values of given keys.
5331 my ( $in, # handle to in stream
5332 $out, # handle to out stream
5333 $options, # options hash
5338 my ( $record, $key, $fh, %min_hash, $min_record );
5340 while ( $record = get_record( $in ) )
5342 foreach $key ( @{ $options->{ "keys" } } )
5344 if ( defined $record->{ $key } )
5346 if ( exists $min_hash{ $key } ) {
5347 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5349 $min_hash{ $key } = $record->{ $key };
5354 put_record( $record, $out ) if not $options->{ "no_stream" };
5357 $fh = write_stream( $options->{ "data_out" } );
5359 foreach $key ( @{ $options->{ "keys" } } )
5361 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5364 put_record( $min_record, $fh );
5370 sub script_upload_to_ucsc
5372 # Martin A. Hansen, August 2007.
5374 # Calculate the mean of values of given keys.
5376 my ( $in, # handle to in stream
5377 $out, # handle to out stream
5378 $options, # options hash
5383 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5384 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5386 $options->{ "short_label" } ||= $options->{ 'table' };
5387 $options->{ "long_label" } ||= $options->{ 'table' };
5388 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5389 $options->{ "priority" } ||= 1;
5390 $options->{ "visibility" } ||= "pack";
5391 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5392 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5394 $file = "$BP_TMP/ucsc_upload.tmp";
5402 if ( $options->{ 'wiggle' } )
5404 $options->{ "visibility" } = "full";
5406 while ( $record = get_record( $in ) )
5408 put_record( $record, $out ) if not $options->{ "no_stream" };
5410 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5411 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5412 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5414 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5416 $fh_out = $fh_hash{ $record->{ "CHR" } };
5418 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5421 map { close $_ } keys %fh_hash;
5423 $fh_out = Maasha::Common::write_open( $file );
5425 foreach $chr ( sort keys %fh_hash )
5427 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5429 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5433 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5435 $chr = $entry->{ 'CHR' };
5436 $beg = $entry->{ 'CHR_BEG' };
5437 $end = $entry->{ 'CHR_END' };
5438 $q_id = $entry->{ 'Q_ID' };
5440 if ( $q_id =~ /_(\d+)$/ ) {
5450 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5455 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5456 $block->[ $i ] += $clones;
5459 $max = Maasha::Calc::max( $max, $end );
5468 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5469 $block->[ $i ] += $clones;
5476 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5478 unlink "$BP_TMP/$chr";
5483 $wig_file = "$options->{ 'table' }.wig";
5484 $wib_file = "$options->{ 'table' }.wib";
5486 $wib_dir = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'database' }/wib";
5488 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5490 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5492 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5493 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5503 $fh_out = Maasha::Common::write_open( $file );
5505 while ( $record = get_record( $in ) )
5507 put_record( $record, $out ) if not $options->{ "no_stream" };
5509 if ( $record->{ "REC_TYPE" } eq "PSL" )
5511 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5512 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5516 $format = "PSL" if not $format;
5518 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5520 # chrom chromStart chromEnd name score strand size secStr conf
5522 print $fh_out join ( "\t",
5524 $record->{ "CHR_BEG" },
5525 $record->{ "CHR_END" } + 1,
5526 $record->{ "Q_ID" },
5527 $record->{ "SCORE" },
5528 $record->{ "STRAND" },
5529 $record->{ "SIZE" },
5530 $record->{ "SEC_STRUCT" },
5531 $record->{ "CONF" },
5534 $format = "BED_SS" if not $format;
5536 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5538 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5540 $format = "BED" if not $format;
5541 $columns = $record->{ "BED_COLS" } if not $columns;
5543 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5545 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5547 $format = "BED" if not $format;
5548 $columns = 6 if not $columns;
5550 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5552 $record->{ "CHR" } = $record->{ "S_ID" };
5553 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5554 $record->{ "CHR_END" } = $record->{ "S_END" };
5555 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5557 $format = "BED" if not $format;
5558 $columns = 6 if not $columns;
5560 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5562 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5564 $record->{ "CHR" } = $record->{ "S_ID" };
5565 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5566 $record->{ "CHR_END" } = $record->{ "S_END" };
5567 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5568 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5570 $format = "BED" if not $format;
5571 $columns = 6 if not $columns;
5573 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5576 if ( $i == $options->{ "chunk_size" } )
5580 if ( $format eq "BED" ) {
5581 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5582 } elsif ( $format eq "PSL" ) {
5583 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5592 $fh_out = Maasha::Common::write_open( $file );
5601 if ( exists $options->{ "database" } and $options->{ "table" } )
5603 if ( $format eq "BED" )
5605 $type = "bed $columns";
5607 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5609 elsif ( $format eq "BED_SS" )
5611 $options->{ "sec_struct" } = 1;
5613 $type = "sec_struct";
5615 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5617 elsif ( $format eq "PSL" )
5621 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5623 elsif ( $format eq "WIGGLE" )
5627 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5632 Maasha::UCSC::update_my_tracks( $options, $type );
5637 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5642 # Martin A. Hansen, July 2007.
5644 # Opens a stream to STDIN or a file,
5646 my ( $path, # path - OPTIONAL
5649 # Returns filehandle.
5653 if ( not -t STDIN ) {
5654 $fh = Maasha::Common::read_stdin();
5655 } elsif ( not $path ) {
5656 # Maasha::Common::error( qq(no data stream) );
5658 $fh = Maasha::Common::read_open( $path );
5661 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5669 # Martin A. Hansen, August 2007.
5671 # Opens a stream to STDOUT or a file.
5673 my ( $path, # path - OPTIONAL
5674 $gzip, # compress data - OPTIONAL
5677 # Returns filehandle.
5682 $fh = Maasha::Common::write_open( $path, $gzip );
5684 $fh = Maasha::Common::write_stdout();
5693 # Martin A. Hansen, July 2007.
5695 # Reads one record at a time and converts that record
5696 # to a Perl data structure (a hash) which is returned.
5701 # Returns data structure.
5703 my ( $block, @lines, $line, $key, $value, %record );
5705 local $/ = "\n---\n";
5711 return if not defined $block;
5713 @lines = split "\n", $block;
5715 foreach $line ( @lines )
5717 ( $key, $value ) = split ": ", $line;
5719 $record{ $key } = $value;
5722 return wantarray ? %record : \%record;
5728 # Martin A. Hansen, July 2007.
5730 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5732 my ( $data, # data structure
5733 $fh, # file handle - OPTIONAL
5738 if ( scalar keys %{ $data } )
5742 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5747 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5758 # Martin A. Hansen, November 2007.
5760 # Extracts files from an explicit GetOpt::Long argument
5761 # allowing for the use of glob. E.g.
5762 # --data_in=test.fna
5763 # --data_in=test.fna,test2.fna
5765 # --data_in=test.fna,/dir/*.fna
5767 my ( $option, # option from GetOpt::Long
5772 my ( $elem, @files );
5774 foreach $elem ( split ",", $option )
5778 } elsif ( $elem =~ /\*/ ) {
5779 push @files, glob( $elem );
5783 return wantarray ? @files : \@files;
5789 # Martin A. Hansen, April 2008.
5791 # Removes temporary directory and exits gracefully.
5792 # This subroutine is meant to be run always as the last
5793 # thing even if a script is dies or is interrupted
5796 my ( $sig, # signal from the %SIG
5799 # print STDERR "signal->$sig<-\n";
5807 if ( $sig =~ /MAASHA_ERROR/ ) {
5808 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
5809 } elsif ( $sig eq "INT" ) {
5810 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
5811 } elsif ( $sig eq "TERM" ) {
5812 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
5814 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
5817 # This is a really bad solution, potentially, anyone can include this module and set
5818 # the BP_TMP to point at any dir and thus take out the machine !!!
5820 Maasha::Common::dir_remove( $BP_TMP );
5829 # This is a really bad solution, potentially, anyone can include this module and set
5830 # the BP_TMP to point at any dir and thus take out the machine !!!
5832 Maasha::Common::dir_remove( $BP_TMP );
5836 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5843 sub script_read_soft
5845 # Martin A. Hansen, December 2007.
5848 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
5850 my ( $in, # handle to in stream
5851 $out, # handle to out stream
5852 $options, # options hash
5857 my ( $data_in, $file, $num, $records, $record );
5859 while ( $record = get_record( $in ) ) {
5860 put_record( $record, $out );
5865 foreach $file ( @{ $options->{ "files" } } )
5867 $records = Maasha::NCBI::soft_parse( $file );
5869 foreach $record ( @{ $records } )
5871 put_record( $record, $out );
5873 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
5881 close $data_in if $data_in;