-require Exporter;
-
-@ISA = qw( Exporter );
-
-@EXPORT_OK = qw(
- read_stream
- write_stream
- get_record
- put_record
-);
-
-use constant {
- SEQ_NAME => 0,
- SEQ => 1,
-};
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-$SIG{ '__DIE__' } = \&sig_handler;
-$SIG{ 'INT' } = \&sig_handler;
-$SIG{ 'TERM' } = \&sig_handler;
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-my ( $script, $TMP_DIR );
-
-$script = &Maasha::Common::get_scriptname();
-$TMP_DIR = &Maasha::Common::get_tmpdir();
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-my $log_fh = &Maasha::Common::append_open( $ENV{ "LOG_DIR" } . "/biopieces.log" );
-
-$log_fh->autoflush( 1 );
-
-&log( $log_fh, $script, \@ARGV );
-
-close $log_fh;
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-my $t0 = gettimeofday();
-
-&run_script( $script );
-
-my $t1 = gettimeofday();
-
-print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-sub log
-{
- # Martin A. Hansen, January 2008.
-
- # Log messages to logfile.
-
- my ( $fh, # filehandle to logfile
- $script, # script name
- $argv, # reference to @ARGV
- ) = @_;
-
- # Returns nothing.
-
- my ( $time_stamp, $user );
-
- $time_stamp = &Maasha::Common::time_stamp();
-
- $user = $ENV{ "USER" };
-
- $script = "biopieces" if $script eq "-e";
-
- print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
-}
-
-
-sub run_script
-{
- # Martin A. Hansen, August 2007.
-
- # Run a specific script.
-
- my ( $script, # script name
- ) = @_;
-
- # Returns nothing.
-
- my ( $options, $in, $out );
-
- &script_list_biotools( $ENV{ 'INST_DIR'} . "/biotools/usage/" ) if $script eq "list_biotools";
-
- &script_print_usage( $ENV{ 'INST_DIR'} . "/biotools/usage/$script" ) if -t STDIN and not @ARGV;
-
- $options = &get_options( $script );
-
- $in = &read_stream( $options->{ "stream_in" } );
- $out = &write_stream( $options->{ "stream_out" } );
-
- if ( $script eq "read_fasta" ) { &script_read_fasta( $in, $out, $options ) }
- elsif ( $script eq "read_align" ) { &script_read_align( $in, $out, $options ) }
- elsif ( $script eq "read_tab" ) { &script_read_tab( $in, $out, $options ) }
- elsif ( $script eq "read_psl" ) { &script_read_psl( $in, $out, $options ) }
- elsif ( $script eq "read_bed" ) { &script_read_bed( $in, $out, $options ) }
- elsif ( $script eq "read_blast_tab" ) { &script_read_blast_tab( $in, $out, $options ) }
- elsif ( $script eq "read_embl" ) { &script_read_embl( $in, $out, $options ) }
- elsif ( $script eq "read_stockholm" ) { &script_read_stockholm( $in, $out, $options ) }
- elsif ( $script eq "read_phastcons" ) { &script_read_phastcons( $in, $out, $options ) }
- elsif ( $script eq "read_soft" ) { &script_read_soft( $in, $out, $options ) }
- elsif ( $script eq "read_gff" ) { &script_read_gff( $in, $out, $options ) }
- elsif ( $script eq "read_2bit" ) { &script_read_2bit( $in, $out, $options ) }
- elsif ( $script eq "read_solexa" ) { &script_read_solexa( $in, $out, $options ) }
- elsif ( $script eq "read_solid" ) { &script_read_solid( $in, $out, $options ) }
- elsif ( $script eq "read_mysql" ) { &script_read_mysql( $in, $out, $options ) }
- elsif ( $script eq "count_seq" ) { &script_count_seq( $in, $out, $options ) }
- elsif ( $script eq "length_seq" ) { &script_length_seq( $in, $out, $options ) }
- elsif ( $script eq "uppercase_seq" ) { &script_uppercase_seq( $in, $out, $options ) }
- elsif ( $script eq "shuffle_seq" ) { &script_shuffle_seq( $in, $out, $options ) }
- elsif ( $script eq "analyze_seq" ) { &script_analyze_seq( $in, $out, $options ) }
- elsif ( $script eq "analyze_tags" ) { &script_analyze_tags( $in, $out, $options ) }
- elsif ( $script eq "complexity_seq" ) { &script_complexity_seq( $in, $out, $options ) }
- elsif ( $script eq "oligo_freq" ) { &script_oligo_freq( $in, $out, $options ) }
- elsif ( $script eq "create_weight_matrix" ) { &script_create_weight_matrix( $in, $out, $options ) }
- elsif ( $script eq "calc_bit_scores" ) { &script_calc_bit_scores( $in, $out, $options ) }
- elsif ( $script eq "reverse_seq" ) { &script_reverse_seq( $in, $out, $options ) }
- elsif ( $script eq "complement_seq" ) { &script_complement_seq( $in, $out, $options ) }
- elsif ( $script eq "remove_indels" ) { &script_remove_indels( $in, $out, $options ) }
- elsif ( $script eq "transliterate_seq" ) { &script_transliterate_seq( $in, $out, $options ) }
- elsif ( $script eq "transliterate_vals" ) { &script_transliterate_vals( $in, $out, $options ) }
- elsif ( $script eq "translate_seq" ) { &script_translate_seq( $in, $out, $options ) }
- elsif ( $script eq "extract_seq" ) { &script_extract_seq( $in, $out, $options ) }
- elsif ( $script eq "get_genome_seq" ) { &script_get_genome_seq( $in, $out, $options ) }
- elsif ( $script eq "get_genome_align" ) { &script_get_genome_align( $in, $out, $options ) }
- elsif ( $script eq "get_genome_phastcons" ) { &script_get_genome_phastcons( $in, $out, $options ) }
- elsif ( $script eq "fold_seq" ) { &script_fold_seq( $in, $out, $options ) }
- elsif ( $script eq "split_seq" ) { &script_split_seq( $in, $out, $options ) }
- elsif ( $script eq "split_bed" ) { &script_split_bed( $in, $out, $options ) }
- elsif ( $script eq "align_seq" ) { &script_align_seq( $in, $out, $options ) }
- elsif ( $script eq "tile_seq" ) { &script_tile_seq( $in, $out, $options ) }
- elsif ( $script eq "invert_align" ) { &script_invert_align( $in, $out, $options ) }
- elsif ( $script eq "patscan_seq" ) { &script_patscan_seq( $in, $out, $options ) }
- elsif ( $script eq "create_blast_db" ) { &script_create_blast_db( $in, $out, $options ) }
- elsif ( $script eq "blast_seq" ) { &script_blast_seq( $in, $out, $options ) }
- elsif ( $script eq "blat_seq" ) { &script_blat_seq( $in, $out, $options ) }
- elsif ( $script eq "match_seq" ) { &script_match_seq( $in, $out, $options ) }
- elsif ( $script eq "create_vmatch_index" ) { &script_create_vmatch_index( $in, $out, $options ) }
- elsif ( $script eq "vmatch_seq" ) { &script_vmatch_seq( $in, $out, $options ) }
- elsif ( $script eq "write_fasta" ) { &script_write_fasta( $in, $out, $options, $options ) }
- elsif ( $script eq "write_align" ) { &script_write_align( $in, $out, $options ) }
- elsif ( $script eq "write_blast" ) { &script_write_blast( $in, $out, $options ) }
- elsif ( $script eq "write_tab" ) { &script_write_tab( $in, $out, $options ) }
- elsif ( $script eq "write_bed" ) { &script_write_bed( $in, $out, $options ) }
- elsif ( $script eq "write_psl" ) { &script_write_psl( $in, $out, $options ) }
- elsif ( $script eq "write_2bit" ) { &script_write_2bit( $in, $out, $options, $options ) }
- elsif ( $script eq "write_solid" ) { &script_write_solid( $in, $out, $options, $options ) }
- elsif ( $script eq "head_records" ) { &script_head_records( $in, $out, $options ) }
- elsif ( $script eq "remove_keys" ) { &script_remove_keys( $in, $out, $options ) }
- elsif ( $script eq "rename_keys" ) { &script_rename_keys( $in, $out, $options ) }
- elsif ( $script eq "uniq_vals" ) { &script_uniq_vals( $in, $out, $options ) }
- elsif ( $script eq "merge_vals" ) { &script_merge_vals( $in, $out, $options ) }
- elsif ( $script eq "grab" ) { &script_grab( $in, $out, $options ) }
- elsif ( $script eq "compute" ) { &script_compute( $in, $out, $options ) }
- elsif ( $script eq "flip_tab" ) { &script_flip_tab( $in, $out, $options ) }
- elsif ( $script eq "add_ident" ) { &script_add_ident( $in, $out, $options ) }
- elsif ( $script eq "count_records" ) { &script_count_records( $in, $out, $options ) }
- elsif ( $script eq "random_records" ) { &script_random_records( $in, $out, $options ) }
- elsif ( $script eq "sort_records" ) { &script_sort_records( $in, $out, $options ) }
- elsif ( $script eq "count_vals" ) { &script_count_vals( $in, $out, $options ) }
- elsif ( $script eq "plot_histogram" ) { &script_plot_histogram( $in, $out, $options ) }
- elsif ( $script eq "plot_lendist" ) { &script_plot_lendist( $in, $out, $options ) }
- elsif ( $script eq "plot_chrdist" ) { &script_plot_chrdist( $in, $out, $options ) }
- elsif ( $script eq "plot_karyogram" ) { &script_plot_karyogram( $in, $out, $options ) }
- elsif ( $script eq "plot_matches" ) { &script_plot_matches( $in, $out, $options ) }
- elsif ( $script eq "plot_seqlogo" ) { &script_plot_seqlogo( $in, $out, $options ) }
- elsif ( $script eq "plot_phastcons_profiles" ) { &script_plot_phastcons_profiles( $in, $out, $options ) }
- elsif ( $script eq "analyze_bed" ) { &script_analyze_bed( $in, $out, $options ) }
- elsif ( $script eq "analyze_vals" ) { &script_analyze_vals( $in, $out, $options ) }
- elsif ( $script eq "length_vals" ) { &script_length_vals( $in, $out, $options ) }
- elsif ( $script eq "sum_vals" ) { &script_sum_vals( $in, $out, $options ) }
- elsif ( $script eq "mean_vals" ) { &script_mean_vals( $in, $out, $options ) }
- elsif ( $script eq "median_vals" ) { &script_median_vals( $in, $out, $options ) }
- elsif ( $script eq "max_vals" ) { &script_max_vals( $in, $out, $options ) }
- elsif ( $script eq "min_vals" ) { &script_min_vals( $in, $out, $options ) }
- elsif ( $script eq "upload_to_ucsc" ) { &script_upload_to_ucsc( $in, $out, $options ) }
-
- close $in if defined $in;
- close $out;
-
- # unset status - missing
- # write log file - missing
-}
-
-
-sub get_options
-{
- # Martin A. Hansen, February 2008.
-
- # Gets options from commandline and checks these vigerously.
-
- my ( $script, # name of script
- ) = @_;
-
- # Returns hash
-
- my ( %options, @options, $opt, @genomes );
-
- if ( $script eq "read_fasta" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_align" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_tab" )
- {
- @options = qw(
- data_in|i=s
- delimit|d=s
- cols|c=s
- keys|k=s
- skip|s=s
- num|n=s
- );
- }
- elsif ( $script eq "read_psl" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_bed" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_blast_tab" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_embl" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- keys|k=s
- feats|f=s
- quals|q=s
- );
- }
- elsif ( $script eq "read_stockholm" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_phastcons" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- min|m=s
- dist|d=s
- threshold|t=f
- gap|g=s
- );
- }
- elsif ( $script eq "read_soft" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_gff" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- );
- }
- elsif ( $script eq "read_2bit" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- no_mask|N
- );
- }
- elsif ( $script eq "read_solexa" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- quality|q=s
- );
- }
- elsif ( $script eq "read_solid" )
- {
- @options = qw(
- data_in|i=s
- num|n=s
- quality|q=s
- );
- }
- elsif ( $script eq "read_mysql" )
- {
- @options = qw(
- database|d=s
- query|q=s
- user|u=s
- password|p=s
- );
- }
- elsif ( $script eq "count_seq" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- );
- }
- elsif ( $script eq "length_seq" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- );
- }
- elsif ( $script eq "oligo_freq" )
- {
- @options = qw(
- word_size|w=s
- all|a
- );
- }
- elsif ( $script eq "create_weight_matrix" )
- {
- @options = qw(
- percent|p
- );
- }
- elsif ( $script eq "transliterate_seq" )
- {
- @options = qw(
- search|s=s
- replace|r=s
- delete|d=s
- );
- }
- elsif ( $script eq "transliterate_vals" )
- {
- @options = qw(
- keys|k=s
- search|s=s
- replace|r=s
- delete|d=s
- );
- }
- elsif ( $script eq "translate_seq" )
- {
- @options = qw(
- frames|f=s
- );
- }
- elsif ( $script eq "extract_seq" )
- {
- @options = qw(
- beg|b=s
- end|e=s
- len|l=s
- );
- }
- elsif ( $script eq "get_genome_seq" )
- {
- @options = qw(
- genome|g=s
- chr|c=s
- beg|b=s
- end|e=s
- len|l=s
- flank|f=s
- mask|m
- );
- }
- elsif ( $script eq "get_genome_align" )
- {
- @options = qw(
- genome|g=s
- chr|c=s
- beg|b=s
- end|e=s
- len|l=s
- strand|s=s
- );
- }
- elsif ( $script eq "get_genome_phastcons" )
- {
- @options = qw(
- genome|g=s
- chr|c=s
- beg|b=s
- end|e=s
- len|l=s
- flank|f=s
- );
- }
- elsif ( $script eq "split_seq" )
- {
- @options = qw(
- word_size|w=s
- uniq|u
- );
- }
- elsif ( $script eq "split_bed" )
- {
- @options = qw(
- window_size|w=s
- step_size|s=s
- );
- }
- elsif ( $script eq "tile_seq" )
- {
- @options = qw(
- identity|i=s
- supress_indels|s
- );
- }
- elsif ( $script eq "invert_align" )
- {
- @options = qw(
- soft|s
- );
- }
- elsif ( $script eq "patscan_seq" )
- {
- @options = qw(
- patterns|p=s
- patterns_in|P=s
- comp|c
- max_hits|h=s
- max_misses|m=s
- genome|g=s
- );
- }
- elsif ( $script eq "create_blast_db" )
- {
- @options = qw(
- no_stream|x
- database|d=s
- );
- }
- elsif ( $script eq "blast_seq" )
- {
- @options = qw(
- database|d=s
- genome|g=s
- program|p=s
- e_val|e=f
- filter|f
- cpus|c=s
- no_filter|F
- );
- }
- elsif ( $script eq "blat_seq" )
- {
- @options = qw(
- genome|g=s
- tile_size|t=s
- step_size|s=s
- min_identity|m=s
- min_score|M=s
- one_off|o=s
- ooc|c
- );
- }
- elsif ( $script eq "match_seq" )
- {
- @options = qw(
- word_size|w=s
- direction|d=s
- );
- }
- elsif ( $script eq "create_vmatch_index" )
- {
- @options = qw(
- index_name|i=s
- prefix_length|p=s
- no_stream|x
- );
- }
- elsif ( $script eq "vmatch_seq" )
- {
- @options = qw(
- genome|g=s
- index_name|i=s
- count|c
- max_hits|m=s
- hamming_dist|h=s
- edit_dist|e=s
- );
- }
- elsif ( $script eq "write_fasta" )
- {
- @options = qw(
- wrap|w=s
- no_stream|x
- data_out|o=s
- compress|Z
- );
- }
- elsif ( $script eq "write_align" )
- {
- @options = qw(
- wrap|w=s
- no_stream|x
- no_ruler|R
- no_consensus|C
- data_out|o=s
- );
- }
- elsif ( $script eq "write_blast" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- comment|c
- compress|Z
- );
- }
- elsif ( $script eq "write_tab" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- delimit|d=s
- keys|k=s
- no_keys|K=s
- comment|c
- compress|Z
- );
- }
- elsif ( $script eq "write_bed" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- compress|Z
- );
- }
- elsif ( $script eq "write_psl" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- compress|Z
- );
- }
- elsif ( $script eq "write_2bit" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- no_mask|N
- );
- }
- elsif ( $script eq "write_solid" )
- {
- @options = qw(
- wrap|w=s
- no_stream|x
- data_out|o=s
- compress|Z
- );
- }
- elsif ( $script eq "plot_seqlogo" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- );
- }
- elsif ( $script eq "plot_phastcons_profiles" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- genome|g=s
- mean|m
- median|M
- flank|f=s
- terminal|t=s
- title|T=s
- xlabel|X=s
- ylabel|Y=s
- );
- }
- elsif ( $script eq "analyze_vals" )
- {
- @options = qw(
- no_stream|x
- keys|k=s
- );
- }
- elsif ( $script eq "head_records" )
- {
- @options = qw(
- num|n=s
- );
- }
- elsif ( $script eq "remove_keys" )
- {
- @options = qw(
- keys|k=s
- save_keys|K=s
- );
- }
- elsif ( $script eq "rename_keys" )
- {
- @options = qw(
- keys|k=s
- );
- }
- elsif ( $script eq "uniq_vals" )
- {
- @options = qw(
- key|k=s
- invert|i
- );
- }
- elsif ( $script eq "merge_vals" )
- {
- @options = qw(
- keys|k=s
- delimit|d=s
- );
- }
- elsif ( $script eq "grab" )
- {
- @options = qw(
- patterns|p=s
- patterns_in|P=s
- regex|r=s
- eval|e=s
- exact_in|E=s
- invert|i
- case_insensitive|c
- keys|k=s
- keys_only|K
- vals_only|V
- );
- }
- elsif ( $script eq "compute" )
- {
- @options = qw(
- eval|e=s
- );
- }
- elsif ( $script eq "add_ident" )
- {
- @options = qw(
- prefix|p=s
- key|k=s
- );
- }
- elsif ( $script eq "count_records" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- );
- }
- elsif ( $script eq "random_records" )
- {
- @options = qw(
- num|n=s
- );
- }
- elsif ( $script eq "sort_records" )
- {
- @options = qw(
- reverse|r
- keys|k=s
- );
- }
- elsif ( $script eq "count_vals" )
- {
- @options = qw(
- keys|k=s
- );
- }
- elsif ( $script eq "plot_histogram" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- terminal|t=s
- title|T=s
- xlabel|X=s
- ylabel|Y=s
- key|k=s
- sort|s=s
- );
- }
- elsif ( $script eq "plot_lendist" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- terminal|t=s
- title|T=s
- xlabel|X=s
- ylabel|Y=s
- key|k=s
- );
- }
- elsif ( $script eq "plot_chrdist" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- terminal|t=s
- title|T=s
- xlabel|X=s
- ylabel|Y=s
- );
- }
- elsif ( $script eq "plot_karyogram" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- genome|g=s
- feat_color|f=s
- );
- }
- elsif ( $script eq "plot_matches" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- terminal|t=s
- title|T=s
- xlabel|X=s
- ylabel|Y=s
- direction|d=s
- );
- }
- elsif ( $script eq "length_vals" )
- {
- @options = qw(
- keys|k=s
- );
- }
- elsif ( $script eq "sum_vals" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- keys|k=s
- );
- }
- elsif ( $script eq "mean_vals" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- keys|k=s
- );
- }
- elsif ( $script eq "median_vals" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- keys|k=s
- );
- }
- elsif ( $script eq "max_vals" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- keys|k=s
- );
- }
- elsif ( $script eq "min_vals" )
- {
- @options = qw(
- no_stream|x
- data_out|o=s
- keys|k=s
- );
- }
- elsif ( $script eq "upload_to_ucsc" )
- {
- @options = qw(
- no_stream|x
- database|d=s
- table|t=s
- short_label|s=s
- long_label|l=s
- group|g=s
- priority|p=f
- use_score|u
- visibility|v=s
- wiggle|w
- color|c=s
- chunk_size|C=s
- );
- }
-
- push @options, qw(
- stream_in|I=s
- stream_out|O=s
- verbose
- );
-
-# print STDERR Dumper( \@options );
-
- GetOptions(
- \%options,
- @options,
- );
-
- $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
- $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
- $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
- $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
- $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
- $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
- $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
-
- # ---- check arguments ----
-
- if ( $options{ 'data_in' } )
- {
- $options{ "files" } = &getopt_files( $options{ 'data_in' } );
-
- &Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
- }
-
- map { &Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
-
-# print STDERR Dumper( \%options );
-
- foreach $opt ( keys %options )
- {
- if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
- {
- &Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
- }
- 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+$/ )
- {
- &Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
- }
- elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
- {
- &Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
- }
- elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
- {
- &Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
- }
- elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
- {
- &Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
- }
- elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
- {
- &Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
- }
- elsif ( $opt eq "genome" )
- {
- @genomes = &Maasha::Config::genomes();
-
- if ( not grep $options{ $opt }, @genomes ) {
- &Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'INST_DIR' }/conf/genomes.conf") );
- }
- }
- elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
- {
- &Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
- }
- elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
- {
- &Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
- }
- }
-
- &Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
- &Maasha::Common::error( qq(no --index_name specified) ) if $script eq "create_vmatch_index" and not $options{ "index_name" };
- &Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
- &Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
- &Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
- &Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
- &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" };
- &Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
- &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" };
-
- if ( $script eq "upload_to_ucsc" )
- {
- &Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
- &Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
- }
-
- return wantarray ? %options : \%options;
-}
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-sub script_print_usage
-{
- # Martin A. Hansen, January 2008.
-
- # Retrieves usage information from file and
- # prints this nicely formatted.
-
- my ( $path, # full path to usage file
- ) = @_;
-
- # Returns nothing.
-
- my ( $script, $fh, $line, @lines, @list, %hash, $key );
-
- $script = ( split "/", $path )[ -1 ];
-
- $fh = &Maasha::Common::read_open( $path );
-
- push @list, "Program name";
-
- $hash{ "Program name" } = [ $script ];
-
- while ( $line = <$fh> )
- {
- chomp $line;
-
- $line =~ s/\$script/$script/g;
-
- if ( $line =~ /^([^:]+):\s+(.+)$/ )
- {
- push @list, $1 if not exists $hash{ $1 };
- push @{ $hash{ $1 } }, $2;
- }
- }
-
- close $fh;
-
- print "\n";
-
- foreach $key ( @list )
- {
- if ( scalar @{ $hash{ $key } } == 1 )
- {
- @lines = &Maasha::Common::wrap_line( $hash{ $key }->[ 0 ], 80 );
-
- printf( "%-15s%s\n", "$key:", shift @lines );
-
- map { printf( "%-15s%s\n", "", $_ ) } @lines;
-
- print "\n";
- }
- else
- {
- print "$key:\n";
-
- map { print " $_\n" } @{ $hash{ $key } };
-
- print "\n";
- }
- }
-
- exit;
-}
-
-
-sub script_list_biotools
-{
- # Martin A. Hansen, January 2008.
-
- # Prints the description from the usage for each of the biotools.
-
- my ( $path, # full path to usage directory
- ) = @_;
-
- # Returns nothing.
-
- my ( @files, $file, $fh, $line, @lines, $program );
-
- @files = &Maasha::Common::ls_files( $path );
-
- foreach $file ( sort @files )
- {
- $program = ( split "/", $file )[ -1 ];
-
- $fh = &Maasha::Common::read_open( $file );
-
- while ( $line = <$fh> )
- {
- chomp $line;
-
- if ( $line =~ /^Description:\s+(.+)/ )
- {
- @lines = &Maasha::Common::wrap_line( $1, 60 );
-
- printf( "%-30s%s\n", $program, shift @lines );
-
- map { printf( "%-30s%s\n", "", $_ ) } @lines;
- }
- }
-
- close $fh;
- }
-
- exit;
-}
-
-
-sub script_read_fasta
-{
- # Martin A. Hansen, August 2007.
-
- # Read sequences from FASTA file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $file, $data_in, $entry, $num );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::Fasta::get_entry( $data_in ) )
- {
- if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
- {
- $record = {
- SEQ_NAME => $entry->[ SEQ_NAME ],
- SEQ => $entry->[ SEQ ],
- SEQ_LEN => length $entry->[ SEQ ],
- };
-
- &put_record( $record, $out );
- }
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_align
-{
- # Martin A. Hansen, August 2007.
-
- # Read aligned sequences from FASTA file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $entry, $record, $file, $data_in, $num );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::Fasta::get_entry( $data_in ) )
- {
- if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
- {
- $record = {
- ALIGN => 1,
- SEQ_NAME => $entry->[ SEQ_NAME ],
- SEQ => $entry->[ SEQ ],
- ALIGN_LEN => length $entry->[ SEQ ],
- };
-
- &put_record( $record, $out );
- }
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_tab
-{
- # Martin A. Hansen, August 2007.
-
- # Read table or table columns from stream or file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
-
- $options->{ 'delimit' } ||= '\s+';
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $skip = $options->{ 'skip' } ||= 0;
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $line = <$data_in> )
- {
- if ( $skip )
- {
- $skip--;
- next;
- }
-
- next if $line =~ /^#|^$/;
-
- chomp $line;
-
- undef $record;
- undef @fields2;
-
- @fields = split /$options->{'delimit'}/, $line;
-
- if ( $options->{ "cols" } ) {
- map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
- } else {
- @fields2 = @fields;
- }
-
- for ( $i = 0; $i < @fields2; $i++ )
- {
- if ( $options->{ "keys" }->[ $i ] ) {
- $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
- } else {
- $record->{ "V" . $i } = $fields2[ $i ];
- }
- }
-
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_psl
-{
- # Martin A. Hansen, August 2007.
-
- # Read psl table from stream or file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, @files, $file, $entries, $entry, $num );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $entries = &Maasha::UCSC::psl_get_entries( $file );
-
- foreach $entry ( @{ $entries } )
- {
- &put_record( $entry, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
- }
-
- NUM:
-}
-
-
-sub script_read_bed
-{
- # Martin A. Hansen, August 2007.
-
- # Read bed table from stream or file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $file, $record, $entry, $data_in, $num );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::UCSC::bed_get_entry( $data_in ) )
- {
- &put_record( $entry, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_blast_tab
-{
- # Martin A. Hansen, September 2007.
-
- # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $file, $line, @fields, $strand, $record, $data_in, $num );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $line = <$data_in> )
- {
- chomp $line;
-
- next if $line =~ /^#/;
-
- @fields = split /\t/, $line;
-
- $record->{ "REC_TYPE" } = "BLAST";
- $record->{ "Q_ID" } = $fields[ 0 ];
- $record->{ "S_ID" } = $fields[ 1 ];
- $record->{ "IDENT" } = $fields[ 2 ];
- $record->{ "ALIGN_LEN" } = $fields[ 3 ];
- $record->{ "MISMATCHES" } = $fields[ 4 ];
- $record->{ "GAPS" } = $fields[ 5 ];
- $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
- $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
- $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
- $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
- $record->{ "E_VAL" } = $fields[ 10 ];
- $record->{ "BIT_SCORE" } = $fields[ 11 ];
-
- if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
- {
- $record->{ "STRAND" } = '-';
-
- ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
- }
- else
- {
- $record->{ "STRAND" } = '+';
- }
-
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_embl
-{
- # Martin A. Hansen, August 2007.
-
- # Read EMBL format.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( %options2, $file, $data_in, $num, $entry, $record );
-
- map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
- map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
- map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::EMBL::get_embl_entry( $data_in ) )
- {
- $record = &Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
-
- my ( $feat, $feat2, $qual, $qual_val, $record_copy );
-
- $record_copy = dclone $record;
-
- delete $record_copy->{ "FT" };
-
- &put_record( $record_copy, $out );
-
- delete $record_copy->{ "SEQ" };
-
- foreach $feat ( keys %{ $record->{ "FT" } } )
- {
- $record_copy->{ "FEAT_TYPE" } = $feat;
-
- foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
- {
- foreach $qual ( keys %{ $feat2 } )
- {
- $qual_val = join "; ", @{ $feat2->{ $qual } };
-
- $qual =~ s/^_//;
- $qual = uc $qual;
-
- $record_copy->{ $qual } = $qual_val;
- }
-
- &put_record( $record_copy, $out );
- }
- }
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_stockholm
-{
- # Martin A. Hansen, August 2007.
-
- # Read Stockholm format.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::Stockholm::get_stockholm_entry( $data_in ) )
- {
- $record = &Maasha::Stockholm::parse_stockholm_entry( $entry );
-
- undef $record_anno;
-
- foreach $key ( keys %{ $record->{ "GF" } } ) {
- $record_anno->{ $key } = $record->{ "GF" }->{ $key };
- }
-
- $record_anno->{ "ALIGN" } = $num;
-
- &put_record( $record_anno, $out );
-
- foreach $seq ( @{ $record->{ "ALIGN" } } )
- {
- undef $record_align;
-
- $record_align = {
- ALIGN => $num,
- SEQ_NAME => $seq->[ 0 ],
- SEQ => $seq->[ 1 ],
- };
-
- &put_record( $record_align, $out );
- }
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_phastcons
-{
- # Martin A. Hansen, December 2007.
-
- # Read PhastCons format.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $data_in, $file, $num, $entry, @records, $record );
-
- $options->{ "min" } ||= 10;
- $options->{ "dist" } ||= 25;
- $options->{ "threshold" } ||= 0.8;
- $options->{ "gap" } ||= 5;
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::UCSC::phastcons_get_entry( $data_in ) )
- {
- @records = &Maasha::UCSC::phastcons_parse_entry( $entry, $options );
-
- foreach $record ( @records )
- {
- $record->{ "REC_TYPE" } = "BED";
- $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
-
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_soft
-{
- # Martin A. Hansen, December 2007.
-
- # Read soft format.
- # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $soft_index = &Maasha::NCBI::soft_index_file( $file );
-
- $fh = &Maasha::Common::read_open( $file );
-
- @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
-
- $plat_table = &Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
-
- @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
-
- $old_end = $platforms[ -1 ]->[ 2 ];
-
- foreach $sample ( @samples )
- {
- $records = &Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
-
- foreach $record ( @{ $records } )
- {
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- $old_end = $sample->[ 2 ];
- }
-
- close $fh;
- }
-
- NUM:
-
- close $data_in if $data_in;
- close $fh if $fh;
-}
-
-
-sub script_read_gff
-{
- # Martin A. Hansen, February 2008.
-
- # Read soft format.
- # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $data_in, $file, $fh, $num, $record, $entry );
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $fh = &Maasha::Common::read_open( $file );
-
- while ( $entry = &Maasha::GFF::get_entry( $fh ) )
- {
- &put_record( $entry, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $fh;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_2bit
-{
- # Martin A. Hansen, March 2008.
-
- # Read sequences from 2bit file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $file, $data_in, $mask, $toc, $line, $num );
-
- $mask = 1 if not $options->{ "no_mask" };
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- $toc = &Maasha::TwoBit::twobit_get_TOC( $data_in );
-
- foreach $line ( @{ $toc } )
- {
- $record->{ "SEQ_NAME" } = $line->[ 0 ];
- $record->{ "SEQ" } = &Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
- $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
-
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_solexa
-{
- # Martin A. Hansen, March 2008.
-
- # Read Solexa sequence reads from file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
-
- $options->{ "quality" } ||= 20;
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
- $base_name = &Maasha::Common::get_basename( $file );
- $base_name =~ s/\..*//;
-
- $seq_count = 0;
-
- while ( $line = <$data_in> )
- {
- @fields = split /:/, $line;
- @seqs = split //, $fields[ 5 ];
- @scores = split / /, $fields[ -1 ];
-
- for ( $i = 0; $i < @scores; $i++ ) {
- $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
- }
-
- $seq = join "", @seqs;
-
- $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
- $record->{ "SEQ" } = $seq;
- $record->{ "SEQ_LEN" } = length $seq;
- $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", &Maasha::Calc::mean( \@scores ) );
-
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $seq_count++;
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_solid
-{
- # Martin A. Hansen, April 2008.
-
- # Read Solid sequence from file.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
-
- $options->{ "quality" } ||= 15;
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $num = 1;
-
- foreach $file ( @{ $options->{ "files" } } )
- {
- $data_in = &Maasha::Common::read_open( $file );
-
- while ( $line = <$data_in> )
- {
- chomp $line;
-
- ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
-
- @scores = split /,/, $seq_qual;
- @seqs = split //, &Maasha::Solid::color_space2seq( $seq_cs );
-
- for ( $i = 0; $i < @seqs; $i++ ) {
- $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
- }
-
- $record = {
- SEQ_NAME => $seq_name,
- SEQ_CS => $seq_cs,
- SEQ_QUAL => $seq_qual,
- SEQ_LEN => length $seq_cs,
- SEQ => join( "", @seqs ),
- SCORE_MEAN => sprintf( "%.2f", &Maasha::Calc::mean( \@scores ) ),
- };
-
- &put_record( $record, $out );
-
- goto NUM if $options->{ "num" } and $num == $options->{ "num" };
-
- $num++;
- }
-
- close $data_in;
- }
-
- NUM:
-
- close $data_in if $data_in;
-}
-
-
-sub script_read_mysql
-{
- # Martin A. Hansen, May 2008.
-
- # Read a MySQL query into stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $dbh, $results );
-
- $options->{ "user" } ||= &Maasha::UCSC::ucsc_get_user();
- $options->{ "password" } ||= &Maasha::UCSC::ucsc_get_password();
-
- while ( $record = &get_record( $in ) ) {
- &put_record( $record, $out );
- }
-
- $dbh = &Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
-
- $results = &Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
-
- &Maasha::SQL::disconnect( $dbh );
-
- map { &put_record( $_ ) } @{ $results };
-}
-
-
-sub script_count_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Count sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $count, $result, $fh );
-
- $count = 0;
-
- while ( $record = &get_record( $in ) )
- {
- $count++ if $record->{ "SEQ" };
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- $result = { "count_seq" => $count };
-
- $fh = &write_stream( $options->{ "data_out" } );
-
- &put_record( $result, $fh );
-
- close $fh;
-}
-
-
-sub script_length_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Determine the length of sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $total );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
- $total += $record->{ "SEQ_LEN" };
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- &put_record( { TOTAL_SEQ_LEN => $total }, $out );
-}
-
-
-sub script_uppercase_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Uppercases sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record );
-
- while ( $record = &get_record( $in ) )
- {
- $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_shuffle_seq
-{
- # Martin A. Hansen, December 2007.
-
- # Shuffle sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record );
-
- while ( $record = &get_record( $in ) )
- {
- $record->{ "SEQ" } = &Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_analyze_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Analyze sequence composition of sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $analysis );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- $analysis = &Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
-
- map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_analyze_tags
-{
- # Martin A. Hansen, August 2008.
-
- # Analyze sequence tags in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
- {
- $clones = $1;
-
- $len_hash{ length( $record->{ "SEQ" } ) }++;
- $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
- }
- }
- elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
- {
- if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
- {
- $clones = $1;
-
- $len_hash{ $record->{ "BED_LEN" } }++;
- $clone_hash{ $record->{ "BED_LEN" } } += $clones;
- }
- }
- }
-
- foreach $key ( sort { $a <=> $b } keys %len_hash )
- {
- $tag_record->{ "TAG_LEN" } = $key;
- $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
- $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
-
- &put_record( $tag_record, $out );
- }
-}
-
-
-sub script_complexity_seq
-{
- # Martin A. Hansen, May 2008.
-
- # Generates an index calculated as the most common di-residue over
- # the sequence length for all sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $index );
-
- while ( $record = &get_record( $in ) )
- {
- $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", &Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_oligo_freq
-{
- # Martin A. Hansen, August 2007.
-
- # Determine the length of sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, %oligos, @freq_table );
-
- $options->{ "word_size" } ||= 7;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- map { $oligos{ $_ }++ } &Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
-
- if ( not $options->{ "all" } )
- {
- @freq_table = &Maasha::Seq::oligo_freq( \%oligos );
-
- map { &put_record( $_, $out ) } @freq_table;
-
- undef %oligos;
- }
- }
-
- &put_record( $record, $out );
- }
-
- if ( $options->{ "all" } )
- {
- @freq_table = &Maasha::Seq::oligo_freq( \%oligos );
-
- map { &put_record( $_, $out ) } @freq_table;
- }
-}
-
-
-sub script_create_weight_matrix
-{
- # Martin A. Hansen, August 2007.
-
- # Creates a weight matrix from an alignmnet.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
-
- $count = 0;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
- {
- $res = substr $record->{ "SEQ" }, $i, 1;
-
- $freq_hash{ $i }{ $res }++;
- $res_hash{ $res } = 1;
- }
-
- $count++;
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-
- foreach $res ( sort keys %res_hash )
- {
- undef $record;
-
- $record->{ "V0" } = $res;
-
- for ( $i = 0; $i < keys %freq_hash; $i++ )
- {
- $freq = $freq_hash{ $i }{ $res } || 0;
-
- if ( $options->{ "percent" } ) {
- $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
- }
-
- $record->{ "V" . ( $i + 1 ) } = $freq;
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_calc_bit_scores
-{
- # Martin A. Hansen, March 2007.
-
- # Calculates the bit scores for each position from an alignmnet in the stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
-
- $count = 0;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
-
- for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
- {
- $res = substr $record->{ "SEQ" }, $i, 1;
-
- next if $res =~ /-|_|~|\./;
-
- $freq_hash{ $i }{ $res }++;
- }
-
- $count++;
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-
- undef $record;
-
- if ( $type eq "protein" ) {
- $bit_max = 4;
- } else {
- $bit_max = 2;
- }
-
- for ( $i = 0; $i < keys %freq_hash; $i++ )
- {
- $bit_height = &Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
-
- $bit_diff = $bit_max - $bit_height;
-
- $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
- }
-
- &put_record( $record, $out );
-}
-
-
-sub script_reverse_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Reverse sequence in record.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } ) {
- $record->{ "SEQ" } = reverse $record->{ "SEQ" };
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_complement_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Complement sequence in record.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $type );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- if ( not $type ) {
- $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
- }
-
- if ( $type eq "rna" ) {
- &Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
- } elsif ( $type eq "dna" ) {
- &Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
- }
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_remove_indels
-{
- # Martin A. Hansen, August 2007.
-
- # Remove indels from sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record );
-
- while ( $record = &get_record( $in ) )
- {
- $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_transliterate_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Transliterate chars from sequence in record.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $search, $replace, $delete );
-
- $search = $options->{ "search" } || "";
- $replace = $options->{ "replace" } || "";
- $delete = $options->{ "delete" } || "";
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- if ( $search and $replace ) {
- eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
- } elsif ( $delete ) {
- eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
- }
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_transliterate_vals
-{
- # Martin A. Hansen, April 2008.
-
- # Transliterate chars from values in record.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $search, $replace, $delete, $key );
-
- $search = $options->{ "search" } || "";
- $replace = $options->{ "replace" } || "";
- $delete = $options->{ "delete" } || "";
-
- while ( $record = &get_record( $in ) )
- {
- foreach $key ( @{ $options->{ "keys" } } )
- {
- if ( exists $record->{ $key } )
- {
- if ( $search and $replace ) {
- eval "\$record->{ $key } =~ tr/$search/$replace/";
- } elsif ( $delete ) {
- eval "\$record->{ $key } =~ tr/$delete//d";
- }
- }
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_translate_seq
-{
- # Martin A. Hansen, February 2008.
-
- # Translate DNA sequence into protein sequence.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $frame, %new_record );
-
- $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- if ( &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
- {
- foreach $frame ( @{ $options->{ "frames" } } )
- {
- %new_record = %{ $record };
-
- $new_record{ "SEQ" } = &Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
- $new_record{ "SEQ_LEN" } = length $record->{ "SEQ" };
- $new_record{ "FRAME" } = $frame;
-
- &put_record( \%new_record, $out );
- }
- }
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-}
-
-
-sub script_extract_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Extract subsequences from sequences in record.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $beg, $end, $len, $record );
-
- if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
- $beg = 0;
- } else {
- $beg = $options->{ "beg" } - 1; # correcting for start offset
- }
-
- if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
- $end = $beg - 1;
- } elsif ( defined $options->{ "end" } ) {
- $end = $options->{ "end" } - 1; # correcting for start offset
- }
-
- $len = $options->{ "len" };
-
-# print "beg->$beg, end->$end, len->$len\n";
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- if ( defined $beg and defined $end )
- {
- if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
- $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
- } else {
- $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
- }
- }
- elsif ( defined $beg and defined $len )
- {
- if ( $len > length $record->{ "SEQ" } ) {
- $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
- } else {
- $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
- }
- }
- elsif ( defined $beg )
- {
- $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
- }
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_get_genome_seq
-{
- # Martin A. Hansen, December 2007.
-
- # Gets a subsequence from a genome.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
-
- $options->{ "flank" } ||= 0;
-
- if ( $options->{ "genome" } )
- {
- $genome_file = &Maasha::Config::genome_fasta( $options->{ 'genome' } );
- $index_file = &Maasha::Config::genome_fasta_index( $options->{ 'genome' } );
-
- $fh = &Maasha::Common::read_open( $genome_file );
- $index = &Maasha::Fasta::index_retrieve( $index_file );
-
- shift @{ $index }; # Get rid of the file size info
-
- map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
-
- if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
- {
- ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
-
- $beg = $index_beg + $options->{ "beg" } - 1;
-
- if ( $options->{ "len" } ) {
- $len = $options->{ "len" };
- } elsif ( $options->{ "end" } ) {
- $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
- }
-
- $beg -= $options->{ "flank" };
- $len += 2 * $options->{ "flank" };
-
- if ( $beg <= $index_beg )
- {
- $len -= $index_beg - $beg;
- $beg = $index_beg;
- }
-
- $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
-
- next if $beg > $index_beg + $index_len;
-
- $record->{ "CHR" } = $options->{ "chr" };
- $record->{ "CHR_BEG" } = $beg - $index_beg;
- $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
-
- $record->{ "SEQ" } = &Maasha::Common::file_read( $fh, $beg, $len );
- $record->{ "SEQ_LEN" } = $len;
-
- &put_record( $record, $out );
- }
- }
-
- while ( $record = &get_record( $in ) )
- {
- if ( $options->{ "genome" } and not $record->{ "SEQ" } )
- {
- if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
- {
- ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
-
- $beg = $record->{ "CHR_BEG" } + $index_beg;
- $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
- }
- elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
- {
- ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
-
- $beg = $record->{ "S_BEG" } + $index_beg;
- $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
- }
- elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
- {
- ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
-
- $beg = $record->{ "S_BEG" } + $index_beg;
- $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
- }
-
- $beg -= $options->{ "flank" };
- $len += 2 * $options->{ "flank" };
-
- if ( $beg <= $index_beg )
- {
- $len -= $index_beg - $beg;
- $beg = $index_beg;
- }
-
- $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
-
- next if $beg > $index_beg + $index_len;
-
- $record->{ "CHR_BEG" } = $beg - $index_beg;
- $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
-
- $record->{ "SEQ" } = &Maasha::Common::file_read( $fh, $beg, $len );
-
- if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
- {
- &Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
- $record->{ "SEQ" } = reverse $record->{ "SEQ" };
- }
-
- if ( $options->{ "mask" } )
- {
- if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
- {
- $record->{ "SEQ" } = lc $record->{ "SEQ" };
-
- @begs = split ",", $record->{ "Q_BEGS" };
- @lens = split ",", $record->{ "BLOCKSIZES" };
-
- for ( $i = 0; $i < @begs; $i++ ) {
- substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
- }
- }
- }
- }
-
- &put_record( $record, $out );
- }
-
- close $fh if $fh;
-}
-
-
-sub script_get_genome_align
-{
- # Martin A. Hansen, April 2008.
-
- # Gets a subalignment from a multiple genome alignment.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
-
- $options->{ "strand" } ||= "+";
-
- $align_num = 1;
-
- $maf_track = &Maasha::Config::maf_track( $options->{ "genome" } );
-
- if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
- {
- $beg = $options->{ "beg" } - 1;
-
- if ( $options->{ "end" } ) {
- $end = $options->{ "end" };
- } elsif ( $options->{ "len" } ) {
- $end = $beg + $options->{ "len" };
- }
-
- $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
-
- foreach $entry ( @{ $align } )
- {
- $entry->{ "ALIGN" } = $align_num;
- $entry->{ "CHR" } = $record->{ "CHR" };
- $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
- $entry->{ "CHR_END" } = $record->{ "CHR_END" };
- $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
- $entry->{ "Q_ID" } = $record->{ "Q_ID" };
- $entry->{ "SCORE" } = $record->{ "SCORE" };
-
- &put_record( $entry, $out );
- }
- }
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "REC_TYPE" } eq "BED" )
- {
- $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
- }
- elsif ( $record->{ "REC_TYPE" } eq "PSL" )
- {
- $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
- }
- elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
- {
- $align = &Maasha::UCSC::maf_extract( $TMP_DIR, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
- }
-
- foreach $entry ( @{ $align } )
- {
- $entry->{ "ALIGN" } = $align_num;
- $entry->{ "CHR" } = $record->{ "CHR" };
- $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
- $entry->{ "CHR_END" } = $record->{ "CHR_END" };
- $entry->{ "STRAND" } = $record->{ "STRAND" };
- $entry->{ "Q_ID" } = $record->{ "Q_ID" };
- $entry->{ "SCORE" } = $record->{ "SCORE" };
-
- &put_record( $entry, $out );
- }
-
- $align_num++;
- }
-}
-
-
-sub script_get_genome_phastcons
-{
- # Martin A. Hansen, February 2008.
-
- # Get phastcons scores from genome intervals.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
-
- $options->{ "flank" } ||= 0;
-
- $phastcons_file = &Maasha::Config::genome_phastcons( $options->{ "genome" } );
- $phastcons_index = &Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
-
- $index = &Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
- $fh_phastcons = &Maasha::Common::read_open( $phastcons_file );
-
- if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
- {
- $options->{ "beg" } -= 1; # request is 1-based
- $options->{ "end" } -= 1; # request is 1-based
-
- if ( $options->{ "len" } ) {
- $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
- }
-
- $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
-
- $record->{ "CHR" } = $options->{ "chr" };
- $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
- $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
-
- $record->{ "PHASTCONS" } = join ",", @{ $scores };
- $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
-
- &put_record( $record, $out );
- }
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "REC_TYPE" } eq "BED" )
- {
- $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
- }
- elsif ( $record->{ "REC_TYPE" } eq "PSL" )
- {
- $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
- }
- elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
- {
- $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
- }
-
- $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
-# $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
-
- &put_record( $record, $out );
- }
-
- close $fh_phastcons if $fh_phastcons;
-}
-
-
-sub script_fold_seq
-{
- # Martin A. Hansen, December 2007.
-
- # Folds sequences in stream into secondary structures.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $type, $struct, $index );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } )
- {
- if ( not $type ) {
- $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
- }
-
- if ( $type ne "protein" )
- {
- ( $struct, $index ) = &Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
- $record->{ "SEC_STRUCT" } = $struct;
- $record->{ "FREE_ENERGY" } = $index;
- $record->{ "SCORE" } = abs int $index;
- $record->{ "SIZE" } = length $struct;
- $record->{ "CONF" } = "1," x $record->{ "SIZE" };
- }
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_split_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Split a sequence in stream into words.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $new_record, $i, $subseq, %lookup );
-
- $options->{ "word_size" } ||= 7;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
- {
- $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
-
- if ( $options->{ "uniq" } and not $lookup{ $subseq } )
- {
- $new_record->{ "REC_TYPE" } = "SPLIT";
- $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
- $new_record->{ "SEQ" } = $subseq;
-
- &put_record( $new_record, $out );
-
- $lookup{ $subseq } = 1;
- }
- else
- {
- $new_record->{ "REC_TYPE" } = "SPLIT";
- $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
- $new_record->{ "SEQ" } = $subseq;
-
- &put_record( $new_record, $out );
- }
- }
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-}
-
-
-sub script_split_bed
-{
- # Martin A. Hansen, June 2008.
-
- # Split a BED record into overlapping windows.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $new_record, $i );
-
- $options->{ "window_size" } ||= 20;
- $options->{ "step_size" } ||= 1;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
- {
- $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
-
- for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
- {
- $new_record->{ "REC_TYPE" } = "BED";
- $new_record->{ "CHR" } = $record->{ "CHR" };
- $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
- $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
- $new_record->{ "BED_LEN" } = $options->{ "window_size" };
- $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
- $new_record->{ "SCORE" } = $record->{ "SCORE" };
- $new_record->{ "STRAND" } = $record->{ "STRAND" };
-
- &put_record( $new_record, $out );
- }
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-}
-
-
-sub script_align_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Align sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, @entries, $entry );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
- } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
- push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
- } else {
- &put_record( $record, $out );
- }
- }
-
- @entries = &Maasha::Align::align( \@entries );
-
- foreach $entry ( @entries )
- {
- if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
- {
- $record = {
- ALIGN => 1,
- SEQ_NAME => $entry->[ SEQ_NAME ],
- SEQ => $entry->[ SEQ ],
- };
-
- &put_record( $record, $out );
- }
- }
-}
-
-
-sub script_tile_seq
-{
- # Martin A. Hansen, February 2008.
-
- # Using the first sequence in stream as reference, tile
- # all subsequent sequences based on pairwise alignments.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $first, $ref_entry, @entries );
-
- $first = 1;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- if ( $first )
- {
- $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
-
- $first = 0;
- }
- else
- {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
- }
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-
- @entries = &Maasha::Align::align_tile( $ref_entry, \@entries, $options );
-
- map { &put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ], ALIGN => 1 }, $out ) } @entries;
-}
-
-
-sub script_invert_align
-{
- # Martin A. Hansen, February 2008.
-
- # Inverts an alignment showing only non-mathing residues
- # using the first sequence as reference.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, @entries );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } and $record->{ "ALIGN" } )
- {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-
- &Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
-
- map { &put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ], ALIGN => 1 }, $out ) } @entries;
-}
-
-
-sub script_patscan_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Locates patterns in sequences using scan_for_matches.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
-
- if ( $options->{ "patterns" } ) {
- $patterns = &Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
- } elsif ( -f $options->{ "patterns_in" } ) {
- $patterns = &Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
- }
-
- $genome_file = &Maasha::Config::genome_fasta( $options->{ 'genome' } ) if $options->{ 'genome' };
-
- push @args, "-c" if $options->{ "comp" };
- push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
- push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
-
- $seq_file = "$TMP_DIR/patscan.seq";
- $pat_file = "$TMP_DIR/patscan.pat";
- $out_file = "$TMP_DIR/patscan.out";
-
- $fh_out = &Maasha::Common::write_open( $seq_file );
-
- $i = 0;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
- {
- $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
-
- &Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
-
- $head_hash{ $i } = $record->{ "SEQ_NAME" };
-
- $i++;
- }
-
-# &put_record( $record, $out );
- }
-
- close $fh_out;
-
- $arg = join " ", @args;
- $arg .= " -p" if $type eq "protein";
-
- foreach $pattern ( @{ $patterns } )
- {
- $fh_out = &Maasha::Common::write_open( $pat_file );
-
- print $fh_out "$pattern\n";
-
- close $fh_out;
-
- if ( $options->{ 'genome' } ) {
- `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
- # &Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
- } else {
- `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
- # &Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
- }
-
- $fh_in = &Maasha::Common::read_open( $out_file );
-
- while ( $entry = &Maasha::Fasta::get_entry( $fh_in ) )
- {
- $result = &Maasha::Patscan::parse_scan_result( $entry, $pattern );
-
- if ( $options->{ 'genome' } )
- {
- $result->{ "CHR" } = $result->{ "S_ID" };
- $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
- $result->{ "CHR_END" } = $result->{ "S_END" };
-
- delete $result->{ "S_ID" };
- delete $result->{ "S_BEG" };
- delete $result->{ "S_END" };
- }
- else
- {
- $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
- }
-
- &put_record( $result, $out );
- }
-
- close $fh_in;
- }
-
- unlink $pat_file;
- unlink $seq_file;
- unlink $out_file;
-}
-
-
-sub script_create_blast_db
-{
- # Martin A. Hansen, September 2007.
-
- # Creates a NCBI BLAST database with formatdb
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $fh, $seq_type, $path, $record );
-
- $path = $options->{ "database" };
-
- $fh = &Maasha::Common::write_open( $path );
-
- while ( $record = &get_record( $in ) )
- {
- &put_record( $record, $out ) if not $options->{ "no_stream" };
-
- if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
- {
- $seq_type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
-
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
- }
- }
-
- close $fh;
-
- if ( $seq_type eq "protein" ) {
- &Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
- } else {
- &Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
- }
-
- unlink $path;
-}
-
-
-sub script_blast_seq
-{
- # Martin A. Hansen, September 2007.
-
- # BLASTs sequences in stream against a given database.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
-
- $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
- $options->{ "filter" } = "F";
- $options->{ "filter" } = "T" if $options->{ "filter" };
- $options->{ "cpus" } ||= 1;
-
- $options->{ "database" } = &Maasha::Config::genome_blast( $options->{ 'genome' } ) if $options->{ 'genome' };
-
- $tmp_in = "$TMP_DIR/blast_query.seq";
- $tmp_out = "$TMP_DIR/blast.result";
-
- $fh_out = &Maasha::Common::write_open( $tmp_in );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- $q_type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
-
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
- }
-
- &put_record( $record, $out );
- }
-
- close $fh_out;
-
- if ( -f $options->{ 'database' } . ".phr" ) {
- $s_type = "protein";
- } else {
- $s_type = "nucleotide";
- }
-
- if ( not $options->{ 'program' } )
- {
- if ( $q_type ne "protein" and $s_type ne "protein" ) {
- $options->{ 'program' } = "blastn";
- } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
- $options->{ 'program' } = "blastp";
- } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
- $options->{ 'program' } = "blastx";
- } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
- $options->{ 'program' } = "tblastn";
- }
- }
-
- &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 );
-
- unlink $tmp_in;
-
- $fh_out = &Maasha::Common::read_open( $tmp_out );
-
- undef $record;
-
- while ( $line = <$fh_out> )
- {
- chomp $line;
-
- next if $line =~ /^#/;
-
- @fields = split /\s+/, $line;
-
- $record->{ "REC_TYPE" } = "BLAST";
- $record->{ "Q_ID" } = $fields[ 0 ];
- $record->{ "S_ID" } = $fields[ 1 ];
- $record->{ "IDENT" } = $fields[ 2 ];
- $record->{ "ALIGN_LEN" } = $fields[ 3 ];
- $record->{ "MISMATCHES" } = $fields[ 4 ];
- $record->{ "GAPS" } = $fields[ 5 ];
- $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
- $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
- $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
- $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
- $record->{ "E_VAL" } = $fields[ 10 ];
- $record->{ "BIT_SCORE" } = $fields[ 11 ];
-
- if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
- {
- $record->{ "STRAND" } = '-';
-
- ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
- }
- else
- {
- $record->{ "STRAND" } = '+';
- }
-
- &put_record( $record, $out );
- }
-
- close $fh_out;
-
- unlink $tmp_out;
-}
-
-
-sub script_blat_seq
-{
- # Martin A. Hansen, August 2007.
-
- # BLATs sequences in stream against a given genome.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
-
- $genome_file = &Maasha::Config::genome_fasta( $options->{ "genome" } );
-
- $options->{ 'tile_size' } ||= 11;
- $options->{ 'one_off' } ||= 0;
- $options->{ 'min_identity' } ||= 90;
- $options->{ 'min_score' } ||= 0;
- $options->{ 'step_size' } ||= $options->{ 'tile_size' };
-
- $blat_args .= " -tileSize=$options->{ 'tile_size' }";
- $blat_args .= " -oneOff=$options->{ 'one_off' }";
- $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
- $blat_args .= " -minScore=$options->{ 'min_score' }";
- $blat_args .= " -stepSize=$options->{ 'step_size' }";
- $blat_args .= " -ooc=" . &Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
-
- $query_file = "$TMP_DIR/blat.seq";
-
- $fh_out = &Maasha::Common::write_open( $query_file );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
- $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
- }
-
- &put_record( $record, $out );
- }
-
- close $fh_out;
-
- $blat_args .= " -t=dnax" if $type eq "protein";
- $blat_args .= " -q=$type";
-
- $result_file = "$TMP_DIR/blat.psl";
-
- &Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
-
- unlink $query_file;
-
- $entries = &Maasha::UCSC::psl_get_entries( $result_file );
-
- map { &put_record( $_, $out ) } @{ $entries };
-
- unlink $result_file;
-}
-
-
-sub script_match_seq
-{
- # Martin A. Hansen, August 2007.
-
- # BLATs sequences in stream against a given genome.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, @entries, $results );
-
- $options->{ "word_size" } ||= 20;
- $options->{ "direction" } ||= "both";
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
- }
-
- &put_record( $record, $out );
- }
-
- if ( @entries == 1 )
- {
- $results = &Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $TMP_DIR );
-
- map { &put_record( $_, $out ) } @{ $results };
- }
- elsif ( @entries == 2 )
- {
- $results = &Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $TMP_DIR );
-
- map { &put_record( $_, $out ) } @{ $results };
- }
-}
-
-
-sub script_create_vmatch_index
-{
- # Martin A. Hansen, January 2008.
-
- # Create a vmatch index from sequences in the stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $file_tmp, $fh_tmp, $type );
-
- if ( $options->{ "index_name" } )
- {
- $file_tmp = $options->{ 'index_name' };
- $fh_tmp = &Maasha::Common::write_open( $file_tmp );
- }
-
- while ( $record = &get_record( $in ) )
- {
- if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
-
- $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- if ( $options->{ "index_name" } )
- {
- close $fh_tmp;
-
- if ( $type eq "protein" ) {
- &Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
- } else {
- &Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
- }
-
- unlink $file_tmp;
- }
-}
-
-
-sub script_vmatch_seq
-{
- # Martin A. Hansen, August 2007.
-
- # Vmatches sequences in stream against a given genome.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( @index_files, @records, $result_file, $fh_in, $record );
-
- $options->{ 'count' } = 1 if $options->{ 'max_hits' };
-
- if ( $options->{ "index_name" } ) {
- @index_files = $options->{ "index_name" };
- } else {
- @index_files = &Maasha::Config::genome_vmatch( $options->{ "genome" } );
- }
-
- while ( $record = &get_record( $in ) )
- {
- push @records, $record;
-
- &put_record( $record, $out );
- }
-
- $result_file = &Maasha::Match::match_vmatch( $TMP_DIR, \@records, \@index_files, $options );
-
- undef @records;
-
- $fh_in = &Maasha::Common::read_open( $result_file );
-
- while ( $record = &Maasha::Match::vmatch_get_entry( $fh_in ) ) {
- &put_record( $record, $out );
- }
-
- close $fh_in;
-
- unlink $result_file;
-}
-
-
-sub script_write_fasta
-{
- # Martin A. Hansen, August 2007.
-
- # Write FASTA entries from sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $fh );
-
- $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- close $fh;
-}
-
-
-sub script_write_align
-{
- # Martin A. Hansen, August 2007.
-
- # Write pretty alignments aligned sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $fh, $record, $align, $old_align, @entries );
-
- $fh = &write_stream( $options->{ "data_out" } ) ;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "ALIGN" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- $align = $record->{ "ALIGN" };
-
- if ( not $old_align )
- {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
-
- $old_align = $align;
- }
- elsif ( $align == $old_align )
- {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
- }
- else
- {
- if ( scalar( @entries ) == 2 ) {
- &Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
- } elsif ( scalar ( @entries ) > 2 ) {
- &Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
- }
-
- undef @entries;
- $old_align = $align;
- }
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- if ( scalar( @entries ) == 2 ) {
- &Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
- } elsif ( scalar ( @entries ) > 2 ) {
- &Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
- }
-
- close $fh if $fh;
-}
-
-
-sub script_write_blast
-{
- # Martin A. Hansen, November 2007.
-
- # Write data in blast table format (-m8 and 9).
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $fh, $record, $first );
-
- $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
-
- $first = 1;
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "REC_TYPE" } eq "BLAST" )
- {
- if ( $options->{ "comment" } and $first )
- {
- 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";
-
- $first = 0;
- }
-
- if ( $record->{ "STRAND" } eq "-" ) {
- ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
- }
-
- print $fh join( "\t",
- $record->{ "Q_ID" },
- $record->{ "S_ID" },
- $record->{ "IDENT" },
- $record->{ "ALIGN_LEN" },
- $record->{ "MISMATCHES" },
- $record->{ "GAPS" },
- $record->{ "Q_BEG" } + 1,
- $record->{ "Q_END" } + 1,
- $record->{ "S_BEG" } + 1,
- $record->{ "S_END" } + 1,
- $record->{ "E_VAL" },
- $record->{ "BIT_SCORE" }
- ), "\n";
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- close $fh;
-}
-
-
-sub script_write_tab
-{
- # Martin A. Hansen, August 2007.
-
- # Write data as table.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
-
- $options->{ "delimit" } ||= "\t";
-
- map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
-
- $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
- while ( $record = &get_record( $in ) )
- {
- undef @vals;
- $ok = 1;
-
- if ( $options->{ "keys" } )
- {
- map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
-
- if ( $ok )
- {
- foreach $key ( @{ $options->{ "keys" } } )
- {
- if ( exists $record->{ $key } )
- {
- push @keys, $key if $options->{ "comment" };
- push @vals, $record->{ $key };
- }
- }
- }
- }
- else
- {
- foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
- {
- next if exists $no_keys{ $key };
-
- push @keys, $key if $options->{ "comment" };
- push @vals, $record->{ $key };
- }
- }
-
- if ( @keys and $options->{ "comment" } )
- {
- print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
-
- delete $options->{ "comment" };
- }
-
- print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- close $fh;
-}
-
-
-sub script_write_bed
-{
- # Martin A. Hansen, August 2007.
-
- # Write BED format for the UCSC genome browser using records in stream.
-
- # Crude - needs lots of work!
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $fh, $record, $new_record );
-
- $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
- {
- &Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
- }
- elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
- {
- $new_record->{ "CHR" } = $record->{ "S_ID" };
- $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
- $new_record->{ "CHR_END" } = $record->{ "S_END" };
- $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
- $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
- $new_record->{ "STRAND" } = $record->{ "STRAND" };
-
- &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
- }
- elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
- {
- &Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
- }
- elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
- {
- $new_record->{ "CHR" } = $record->{ "S_ID" };
- $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
- $new_record->{ "CHR_END" } = $record->{ "S_END" };
- $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
- $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
- $new_record->{ "STRAND" } = $record->{ "STRAND" };
-
- &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
- }
- elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
- {
- $new_record->{ "CHR" } = $record->{ "S_ID" };
- $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
- $new_record->{ "CHR_END" } = $record->{ "S_END" };
- $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
- $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
- $new_record->{ "STRAND" } = $record->{ "STRAND" };
-
- &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
- }
- elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
- {
- &Maasha::UCSC::bed_put_entry( $record, $fh );
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- close $fh;
-}
-
-
-sub script_write_psl
-{
- # Martin A. Hansen, August 2007.
-
- # Write PSL output from stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $fh, $record, @output, $first );
-
- $first = 1;
-
- $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
- while ( $record = &get_record( $in ) )
- {
- &put_record( $record, $out ) if not $options->{ "no_stream" };
-
- if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
- {
- &Maasha::UCSC::psl_put_header( $fh ) if $first;
- &Maasha::UCSC::psl_put_entry( $record, $fh );
- $first = 0;
- }
- }
-
- close $fh;
-}
-
-
-sub script_write_2bit
-{
- # Martin A. Hansen, March 2008.
-
- # Write sequence entries from stream in 2bit format.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
-
- $mask = 1 if not $options->{ "no_mask" };
-
- $tmp_file = "$TMP_DIR/write_2bit.fna";
- $fh_tmp = &Maasha::Common::write_open( $tmp_file );
-
- $fh_out = &write_stream( $options->{ "data_out" } );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- close $fh_tmp;
-
- $fh_in = &Maasha::Common::read_open( $tmp_file );
-
- &Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
-
- close $fh_in;
- close $fh_out;
-
- unlink $tmp_file;
-}
-
-
-sub script_write_solid
-{
- # Martin A. Hansen, April 2008.
-
- # Write di-base encoded Solid sequence from entries in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $fh, $seq_cs );
-
- $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
- {
- $seq_cs = &Maasha::Solid::seq2color_space( $record->{ "SEQ" } );
-
- &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- close $fh;
-}
-
-
-sub script_plot_seqlogo
-{
- # Martin A. Hansen, August 2007.
-
- # Calculates and writes a sequence logo for alignments.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, @entries, $logo, $fh );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
- push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- $logo = &Maasha::Plot::seq_logo( \@entries );
-
- $fh = &write_stream( $options->{ "data_out" } );
-
- print $fh $logo;
-
- close $fh;
-}
-
-
-sub script_plot_phastcons_profiles
-{
- # Martin A. Hansen, January 2008.
-
- # Plots PhastCons profiles.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
-
- $options->{ "title" } ||= "PhastCons Profiles";
-
- $phastcons_file = &Maasha::Config::genome_phastcons( $options->{ "genome" } );
- $phastcons_index = &Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
-
- $index = &Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
- $fh_phastcons = &Maasha::Common::read_open( $phastcons_file );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
- {
- $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
-
- push @{ $AoA }, [ @{ $scores } ];
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- &Maasha::UCSC::phastcons_normalize( $AoA );
-
- $AoA = [ [ &Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
- $AoA = [ [ &Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
-
- $AoA = &Maasha::Matrix::matrix_flip( $AoA );
-
- $plot = &Maasha::Plot::lineplot_simple( $AoA, $options, $TMP_DIR );
-
- $fh = &write_stream( $options->{ "data_out" } );
-
- print $fh "$_\n" foreach @{ $plot };
-
- close $fh;
-}
-
-
-sub script_analyze_bed
-{
- # Martin A. Hansen, March 2008.
-
- # Analyze BED entries in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record );
-
- while ( $record = &get_record( $in ) )
- {
- $record = &Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_analyze_vals
-{
- # Martin A. Hansen, August 2007.
-
- # Analyze values for given keys in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $key, @keys, %key_hash, $analysis, $len );
-
- map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
-
- while ( $record = &get_record( $in ) )
- {
- foreach $key ( keys %{ $record } )
- {
- next if $options->{ "keys" } and not exists $key_hash{ $key };
-
- $analysis->{ $key }->{ "COUNT" }++;
-
- if ( &Maasha::Calc::is_a_number( $record->{ $key } ) )
- {
- $analysis->{ $key }->{ "TYPE" } = "num";
- $analysis->{ $key }->{ "SUM" } += $record->{ $key };
- $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
- $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
- }
- else
- {
- $len = length $record->{ $key };
-
- $analysis->{ $key }->{ "TYPE" } = "alph";
- $analysis->{ $key }->{ "SUM" } += $len;
- $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
- $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
- }
- }
-
- &put_record( $record, $out ) if not $options->{ "no_stream" };
- }
-
- foreach $key ( keys %{ $analysis } )
- {
- $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
- $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUN" };
- }
-
- my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
-
- $keys = "KEY ";
- $types = "TYPE ";
- $counts = "COUNT";
- $mins = "MIN ";
- $maxs = "MAX ";
- $sums = "SUM ";
- $means = "MEAN ";
-
- if ( $options->{ "keys" } ) {
- @keys = @{ $options->{ "keys" } };
- } else {
- @keys = keys %{ $analysis };
- }
-
- foreach $key ( @keys )
- {
- $keys .= sprintf "% 15s", $key;
- $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
- $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
- $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
- $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
- $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
- $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
- }
-
- print $out "$keys\n";
- print $out "$types\n";
- print $out "$counts\n";
- print $out "$mins\n";
- print $out "$maxs\n";
- print $out "$sums\n";
- print $out "$means\n";
-}
-
-
-sub script_head_records
-{
- # Martin A. Hansen, August 2007.
-
- # Display the first sequences in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $count );
-
- $options->{ "num" } ||= 10;
-
- $count = 0;
-
- while ( $record = &get_record( $in ) )
- {
- $count++;
-
- &put_record( $record, $out );
-
- last if $count == $options->{ "num" };
- }
-}
-
-
-sub script_remove_keys
-{
- # Martin A. Hansen, August 2007.
-
- # Remove keys from stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $new_record );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $options->{ "keys" } )
- {
- map { delete $record->{ $_ } } @{ $options->{ "keys" } };
- }
- elsif ( $options->{ "save_keys" } )
- {
- map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
-
- $record = $new_record;
- }
-
- &put_record( $record, $out ) if keys %{ $record };
- }
-}
-
-
-sub script_rename_keys
-{
- # Martin A. Hansen, August 2007.
-
- # Rename keys in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record );
-
- while ( $record = &get_record( $in ) )
- {
- if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
- {
- $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
-
- delete $record->{ $options->{ "keys" }->[ 0 ] };
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_uniq_vals
-{
- # Martin A. Hansen, August 2007.
-
- # Find unique values in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( %hash, $record );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $record->{ $options->{ "key" } } )
- {
- if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
- {
- &put_record( $record, $out );
-
- $hash{ $record->{ $options->{ "key" } } } = 1;
- }
- elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
- {
- &put_record( $record, $out );
- }
- else
- {
- $hash{ $record->{ $options->{ "key" } } } = 1;
- }
- }
- else
- {
- &put_record( $record, $out );
- }
- }
-}
-
-
-sub script_merge_vals
-{
- # Martin A. Hansen, August 2007.
-
- # Rename keys in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, @join, $i );
-
- $options->{ "delimit" } ||= '_';
-
- while ( $record = &get_record( $in ) )
- {
- if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
- {
- @join = $record->{ $options->{ "keys" }->[ 0 ] };
-
- for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
- push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
- }
-
- $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_grab
-{
- # Martin A. Hansen, August 2007.
-
- # Grab for records in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
-
- if ( $options->{ "patterns" } )
- {
- $patterns = [ split ",", $options->{ "patterns" } ];
- }
- elsif ( -f $options->{ "patterns_in" } )
- {
- $patterns = &Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
- }
- elsif ( -f $options->{ "exact_in" } )
- {
- $patterns = &Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
-
- map { $lookup_hash{ $_ } = 1 } @{ $patterns };
-
- undef $patterns;
- }
-
- if ( $options->{ "eval" } )
- {
- if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
- {
- $key = $1;
- $op = $2;
- $val = $3;
- }
- }
-
- while ( $record = &get_record( $in ) )
- {
- $pos = -1;
-
- if ( %lookup_hash )
- {
- if ( $options->{ "keys" } )
- {
- foreach $key ( @{ $options->{ "keys" } } )
- {
- if ( exists $lookup_hash{ $record->{ $key } } )
- {
- $pos = 1;
- goto FOUND;
- }
- }
- }
- else
- {
- foreach $key ( keys %{ $record } )
- {
- if ( not $options->{ "vals_only" } )
- {
- if ( exists $lookup_hash{ $key } )
- {
- $pos = 1;
- goto FOUND;
- }
- }
-
- if ( not $options->{ "keys_only" } )
- {
- if ( exists $lookup_hash{ $record->{ $key } } )
- {
- $pos = 1;
- goto FOUND;
- }
- }
- }
- }
- }
- elsif ( $patterns )
- {
- foreach $pattern ( @{ $patterns } )
- {
- if ( $options->{ "keys" } )
- {
- foreach $key ( @{ $options->{ "keys" } } )
- {
- $pos = index $record->{ $key }, $pattern;
-
- goto FOUND if $pos >= 0;
- }
- }
- else
- {
- foreach $key ( keys %{ $record } )
- {
- if ( not $options->{ "vals_only" } )
- {
- $pos = index $key, $pattern;
-
- goto FOUND if $pos >= 0;
- }
-
- if ( not $options->{ "keys_only" } )
- {
- $pos = index $record->{ $key }, $pattern;
-
- goto FOUND if $pos >= 0;
- }
- }
- }
- }
- }
- elsif ( $options->{ "regex" } )
- {
- if ( $options->{ "keys" } )
- {
- foreach $key ( @{ $options->{ "keys" } } )
- {
- if ( $options->{ "case_insensitive" } ) {
- $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
- } else {
- $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
- }
-
- goto FOUND if $pos >= 0;
- }
- }
- else
- {
- foreach $key ( keys %{ $record } )
- {
- if ( not $options->{ "vals_only" } )
- {
- if ( $options->{ "case_insensitive" } ) {
- $pos = 1 if $key =~ /$options->{'regex'}/i;
- } else {
- $pos = 1 if $key =~ /$options->{'regex'}/;
- }
-
- goto FOUND if $pos >= 0;
- }
-
- if ( not $options->{ "keys_only" } )
- {
- if ( $options->{ "case_insensitive" } ) {
- $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
- } else {
- $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
- }
-
- goto FOUND if $pos >= 0;
- }
- }
- }
- }
- elsif ( $options->{ "eval" } )
- {
- if ( defined $record->{ $key } )
- {
- if ( $op eq "<" and $record->{ $key } < $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq ">" and $record->{ $key } > $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq "=" and $record->{ $key } == $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
- $pos = 1 and goto FOUND;
- } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
- $pos = 1 and goto FOUND;
- }
- }
- }
-
- FOUND:
-
- if ( $pos >= 0 and not $options->{ "invert" } ) {
- &put_record( $record, $out );
- } elsif ( $pos < 0 and $options->{ "invert" } ) {
- &put_record( $record, $out );
- }
- }
-}
-
-
-sub script_compute
-{
- # Martin A. Hansen, August 2007.
-
- # Evaluate extression for records in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $eval_key, $eval_val, $check, @keys );
-
- while ( $record = &get_record( $in ) )
- {
- if ( $options->{ "eval" } )
- {
- if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
- {
- $eval_key = $1;
- $eval_val = $2;
- }
-
- if ( not $check )
- {
- @keys = split /\W+/, $eval_val;
- @keys = grep { ! /^\d+$/ } @keys;
-
- $check = 1;
- }
-
- map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
-
- $record->{ $eval_key } = eval "$eval_val" or &Maasha::Common::error( "eval failed -> $@" );
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_flip_tab
-{
- # Martin A. Hansen, June 2008.
-
- # Flip a table.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
-
- while ( $record = &get_record( $in ) )
- {
- undef @rows;
-
- foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
- {
- push @rows, $record->{ $key };
-
- }
-
- push @matrix, [ @rows ];
- }
-
- undef $record;
-
- @matrix = &Maasha::Matrix::matrix_flip( \@matrix );
-
- foreach $row ( @matrix )
- {
- for ( $i = 0; $i < @{ $row }; $i++ ) {
- $record->{ "V$i" } = $row->[ $i ];
- }
-
- &put_record( $record, $out );
- }
-}
-
-
-sub script_add_ident
-{
- # Martin A. Hansen, May 2008.
-
- # Add a unique identifier to each record in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $key, $prefix, $i );
-
- $key = $options->{ "key" } || "ID";
- $prefix = $options->{ "prefix" } || "ID";
-
- $i = 0;
-
- while ( $record = &get_record( $in ) )
- {
- $record->{ $key } = sprintf( "$prefix%08d", $i );
-
- &put_record( $record, $out );
-
- $i++;
- }
-}
-
-
-sub script_count_records
-{
- # Martin A. Hansen, August 2007.
-
- # Count records in stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $count, $result, $fh, $line );
-
- $count = 0;
-
- if ( $options->{ "no_stream" } )
- {
- while ( $line = <$in> )
- {
- chomp $line;
-
- $count++ if $line eq "---";
- }
- }
- else
- {
- while ( $record = &get_record( $in ) )
- {
- &put_record( $record, $out );
-
- $count++;
- }
- }
-
- $result = { "count_records" => $count };
-
- $fh = &write_stream( $options->{ "data_out" } );
-
- &put_record( $result, $fh );
-
- close $fh;
-}
-
-
-sub script_random_records
-{
- # Martin A. Hansen, August 2007.
-
- # Pick a number or random records from stream.
-
- my ( $in, # handle to in stream
- $out, # handle to out stream
- $options, # options hash
- ) = @_;
-
- # Returns nothing.
-
- my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
-
- $options->{ "num" } ||= 10;
-
- $tmp_file = "$TMP_DIR/random_records.tmp";
-
- $fh_out = &Maasha::Common::write_open( $tmp_file );
-
- $count = 0;
-
- while ( $record = &get_record( $in ) )
- {
- &put_record( $record, $fh_out );
-
- $count++;
- }
-
- close $fh_out;
-
- $max = 0;
- $i = 0;
-
- &Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
-
- while ( $i < $options->{ "num" } )
- {
- $rand = int( rand( $count ) );
-
- if ( not exists $rand_hash{ $rand } )
- {
- $rand_hash{ $rand } = 1;
-
- $max = $rand if $rand > $max;
-
- $i++;
- }
- }
-
- $fh_in = &Maasha::Common::read_open( $tmp_file );
-
- $count = 0;
-
- while ( $record = &get_record( $fh_in ) )
- {
- &put_record( $record, $out ) if exists $rand_hash{ $count };
-
- last if $count == $max;
-
- $count++;
- }
-
- close $fh_in;
-
- unlink $tmp_file;
-}
-
-
-sub script_sort_records
-{
- # Martin A. Hansen, August 2007.
-
- # Sort to sort records according to keys.