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;
56 use vars qw( @ISA @EXPORT_OK );
60 @ISA = qw( Exporter );
75 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
78 $SIG{ '__DIE__' } = \&sig_handler;
79 $SIG{ 'INT' } = \&sig_handler;
80 $SIG{ 'TERM' } = \&sig_handler;
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
86 my ( $script, $BP_TMP );
88 $script = Maasha::Common::get_scriptname();
89 $BP_TMP = Maasha::Common::get_tmpdir();
92 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
95 my $log_fh = Maasha::Common::append_open( $ENV{ "LOG_DIR" } . "/biopieces.log" );
97 $log_fh->autoflush( 1 );
99 log( $log_fh, $script, \@ARGV );
104 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
107 my $t0 = gettimeofday();
109 run_script( $script );
111 my $t1 = gettimeofday();
113 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
116 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121 # Martin A. Hansen, January 2008.
123 # Log messages to logfile.
125 my ( $fh, # filehandle to logfile
126 $script, # script name
127 $argv, # reference to @ARGV
132 my ( $time_stamp, $user );
134 $time_stamp = Maasha::Common::time_stamp();
136 $user = $ENV{ "USER" };
138 $script = "biopieces" if $script eq "-e";
140 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
146 # Martin A. Hansen, August 2007.
148 # Run a specific script.
150 my ( $script, # script name
155 my ( $options, $in, $out );
157 script_list_biopieces( $ENV{ 'INST_DIR'} . "/biopieces/usage/" ) if $script eq "list_biopieces";
159 script_print_usage( $ENV{ 'INST_DIR'} . "/biopieces/usage/$script" ) if -t STDIN and not @ARGV;
161 $options = get_options( $script );
163 $in = read_stream( $options->{ "stream_in" } );
164 $out = write_stream( $options->{ "stream_out" } );
166 if ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
167 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
168 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
169 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
170 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
171 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
172 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
173 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
174 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
175 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
176 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
177 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
178 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
179 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
180 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
181 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
182 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
183 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
184 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
185 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
186 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
187 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
188 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
189 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
190 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
191 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
192 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
193 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
194 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
195 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
196 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
197 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
198 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
199 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
200 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
201 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
202 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
203 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
204 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
205 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
206 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
207 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
208 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
209 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
210 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
211 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
212 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options, $options ) }
213 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
214 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
215 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
216 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
217 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
218 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options, $options ) }
219 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options, $options ) }
220 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
221 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
222 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
223 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
224 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
225 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
226 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
227 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
228 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
229 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
230 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
231 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
232 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
233 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
234 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
235 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
236 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
237 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
238 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
239 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
240 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
241 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
242 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
243 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
244 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
245 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
246 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
247 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
248 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
250 close $in if defined $in;
253 # unset status - missing
254 # write log file - missing
260 # Martin A. Hansen, February 2008.
262 # Gets options from commandline and checks these vigerously.
264 my ( $script, # name of script
269 my ( %options, @options, $opt, @genomes );
271 if ( $script eq "read_fasta" )
278 elsif ( $script eq "read_tab" )
289 elsif ( $script eq "read_psl" )
296 elsif ( $script eq "read_bed" )
303 elsif ( $script eq "read_blast_tab" )
310 elsif ( $script eq "read_embl" )
320 elsif ( $script eq "read_stockholm" )
327 elsif ( $script eq "read_phastcons" )
338 elsif ( $script eq "read_soft" )
345 elsif ( $script eq "read_gff" )
352 elsif ( $script eq "read_2bit" )
360 elsif ( $script eq "read_solexa" )
368 elsif ( $script eq "read_solid" )
376 elsif ( $script eq "read_mysql" )
385 elsif ( $script eq "length_seq" )
392 elsif ( $script eq "oligo_freq" )
399 elsif ( $script eq "create_weight_matrix" )
405 elsif ( $script eq "transliterate_seq" )
413 elsif ( $script eq "transliterate_vals" )
422 elsif ( $script eq "translate_seq" )
428 elsif ( $script eq "extract_seq" )
436 elsif ( $script eq "get_genome_seq" )
448 elsif ( $script eq "get_genome_align" )
459 elsif ( $script eq "get_genome_phastcons" )
470 elsif ( $script eq "split_seq" )
477 elsif ( $script eq "split_bed" )
484 elsif ( $script eq "tile_seq" )
491 elsif ( $script eq "invert_align" )
497 elsif ( $script eq "patscan_seq" )
508 elsif ( $script eq "create_blast_db" )
515 elsif ( $script eq "blast_seq" )
527 elsif ( $script eq "blat_seq" )
539 elsif ( $script eq "match_seq" )
546 elsif ( $script eq "create_vmatch_index" )
554 elsif ( $script eq "vmatch_seq" )
565 elsif ( $script eq "write_fasta" )
574 elsif ( $script eq "write_align" )
584 elsif ( $script eq "write_blast" )
593 elsif ( $script eq "write_tab" )
605 elsif ( $script eq "write_bed" )
613 elsif ( $script eq "write_psl" )
621 elsif ( $script eq "write_2bit" )
629 elsif ( $script eq "write_solid" )
638 elsif ( $script eq "plot_seqlogo" )
645 elsif ( $script eq "plot_phastcons_profiles" )
660 elsif ( $script eq "analyze_vals" )
667 elsif ( $script eq "head_records" )
673 elsif ( $script eq "remove_keys" )
680 elsif ( $script eq "rename_keys" )
686 elsif ( $script eq "uniq_vals" )
693 elsif ( $script eq "merge_vals" )
700 elsif ( $script eq "grab" )
715 elsif ( $script eq "compute" )
721 elsif ( $script eq "add_ident" )
728 elsif ( $script eq "count_records" )
735 elsif ( $script eq "random_records" )
741 elsif ( $script eq "sort_records" )
748 elsif ( $script eq "count_vals" )
754 elsif ( $script eq "plot_histogram" )
767 elsif ( $script eq "plot_lendist" )
779 elsif ( $script eq "plot_chrdist" )
790 elsif ( $script eq "plot_karyogram" )
799 elsif ( $script eq "plot_matches" )
811 elsif ( $script eq "length_vals" )
817 elsif ( $script eq "sum_vals" )
825 elsif ( $script eq "mean_vals" )
833 elsif ( $script eq "median_vals" )
841 elsif ( $script eq "max_vals" )
849 elsif ( $script eq "min_vals" )
857 elsif ( $script eq "upload_to_ucsc" )
881 # print STDERR Dumper( \@options );
888 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
889 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
890 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
891 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
892 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
893 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
894 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
896 # ---- check arguments ----
898 if ( $options{ 'data_in' } )
900 $options{ "files" } = getopt_files( $options{ 'data_in' } );
902 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
905 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
907 # print STDERR Dumper( \%options );
909 foreach $opt ( keys %options )
911 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
913 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
915 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+$/ )
917 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
919 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
921 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
923 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
925 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
927 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
929 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
931 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
933 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
935 elsif ( $opt eq "genome" )
937 @genomes = Maasha::Config::genomes();
939 if ( not grep $options{ $opt }, @genomes ) {
940 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'INST_DIR' }/conf/genomes.conf") );
943 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
945 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
947 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
949 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
953 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
954 Maasha::Common::error( qq(no --index_name specified) ) if $script eq "create_vmatch_index" and not $options{ "index_name" };
955 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
956 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
957 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
958 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
959 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
960 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
961 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" };
963 if ( $script eq "upload_to_ucsc" )
965 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
966 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
969 return wantarray ? %options : \%options;
973 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
976 sub script_print_usage
978 # Martin A. Hansen, January 2008.
980 # Retrieves usage information from file and
981 # prints this nicely formatted.
983 my ( $path, # full path to usage file
988 my ( $script, $fh, $line, @lines, @list, %hash, $key );
990 $script = ( split "/", $path )[ -1 ];
992 $fh = Maasha::Common::read_open( $path );
994 push @list, "Program name";
996 $hash{ "Program name" } = [ $script ];
998 while ( $line = <$fh> )
1002 $line =~ s/\$script/$script/g;
1004 if ( $line =~ /^([^:]+):\s+(.+)$/ )
1006 push @list, $1 if not exists $hash{ $1 };
1007 push @{ $hash{ $1 } }, $2;
1015 foreach $key ( @list )
1017 if ( scalar @{ $hash{ $key } } == 1 )
1019 @lines = Maasha::Common::wrap_line( $hash{ $key }->[ 0 ], 80 );
1021 printf( "%-15s%s\n", "$key:", shift @lines );
1023 map { printf( "%-15s%s\n", "", $_ ) } @lines;
1031 map { print " $_\n" } @{ $hash{ $key } };
1041 sub script_list_biopieces
1043 # Martin A. Hansen, January 2008.
1045 # Prints the description from the usage for each of the biopieces.
1047 my ( $path, # full path to usage directory
1052 my ( @files, $file, $fh, $line, @lines, $program );
1054 @files = Maasha::Common::ls_files( $path );
1056 foreach $file ( sort @files )
1058 $program = ( split "/", $file )[ -1 ];
1060 $fh = Maasha::Common::read_open( $file );
1062 while ( $line = <$fh> )
1066 if ( $line =~ /^Description:\s+(.+)/ )
1068 @lines = Maasha::Common::wrap_line( $1, 60 );
1070 printf( "%-30s%s\n", $program, shift @lines );
1072 map { printf( "%-30s%s\n", "", $_ ) } @lines;
1083 sub script_read_fasta
1085 # Martin A. Hansen, August 2007.
1087 # Read sequences from FASTA file.
1089 my ( $in, # handle to in stream
1090 $out, # handle to out stream
1091 $options, # options hash
1096 my ( $record, $file, $data_in, $entry, $num );
1098 while ( $record = get_record( $in ) ) {
1099 put_record( $record, $out );
1104 foreach $file ( @{ $options->{ "files" } } )
1106 $data_in = Maasha::Common::read_open( $file );
1108 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1110 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1113 SEQ_NAME => $entry->[ SEQ_NAME ],
1114 SEQ => $entry->[ SEQ ],
1115 SEQ_LEN => length $entry->[ SEQ ],
1118 put_record( $record, $out );
1121 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1131 close $data_in if $data_in;
1137 # Martin A. Hansen, August 2007.
1139 # Read table or table columns from stream or file.
1141 my ( $in, # handle to in stream
1142 $out, # handle to out stream
1143 $options, # options hash
1148 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1150 $options->{ 'delimit' } ||= '\s+';
1152 while ( $record = get_record( $in ) ) {
1153 put_record( $record, $out );
1156 $skip = $options->{ 'skip' } ||= 0;
1159 foreach $file ( @{ $options->{ "files" } } )
1161 $data_in = Maasha::Common::read_open( $file );
1163 while ( $line = <$data_in> )
1171 next if $line =~ /^#|^$/;
1178 @fields = split /$options->{'delimit'}/, $line;
1180 if ( $options->{ "cols" } ) {
1181 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1186 for ( $i = 0; $i < @fields2; $i++ )
1188 if ( $options->{ "keys" }->[ $i ] ) {
1189 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1191 $record->{ "V" . $i } = $fields2[ $i ];
1195 put_record( $record, $out );
1197 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1207 close $data_in if $data_in;
1213 # Martin A. Hansen, August 2007.
1215 # Read psl table from stream or file.
1217 my ( $in, # handle to in stream
1218 $out, # handle to out stream
1219 $options, # options hash
1224 my ( $record, @files, $file, $entries, $entry, $num );
1226 while ( $record = get_record( $in ) ) {
1227 put_record( $record, $out );
1232 foreach $file ( @{ $options->{ "files" } } )
1234 $entries = Maasha::UCSC::psl_get_entries( $file );
1236 foreach $entry ( @{ $entries } )
1238 put_record( $entry, $out );
1240 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1252 # Martin A. Hansen, August 2007.
1254 # Read bed table from stream or file.
1256 my ( $in, # handle to in stream
1257 $out, # handle to out stream
1258 $options, # options hash
1263 my ( $file, $record, $entry, $data_in, $num );
1265 while ( $record = get_record( $in ) ) {
1266 put_record( $record, $out );
1271 foreach $file ( @{ $options->{ "files" } } )
1273 $data_in = Maasha::Common::read_open( $file );
1275 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1277 put_record( $entry, $out );
1279 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1289 close $data_in if $data_in;
1293 sub script_read_blast_tab
1295 # Martin A. Hansen, September 2007.
1297 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1299 my ( $in, # handle to in stream
1300 $out, # handle to out stream
1301 $options, # options hash
1306 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1308 while ( $record = get_record( $in ) ) {
1309 put_record( $record, $out );
1314 foreach $file ( @{ $options->{ "files" } } )
1316 $data_in = Maasha::Common::read_open( $file );
1318 while ( $line = <$data_in> )
1322 next if $line =~ /^#/;
1324 @fields = split /\t/, $line;
1326 $record->{ "REC_TYPE" } = "BLAST";
1327 $record->{ "Q_ID" } = $fields[ 0 ];
1328 $record->{ "S_ID" } = $fields[ 1 ];
1329 $record->{ "IDENT" } = $fields[ 2 ];
1330 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1331 $record->{ "MISMATCHES" } = $fields[ 4 ];
1332 $record->{ "GAPS" } = $fields[ 5 ];
1333 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1334 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1335 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1336 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1337 $record->{ "E_VAL" } = $fields[ 10 ];
1338 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1340 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1342 $record->{ "STRAND" } = '-';
1344 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1348 $record->{ "STRAND" } = '+';
1351 put_record( $record, $out );
1353 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1363 close $data_in if $data_in;
1367 sub script_read_embl
1369 # Martin A. Hansen, August 2007.
1373 my ( $in, # handle to in stream
1374 $out, # handle to out stream
1375 $options, # options hash
1380 my ( %options2, $file, $data_in, $num, $entry, $record );
1382 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1383 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1384 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1386 while ( $record = get_record( $in ) ) {
1387 put_record( $record, $out );
1392 foreach $file ( @{ $options->{ "files" } } )
1394 $data_in = Maasha::Common::read_open( $file );
1396 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1398 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1400 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1402 $record_copy = dclone $record;
1404 delete $record_copy->{ "FT" };
1406 put_record( $record_copy, $out );
1408 delete $record_copy->{ "SEQ" };
1410 foreach $feat ( keys %{ $record->{ "FT" } } )
1412 $record_copy->{ "FEAT_TYPE" } = $feat;
1414 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1416 foreach $qual ( keys %{ $feat2 } )
1418 $qual_val = join "; ", @{ $feat2->{ $qual } };
1423 $record_copy->{ $qual } = $qual_val;
1426 put_record( $record_copy, $out );
1430 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1440 close $data_in if $data_in;
1444 sub script_read_stockholm
1446 # Martin A. Hansen, August 2007.
1448 # Read Stockholm format.
1450 my ( $in, # handle to in stream
1451 $out, # handle to out stream
1452 $options, # options hash
1457 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1459 while ( $record = get_record( $in ) ) {
1460 put_record( $record, $out );
1465 foreach $file ( @{ $options->{ "files" } } )
1467 $data_in = Maasha::Common::read_open( $file );
1469 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1471 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1475 foreach $key ( keys %{ $record->{ "GF" } } ) {
1476 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1479 $record_anno->{ "ALIGN" } = $num;
1481 put_record( $record_anno, $out );
1483 foreach $seq ( @{ $record->{ "ALIGN" } } )
1485 undef $record_align;
1488 SEQ_NAME => $seq->[ 0 ],
1492 put_record( $record_align, $out );
1495 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1505 close $data_in if $data_in;
1509 sub script_read_phastcons
1511 # Martin A. Hansen, December 2007.
1513 # Read PhastCons format.
1515 my ( $in, # handle to in stream
1516 $out, # handle to out stream
1517 $options, # options hash
1522 my ( $data_in, $file, $num, $entry, @records, $record );
1524 $options->{ "min" } ||= 10;
1525 $options->{ "dist" } ||= 25;
1526 $options->{ "threshold" } ||= 0.8;
1527 $options->{ "gap" } ||= 5;
1529 while ( $record = get_record( $in ) ) {
1530 put_record( $record, $out );
1535 foreach $file ( @{ $options->{ "files" } } )
1537 $data_in = Maasha::Common::read_open( $file );
1539 while ( $entry = Maasha::UCSC::phastcons_get_entry( $data_in ) )
1541 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1543 foreach $record ( @records )
1545 $record->{ "REC_TYPE" } = "BED";
1546 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1548 put_record( $record, $out );
1550 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1561 close $data_in if $data_in;
1565 sub script_read_soft
1567 # Martin A. Hansen, December 2007.
1570 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1572 my ( $in, # handle to in stream
1573 $out, # handle to out stream
1574 $options, # options hash
1579 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1581 while ( $record = get_record( $in ) ) {
1582 put_record( $record, $out );
1587 foreach $file ( @{ $options->{ "files" } } )
1589 $soft_index = Maasha::NCBI::soft_index_file( $file );
1591 $fh = Maasha::Common::read_open( $file );
1593 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1595 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1597 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1599 $old_end = $platforms[ -1 ]->[ 2 ];
1601 foreach $sample ( @samples )
1603 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1605 foreach $record ( @{ $records } )
1607 put_record( $record, $out );
1609 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1614 $old_end = $sample->[ 2 ];
1622 close $data_in if $data_in;
1629 # Martin A. Hansen, February 2008.
1632 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1634 my ( $in, # handle to in stream
1635 $out, # handle to out stream
1636 $options, # options hash
1641 my ( $data_in, $file, $fh, $num, $record, $entry );
1643 while ( $record = get_record( $in ) ) {
1644 put_record( $record, $out );
1649 foreach $file ( @{ $options->{ "files" } } )
1651 $fh = Maasha::Common::read_open( $file );
1653 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1655 put_record( $entry, $out );
1657 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1667 close $data_in if $data_in;
1671 sub script_read_2bit
1673 # Martin A. Hansen, March 2008.
1675 # Read sequences from 2bit file.
1677 my ( $in, # handle to in stream
1678 $out, # handle to out stream
1679 $options, # options hash
1684 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1686 $mask = 1 if not $options->{ "no_mask" };
1688 while ( $record = get_record( $in ) ) {
1689 put_record( $record, $out );
1694 foreach $file ( @{ $options->{ "files" } } )
1696 $data_in = Maasha::Common::read_open( $file );
1698 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1700 foreach $line ( @{ $toc } )
1702 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1703 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1704 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1706 put_record( $record, $out );
1708 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1718 close $data_in if $data_in;
1722 sub script_read_solexa
1724 # Martin A. Hansen, March 2008.
1726 # Read Solexa sequence reads from file.
1728 my ( $in, # handle to in stream
1729 $out, # handle to out stream
1730 $options, # options hash
1735 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1737 $options->{ "quality" } ||= 20;
1739 while ( $record = get_record( $in ) ) {
1740 put_record( $record, $out );
1745 foreach $file ( @{ $options->{ "files" } } )
1747 $data_in = Maasha::Common::read_open( $file );
1748 $base_name = Maasha::Common::get_basename( $file );
1749 $base_name =~ s/\..*//;
1753 while ( $line = <$data_in> )
1755 @fields = split /:/, $line;
1756 @seqs = split //, $fields[ 5 ];
1757 @scores = split / /, $fields[ -1 ];
1759 for ( $i = 0; $i < @scores; $i++ ) {
1760 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1763 $seq = join "", @seqs;
1765 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1766 $record->{ "SEQ" } = $seq;
1767 $record->{ "SEQ_LEN" } = length $seq;
1768 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1770 put_record( $record, $out );
1772 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1783 close $data_in if $data_in;
1787 sub script_read_solid
1789 # Martin A. Hansen, April 2008.
1791 # Read Solid sequence from file.
1793 my ( $in, # handle to in stream
1794 $out, # handle to out stream
1795 $options, # options hash
1800 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1802 $options->{ "quality" } ||= 15;
1804 while ( $record = get_record( $in ) ) {
1805 put_record( $record, $out );
1810 foreach $file ( @{ $options->{ "files" } } )
1812 $data_in = Maasha::Common::read_open( $file );
1814 while ( $line = <$data_in> )
1818 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1820 @scores = split /,/, $seq_qual;
1821 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1823 for ( $i = 0; $i < @seqs; $i++ ) {
1824 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1828 SEQ_NAME => $seq_name,
1830 SEQ_QUAL => $seq_qual,
1831 SEQ_LEN => length $seq_cs,
1832 SEQ => join( "", @seqs ),
1833 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1836 put_record( $record, $out );
1838 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1848 close $data_in if $data_in;
1852 sub script_read_mysql
1854 # Martin A. Hansen, May 2008.
1856 # Read a MySQL query into stream.
1858 my ( $in, # handle to in stream
1859 $out, # handle to out stream
1860 $options, # options hash
1865 my ( $record, $dbh, $results );
1867 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1868 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1870 while ( $record = get_record( $in ) ) {
1871 put_record( $record, $out );
1874 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1876 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1878 Maasha::SQL::disconnect( $dbh );
1880 map { put_record( $_ ) } @{ $results };
1884 sub script_length_seq
1886 # Martin A. Hansen, August 2007.
1888 # Determine the length of sequences in stream.
1890 my ( $in, # handle to in stream
1891 $out, # handle to out stream
1892 $options, # options hash
1897 my ( $record, $total );
1899 while ( $record = get_record( $in ) )
1901 if ( $record->{ "SEQ" } )
1903 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1904 $total += $record->{ "SEQ_LEN" };
1907 put_record( $record, $out ) if not $options->{ "no_stream" };
1910 put_record( { TOTAL_SEQ_LEN => $total }, $out );
1914 sub script_uppercase_seq
1916 # Martin A. Hansen, August 2007.
1918 # Uppercases sequences in stream.
1920 my ( $in, # handle to in stream
1921 $out, # handle to out stream
1928 while ( $record = get_record( $in ) )
1930 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
1932 put_record( $record, $out );
1937 sub script_shuffle_seq
1939 # Martin A. Hansen, December 2007.
1941 # Shuffle sequences in stream.
1943 my ( $in, # handle to in stream
1944 $out, # handle to out stream
1951 while ( $record = get_record( $in ) )
1953 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
1955 put_record( $record, $out );
1960 sub script_analyze_seq
1962 # Martin A. Hansen, August 2007.
1964 # Analyze sequence composition of sequences in stream.
1966 my ( $in, # handle to in stream
1967 $out, # handle to out stream
1972 my ( $record, $analysis );
1974 while ( $record = get_record( $in ) )
1976 if ( $record->{ "SEQ" } )
1978 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
1980 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
1983 put_record( $record, $out );
1988 sub script_analyze_tags
1990 # Martin A. Hansen, August 2008.
1992 # Analyze sequence tags in stream.
1994 my ( $in, # handle to in stream
1995 $out, # handle to out stream
2000 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2002 while ( $record = get_record( $in ) )
2004 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2006 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2010 $len_hash{ length( $record->{ "SEQ" } ) }++;
2011 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2014 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2016 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2020 $len_hash{ $record->{ "BED_LEN" } }++;
2021 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2026 foreach $key ( sort { $a <=> $b } keys %len_hash )
2028 $tag_record->{ "TAG_LEN" } = $key;
2029 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2030 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2032 put_record( $tag_record, $out );
2037 sub script_complexity_seq
2039 # Martin A. Hansen, May 2008.
2041 # Generates an index calculated as the most common di-residue over
2042 # the sequence length for all sequences in stream.
2044 my ( $in, # handle to in stream
2045 $out, # handle to out stream
2050 my ( $record, $index );
2052 while ( $record = get_record( $in ) )
2054 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2056 put_record( $record, $out );
2061 sub script_oligo_freq
2063 # Martin A. Hansen, August 2007.
2065 # Determine the length of sequences in stream.
2067 my ( $in, # handle to in stream
2068 $out, # handle to out stream
2069 $options, # options hash
2074 my ( $record, %oligos, @freq_table );
2076 $options->{ "word_size" } ||= 7;
2078 while ( $record = get_record( $in ) )
2080 if ( $record->{ "SEQ" } )
2082 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2084 if ( not $options->{ "all" } )
2086 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2088 map { put_record( $_, $out ) } @freq_table;
2094 put_record( $record, $out );
2097 if ( $options->{ "all" } )
2099 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2101 map { put_record( $_, $out ) } @freq_table;
2106 sub script_create_weight_matrix
2108 # Martin A. Hansen, August 2007.
2110 # Creates a weight matrix from an alignmnet.
2112 my ( $in, # handle to in stream
2113 $out, # handle to out stream
2114 $options, # options hash
2119 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2123 while ( $record = get_record( $in ) )
2125 if ( $record->{ "SEQ" } )
2127 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2129 $res = substr $record->{ "SEQ" }, $i, 1;
2131 $freq_hash{ $i }{ $res }++;
2132 $res_hash{ $res } = 1;
2139 put_record( $record, $out );
2143 foreach $res ( sort keys %res_hash )
2147 $record->{ "V0" } = $res;
2149 for ( $i = 0; $i < keys %freq_hash; $i++ )
2151 $freq = $freq_hash{ $i }{ $res } || 0;
2153 if ( $options->{ "percent" } ) {
2154 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2157 $record->{ "V" . ( $i + 1 ) } = $freq;
2160 put_record( $record, $out );
2165 sub script_calc_bit_scores
2167 # Martin A. Hansen, March 2007.
2169 # Calculates the bit scores for each position from an alignmnet in the stream.
2171 my ( $in, # handle to in stream
2172 $out, # handle to out stream
2177 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2181 while ( $record = get_record( $in ) )
2183 if ( $record->{ "SEQ" } )
2185 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2187 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2189 $res = substr $record->{ "SEQ" }, $i, 1;
2191 next if $res =~ /-|_|~|\./;
2193 $freq_hash{ $i }{ $res }++;
2200 put_record( $record, $out );
2206 if ( $type eq "protein" ) {
2212 for ( $i = 0; $i < keys %freq_hash; $i++ )
2214 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2216 $bit_diff = $bit_max - $bit_height;
2218 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2221 put_record( $record, $out );
2225 sub script_reverse_seq
2227 # Martin A. Hansen, August 2007.
2229 # Reverse sequence in record.
2231 my ( $in, # handle to in stream
2232 $out, # handle to out stream
2239 while ( $record = get_record( $in ) )
2241 if ( $record->{ "SEQ" } ) {
2242 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2245 put_record( $record, $out );
2250 sub script_complement_seq
2252 # Martin A. Hansen, August 2007.
2254 # Complement sequence in record.
2256 my ( $in, # handle to in stream
2257 $out, # handle to out stream
2262 my ( $record, $type );
2264 while ( $record = get_record( $in ) )
2266 if ( $record->{ "SEQ" } )
2269 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2272 if ( $type eq "rna" ) {
2273 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2274 } elsif ( $type eq "dna" ) {
2275 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2279 put_record( $record, $out );
2284 sub script_remove_indels
2286 # Martin A. Hansen, August 2007.
2288 # Remove indels from sequences in stream.
2290 my ( $in, # handle to in stream
2291 $out, # handle to out stream
2298 while ( $record = get_record( $in ) )
2300 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2302 put_record( $record, $out );
2307 sub script_transliterate_seq
2309 # Martin A. Hansen, August 2007.
2311 # Transliterate chars from sequence in record.
2313 my ( $in, # handle to in stream
2314 $out, # handle to out stream
2315 $options, # options hash
2320 my ( $record, $search, $replace, $delete );
2322 $search = $options->{ "search" } || "";
2323 $replace = $options->{ "replace" } || "";
2324 $delete = $options->{ "delete" } || "";
2326 while ( $record = get_record( $in ) )
2328 if ( $record->{ "SEQ" } )
2330 if ( $search and $replace ) {
2331 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2332 } elsif ( $delete ) {
2333 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2337 put_record( $record, $out );
2342 sub script_transliterate_vals
2344 # Martin A. Hansen, April 2008.
2346 # Transliterate chars from values in record.
2348 my ( $in, # handle to in stream
2349 $out, # handle to out stream
2350 $options, # options hash
2355 my ( $record, $search, $replace, $delete, $key );
2357 $search = $options->{ "search" } || "";
2358 $replace = $options->{ "replace" } || "";
2359 $delete = $options->{ "delete" } || "";
2361 while ( $record = get_record( $in ) )
2363 foreach $key ( @{ $options->{ "keys" } } )
2365 if ( exists $record->{ $key } )
2367 if ( $search and $replace ) {
2368 eval "\$record->{ $key } =~ tr/$search/$replace/";
2369 } elsif ( $delete ) {
2370 eval "\$record->{ $key } =~ tr/$delete//d";
2375 put_record( $record, $out );
2380 sub script_translate_seq
2382 # Martin A. Hansen, February 2008.
2384 # Translate DNA sequence into protein sequence.
2386 my ( $in, # handle to in stream
2387 $out, # handle to out stream
2388 $options, # options hash
2393 my ( $record, $frame, %new_record );
2395 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2397 while ( $record = get_record( $in ) )
2399 if ( $record->{ "SEQ" } )
2401 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2403 foreach $frame ( @{ $options->{ "frames" } } )
2405 %new_record = %{ $record };
2407 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2408 $new_record{ "SEQ_LEN" } = length $record->{ "SEQ" };
2409 $new_record{ "FRAME" } = $frame;
2411 put_record( \%new_record, $out );
2417 put_record( $record, $out );
2423 sub script_extract_seq
2425 # Martin A. Hansen, August 2007.
2427 # Extract subsequences from sequences in record.
2429 my ( $in, # handle to in stream
2430 $out, # handle to out stream
2431 $options, # options hash
2436 my ( $beg, $end, $len, $record );
2438 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2441 $beg = $options->{ "beg" } - 1; # correcting for start offset
2444 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2446 } elsif ( defined $options->{ "end" } ) {
2447 $end = $options->{ "end" } - 1; # correcting for start offset
2450 $len = $options->{ "len" };
2452 # print "beg->$beg, end->$end, len->$len\n";
2454 while ( $record = get_record( $in ) )
2456 if ( $record->{ "SEQ" } )
2458 if ( defined $beg and defined $end )
2460 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2461 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2463 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2466 elsif ( defined $beg and defined $len )
2468 if ( $len > length $record->{ "SEQ" } ) {
2469 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2471 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2474 elsif ( defined $beg )
2476 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2480 put_record( $record, $out );
2485 sub script_get_genome_seq
2487 # Martin A. Hansen, December 2007.
2489 # Gets a subsequence from a genome.
2491 my ( $in, # handle to in stream
2492 $out, # handle to out stream
2493 $options, # options hash
2498 my ( $record, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2500 $options->{ "flank" } ||= 0;
2502 if ( $options->{ "genome" } )
2504 $genome_file = Maasha::Config::genome_fasta( $options->{ 'genome' } );
2505 $index_file = Maasha::Config::genome_fasta_index( $options->{ 'genome' } );
2507 $fh = Maasha::Common::read_open( $genome_file );
2508 $index = Maasha::Fasta::index_retrieve( $index_file );
2510 shift @{ $index }; # Get rid of the file size info
2512 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2514 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2516 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2518 $beg = $index_beg + $options->{ "beg" } - 1;
2520 if ( $options->{ "len" } ) {
2521 $len = $options->{ "len" };
2522 } elsif ( $options->{ "end" } ) {
2523 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2526 $beg -= $options->{ "flank" };
2527 $len += 2 * $options->{ "flank" };
2529 if ( $beg <= $index_beg )
2531 $len -= $index_beg - $beg;
2535 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2537 next if $beg > $index_beg + $index_len;
2539 $record->{ "CHR" } = $options->{ "chr" };
2540 $record->{ "CHR_BEG" } = $beg - $index_beg;
2541 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2543 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2544 $record->{ "SEQ_LEN" } = $len;
2546 put_record( $record, $out );
2550 while ( $record = get_record( $in ) )
2552 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2554 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2556 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2558 $beg = $record->{ "CHR_BEG" } + $index_beg;
2559 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2561 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2563 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2565 $beg = $record->{ "S_BEG" } + $index_beg;
2566 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2568 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2570 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2572 $beg = $record->{ "S_BEG" } + $index_beg;
2573 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2576 $beg -= $options->{ "flank" };
2577 $len += 2 * $options->{ "flank" };
2579 if ( $beg <= $index_beg )
2581 $len -= $index_beg - $beg;
2585 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2587 next if $beg > $index_beg + $index_len;
2589 $record->{ "CHR_BEG" } = $beg - $index_beg;
2590 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2592 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2594 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2596 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2597 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2600 if ( $options->{ "mask" } )
2602 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2604 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2606 @begs = split ",", $record->{ "Q_BEGS" };
2607 @lens = split ",", $record->{ "BLOCKSIZES" };
2609 for ( $i = 0; $i < @begs; $i++ ) {
2610 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2616 put_record( $record, $out );
2623 sub script_get_genome_align
2625 # Martin A. Hansen, April 2008.
2627 # Gets a subalignment from a multiple genome alignment.
2629 my ( $in, # handle to in stream
2630 $out, # handle to out stream
2631 $options, # options hash
2636 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2638 $options->{ "strand" } ||= "+";
2642 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2644 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2646 $beg = $options->{ "beg" } - 1;
2648 if ( $options->{ "end" } ) {
2649 $end = $options->{ "end" };
2650 } elsif ( $options->{ "len" } ) {
2651 $end = $beg + $options->{ "len" };
2654 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2656 foreach $entry ( @{ $align } )
2658 $entry->{ "CHR" } = $record->{ "CHR" };
2659 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2660 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2661 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2662 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2663 $entry->{ "SCORE" } = $record->{ "SCORE" };
2665 put_record( $entry, $out );
2669 while ( $record = get_record( $in ) )
2671 if ( $record->{ "REC_TYPE" } eq "BED" )
2673 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2675 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2677 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2679 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2681 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2684 foreach $entry ( @{ $align } )
2686 $entry->{ "CHR" } = $record->{ "CHR" };
2687 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2688 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2689 $entry->{ "STRAND" } = $record->{ "STRAND" };
2690 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2691 $entry->{ "SCORE" } = $record->{ "SCORE" };
2693 put_record( $entry, $out );
2701 sub script_get_genome_phastcons
2703 # Martin A. Hansen, February 2008.
2705 # Get phastcons scores from genome intervals.
2707 my ( $in, # handle to in stream
2708 $out, # handle to out stream
2709 $options, # options hash
2714 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2716 $options->{ "flank" } ||= 0;
2718 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2719 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2721 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2722 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2724 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2726 $options->{ "beg" } -= 1; # request is 1-based
2727 $options->{ "end" } -= 1; # request is 1-based
2729 if ( $options->{ "len" } ) {
2730 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2733 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2735 $record->{ "CHR" } = $options->{ "chr" };
2736 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2737 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2739 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2740 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2742 put_record( $record, $out );
2745 while ( $record = get_record( $in ) )
2747 if ( $record->{ "REC_TYPE" } eq "BED" )
2749 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2751 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2753 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2755 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2757 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2760 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2761 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2763 put_record( $record, $out );
2766 close $fh_phastcons if $fh_phastcons;
2772 # Martin A. Hansen, December 2007.
2774 # Folds sequences in stream into secondary structures.
2776 my ( $in, # handle to in stream
2777 $out, # handle to out stream
2782 my ( $record, $type, $struct, $index );
2784 while ( $record = get_record( $in ) )
2786 if ( $record->{ "SEQ" } )
2789 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2792 if ( $type ne "protein" )
2794 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2795 $record->{ "SEC_STRUCT" } = $struct;
2796 $record->{ "FREE_ENERGY" } = $index;
2797 $record->{ "SCORE" } = abs int $index;
2798 $record->{ "SIZE" } = length $struct;
2799 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2803 put_record( $record, $out );
2808 sub script_split_seq
2810 # Martin A. Hansen, August 2007.
2812 # Split a sequence in stream into words.
2814 my ( $in, # handle to in stream
2815 $out, # handle to out stream
2816 $options, # options hash
2821 my ( $record, $new_record, $i, $subseq, %lookup );
2823 $options->{ "word_size" } ||= 7;
2825 while ( $record = get_record( $in ) )
2827 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2829 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2831 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2833 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2835 $new_record->{ "REC_TYPE" } = "SPLIT";
2836 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2837 $new_record->{ "SEQ" } = $subseq;
2839 put_record( $new_record, $out );
2841 $lookup{ $subseq } = 1;
2845 $new_record->{ "REC_TYPE" } = "SPLIT";
2846 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2847 $new_record->{ "SEQ" } = $subseq;
2849 put_record( $new_record, $out );
2855 put_record( $record, $out );
2861 sub script_split_bed
2863 # Martin A. Hansen, June 2008.
2865 # Split a BED record into overlapping windows.
2867 my ( $in, # handle to in stream
2868 $out, # handle to out stream
2869 $options, # options hash
2874 my ( $record, $new_record, $i );
2876 $options->{ "window_size" } ||= 20;
2877 $options->{ "step_size" } ||= 1;
2879 while ( $record = get_record( $in ) )
2881 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2883 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2885 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2887 $new_record->{ "REC_TYPE" } = "BED";
2888 $new_record->{ "CHR" } = $record->{ "CHR" };
2889 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
2890 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2891 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
2892 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
2893 $new_record->{ "SCORE" } = $record->{ "SCORE" };
2894 $new_record->{ "STRAND" } = $record->{ "STRAND" };
2896 put_record( $new_record, $out );
2901 put_record( $record, $out );
2907 sub script_align_seq
2909 # Martin A. Hansen, August 2007.
2911 # Align sequences in stream.
2913 my ( $in, # handle to in stream
2914 $out, # handle to out stream
2919 my ( $record, @entries, $entry );
2921 while ( $record = get_record( $in ) )
2923 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2924 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2925 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
2926 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
2928 put_record( $record, $out );
2932 @entries = Maasha::Align::align( \@entries );
2934 foreach $entry ( @entries )
2936 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
2939 SEQ_NAME => $entry->[ SEQ_NAME ],
2940 SEQ => $entry->[ SEQ ],
2943 put_record( $record, $out );
2951 # Martin A. Hansen, February 2008.
2953 # Using the first sequence in stream as reference, tile
2954 # all subsequent sequences based on pairwise alignments.
2956 my ( $in, # handle to in stream
2957 $out, # handle to out stream
2958 $options, # options hash
2963 my ( $record, $first, $ref_entry, @entries );
2967 while ( $record = get_record( $in ) )
2969 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2973 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2979 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2984 put_record( $record, $out );
2988 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2990 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2994 sub script_invert_align
2996 # Martin A. Hansen, February 2008.
2998 # Inverts an alignment showing only non-mathing residues
2999 # using the first sequence as reference.
3001 my ( $in, # handle to in stream
3002 $out, # handle to out stream
3003 $options, # options hash
3008 my ( $record, @entries );
3010 while ( $record = get_record( $in ) )
3012 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3014 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3018 put_record( $record, $out );
3022 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3024 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3028 sub script_patscan_seq
3030 # Martin A. Hansen, August 2007.
3032 # Locates patterns in sequences using scan_for_matches.
3034 my ( $in, # handle to in stream
3035 $out, # handle to out stream
3036 $options, # options hash
3041 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3043 if ( $options->{ "patterns" } ) {
3044 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3045 } elsif ( -f $options->{ "patterns_in" } ) {
3046 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3049 $genome_file = Maasha::Config::genome_fasta( $options->{ 'genome' } ) if $options->{ 'genome' };
3051 push @args, "-c" if $options->{ "comp" };
3052 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3053 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3055 $seq_file = "$BP_TMP/patscan.seq";
3056 $pat_file = "$BP_TMP/patscan.pat";
3057 $out_file = "$BP_TMP/patscan.out";
3059 $fh_out = Maasha::Common::write_open( $seq_file );
3063 while ( $record = get_record( $in ) )
3065 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3067 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3069 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3071 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3076 # put_record( $record, $out );
3081 $arg = join " ", @args;
3082 $arg .= " -p" if $type eq "protein";
3084 foreach $pattern ( @{ $patterns } )
3086 $fh_out = Maasha::Common::write_open( $pat_file );
3088 print $fh_out "$pattern\n";
3092 if ( $options->{ 'genome' } ) {
3093 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3094 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3096 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3097 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3100 $fh_in = Maasha::Common::read_open( $out_file );
3102 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3104 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3106 if ( $options->{ 'genome' } )
3108 $result->{ "CHR" } = $result->{ "S_ID" };
3109 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3110 $result->{ "CHR_END" } = $result->{ "S_END" };
3112 delete $result->{ "S_ID" };
3113 delete $result->{ "S_BEG" };
3114 delete $result->{ "S_END" };
3118 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3121 put_record( $result, $out );
3133 sub script_create_blast_db
3135 # Martin A. Hansen, September 2007.
3137 # Creates a NCBI BLAST database with formatdb
3139 my ( $in, # handle to in stream
3140 $out, # handle to out stream
3141 $options, # options hash
3146 my ( $fh, $seq_type, $path, $record );
3148 $path = $options->{ "database" };
3150 $fh = Maasha::Common::write_open( $path );
3152 while ( $record = get_record( $in ) )
3154 put_record( $record, $out ) if not $options->{ "no_stream" };
3156 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3158 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3160 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3166 if ( $seq_type eq "protein" ) {
3167 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3169 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3176 sub script_blast_seq
3178 # Martin A. Hansen, September 2007.
3180 # BLASTs sequences in stream against a given database.
3182 my ( $in, # handle to in stream
3183 $out, # handle to out stream
3184 $options, # options hash
3189 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3191 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3192 $options->{ "filter" } = "F";
3193 $options->{ "filter" } = "T" if $options->{ "filter" };
3194 $options->{ "cpus" } ||= 1;
3196 $options->{ "database" } = Maasha::Config::genome_blast( $options->{ 'genome' } ) if $options->{ 'genome' };
3198 $tmp_in = "$BP_TMP/blast_query.seq";
3199 $tmp_out = "$BP_TMP/blast.result";
3201 $fh_out = Maasha::Common::write_open( $tmp_in );
3203 while ( $record = get_record( $in ) )
3205 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3207 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3209 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3212 put_record( $record, $out );
3217 if ( -f $options->{ 'database' } . ".phr" ) {
3218 $s_type = "protein";
3220 $s_type = "nucleotide";
3223 if ( not $options->{ 'program' } )
3225 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3226 $options->{ 'program' } = "blastn";
3227 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3228 $options->{ 'program' } = "blastp";
3229 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3230 $options->{ 'program' } = "blastx";
3231 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3232 $options->{ 'program' } = "tblastn";
3236 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 );
3240 $fh_out = Maasha::Common::read_open( $tmp_out );
3244 while ( $line = <$fh_out> )
3248 next if $line =~ /^#/;
3250 @fields = split /\s+/, $line;
3252 $record->{ "REC_TYPE" } = "BLAST";
3253 $record->{ "Q_ID" } = $fields[ 0 ];
3254 $record->{ "S_ID" } = $fields[ 1 ];
3255 $record->{ "IDENT" } = $fields[ 2 ];
3256 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3257 $record->{ "MISMATCHES" } = $fields[ 4 ];
3258 $record->{ "GAPS" } = $fields[ 5 ];
3259 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3260 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3261 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3262 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3263 $record->{ "E_VAL" } = $fields[ 10 ];
3264 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3266 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3268 $record->{ "STRAND" } = '-';
3270 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3274 $record->{ "STRAND" } = '+';
3277 put_record( $record, $out );
3288 # Martin A. Hansen, August 2007.
3290 # BLATs sequences in stream against a given genome.
3292 my ( $in, # handle to in stream
3293 $out, # handle to out stream
3294 $options, # options hash
3299 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3301 $genome_file = Maasha::Config::genome_fasta( $options->{ "genome" } );
3303 $options->{ 'tile_size' } ||= 11;
3304 $options->{ 'one_off' } ||= 0;
3305 $options->{ 'min_identity' } ||= 90;
3306 $options->{ 'min_score' } ||= 0;
3307 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3309 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3310 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3311 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3312 $blat_args .= " -minScore=$options->{ 'min_score' }";
3313 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3314 $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3316 $query_file = "$BP_TMP/blat.seq";
3318 $fh_out = Maasha::Common::write_open( $query_file );
3320 while ( $record = get_record( $in ) )
3322 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3324 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3325 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3328 put_record( $record, $out );
3333 $blat_args .= " -t=dnax" if $type eq "protein";
3334 $blat_args .= " -q=$type";
3336 $result_file = "$BP_TMP/blat.psl";
3338 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3342 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3344 map { put_record( $_, $out ) } @{ $entries };
3346 unlink $result_file;
3350 sub script_match_seq
3352 # Martin A. Hansen, August 2007.
3354 # BLATs sequences in stream against a given genome.
3356 my ( $in, # handle to in stream
3357 $out, # handle to out stream
3358 $options, # options hash
3363 my ( $record, @entries, $results );
3365 $options->{ "word_size" } ||= 20;
3366 $options->{ "direction" } ||= "both";
3368 while ( $record = get_record( $in ) )
3370 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3371 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3374 put_record( $record, $out );
3377 if ( @entries == 1 )
3379 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3381 map { put_record( $_, $out ) } @{ $results };
3383 elsif ( @entries == 2 )
3385 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3387 map { put_record( $_, $out ) } @{ $results };
3392 sub script_create_vmatch_index
3394 # Martin A. Hansen, January 2008.
3396 # Create a vmatch index from sequences in the stream.
3398 my ( $in, # handle to in stream
3399 $out, # handle to out stream
3400 $options, # options hash
3405 my ( $record, $file_tmp, $fh_tmp, $type );
3407 if ( $options->{ "index_name" } )
3409 $file_tmp = $options->{ 'index_name' };
3410 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3413 while ( $record = get_record( $in ) )
3415 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3417 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3419 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3422 put_record( $record, $out ) if not $options->{ "no_stream" };
3425 if ( $options->{ "index_name" } )
3429 if ( $type eq "protein" ) {
3430 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3432 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3440 sub script_vmatch_seq
3442 # Martin A. Hansen, August 2007.
3444 # Vmatches sequences in stream against a given genome.
3446 my ( $in, # handle to in stream
3447 $out, # handle to out stream
3448 $options, # options hash
3453 my ( @index_files, @records, $result_file, $fh_in, $record );
3455 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3457 if ( $options->{ "index_name" } ) {
3458 @index_files = $options->{ "index_name" };
3460 @index_files = Maasha::Config::genome_vmatch( $options->{ "genome" } );
3463 while ( $record = get_record( $in ) )
3465 push @records, $record;
3467 put_record( $record, $out );
3470 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3474 $fh_in = Maasha::Common::read_open( $result_file );
3476 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3477 put_record( $record, $out );
3482 unlink $result_file;
3486 sub script_write_fasta
3488 # Martin A. Hansen, August 2007.
3490 # Write FASTA entries from sequences in stream.
3492 my ( $in, # handle to in stream
3493 $out, # handle to out stream
3494 $options, # options hash
3499 my ( $record, $fh );
3501 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3503 while ( $record = get_record( $in ) )
3505 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3506 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3509 put_record( $record, $out ) if not $options->{ "no_stream" };
3516 sub script_write_align
3518 # Martin A. Hansen, August 2007.
3520 # Write pretty alignments aligned sequences in stream.
3522 my ( $in, # handle to in stream
3523 $out, # handle to out stream
3524 $options, # options hash
3529 my ( $fh, $record, @entries );
3531 $fh = write_stream( $options->{ "data_out" } ) ;
3533 while ( $record = get_record( $in ) )
3535 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3536 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3539 put_record( $record, $out ) if not $options->{ "no_stream" };
3542 if ( scalar( @entries ) == 2 ) {
3543 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3544 } elsif ( scalar ( @entries ) > 2 ) {
3545 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3552 sub script_write_blast
3554 # Martin A. Hansen, November 2007.
3556 # Write data in blast table format (-m8 and 9).
3558 my ( $in, # handle to in stream
3559 $out, # handle to out stream
3560 $options, # options hash
3565 my ( $fh, $record, $first );
3567 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3571 while ( $record = get_record( $in ) )
3573 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3575 if ( $options->{ "comment" } and $first )
3577 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";
3582 if ( $record->{ "STRAND" } eq "-" ) {
3583 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3586 print $fh join( "\t",
3587 $record->{ "Q_ID" },
3588 $record->{ "S_ID" },
3589 $record->{ "IDENT" },
3590 $record->{ "ALIGN_LEN" },
3591 $record->{ "MISMATCHES" },
3592 $record->{ "GAPS" },
3593 $record->{ "Q_BEG" } + 1,
3594 $record->{ "Q_END" } + 1,
3595 $record->{ "S_BEG" } + 1,
3596 $record->{ "S_END" } + 1,
3597 $record->{ "E_VAL" },
3598 $record->{ "BIT_SCORE" }
3602 put_record( $record, $out ) if not $options->{ "no_stream" };
3609 sub script_write_tab
3611 # Martin A. Hansen, August 2007.
3613 # Write data as table.
3615 my ( $in, # handle to in stream
3616 $out, # handle to out stream
3617 $options, # options hash
3622 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3624 $options->{ "delimit" } ||= "\t";
3626 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3628 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3630 while ( $record = get_record( $in ) )
3635 if ( $options->{ "keys" } )
3637 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3641 foreach $key ( @{ $options->{ "keys" } } )
3643 if ( exists $record->{ $key } )
3645 push @keys, $key if $options->{ "comment" };
3646 push @vals, $record->{ $key };
3653 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3655 next if exists $no_keys{ $key };
3657 push @keys, $key if $options->{ "comment" };
3658 push @vals, $record->{ $key };
3662 if ( @keys and $options->{ "comment" } )
3664 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3666 delete $options->{ "comment" };
3669 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3671 put_record( $record, $out ) if not $options->{ "no_stream" };
3678 sub script_write_bed
3680 # Martin A. Hansen, August 2007.
3682 # Write BED format for the UCSC genome browser using records in stream.
3684 # Crude - needs lots of work!
3686 my ( $in, # handle to in stream
3687 $out, # handle to out stream
3688 $options, # options hash
3693 my ( $fh, $record, $new_record );
3695 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3697 while ( $record = get_record( $in ) )
3699 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3701 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3703 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3705 $new_record->{ "CHR" } = $record->{ "S_ID" };
3706 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3707 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3708 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3709 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
3710 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3712 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3714 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
3716 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
3718 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
3720 $new_record->{ "CHR" } = $record->{ "S_ID" };
3721 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3722 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3723 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3724 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3725 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3727 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3729 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
3731 $new_record->{ "CHR" } = $record->{ "S_ID" };
3732 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3733 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3734 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3735 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3736 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3738 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3740 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
3742 Maasha::UCSC::bed_put_entry( $record, $fh );
3745 put_record( $record, $out ) if not $options->{ "no_stream" };
3752 sub script_write_psl
3754 # Martin A. Hansen, August 2007.
3756 # Write PSL output from stream.
3758 my ( $in, # handle to in stream
3759 $out, # handle to out stream
3760 $options, # options hash
3765 my ( $fh, $record, @output, $first );
3769 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3771 while ( $record = get_record( $in ) )
3773 put_record( $record, $out ) if not $options->{ "no_stream" };
3775 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3777 Maasha::UCSC::psl_put_header( $fh ) if $first;
3778 Maasha::UCSC::psl_put_entry( $record, $fh );
3787 sub script_write_2bit
3789 # Martin A. Hansen, March 2008.
3791 # Write sequence entries from stream in 2bit format.
3793 my ( $in, # handle to in stream
3794 $out, # handle to out stream
3795 $options, # options hash
3800 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
3802 $mask = 1 if not $options->{ "no_mask" };
3804 $tmp_file = "$BP_TMP/write_2bit.fna";
3805 $fh_tmp = Maasha::Common::write_open( $tmp_file );
3807 $fh_out = write_stream( $options->{ "data_out" } );
3809 while ( $record = get_record( $in ) )
3811 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3812 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3815 put_record( $record, $out ) if not $options->{ "no_stream" };
3820 $fh_in = Maasha::Common::read_open( $tmp_file );
3822 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3831 sub script_write_solid
3833 # Martin A. Hansen, April 2008.
3835 # Write di-base encoded Solid sequence from entries in stream.
3837 my ( $in, # handle to in stream
3838 $out, # handle to out stream
3839 $options, # options hash
3844 my ( $record, $fh, $seq_cs );
3846 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3848 while ( $record = get_record( $in ) )
3850 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3852 $seq_cs = Maasha::Solid::seq2color_space( $record->{ "SEQ" } );
3854 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
3857 put_record( $record, $out ) if not $options->{ "no_stream" };
3864 sub script_plot_seqlogo
3866 # Martin A. Hansen, August 2007.
3868 # Calculates and writes a sequence logo for alignments.
3870 my ( $in, # handle to in stream
3871 $out, # handle to out stream
3872 $options, # options hash
3877 my ( $record, @entries, $logo, $fh );
3879 while ( $record = get_record( $in ) )
3881 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3882 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3885 put_record( $record, $out ) if not $options->{ "no_stream" };
3888 $logo = Maasha::Plot::seq_logo( \@entries );
3890 $fh = write_stream( $options->{ "data_out" } );
3898 sub script_plot_phastcons_profiles
3900 # Martin A. Hansen, January 2008.
3902 # Plots PhastCons profiles.
3904 my ( $in, # handle to in stream
3905 $out, # handle to out stream
3906 $options, # options hash
3911 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
3913 $options->{ "title" } ||= "PhastCons Profiles";
3915 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3916 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3918 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
3919 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3921 while ( $record = get_record( $in ) )
3923 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3925 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3927 push @{ $AoA }, [ @{ $scores } ];
3930 put_record( $record, $out ) if not $options->{ "no_stream" };
3933 Maasha::UCSC::phastcons_normalize( $AoA );
3935 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
3936 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
3938 $AoA = Maasha::Matrix::matrix_flip( $AoA );
3940 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
3942 $fh = write_stream( $options->{ "data_out" } );
3944 print $fh "$_\n" foreach @{ $plot };
3950 sub script_analyze_bed
3952 # Martin A. Hansen, March 2008.
3954 # Analyze BED entries in stream.
3956 my ( $in, # handle to in stream
3957 $out, # handle to out stream
3958 $options, # options hash
3965 while ( $record = get_record( $in ) )
3967 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
3969 put_record( $record, $out );
3974 sub script_analyze_vals
3976 # Martin A. Hansen, August 2007.
3978 # Analyze values for given keys in stream.
3980 my ( $in, # handle to in stream
3981 $out, # handle to out stream
3982 $options, # options hash
3987 my ( $record, $key, @keys, %key_hash, $analysis, $len );
3989 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
3991 while ( $record = get_record( $in ) )
3993 foreach $key ( keys %{ $record } )
3995 next if $options->{ "keys" } and not exists $key_hash{ $key };
3997 $analysis->{ $key }->{ "COUNT" }++;
3999 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4001 $analysis->{ $key }->{ "TYPE" } = "num";
4002 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4003 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4004 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4008 $len = length $record->{ $key };
4010 $analysis->{ $key }->{ "TYPE" } = "alph";
4011 $analysis->{ $key }->{ "SUM" } += $len;
4012 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4013 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4017 put_record( $record, $out ) if not $options->{ "no_stream" };
4020 foreach $key ( keys %{ $analysis } )
4022 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4023 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUN" };
4026 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4036 if ( $options->{ "keys" } ) {
4037 @keys = @{ $options->{ "keys" } };
4039 @keys = keys %{ $analysis };
4042 foreach $key ( @keys )
4044 $keys .= sprintf "% 15s", $key;
4045 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4046 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4047 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4048 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4049 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4050 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4053 print $out "$keys\n";
4054 print $out "$types\n";
4055 print $out "$counts\n";
4056 print $out "$mins\n";
4057 print $out "$maxs\n";
4058 print $out "$sums\n";
4059 print $out "$means\n";
4063 sub script_head_records
4065 # Martin A. Hansen, August 2007.
4067 # Display the first sequences in stream.
4069 my ( $in, # handle to in stream
4070 $out, # handle to out stream
4071 $options, # options hash
4076 my ( $record, $count );
4078 $options->{ "num" } ||= 10;
4082 while ( $record = get_record( $in ) )
4086 put_record( $record, $out );
4088 last if $count == $options->{ "num" };
4093 sub script_remove_keys
4095 # Martin A. Hansen, August 2007.
4097 # Remove keys from stream.
4099 my ( $in, # handle to in stream
4100 $out, # handle to out stream
4101 $options, # options hash
4106 my ( $record, $new_record );
4108 while ( $record = get_record( $in ) )
4110 if ( $options->{ "keys" } )
4112 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4114 elsif ( $options->{ "save_keys" } )
4116 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4118 $record = $new_record;
4121 put_record( $record, $out ) if keys %{ $record };
4126 sub script_rename_keys
4128 # Martin A. Hansen, August 2007.
4130 # Rename keys in stream.
4132 my ( $in, # handle to in stream
4133 $out, # handle to out stream
4134 $options, # options hash
4141 while ( $record = get_record( $in ) )
4143 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4145 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4147 delete $record->{ $options->{ "keys" }->[ 0 ] };
4150 put_record( $record, $out );
4155 sub script_uniq_vals
4157 # Martin A. Hansen, August 2007.
4159 # Find unique values in stream.
4161 my ( $in, # handle to in stream
4162 $out, # handle to out stream
4163 $options, # options hash
4168 my ( %hash, $record );
4170 while ( $record = get_record( $in ) )
4172 if ( $record->{ $options->{ "key" } } )
4174 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4176 put_record( $record, $out );
4178 $hash{ $record->{ $options->{ "key" } } } = 1;
4180 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4182 put_record( $record, $out );
4186 $hash{ $record->{ $options->{ "key" } } } = 1;
4191 put_record( $record, $out );
4197 sub script_merge_vals
4199 # Martin A. Hansen, August 2007.
4201 # Rename keys in stream.
4203 my ( $in, # handle to in stream
4204 $out, # handle to out stream
4205 $options, # options hash
4210 my ( $record, @join, $i );
4212 $options->{ "delimit" } ||= '_';
4214 while ( $record = get_record( $in ) )
4216 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4218 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4220 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4221 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4224 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4227 put_record( $record, $out );
4234 # Martin A. Hansen, August 2007.
4236 # Grab for records in stream.
4238 my ( $in, # handle to in stream
4239 $out, # handle to out stream
4240 $options, # options hash
4245 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4247 if ( $options->{ "patterns" } )
4249 $patterns = [ split ",", $options->{ "patterns" } ];
4251 elsif ( -f $options->{ "patterns_in" } )
4253 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4255 elsif ( -f $options->{ "exact_in" } )
4257 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4259 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4264 if ( $options->{ "eval" } )
4266 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4274 while ( $record = get_record( $in ) )
4280 if ( $options->{ "keys" } )
4282 foreach $key ( @{ $options->{ "keys" } } )
4284 if ( exists $lookup_hash{ $record->{ $key } } )
4293 foreach $key ( keys %{ $record } )
4295 if ( not $options->{ "vals_only" } )
4297 if ( exists $lookup_hash{ $key } )
4304 if ( not $options->{ "keys_only" } )
4306 if ( exists $lookup_hash{ $record->{ $key } } )
4317 foreach $pattern ( @{ $patterns } )
4319 if ( $options->{ "keys" } )
4321 foreach $key ( @{ $options->{ "keys" } } )
4323 $pos = index $record->{ $key }, $pattern;
4325 goto FOUND if $pos >= 0;
4330 foreach $key ( keys %{ $record } )
4332 if ( not $options->{ "vals_only" } )
4334 $pos = index $key, $pattern;
4336 goto FOUND if $pos >= 0;
4339 if ( not $options->{ "keys_only" } )
4341 $pos = index $record->{ $key }, $pattern;
4343 goto FOUND if $pos >= 0;
4349 elsif ( $options->{ "regex" } )
4351 if ( $options->{ "keys" } )
4353 foreach $key ( @{ $options->{ "keys" } } )
4355 if ( $options->{ "case_insensitive" } ) {
4356 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4358 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4361 goto FOUND if $pos >= 0;
4366 foreach $key ( keys %{ $record } )
4368 if ( not $options->{ "vals_only" } )
4370 if ( $options->{ "case_insensitive" } ) {
4371 $pos = 1 if $key =~ /$options->{'regex'}/i;
4373 $pos = 1 if $key =~ /$options->{'regex'}/;
4376 goto FOUND if $pos >= 0;
4379 if ( not $options->{ "keys_only" } )
4381 if ( $options->{ "case_insensitive" } ) {
4382 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4384 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4387 goto FOUND if $pos >= 0;
4392 elsif ( $options->{ "eval" } )
4394 if ( defined $record->{ $key } )
4396 if ( $op eq "<" and $record->{ $key } < $val ) {
4397 $pos = 1 and goto FOUND;
4398 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4399 $pos = 1 and goto FOUND;
4400 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4401 $pos = 1 and goto FOUND;
4402 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4403 $pos = 1 and goto FOUND;
4404 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4405 $pos = 1 and goto FOUND;
4406 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4407 $pos = 1 and goto FOUND;
4408 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4409 $pos = 1 and goto FOUND;
4410 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4411 $pos = 1 and goto FOUND;
4418 if ( $pos >= 0 and not $options->{ "invert" } ) {
4419 put_record( $record, $out );
4420 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4421 put_record( $record, $out );
4429 # Martin A. Hansen, August 2007.
4431 # Evaluate extression for records in stream.
4433 my ( $in, # handle to in stream
4434 $out, # handle to out stream
4435 $options, # options hash
4440 my ( $record, $eval_key, $eval_val, $check, @keys );
4442 while ( $record = get_record( $in ) )
4444 if ( $options->{ "eval" } )
4446 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4454 @keys = split /\W+/, $eval_val;
4455 @keys = grep { ! /^\d+$/ } @keys;
4460 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4462 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4465 put_record( $record, $out );
4472 # Martin A. Hansen, June 2008.
4476 my ( $in, # handle to in stream
4477 $out, # handle to out stream
4478 $options, # options hash
4483 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4485 while ( $record = get_record( $in ) )
4489 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4491 push @rows, $record->{ $key };
4495 push @matrix, [ @rows ];
4500 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4502 foreach $row ( @matrix )
4504 for ( $i = 0; $i < @{ $row }; $i++ ) {
4505 $record->{ "V$i" } = $row->[ $i ];
4508 put_record( $record, $out );
4513 sub script_add_ident
4515 # Martin A. Hansen, May 2008.
4517 # Add a unique identifier to each record in stream.
4519 my ( $in, # handle to in stream
4520 $out, # handle to out stream
4521 $options, # options hash
4526 my ( $record, $key, $prefix, $i );
4528 $key = $options->{ "key" } || "ID";
4529 $prefix = $options->{ "prefix" } || "ID";
4533 while ( $record = get_record( $in ) )
4535 $record->{ $key } = sprintf( "$prefix%08d", $i );
4537 put_record( $record, $out );
4544 sub script_count_records
4546 # Martin A. Hansen, August 2007.
4548 # Count records in stream.
4550 my ( $in, # handle to in stream
4551 $out, # handle to out stream
4552 $options, # options hash
4557 my ( $record, $count, $result, $fh, $line );
4561 if ( $options->{ "no_stream" } )
4563 while ( $line = <$in> )
4567 $count++ if $line eq "---";
4572 while ( $record = get_record( $in ) )
4574 put_record( $record, $out );
4580 $result = { "count_records" => $count };
4582 $fh = write_stream( $options->{ "data_out" } );
4584 put_record( $result, $fh );
4590 sub script_random_records
4592 # Martin A. Hansen, August 2007.
4594 # Pick a number or random records from stream.
4596 my ( $in, # handle to in stream
4597 $out, # handle to out stream
4598 $options, # options hash
4603 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4605 $options->{ "num" } ||= 10;
4607 $tmp_file = "$BP_TMP/random_records.tmp";
4609 $fh_out = Maasha::Common::write_open( $tmp_file );
4613 while ( $record = get_record( $in ) )
4615 put_record( $record, $fh_out );
4625 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4627 while ( $i < $options->{ "num" } )
4629 $rand = int( rand( $count ) );
4631 if ( not exists $rand_hash{ $rand } )
4633 $rand_hash{ $rand } = 1;
4635 $max = $rand if $rand > $max;
4641 $fh_in = Maasha::Common::read_open( $tmp_file );
4645 while ( $record = get_record( $fh_in ) )
4647 put_record( $record, $out ) if exists $rand_hash{ $count };
4649 last if $count == $max;
4660 sub script_sort_records
4662 # Martin A. Hansen, August 2007.
4664 # Sort to sort records according to keys.
4666 my ( $in, # handle to in stream
4667 $out, # handle to out stream
4668 $options, # options hash
4673 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4675 foreach $key ( @{ $options->{ "keys" } } )
4677 if ( $key =~ s/n$// ) {
4678 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4680 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4684 $sort_str = join " or ", @sort_cmd;
4685 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4687 while ( $record = get_record( $in ) ) {
4688 push @records, $record;
4691 @records = sort $sort_sub @records;
4693 if ( $options->{ "reverse" } )
4695 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4696 put_record( $records[ $i ], $out );
4701 for ( $i = 0; $i < scalar @records; $i++ ) {
4702 put_record( $records[ $i ], $out );
4708 sub script_count_vals
4710 # Martin A. Hansen, August 2007.
4712 # Count records in stream.
4714 my ( $in, # handle to in stream
4715 $out, # handle to out stream
4716 $options, # options hash
4721 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4723 $tmp_file = "$BP_TMP/count_cache.tmp";
4725 $fh_out = Maasha::Common::write_open( $tmp_file );
4729 while ( $record = get_record( $in ) )
4731 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4733 push @records, $record;
4735 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4737 map { put_record( $_, $fh_out ) } @records;
4744 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4755 $fh_in = Maasha::Common::read_open( $tmp_file );
4757 while ( $record = get_record( $fh_in ) )
4759 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4761 put_record( $record, $out );
4763 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4771 foreach $record ( @records )
4773 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4775 put_record( $record, $out );
4782 sub script_plot_histogram
4784 # Martin A. Hansen, September 2007.
4786 # Plot a simple histogram for a given key using GNU plot.
4788 my ( $in, # handle to in stream
4789 $out, # handle to out stream
4790 $options, # options hash
4795 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4797 $options->{ "title" } ||= "Histogram";
4798 $options->{ "sort" } ||= "num";
4800 while ( $record = get_record( $in ) )
4802 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
4804 put_record( $record, $out ) if not $options->{ "no_stream" };
4807 if ( $options->{ "sort" } eq "num" ) {
4808 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4810 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4813 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4815 $fh = write_stream( $options->{ "data_out" } );
4817 print $fh "$_\n" foreach @{ $result };
4823 sub script_plot_lendist
4825 # Martin A. Hansen, August 2007.
4827 # Plot length distribution using GNU plot.
4829 my ( $in, # handle to in stream
4830 $out, # handle to out stream
4831 $options, # options hash
4836 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4838 $options->{ "title" } ||= "Length Distribution";
4840 while ( $record = get_record( $in ) )
4842 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
4844 put_record( $record, $out ) if not $options->{ "no_stream" };
4847 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4849 for ( $i = 0; $i < $max; $i++ ) {
4850 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4853 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4855 $fh = write_stream( $options->{ "data_out" } );
4857 print $fh "$_\n" foreach @{ $result };
4863 sub script_plot_chrdist
4865 # Martin A. Hansen, August 2007.
4867 # Plot chromosome distribution using GNU plot.
4869 my ( $in, # handle to in stream
4870 $out, # handle to out stream
4871 $options, # options hash
4876 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
4878 $options->{ "title" } ||= "Chromosome Distribution";
4880 while ( $record = get_record( $in ) )
4882 if ( $record->{ "CHR" } ) { # generic
4883 $data_hash{ $record->{ "CHR" } }++;
4884 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
4885 $data_hash{ $record->{ "S_ID" } }++;
4886 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
4887 $data_hash{ $record->{ "S_ID" } }++;
4888 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
4889 $data_hash{ $record->{ "S_ID" } }++;
4892 put_record( $record, $out ) if not $options->{ "no_stream" };
4895 foreach $elem ( keys %data_hash )
4899 $sort_key =~ s/chr//i;
4901 $sort_key =~ s/^X(.*)/99$1/;
4902 $sort_key =~ s/^Y(.*)/99$1/;
4903 $sort_key =~ s/^Z(.*)/999$1/;
4904 $sort_key =~ s/^M(.*)/9999$1/;
4905 $sort_key =~ s/^U(.*)/99999$1/;
4907 $count = $sort_key =~ tr/_//;
4909 $sort_key =~ s/_.*/"999999" x $count/ex;
4911 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
4914 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
4916 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
4918 $fh = write_stream( $options->{ "data_out" } );
4920 print $fh "$_\n" foreach @{ $result };
4926 sub script_plot_karyogram
4928 # Martin A. Hansen, August 2007.
4930 # Plot hits on karyogram.
4932 my ( $in, # handle to in stream
4933 $out, # handle to out stream
4934 $options, # options hash
4939 my ( %options, $record, @data, $fh, $result, %data_hash );
4941 $options->{ "genome" } ||= "human";
4942 $options->{ "feat_color" } ||= "black";
4944 while ( $record = get_record( $in ) )
4946 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4948 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
4951 put_record( $record, $out ) if not $options->{ "no_stream" };
4954 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
4956 $fh = write_stream( $options->{ "data_out" } );
4964 sub script_plot_matches
4966 # Martin A. Hansen, August 2007.
4968 # Plot matches in 2D generating a dotplot.
4970 my ( $in, # handle to in stream
4971 $out, # handle to out stream
4972 $options, # options hash
4977 my ( $record, @data, $fh, $result, %data_hash );
4979 $options->{ "direction" } ||= "both";
4981 while ( $record = get_record( $in ) )
4983 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
4984 push @data, $record;
4987 put_record( $record, $out ) if not $options->{ "no_stream" };
4990 $options->{ "title" } ||= "plot_matches";
4991 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
4992 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
4994 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
4996 $fh = write_stream( $options->{ "data_out" } );
4998 print $fh "$_\n" foreach @{ $result };
5004 sub script_length_vals
5006 # Martin A. Hansen, August 2007.
5008 # Determine the length of the value for given keys.
5010 my ( $in, # handle to in stream
5011 $out, # handle to out stream
5012 $options, # options hash
5017 my ( $record, $key );
5019 while ( $record = get_record( $in ) )
5021 foreach $key ( @{ $options->{ "keys" } } )
5023 if ( $record->{ $key } ) {
5024 $record->{ $key . "_LEN" } = length $record->{ $key };
5028 put_record( $record, $out );
5035 # Martin A. Hansen, August 2007.
5037 # Calculates the sums for values of given keys.
5039 my ( $in, # handle to in stream
5040 $out, # handle to out stream
5041 $options, # options hash
5046 my ( $record, $key, %sum_hash, $fh );
5048 while ( $record = get_record( $in ) )
5050 foreach $key ( @{ $options->{ "keys" } } )
5052 if ( $record->{ $key } ) {
5053 $sum_hash{ $key } += $record->{ $key };
5057 put_record( $record, $out ) if not $options->{ "no_stream" };
5060 $fh = write_stream( $options->{ "data_out" } );
5062 foreach $key ( @{ $options->{ "keys" } } ) {
5063 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5070 sub script_mean_vals
5072 # Martin A. Hansen, August 2007.
5074 # Calculate the mean of values of given keys.
5076 my ( $in, # handle to in stream
5077 $out, # handle to out stream
5078 $options, # options hash
5083 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5085 while ( $record = get_record( $in ) )
5087 foreach $key ( @{ $options->{ "keys" } } )
5089 if ( $record->{ $key } )
5091 $sum_hash{ $key } += $record->{ $key };
5092 $count_hash{ $key }++;
5096 put_record( $record, $out ) if not $options->{ "no_stream" };
5099 $fh = write_stream( $options->{ "data_out" } );
5101 foreach $key ( @{ $options->{ "keys" } } )
5103 if ( $count_hash{ $key } ) {
5104 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5109 put_record( { $key . "_MEAN" => $mean } , $fh );
5116 sub script_median_vals
5118 # Martin A. Hansen, March 2008.
5120 # Calculate the median values of given keys.
5122 my ( $in, # handle to in stream
5123 $out, # handle to out stream
5124 $options, # options hash
5129 my ( $record, $key, %median_hash, $median, $fh );
5131 while ( $record = get_record( $in ) )
5133 foreach $key ( @{ $options->{ "keys" } } ) {
5134 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5137 put_record( $record, $out ) if not $options->{ "no_stream" };
5140 $fh = write_stream( $options->{ "data_out" } );
5142 foreach $key ( @{ $options->{ "keys" } } )
5144 if ( $median_hash{ $key } ) {
5145 $median = Maasha::Calc::median( $median_hash{ $key } );
5150 put_record( { $key . "_MEDIAN" => $median } , $fh );
5159 # Martin A. Hansen, February 2008.
5161 # Determine the maximum values of given keys.
5163 my ( $in, # handle to in stream
5164 $out, # handle to out stream
5165 $options, # options hash
5170 my ( $record, $key, $fh, %max_hash, $max_record );
5172 while ( $record = get_record( $in ) )
5174 foreach $key ( @{ $options->{ "keys" } } )
5176 if ( $record->{ $key } )
5178 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5182 put_record( $record, $out ) if not $options->{ "no_stream" };
5185 $fh = write_stream( $options->{ "data_out" } );
5187 foreach $key ( @{ $options->{ "keys" } } )
5189 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5192 put_record( $max_record, $fh );
5200 # Martin A. Hansen, February 2008.
5202 # Determine the minimum values of given keys.
5204 my ( $in, # handle to in stream
5205 $out, # handle to out stream
5206 $options, # options hash
5211 my ( $record, $key, $fh, %min_hash, $min_record );
5213 while ( $record = get_record( $in ) )
5215 foreach $key ( @{ $options->{ "keys" } } )
5217 if ( defined $record->{ $key } )
5219 if ( exists $min_hash{ $key } ) {
5220 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5222 $min_hash{ $key } = $record->{ $key };
5227 put_record( $record, $out ) if not $options->{ "no_stream" };
5230 $fh = write_stream( $options->{ "data_out" } );
5232 foreach $key ( @{ $options->{ "keys" } } )
5234 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5237 put_record( $min_record, $fh );
5243 sub script_upload_to_ucsc
5245 # Martin A. Hansen, August 2007.
5247 # Calculate the mean of values of given keys.
5249 my ( $in, # handle to in stream
5250 $out, # handle to out stream
5251 $options, # options hash
5256 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5257 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5259 $options->{ "short_label" } ||= $options->{ 'table' };
5260 $options->{ "long_label" } ||= $options->{ 'table' };
5261 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5262 $options->{ "priority" } ||= 1;
5263 $options->{ "visibility" } ||= "pack";
5264 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5265 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5267 $file = "$BP_TMP/ucsc_upload.tmp";
5275 if ( $options->{ 'wiggle' } )
5277 $options->{ "visibility" } = "full";
5279 while ( $record = get_record( $in ) )
5281 put_record( $record, $out ) if not $options->{ "no_stream" };
5283 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5284 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5285 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5287 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5289 $fh_out = $fh_hash{ $record->{ "CHR" } };
5291 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5294 map { close $_ } keys %fh_hash;
5296 $fh_out = Maasha::Common::write_open( $file );
5298 foreach $chr ( sort keys %fh_hash )
5300 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5302 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5306 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5308 $chr = $entry->{ 'CHR' };
5309 $beg = $entry->{ 'CHR_BEG' };
5310 $end = $entry->{ 'CHR_END' };
5311 $q_id = $entry->{ 'Q_ID' };
5313 if ( $q_id =~ /_(\d+)$/ ) {
5323 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5328 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5329 $block->[ $i ] += $clones;
5332 $max = Maasha::Calc::max( $max, $end );
5341 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5342 $block->[ $i ] += $clones;
5349 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5351 unlink "$BP_TMP/$chr";
5356 $wig_file = "$options->{ 'table' }.wig";
5357 $wib_file = "$options->{ 'table' }.wib";
5359 $wib_dir = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'database' }/wib";
5361 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5363 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5365 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5366 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5376 $fh_out = Maasha::Common::write_open( $file );
5378 while ( $record = get_record( $in ) )
5380 put_record( $record, $out ) if not $options->{ "no_stream" };
5382 if ( $record->{ "REC_TYPE" } eq "PSL" )
5384 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5385 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5389 $format = "PSL" if not $format;
5391 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5393 # chrom chromStart chromEnd name score strand size secStr conf
5395 print $fh_out join ( "\t",
5397 $record->{ "CHR_BEG" },
5398 $record->{ "CHR_END" } + 1,
5399 $record->{ "Q_ID" },
5400 $record->{ "SCORE" },
5401 $record->{ "STRAND" },
5402 $record->{ "SIZE" },
5403 $record->{ "SEC_STRUCT" },
5404 $record->{ "CONF" },
5407 $format = "BED_SS" if not $format;
5409 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5411 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5413 $format = "BED" if not $format;
5414 $columns = $record->{ "BED_COLS" } if not $columns;
5416 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5418 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5420 $format = "BED" if not $format;
5421 $columns = 6 if not $columns;
5423 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5425 $record->{ "CHR" } = $record->{ "S_ID" };
5426 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5427 $record->{ "CHR_END" } = $record->{ "S_END" };
5428 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5430 $format = "BED" if not $format;
5431 $columns = 6 if not $columns;
5433 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5435 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5437 $record->{ "CHR" } = $record->{ "S_ID" };
5438 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5439 $record->{ "CHR_END" } = $record->{ "S_END" };
5440 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5441 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5443 $format = "BED" if not $format;
5444 $columns = 6 if not $columns;
5446 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5449 if ( $i == $options->{ "chunk_size" } )
5453 if ( $format eq "BED" ) {
5454 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5455 } elsif ( $format eq "PSL" ) {
5456 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5465 $fh_out = Maasha::Common::write_open( $file );
5474 if ( exists $options->{ "database" } and $options->{ "table" } )
5476 if ( $format eq "BED" )
5478 $type = "bed $columns";
5480 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5482 elsif ( $format eq "BED_SS" )
5484 $options->{ "sec_struct" } = 1;
5486 $type = "sec_struct";
5488 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5490 elsif ( $format eq "PSL" )
5494 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5496 elsif ( $format eq "WIGGLE" )
5500 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5505 Maasha::UCSC::update_my_tracks( $options, $type );
5510 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5515 # Martin A. Hansen, July 2007.
5517 # Opens a stream to STDIN or a file,
5519 my ( $path, # path - OPTIONAL
5522 # Returns filehandle.
5526 if ( not -t STDIN ) {
5527 $fh = Maasha::Common::read_stdin();
5528 } elsif ( not $path ) {
5529 Maasha::Common::error( qq(no data stream) );
5531 $fh = Maasha::Common::read_open( $path );
5534 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5542 # Martin A. Hansen, August 2007.
5544 # Opens a stream to STDOUT or a file.
5546 my ( $path, # path - OPTIONAL
5547 $gzip, # compress data - OPTIONAL
5550 # Returns filehandle.
5555 $fh = Maasha::Common::write_open( $path, $gzip );
5557 $fh = Maasha::Common::write_stdout();
5566 # Martin A. Hansen, July 2007.
5568 # Reads one record at a time and converts that record
5569 # to a Perl data structure (a hash) which is returned.
5574 # Returns data structure.
5576 my ( $block, @lines, $line, $key, $value, %record );
5578 local $/ = "\n---\n";
5584 return if not defined $block;
5586 @lines = split "\n", $block;
5588 foreach $line ( @lines )
5590 ( $key, $value ) = split ": ", $line;
5592 $record{ $key } = $value;
5595 return wantarray ? %record : \%record;
5601 # Martin A. Hansen, July 2007.
5603 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5605 my ( $data, # data structure
5606 $fh, # file handle - OPTIONAL
5611 if ( scalar keys %{ $data } )
5615 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5620 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5631 # Martin A. Hansen, November 2007.
5633 # Extracts files from an explicit GetOpt::Long argument
5634 # allowing for the use of glob. E.g.
5635 # --data_in=test.fna
5636 # --data_in=test.fna,test2.fna
5638 # --data_in=test.fna,/dir/*.fna
5640 my ( $option, # option from GetOpt::Long
5645 my ( $elem, @files );
5647 foreach $elem ( split ",", $option )
5651 } elsif ( $elem =~ /\*/ ) {
5652 push @files, glob( $elem );
5656 return wantarray ? @files : \@files;
5662 # Martin A. Hansen, April 2008.
5664 # Removes temporary directory and exits gracefully.
5665 # This subroutine is meant to be run always as the last
5666 # thing even if a script is dies or is interrupted
5669 my ( $sig, # signal from the %SIG
5672 # print STDERR "signal->$sig<-\n";
5680 if ( $sig =~ /MAASHA_ERROR/ ) {
5681 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
5682 } elsif ( $sig eq "INT" ) {
5683 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
5684 } elsif ( $sig eq "TERM" ) {
5685 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
5687 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
5690 # This is a really bad solution, potentially, anyone can include this module and set
5691 # the BP_TMP to point at any dir and thus take out the machine !!!
5693 Maasha::Common::dir_remove( $BP_TMP );
5702 # This is a really bad solution, potentially, anyone can include this module and set
5703 # the BP_TMP to point at any dir and thus take out the machine !!!
5705 Maasha::Common::dir_remove( $BP_TMP );
5709 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5716 sub script_read_soft
5718 # Martin A. Hansen, December 2007.
5721 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
5723 my ( $in, # handle to in stream
5724 $out, # handle to out stream
5725 $options, # options hash
5730 my ( $data_in, $file, $num, $records, $record );
5732 while ( $record = get_record( $in ) ) {
5733 put_record( $record, $out );
5738 foreach $file ( @{ $options->{ "files" } } )
5740 $records = Maasha::NCBI::soft_parse( $file );
5742 foreach $record ( @{ $records } )
5744 put_record( $record, $out );
5746 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
5754 close $data_in if $data_in;