1 package Maasha::BioRun;
4 # Copyright (C) 2007-2009 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines that contains Biopieces which are run.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
37 use Maasha::Biopieces;
46 use Maasha::Stockholm;
52 use Maasha::UCSC::BED;
53 use Maasha::UCSC::Wiggle;
62 use vars qw( @ISA @EXPORT_OK );
66 @ISA = qw( Exporter );
74 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
77 my ( $script, $BP_TMP );
79 $script = Maasha::Common::get_scriptname();
80 $BP_TMP = Maasha::Common::get_tmpdir();
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
86 run_script( $script );
89 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
94 # Martin A. Hansen, August 2007.
96 # Run a specific script.
98 my ( $script, # script name
103 my ( $t0, $t1, $options, $in, $out );
105 Maasha::Biopieces::log_biopiece();
107 $t0 = gettimeofday();
109 $options = get_options( $script );
111 $options->{ "SCRIPT" } = $script;
113 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
115 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
116 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
118 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
119 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
120 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
121 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
122 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
123 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
124 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
125 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
126 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
127 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
128 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
129 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
130 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
131 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
132 elsif ( $script eq "read_ucsc_config" ) { script_read_ucsc_config( $in, $out, $options ) }
133 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
134 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
135 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
136 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
137 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
138 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
139 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
140 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
141 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
142 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
143 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
144 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
145 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
146 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
147 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
148 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
149 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
150 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
151 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
152 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
153 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
154 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
155 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
156 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
157 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
158 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
159 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
160 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
161 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
162 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
163 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
164 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
165 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
166 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
167 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
168 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
169 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
170 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
171 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
172 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
173 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
174 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
175 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
176 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
177 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
178 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
179 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
180 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
182 close $in if defined $in;
185 $t1 = gettimeofday();
187 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
193 # Martin A. Hansen, February 2008.
195 # Gets options from commandline and checks these vigerously.
197 my ( $script, # name of script
202 my ( %options, @options, $opt, @genomes, $real );
204 if ( $script eq "print_usage" )
210 elsif ( $script eq "read_psl" )
217 elsif ( $script eq "read_bed" )
226 elsif ( $script eq "read_fixedstep" )
233 elsif ( $script eq "read_blast_tab" )
240 elsif ( $script eq "read_embl" )
250 elsif ( $script eq "read_stockholm" )
257 elsif ( $script eq "read_phastcons" )
268 elsif ( $script eq "read_soft" )
276 elsif ( $script eq "read_gff" )
283 elsif ( $script eq "read_2bit" )
291 elsif ( $script eq "read_solexa" )
300 elsif ( $script eq "read_solid" )
308 elsif ( $script eq "read_mysql" )
317 elsif ( $script eq "read_ucsc_config" )
324 elsif ( $script eq "length_seq" )
331 elsif ( $script eq "oligo_freq" )
338 elsif ( $script eq "transliterate_seq" )
346 elsif ( $script eq "transliterate_vals" )
355 elsif ( $script eq "translate_seq" )
361 elsif ( $script eq "get_genome_align" )
372 elsif ( $script eq "get_genome_phastcons" )
383 elsif ( $script eq "tile_seq" )
390 elsif ( $script eq "invert_align" )
396 elsif ( $script eq "patscan_seq" )
407 elsif ( $script eq "soap_seq" )
418 elsif ( $script eq "match_seq" )
425 elsif ( $script eq "create_vmatch_index" )
433 elsif ( $script eq "write_bed" )
443 elsif ( $script eq "write_psl" )
451 elsif ( $script eq "write_fixedstep" )
459 elsif ( $script eq "write_2bit" )
467 elsif ( $script eq "write_solid" )
476 elsif ( $script eq "write_ucsc_config" )
483 elsif ( $script eq "plot_seqlogo" )
490 elsif ( $script eq "plot_phastcons_profiles" )
505 elsif ( $script eq "head_records" )
511 elsif ( $script eq "remove_keys" )
518 elsif ( $script eq "remove_adaptor" )
527 elsif ( $script eq "remove_mysql_tables" )
538 elsif ( $script eq "remove_ucsc_tracks" )
550 elsif ( $script eq "rename_keys" )
556 elsif ( $script eq "uniq_vals" )
563 elsif ( $script eq "merge_vals" )
570 elsif ( $script eq "merge_records" )
577 elsif ( $script eq "sort_records" )
584 elsif ( $script eq "plot_histogram" )
597 elsif ( $script eq "plot_lendist" )
609 elsif ( $script eq "plot_chrdist" )
620 elsif ( $script eq "plot_karyogram" )
629 elsif ( $script eq "plot_matches" )
641 elsif ( $script eq "length_vals" )
647 elsif ( $script eq "sum_vals" )
655 elsif ( $script eq "mean_vals" )
663 elsif ( $script eq "median_vals" )
671 elsif ( $script eq "max_vals" )
679 elsif ( $script eq "min_vals" )
687 elsif ( $script eq "upload_to_ucsc" )
711 # print STDERR Dumper( \@options );
718 # print STDERR Dumper( \%options );
720 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
721 return wantarray ? %options : \%options;
724 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
725 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
726 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
727 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
728 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
729 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
730 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
731 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
732 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
733 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
735 # ---- check arguments ----
737 if ( $options{ 'data_in' } )
739 $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
741 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
744 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
746 # print STDERR Dumper( \%options );
748 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
750 foreach $opt ( keys %options )
752 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
754 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
756 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
758 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
760 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
762 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
764 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
766 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
768 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
770 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
772 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
774 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
776 elsif ( $opt eq "genome" )
778 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
779 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
781 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
782 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
785 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
787 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
789 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
791 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
793 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
795 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
797 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
799 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
801 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
803 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
807 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
808 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
809 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
810 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
811 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /get_genome_align|get_genome_phastcons|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
812 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
813 Maasha::Common::error( qq(no --keys speficied) ) if $script =~ /sort_records|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
815 if ( $script eq "upload_to_ucsc" )
817 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
818 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
821 return wantarray ? %options : \%options;
825 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
828 sub script_print_usage
830 # Martin A. Hansen, January 2008.
832 # Retrieves usage information from file and
833 # prints this nicely formatted.
835 my ( $in, # handle to in stream
836 $out, # handle to out stream
837 $options, # options hash
842 my ( $file, $wiki, $lines );
844 if ( $options->{ 'data_in' } ) {
845 $file = $options->{ 'data_in' };
847 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
850 $wiki = Maasha::Gwiki::gwiki_read( $file );
852 ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
854 if ( not $options->{ "help" } ) {
855 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
858 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
860 print STDERR "$_\n" foreach @{ $lines };
868 # Martin A. Hansen, August 2007.
870 # Read psl table from stream or file.
872 my ( $in, # handle to in stream
873 $out, # handle to out stream
874 $options, # options hash
879 my ( $record, $file, $data_in, $num );
881 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
882 Maasha::Biopieces::put_record( $record, $out );
887 foreach $file ( @{ $options->{ "files" } } )
889 $data_in = Maasha::Common::read_open( $file );
891 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
893 Maasha::Biopieces::put_record( $record, $out );
895 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
907 # Martin A. Hansen, August 2007.
909 # Read bed table from stream or file.
911 my ( $in, # handle to in stream
912 $out, # handle to out stream
913 $options, # options hash
918 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
920 $cols = $options->{ 'cols' }->[ 0 ];
922 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
923 Maasha::Biopieces::put_record( $record, $out );
928 foreach $file ( @{ $options->{ "files" } } )
930 $data_in = Maasha::Common::read_open( $file );
932 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
934 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
936 Maasha::Biopieces::put_record( $record, $out );
938 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
948 close $data_in if $data_in;
952 sub script_read_fixedstep
954 # Martin A. Hansen, Juli 2008.
956 # Read fixedstep wiggle format from stream or file.
958 my ( $in, # handle to in stream
959 $out, # handle to out stream
960 $options, # options hash
965 my ( $file, $record, $entry, $data_in, $num );
967 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
968 Maasha::Biopieces::put_record( $record, $out );
973 foreach $file ( @{ $options->{ "files" } } )
975 $data_in = Maasha::Common::read_open( $file );
977 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
979 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
981 Maasha::Biopieces::put_record( $record, $out );
983 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
993 close $data_in if $data_in;
997 sub script_read_blast_tab
999 # Martin A. Hansen, September 2007.
1001 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1003 my ( $in, # handle to in stream
1004 $out, # handle to out stream
1005 $options, # options hash
1010 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1012 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1013 Maasha::Biopieces::put_record( $record, $out );
1018 foreach $file ( @{ $options->{ "files" } } )
1020 $data_in = Maasha::Common::read_open( $file );
1022 while ( $line = <$data_in> )
1026 next if $line =~ /^#/;
1028 @fields = split /\t/, $line;
1030 $record->{ "REC_TYPE" } = "BLAST";
1031 $record->{ "Q_ID" } = $fields[ 0 ];
1032 $record->{ "S_ID" } = $fields[ 1 ];
1033 $record->{ "IDENT" } = $fields[ 2 ];
1034 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1035 $record->{ "MISMATCHES" } = $fields[ 4 ];
1036 $record->{ "GAPS" } = $fields[ 5 ];
1037 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1038 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1039 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1040 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1041 $record->{ "E_VAL" } = $fields[ 10 ];
1042 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1044 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1046 $record->{ "STRAND" } = '-';
1048 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1052 $record->{ "STRAND" } = '+';
1055 Maasha::Biopieces::put_record( $record, $out );
1057 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1067 close $data_in if $data_in;
1071 sub script_read_embl
1073 # Martin A. Hansen, August 2007.
1077 my ( $in, # handle to in stream
1078 $out, # handle to out stream
1079 $options, # options hash
1084 my ( %options2, $file, $data_in, $num, $entry, $record );
1086 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1087 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1088 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1090 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1091 Maasha::Biopieces::put_record( $record, $out );
1096 foreach $file ( @{ $options->{ "files" } } )
1098 $data_in = Maasha::Common::read_open( $file );
1100 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1102 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1104 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1106 $record_copy = dclone $record;
1108 delete $record_copy->{ "FT" };
1110 Maasha::Biopieces::put_record( $record_copy, $out );
1112 delete $record_copy->{ "SEQ" };
1114 foreach $feat ( keys %{ $record->{ "FT" } } )
1116 $record_copy->{ "FEAT_TYPE" } = $feat;
1118 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1120 foreach $qual ( keys %{ $feat2 } )
1122 $qual_val = join "; ", @{ $feat2->{ $qual } };
1127 $record_copy->{ $qual } = $qual_val;
1130 Maasha::Biopieces::put_record( $record_copy, $out );
1134 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1144 close $data_in if $data_in;
1148 sub script_read_stockholm
1150 # Martin A. Hansen, August 2007.
1152 # Read Stockholm format.
1154 my ( $in, # handle to in stream
1155 $out, # handle to out stream
1156 $options, # options hash
1161 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1163 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1164 Maasha::Biopieces::put_record( $record, $out );
1169 foreach $file ( @{ $options->{ "files" } } )
1171 $data_in = Maasha::Common::read_open( $file );
1173 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1175 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1179 foreach $key ( keys %{ $record->{ "GF" } } ) {
1180 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1183 $record_anno->{ "ALIGN" } = $num;
1185 Maasha::Biopieces::put_record( $record_anno, $out );
1187 foreach $seq ( @{ $record->{ "ALIGN" } } )
1189 undef $record_align;
1192 SEQ_NAME => $seq->[ 0 ],
1196 Maasha::Biopieces::put_record( $record_align, $out );
1199 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1209 close $data_in if $data_in;
1213 sub script_read_phastcons
1215 # Martin A. Hansen, December 2007.
1217 # Read PhastCons format.
1219 my ( $in, # handle to in stream
1220 $out, # handle to out stream
1221 $options, # options hash
1226 my ( $data_in, $file, $num, $entry, @records, $record );
1228 $options->{ "min" } ||= 10;
1229 $options->{ "dist" } ||= 25;
1230 $options->{ "threshold" } ||= 0.8;
1231 $options->{ "gap" } ||= 5;
1233 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1234 Maasha::Biopieces::put_record( $record, $out );
1239 foreach $file ( @{ $options->{ "files" } } )
1241 $data_in = Maasha::Common::read_open( $file );
1243 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1245 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1247 foreach $record ( @records )
1249 $record->{ "REC_TYPE" } = "BED";
1250 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1252 Maasha::Biopieces::put_record( $record, $out );
1254 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1265 close $data_in if $data_in;
1269 sub script_read_soft
1271 # Martin A. Hansen, December 2007.
1274 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1276 my ( $in, # handle to in stream
1277 $out, # handle to out stream
1278 $options, # options hash
1283 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1285 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1286 Maasha::Biopieces::put_record( $record, $out );
1291 foreach $file ( @{ $options->{ "files" } } )
1293 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1295 $soft_index = Maasha::NCBI::soft_index_file( $file );
1297 $fh = Maasha::Common::read_open( $file );
1299 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1301 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1303 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1305 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1307 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1309 foreach $sample ( @samples )
1312 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1314 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1316 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1318 foreach $record ( @{ $records } )
1320 Maasha::Biopieces::put_record( $record, $out );
1322 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1327 $old_end = $sample->{ "LINE_END" };
1335 close $data_in if $data_in;
1342 # Martin A. Hansen, February 2008.
1345 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1347 my ( $in, # handle to in stream
1348 $out, # handle to out stream
1349 $options, # options hash
1354 my ( $data_in, $file, $fh, $num, $record, $entry );
1356 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1357 Maasha::Biopieces::put_record( $record, $out );
1362 foreach $file ( @{ $options->{ "files" } } )
1364 $fh = Maasha::Common::read_open( $file );
1366 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1368 Maasha::Biopieces::put_record( $entry, $out );
1370 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1380 close $data_in if $data_in;
1384 sub script_read_2bit
1386 # Martin A. Hansen, March 2008.
1388 # Read sequences from 2bit file.
1390 my ( $in, # handle to in stream
1391 $out, # handle to out stream
1392 $options, # options hash
1397 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1399 $mask = 1 if not $options->{ "no_mask" };
1401 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1402 Maasha::Biopieces::put_record( $record, $out );
1407 foreach $file ( @{ $options->{ "files" } } )
1409 $data_in = Maasha::Common::read_open( $file );
1411 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1413 foreach $line ( @{ $toc } )
1415 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1416 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1417 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1419 Maasha::Biopieces::put_record( $record, $out );
1421 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1431 close $data_in if $data_in;
1435 sub script_read_solexa
1437 # Martin A. Hansen, March 2008.
1439 # Read Solexa sequence reads from file.
1441 my ( $in, # handle to in stream
1442 $out, # handle to out stream
1443 $options, # options hash
1448 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1450 $options->{ "format" } ||= "octal";
1451 $options->{ "quality" } ||= 20;
1453 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1454 Maasha::Biopieces::put_record( $record, $out );
1459 foreach $file ( @{ $options->{ "files" } } )
1461 $data_in = Maasha::Common::read_open( $file );
1463 if ( $options->{ "format" } eq "octal" )
1465 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1467 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1469 Maasha::Biopieces::put_record( $record, $out );
1471 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1478 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1480 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1482 Maasha::Biopieces::put_record( $record, $out );
1484 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1495 close $data_in if $data_in;
1499 sub script_read_solid
1501 # Martin A. Hansen, April 2008.
1503 # Read Solid sequence from file.
1505 my ( $in, # handle to in stream
1506 $out, # handle to out stream
1507 $options, # options hash
1512 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1514 $options->{ "quality" } ||= 15;
1516 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1517 Maasha::Biopieces::put_record( $record, $out );
1522 foreach $file ( @{ $options->{ "files" } } )
1524 $data_in = Maasha::Common::read_open( $file );
1526 while ( $line = <$data_in> )
1530 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1532 @scores = split /,/, $seq_qual;
1533 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1535 for ( $i = 0; $i < @seqs; $i++ ) {
1536 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1540 REC_TYPE => 'SOLID',
1541 SEQ_NAME => $seq_name,
1543 SEQ_QUAL => join( ";", @scores ),
1544 SEQ_LEN => length $seq_cs,
1545 SEQ => join( "", @seqs ),
1546 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1549 Maasha::Biopieces::put_record( $record, $out );
1551 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1561 close $data_in if $data_in;
1565 sub script_read_mysql
1567 # Martin A. Hansen, May 2008.
1569 # Read a MySQL query into stream.
1571 my ( $in, # handle to in stream
1572 $out, # handle to out stream
1573 $options, # options hash
1578 my ( $record, $dbh, $results );
1580 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1581 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1583 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1584 Maasha::Biopieces::put_record( $record, $out );
1587 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1589 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1591 Maasha::SQL::disconnect( $dbh );
1593 map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1597 sub script_read_ucsc_config
1599 # Martin A. Hansen, November 2008.
1601 # Read track entries from UCSC Genome Browser '.ra' files.
1603 my ( $in, # handle to in stream
1604 $out, # handle to out stream
1605 $options, # options hash
1610 my ( $record, $file, $data_in, $entry, $num );
1612 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1613 Maasha::Biopieces::put_record( $record, $out );
1618 foreach $file ( @{ $options->{ "files" } } )
1620 $data_in = Maasha::Common::read_open( $file );
1622 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
1624 $record->{ 'REC_TYPE' } = "UCSC Config";
1626 Maasha::Biopieces::put_record( $record, $out );
1628 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1638 close $data_in if $data_in;
1642 sub script_length_seq
1644 # Martin A. Hansen, August 2007.
1646 # Determine the length of sequences in stream.
1648 my ( $in, # handle to in stream
1649 $out, # handle to out stream
1650 $options, # options hash
1655 my ( $record, $total );
1657 while ( $record = Maasha::Biopieces::get_record( $in ) )
1659 if ( $record->{ "SEQ" } )
1661 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1662 $total += $record->{ "SEQ_LEN" };
1665 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1668 Maasha::Biopieces::put_record( { TOTAL_SEQ_LEN => $total }, $out );
1672 sub script_complexity_seq
1674 # Martin A. Hansen, May 2008.
1676 # Generates an index calculated as the most common di-residue over
1677 # the sequence length for all sequences in stream.
1679 my ( $in, # handle to in stream
1680 $out, # handle to out stream
1685 my ( $record, $index );
1687 while ( $record = Maasha::Biopieces::get_record( $in ) )
1689 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1691 Maasha::Biopieces::put_record( $record, $out );
1696 sub script_oligo_freq
1698 # Martin A. Hansen, August 2007.
1700 # Determine the length of sequences in stream.
1702 my ( $in, # handle to in stream
1703 $out, # handle to out stream
1704 $options, # options hash
1709 my ( $record, %oligos, @freq_table );
1711 $options->{ "word_size" } ||= 7;
1713 while ( $record = Maasha::Biopieces::get_record( $in ) )
1715 if ( $record->{ "SEQ" } )
1717 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
1719 if ( not $options->{ "all" } )
1721 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1723 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1729 Maasha::Biopieces::put_record( $record, $out );
1732 if ( $options->{ "all" } )
1734 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1736 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1741 sub script_remove_indels
1743 # Martin A. Hansen, August 2007.
1745 # Remove indels from sequences in stream.
1747 my ( $in, # handle to in stream
1748 $out, # handle to out stream
1755 while ( $record = Maasha::Biopieces::get_record( $in ) )
1757 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
1759 Maasha::Biopieces::put_record( $record, $out );
1764 sub script_transliterate_seq
1766 # Martin A. Hansen, August 2007.
1768 # Transliterate chars from sequence in record.
1770 my ( $in, # handle to in stream
1771 $out, # handle to out stream
1772 $options, # options hash
1777 my ( $record, $search, $replace, $delete );
1779 $search = $options->{ "search" } || "";
1780 $replace = $options->{ "replace" } || "";
1781 $delete = $options->{ "delete" } || "";
1783 while ( $record = Maasha::Biopieces::get_record( $in ) )
1785 if ( $record->{ "SEQ" } )
1787 if ( $search and $replace ) {
1788 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
1789 } elsif ( $delete ) {
1790 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
1794 Maasha::Biopieces::put_record( $record, $out );
1799 sub script_transliterate_vals
1801 # Martin A. Hansen, April 2008.
1803 # Transliterate chars from values in record.
1805 my ( $in, # handle to in stream
1806 $out, # handle to out stream
1807 $options, # options hash
1812 my ( $record, $search, $replace, $delete, $key );
1814 $search = $options->{ "search" } || "";
1815 $replace = $options->{ "replace" } || "";
1816 $delete = $options->{ "delete" } || "";
1818 while ( $record = Maasha::Biopieces::get_record( $in ) )
1820 foreach $key ( @{ $options->{ "keys" } } )
1822 if ( exists $record->{ $key } )
1824 if ( $search and $replace ) {
1825 eval "\$record->{ $key } =~ tr/$search/$replace/";
1826 } elsif ( $delete ) {
1827 eval "\$record->{ $key } =~ tr/$delete//d";
1832 Maasha::Biopieces::put_record( $record, $out );
1837 sub script_translate_seq
1839 # Martin A. Hansen, February 2008.
1841 # Translate DNA sequence into protein sequence.
1843 my ( $in, # handle to in stream
1844 $out, # handle to out stream
1845 $options, # options hash
1850 my ( $record, $frame, %new_record );
1852 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
1854 while ( $record = Maasha::Biopieces::get_record( $in ) )
1856 if ( $record->{ "SEQ" } )
1858 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
1860 foreach $frame ( @{ $options->{ "frames" } } )
1862 %new_record = %{ $record };
1864 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
1865 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
1866 $new_record{ "FRAME" } = $frame;
1868 Maasha::Biopieces::put_record( \%new_record, $out );
1874 Maasha::Biopieces::put_record( $record, $out );
1880 sub script_get_genome_align
1882 # Martin A. Hansen, April 2008.
1884 # Gets a subalignment from a multiple genome alignment.
1886 my ( $in, # handle to in stream
1887 $out, # handle to out stream
1888 $options, # options hash
1893 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
1895 $options->{ "strand" } ||= "+";
1899 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
1901 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
1903 $beg = $options->{ "beg" } - 1;
1905 if ( $options->{ "end" } ) {
1906 $end = $options->{ "end" };
1907 } elsif ( $options->{ "len" } ) {
1908 $end = $beg + $options->{ "len" };
1911 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
1913 foreach $entry ( @{ $align } )
1915 $entry->{ "CHR" } = $record->{ "CHR" };
1916 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
1917 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
1918 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
1919 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
1920 $entry->{ "SCORE" } = $record->{ "SCORE" };
1922 Maasha::Biopieces::put_record( $entry, $out );
1926 while ( $record = Maasha::Biopieces::get_record( $in ) )
1928 if ( $record->{ "REC_TYPE" } eq "BED" )
1930 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
1932 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
1934 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
1936 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
1938 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
1940 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
1942 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
1945 foreach $entry ( @{ $align } )
1947 $entry->{ "CHR" } = $record->{ "CHR" };
1948 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
1949 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
1950 $entry->{ "STRAND" } = $record->{ "STRAND" };
1951 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
1952 $entry->{ "SCORE" } = $record->{ "SCORE" };
1954 Maasha::Biopieces::put_record( $entry, $out );
1962 sub script_get_genome_phastcons
1964 # Martin A. Hansen, February 2008.
1966 # Get phastcons scores from genome intervals.
1968 my ( $in, # handle to in stream
1969 $out, # handle to out stream
1970 $options, # options hash
1975 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
1977 $options->{ "flank" } ||= 0;
1979 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
1980 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
1982 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
1983 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
1985 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
1987 $options->{ "beg" } -= 1; # request is 1-based
1988 $options->{ "end" } -= 1; # request is 1-based
1990 if ( $options->{ "len" } ) {
1991 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
1994 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
1996 $record->{ "CHR" } = $options->{ "chr" };
1997 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
1998 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2000 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2001 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2003 Maasha::Biopieces::put_record( $record, $out );
2006 while ( $record = Maasha::Biopieces::get_record( $in ) )
2008 if ( $record->{ "REC_TYPE" } eq "BED" )
2010 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2012 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2014 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2016 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2018 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2021 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2022 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2024 Maasha::Biopieces::put_record( $record, $out );
2027 close $fh_phastcons if $fh_phastcons;
2033 # Martin A. Hansen, December 2007.
2035 # Folds sequences in stream into secondary structures.
2037 my ( $in, # handle to in stream
2038 $out, # handle to out stream
2043 my ( $record, $type, $struct, $index );
2045 while ( $record = Maasha::Biopieces::get_record( $in ) )
2047 if ( $record->{ "SEQ" } )
2050 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2053 if ( $type ne "protein" )
2055 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2056 $record->{ "SEC_STRUCT" } = $struct;
2057 $record->{ "FREE_ENERGY" } = $index;
2058 $record->{ "SCORE" } = abs int $index;
2059 $record->{ "SIZE" } = length $struct;
2060 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2064 Maasha::Biopieces::put_record( $record, $out );
2071 # Martin A. Hansen, February 2008.
2073 # Using the first sequence in stream as reference, tile
2074 # all subsequent sequences based on pairwise alignments.
2076 my ( $in, # handle to in stream
2077 $out, # handle to out stream
2078 $options, # options hash
2083 my ( $record, $first, $ref_entry, @entries );
2087 while ( $record = Maasha::Biopieces::get_record( $in ) )
2089 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2093 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2099 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2104 Maasha::Biopieces::put_record( $record, $out );
2108 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2110 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2114 sub script_invert_align
2116 # Martin A. Hansen, February 2008.
2118 # Inverts an alignment showing only non-mathing residues
2119 # using the first sequence as reference.
2121 my ( $in, # handle to in stream
2122 $out, # handle to out stream
2123 $options, # options hash
2128 my ( $record, @entries );
2130 while ( $record = Maasha::Biopieces::get_record( $in ) )
2132 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2134 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2138 Maasha::Biopieces::put_record( $record, $out );
2142 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
2144 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2148 sub script_patscan_seq
2150 # Martin A. Hansen, August 2007.
2152 # Locates patterns in sequences using scan_for_matches.
2154 my ( $in, # handle to in stream
2155 $out, # handle to out stream
2156 $options, # options hash
2161 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
2163 if ( $options->{ "patterns" } ) {
2164 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
2165 } elsif ( -f $options->{ "patterns_in" } ) {
2166 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
2169 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
2171 push @args, "-c" if $options->{ "comp" };
2172 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
2173 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
2175 $seq_file = "$BP_TMP/patscan.seq";
2176 $pat_file = "$BP_TMP/patscan.pat";
2177 $out_file = "$BP_TMP/patscan.out";
2179 $fh_out = Maasha::Common::write_open( $seq_file );
2183 while ( $record = Maasha::Biopieces::get_record( $in ) )
2185 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
2187 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2189 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
2191 $head_hash{ $i } = $record->{ "SEQ_NAME" };
2199 $arg = join " ", @args;
2200 $arg .= " -p" if $type eq "protein";
2202 foreach $pattern ( @{ $patterns } )
2204 $fh_out = Maasha::Common::write_open( $pat_file );
2206 print $fh_out "$pattern\n";
2210 if ( $options->{ 'genome' } ) {
2211 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
2212 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
2214 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
2215 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
2218 $fh_in = Maasha::Common::read_open( $out_file );
2220 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
2222 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
2224 if ( $options->{ 'genome' } )
2226 $result->{ "CHR" } = $result->{ "S_ID" };
2227 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
2228 $result->{ "CHR_END" } = $result->{ "S_END" };
2230 delete $result->{ "S_ID" };
2231 delete $result->{ "S_BEG" };
2232 delete $result->{ "S_END" };
2236 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
2239 Maasha::Biopieces::put_record( $result, $out );
2253 # Martin A. Hansen, July 2008.
2255 # soap sequences in stream against a given file or genome.
2257 my ( $in, # handle to in stream
2258 $out, # handle to out stream
2259 $options, # options hash
2264 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
2266 $options->{ "seed_size" } ||= 10;
2267 $options->{ "mismatches" } ||= 2;
2268 $options->{ "gap_size" } ||= 0;
2269 $options->{ "cpus" } ||= 1;
2271 if ( $options->{ "genome" } ) {
2272 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2275 $tmp_in = "$BP_TMP/soap_query.seq";
2276 $tmp_out = "$BP_TMP/soap.result";
2278 $fh_out = Maasha::Common::write_open( $tmp_in );
2282 while ( $record = Maasha::Biopieces::get_record( $in ) )
2284 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2286 Maasha::Fasta::put_entry( $entry, $fh_out );
2291 Maasha::Biopieces::put_record( $record, $out );
2299 "-s $options->{ 'seed_size' }",
2302 "-v $options->{ 'mismatches' }",
2303 "-g $options->{ 'gap_size' }",
2304 "-p $options->{ 'cpus' }",
2305 "-d $options->{ 'in_file' }",
2309 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
2311 Maasha::Common::run( "soap", $args, 1 );
2315 $fh_out = Maasha::Common::read_open( $tmp_out );
2319 while ( $line = <$fh_out> )
2323 @fields = split /\t/, $line;
2325 $record->{ "REC_TYPE" } = "SOAP";
2326 $record->{ "Q_ID" } = $fields[ 0 ];
2327 $record->{ "SCORE" } = $fields[ 3 ];
2328 $record->{ "STRAND" } = $fields[ 6 ];
2329 $record->{ "S_ID" } = $fields[ 7 ];
2330 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
2331 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
2333 Maasha::Biopieces::put_record( $record, $out );
2343 sub script_match_seq
2345 # Martin A. Hansen, August 2007.
2347 # BLATs sequences in stream against a given genome.
2349 my ( $in, # handle to in stream
2350 $out, # handle to out stream
2351 $options, # options hash
2356 my ( $record, @entries, $results );
2358 $options->{ "word_size" } ||= 20;
2359 $options->{ "direction" } ||= "both";
2361 while ( $record = Maasha::Biopieces::get_record( $in ) )
2363 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2364 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2367 Maasha::Biopieces::put_record( $record, $out );
2370 if ( @entries == 1 )
2372 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
2374 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
2376 elsif ( @entries == 2 )
2378 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
2380 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
2385 sub script_create_vmatch_index
2387 # Martin A. Hansen, January 2008.
2389 # Create a vmatch index from sequences in the stream.
2391 my ( $in, # handle to in stream
2392 $out, # handle to out stream
2393 $options, # options hash
2398 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
2400 if ( $options->{ "index_name" } )
2402 $file_tmp = $options->{ 'index_name' };
2403 $fh_tmp = Maasha::Common::write_open( $file_tmp );
2406 while ( $record = Maasha::Biopieces::get_record( $in ) )
2408 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2410 Maasha::Fasta::put_entry( $entry, $fh_tmp );
2412 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
2415 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2418 if ( $options->{ "index_name" } )
2422 if ( $type eq "protein" ) {
2423 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
2425 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
2433 sub script_write_bed
2435 # Martin A. Hansen, August 2007.
2437 # Write BED format for the UCSC genome browser using records in stream.
2439 my ( $in, # handle to in stream
2440 $out, # handle to out stream
2441 $options, # options hash
2446 my ( $cols, $fh, $record, $bed_entry, $new_record );
2448 $cols = $options->{ 'cols' }->[ 0 ];
2450 $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
2452 while ( $record = Maasha::Biopieces::get_record( $in ) )
2454 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
2456 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
2457 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
2460 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
2467 sub script_write_psl
2469 # Martin A. Hansen, August 2007.
2471 # Write PSL output from stream.
2473 my ( $in, # handle to in stream
2474 $out, # handle to out stream
2475 $options, # options hash
2480 my ( $fh, $record, @output, $first );
2484 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
2486 while ( $record = Maasha::Biopieces::get_record( $in ) )
2488 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2490 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
2492 Maasha::UCSC::psl_put_header( $fh ) if $first;
2493 Maasha::UCSC::psl_put_entry( $record, $fh );
2502 sub script_write_fixedstep
2504 # Martin A. Hansen, Juli 2008.
2506 # Write fixedStep entries from recrods in the stream.
2508 my ( $in, # handle to in stream
2509 $out, # handle to out stream
2510 $options, # options hash
2515 my ( $fh, $record, $entry );
2517 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
2519 while ( $record = Maasha::Biopieces::get_record( $in ) )
2521 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
2522 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
2525 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2532 sub script_write_2bit
2534 # Martin A. Hansen, March 2008.
2536 # Write sequence entries from stream in 2bit format.
2538 my ( $in, # handle to in stream
2539 $out, # handle to out stream
2540 $options, # options hash
2545 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
2547 $mask = 1 if not $options->{ "no_mask" };
2549 $tmp_file = "$BP_TMP/write_2bit.fna";
2550 $fh_tmp = Maasha::Common::write_open( $tmp_file );
2552 $fh_out = write_stream( $options->{ "data_out" } );
2554 while ( $record = Maasha::Biopieces::get_record( $in ) )
2556 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
2557 Maasha::Fasta::put_entry( $entry, $fh_tmp );
2560 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2565 $fh_in = Maasha::Common::read_open( $tmp_file );
2567 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
2576 sub script_write_solid
2578 # Martin A. Hansen, April 2008.
2580 # Write di-base encoded Solid sequence from entries in stream.
2582 my ( $in, # handle to in stream
2583 $out, # handle to out stream
2584 $options, # options hash
2589 my ( $record, $fh, $entry );
2591 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
2593 while ( $record = Maasha::Biopieces::get_record( $in ) )
2595 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2597 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
2599 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
2602 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2609 sub script_write_ucsc_config
2611 # Martin A. Hansen, November 2008.
2613 # Write UCSC Genome Broser configuration (.ra file type) from
2614 # records in the stream.
2616 my ( $in, # handle to in stream
2617 $out, # handle to out stream
2618 $options, # options hash
2623 my ( $record, $fh );
2625 $fh = write_stream( $options->{ "data_out" } );
2627 while ( $record = Maasha::Biopieces::get_record( $in ) )
2629 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
2631 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2638 sub script_plot_seqlogo
2640 # Martin A. Hansen, August 2007.
2642 # Calculates and writes a sequence logo for alignments.
2644 my ( $in, # handle to in stream
2645 $out, # handle to out stream
2646 $options, # options hash
2651 my ( $record, @entries, $logo, $fh );
2653 while ( $record = Maasha::Biopieces::get_record( $in ) )
2655 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2656 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2659 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2662 $logo = Maasha::Plot::seq_logo( \@entries );
2664 $fh = write_stream( $options->{ "data_out" } );
2672 sub script_plot_phastcons_profiles
2674 # Martin A. Hansen, January 2008.
2676 # Plots PhastCons profiles.
2678 my ( $in, # handle to in stream
2679 $out, # handle to out stream
2680 $options, # options hash
2685 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
2687 $options->{ "title" } ||= "PhastCons Profiles";
2689 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2690 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2692 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
2693 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2695 while ( $record = Maasha::Biopieces::get_record( $in ) )
2697 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2699 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
2700 $record->{ "CHR_BEG" },
2701 $record->{ "CHR_END" },
2702 $options->{ "flank" } );
2704 push @{ $AoA }, [ @{ $scores } ];
2707 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2710 Maasha::UCSC::phastcons_normalize( $AoA );
2712 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
2713 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
2715 $AoA = Maasha::Matrix::matrix_flip( $AoA );
2717 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
2719 $fh = write_stream( $options->{ "data_out" } );
2721 print $fh "$_\n" foreach @{ $plot };
2727 sub script_head_records
2729 # Martin A. Hansen, August 2007.
2731 # Display the first sequences in stream.
2733 my ( $in, # handle to in stream
2734 $out, # handle to out stream
2735 $options, # options hash
2740 my ( $record, $count );
2742 $options->{ "num" } ||= 10;
2746 while ( $record = Maasha::Biopieces::get_record( $in ) )
2750 Maasha::Biopieces::put_record( $record, $out );
2752 last if $count == $options->{ "num" };
2757 sub script_remove_keys
2759 # Martin A. Hansen, August 2007.
2761 # Remove keys from stream.
2763 my ( $in, # handle to in stream
2764 $out, # handle to out stream
2765 $options, # options hash
2770 my ( $record, $new_record );
2772 while ( $record = Maasha::Biopieces::get_record( $in ) )
2774 if ( $options->{ "keys" } )
2776 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
2778 elsif ( $options->{ "save_keys" } )
2780 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
2782 $record = $new_record;
2785 Maasha::Biopieces::put_record( $record, $out ) if keys %{ $record };
2790 sub script_remove_adaptor
2792 # Martin A. Hansen, August 2008.
2794 # Find and remove adaptor from sequences in the stream.
2796 my ( $in, # handle to in stream
2797 $out, # handle to out stream
2798 $options, # options hash
2803 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
2805 $options->{ "remove" } ||= "after";
2807 $max_mismatch = $options->{ "mismatches" } || 0;
2808 $offset = $options->{ "offset" };
2810 if ( not defined $offset ) {
2816 $adaptor = uc $options->{ "adaptor" };
2817 $adaptor_len = length $adaptor;
2819 while ( $record = Maasha::Biopieces::get_record( $in ) )
2821 if ( $record->{ "SEQ" } )
2823 $seq = uc $record->{ "SEQ" };
2824 $seq_len = length $seq;
2826 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
2828 $record->{ "ADAPTOR_POS" } = $pos;
2830 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
2832 if ( $options->{ "remove" } eq "after" )
2834 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
2835 $record->{ "SEQ_LEN" } = $pos;
2839 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
2840 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2844 Maasha::Biopieces::put_record( $record, $out );
2848 Maasha::Biopieces::put_record( $record, $out );
2854 sub script_remove_mysql_tables
2856 # Martin A. Hansen, November 2008.
2858 # Remove MySQL tables from values in stream.
2860 my ( $in, # handle to in stream
2861 $out, # handle to out stream
2862 $options, # options hash
2867 my ( $record, %table_hash, $dbh, $table );
2869 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2870 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2872 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
2874 while ( $record = Maasha::Biopieces::get_record( $in ) )
2876 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
2878 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
2881 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2883 foreach $table ( sort keys %table_hash )
2885 if ( Maasha::SQL::table_exists( $dbh, $table ) )
2887 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
2888 Maasha::SQL::delete_table( $dbh, $table );
2889 print STDERR "done.\n" if $options->{ 'verbose' };
2893 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
2897 Maasha::SQL::disconnect( $dbh );
2901 sub script_remove_ucsc_tracks
2903 # Martin A. Hansen, November 2008.
2905 # Remove track from MySQL tables and config file.
2907 my ( $in, # handle to in stream
2908 $out, # handle to out stream
2909 $options, # options hash
2914 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
2916 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
2917 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
2918 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
2920 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
2922 while ( $record = Maasha::Biopieces::get_record( $in ) )
2924 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
2926 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
2929 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
2931 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
2932 push @tracks, $track;
2937 foreach $track ( @tracks )
2939 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
2940 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
2942 push @new_tracks, $track;
2946 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
2948 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
2950 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
2954 # ---- locate track in database ----
2956 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2958 foreach $track ( sort keys %track_hash )
2960 if ( Maasha::SQL::table_exists( $dbh, $track ) )
2962 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
2963 Maasha::SQL::delete_table( $dbh, $track );
2964 print STDERR "done.\n" if $options->{ 'verbose' };
2968 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
2972 Maasha::SQL::disconnect( $dbh );
2974 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
2978 sub script_rename_keys
2980 # Martin A. Hansen, August 2007.
2982 # Rename keys in stream.
2984 my ( $in, # handle to in stream
2985 $out, # handle to out stream
2986 $options, # options hash
2993 while ( $record = Maasha::Biopieces::get_record( $in ) )
2995 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
2997 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
2999 delete $record->{ $options->{ "keys" }->[ 0 ] };
3002 Maasha::Biopieces::put_record( $record, $out );
3007 sub script_uniq_vals
3009 # Martin A. Hansen, August 2007.
3011 # Find unique values in stream.
3013 my ( $in, # handle to in stream
3014 $out, # handle to out stream
3015 $options, # options hash
3020 my ( %hash, $record );
3022 while ( $record = Maasha::Biopieces::get_record( $in ) )
3024 if ( $record->{ $options->{ "key" } } )
3026 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
3028 Maasha::Biopieces::put_record( $record, $out );
3030 $hash{ $record->{ $options->{ "key" } } } = 1;
3032 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
3034 Maasha::Biopieces::put_record( $record, $out );
3038 $hash{ $record->{ $options->{ "key" } } } = 1;
3043 Maasha::Biopieces::put_record( $record, $out );
3049 sub script_merge_vals
3051 # Martin A. Hansen, August 2007.
3053 # Rename keys in stream.
3055 my ( $in, # handle to in stream
3056 $out, # handle to out stream
3057 $options, # options hash
3062 my ( $record, @join, $i );
3064 $options->{ "delimit" } ||= '_';
3066 while ( $record = Maasha::Biopieces::get_record( $in ) )
3068 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3070 @join = $record->{ $options->{ "keys" }->[ 0 ] };
3072 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
3073 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
3076 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
3079 Maasha::Biopieces::put_record( $record, $out );
3084 sub script_merge_records
3086 # Martin A. Hansen, July 2008.
3088 # Merges records in the stream based on identical values of two given keys.
3090 my ( $in, # handle to in stream
3091 $out, # handle to out stream
3092 $options, # options hash
3097 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
3098 $num1, $num2, $num, $cmp, $i );
3100 $merge = $options->{ "merge" } || "AandB";
3102 $file1 = "$BP_TMP/merge_records1.tmp";
3103 $file2 = "$BP_TMP/merge_records2.tmp";
3105 $fh1 = Maasha::Common::write_open( $file1 );
3106 $fh2 = Maasha::Common::write_open( $file2 );
3108 $key1 = $options->{ "keys" }->[ 0 ];
3109 $key2 = $options->{ "keys" }->[ 1 ];
3111 $num = $key2 =~ s/n$//;
3115 while ( $record = Maasha::Biopieces::get_record( $in ) )
3117 if ( exists $record->{ $key1 } )
3120 @vals1 = $record->{ $key1 };
3122 delete $record->{ $key1 };
3124 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
3126 print $fh1 join( "\t", @vals1 ), "\n";
3130 elsif ( exists $record->{ $key2 } )
3133 @vals2 = $record->{ $key2 };
3135 delete $record->{ $key2 };
3137 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
3139 print $fh2 join( "\t", @vals2 ), "\n";
3150 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3151 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3155 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3156 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3159 $fh1 = Maasha::Common::read_open( $file1 );
3160 $fh2 = Maasha::Common::read_open( $file2 );
3162 @vals1 = Maasha::Common::get_fields( $fh1 );
3163 @vals2 = Maasha::Common::get_fields( $fh2 );
3165 while ( $num1 > 0 and $num2 > 0 )
3170 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
3172 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
3177 if ( $merge =~ /^(AorB|AnotB)$/ )
3179 for ( $i = 0; $i < @keys1; $i++ ) {
3180 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3183 Maasha::Biopieces::put_record( $record, $out );
3186 @vals1 = Maasha::Common::get_fields( $fh1 );
3191 if ( $merge =~ /^(BorA|BnotA)$/ )
3193 for ( $i = 0; $i < @keys2; $i++ ) {
3194 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3197 Maasha::Biopieces::put_record( $record, $out );
3200 @vals2 = Maasha::Common::get_fields( $fh2 );
3205 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
3207 for ( $i = 0; $i < @keys1; $i++ ) {
3208 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3211 for ( $i = 1; $i < @keys2; $i++ ) {
3212 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3215 Maasha::Biopieces::put_record( $record, $out );
3218 @vals1 = Maasha::Common::get_fields( $fh1 );
3219 @vals2 = Maasha::Common::get_fields( $fh2 );
3231 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
3235 for ( $i = 0; $i < @keys1; $i++ ) {
3236 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3239 Maasha::Biopieces::put_record( $record, $out );
3242 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
3246 for ( $i = 0; $i < @keys2; $i++ ) {
3247 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3250 Maasha::Biopieces::put_record( $record, $out );
3257 # Martin A. Hansen, June 2008.
3261 my ( $in, # handle to in stream
3262 $out, # handle to out stream
3263 $options, # options hash
3268 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
3270 while ( $record = Maasha::Biopieces::get_record( $in ) )
3274 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3276 push @rows, $record->{ $key };
3280 push @matrix, [ @rows ];
3285 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
3287 foreach $row ( @matrix )
3289 for ( $i = 0; $i < @{ $row }; $i++ ) {
3290 $record->{ "V$i" } = $row->[ $i ];
3293 Maasha::Biopieces::put_record( $record, $out );
3298 sub script_sort_records
3300 # Martin A. Hansen, August 2007.
3302 # Sort to sort records according to keys.
3304 my ( $in, # handle to in stream
3305 $out, # handle to out stream
3306 $options, # options hash
3311 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
3313 foreach $key ( @{ $options->{ "keys" } } )
3315 if ( $key =~ s/n$// ) {
3316 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
3318 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
3322 $sort_str = join " or ", @sort_cmd;
3323 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
3325 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
3326 push @records, $record;
3329 @records = sort $sort_sub @records;
3331 if ( $options->{ "reverse" } )
3333 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
3334 Maasha::Biopieces::put_record( $records[ $i ], $out );
3339 for ( $i = 0; $i < scalar @records; $i++ ) {
3340 Maasha::Biopieces::put_record( $records[ $i ], $out );
3346 sub script_plot_histogram
3348 # Martin A. Hansen, September 2007.
3350 # Plot a simple histogram for a given key using GNU plot.
3352 my ( $in, # handle to in stream
3353 $out, # handle to out stream
3354 $options, # options hash
3359 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
3361 $options->{ "title" } ||= "Histogram";
3362 $options->{ "sort" } ||= "num";
3364 while ( $record = Maasha::Biopieces::get_record( $in ) )
3366 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
3368 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3371 if ( $options->{ "sort" } eq "num" ) {
3372 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
3374 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
3377 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
3379 $fh = write_stream( $options->{ "data_out" } );
3381 print $fh "$_\n" foreach @{ $result };
3387 sub script_plot_lendist
3389 # Martin A. Hansen, August 2007.
3391 # Plot length distribution using GNU plot.
3393 my ( $in, # handle to in stream
3394 $out, # handle to out stream
3395 $options, # options hash
3400 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
3402 $options->{ "title" } ||= "Length Distribution";
3404 while ( $record = Maasha::Biopieces::get_record( $in ) )
3406 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
3408 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3411 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
3413 for ( $i = 0; $i < $max; $i++ ) {
3414 push @data_list, [ $i, $data_hash{ $i } || 0 ];
3417 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
3419 $fh = write_stream( $options->{ "data_out" } );
3421 print $fh "$_\n" foreach @{ $result };
3427 sub script_plot_chrdist
3429 # Martin A. Hansen, August 2007.
3431 # Plot chromosome distribution using GNU plot.
3433 my ( $in, # handle to in stream
3434 $out, # handle to out stream
3435 $options, # options hash
3440 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
3442 $options->{ "title" } ||= "Chromosome Distribution";
3444 while ( $record = Maasha::Biopieces::get_record( $in ) )
3446 if ( $record->{ "CHR" } ) { # generic
3447 $data_hash{ $record->{ "CHR" } }++;
3448 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
3449 $data_hash{ $record->{ "S_ID" } }++;
3450 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
3451 $data_hash{ $record->{ "S_ID" } }++;
3452 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
3453 $data_hash{ $record->{ "S_ID" } }++;
3456 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3459 foreach $elem ( keys %data_hash )
3463 $sort_key =~ s/chr//i;
3465 $sort_key =~ s/^X(.*)/99$1/;
3466 $sort_key =~ s/^Y(.*)/99$1/;
3467 $sort_key =~ s/^Z(.*)/999$1/;
3468 $sort_key =~ s/^M(.*)/9999$1/;
3469 $sort_key =~ s/^U(.*)/99999$1/;
3471 $count = $sort_key =~ tr/_//;
3473 $sort_key =~ s/_.*/"999999" x $count/ex;
3475 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
3478 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
3480 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
3482 $fh = write_stream( $options->{ "data_out" } );
3484 print $fh "$_\n" foreach @{ $result };
3490 sub script_plot_karyogram
3492 # Martin A. Hansen, August 2007.
3494 # Plot hits on karyogram.
3496 my ( $in, # handle to in stream
3497 $out, # handle to out stream
3498 $options, # options hash
3503 my ( %options, $record, @data, $fh, $result, %data_hash );
3505 $options->{ "genome" } ||= "human";
3506 $options->{ "feat_color" } ||= "black";
3508 while ( $record = Maasha::Biopieces::get_record( $in ) )
3510 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3512 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
3515 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3518 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
3520 $fh = write_stream( $options->{ "data_out" } );
3528 sub script_plot_matches
3530 # Martin A. Hansen, August 2007.
3532 # Plot matches in 2D generating a dotplot.
3534 my ( $in, # handle to in stream
3535 $out, # handle to out stream
3536 $options, # options hash
3541 my ( $record, @data, $fh, $result, %data_hash );
3543 $options->{ "direction" } ||= "both";
3545 while ( $record = Maasha::Biopieces::get_record( $in ) )
3547 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
3548 push @data, $record;
3551 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3554 $options->{ "title" } ||= "plot_matches";
3555 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
3556 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
3558 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
3560 $fh = write_stream( $options->{ "data_out" } );
3562 print $fh "$_\n" foreach @{ $result };
3568 sub script_length_vals
3570 # Martin A. Hansen, August 2007.
3572 # Determine the length of the value for given keys.
3574 my ( $in, # handle to in stream
3575 $out, # handle to out stream
3576 $options, # options hash
3581 my ( $record, $key );
3583 while ( $record = Maasha::Biopieces::get_record( $in ) )
3585 foreach $key ( @{ $options->{ "keys" } } )
3587 if ( $record->{ $key } ) {
3588 $record->{ $key . "_LEN" } = length $record->{ $key };
3592 Maasha::Biopieces::put_record( $record, $out );
3599 # Martin A. Hansen, August 2007.
3601 # Calculates the sums for values of given keys.
3603 my ( $in, # handle to in stream
3604 $out, # handle to out stream
3605 $options, # options hash
3610 my ( $record, $key, %sum_hash, $fh );
3612 while ( $record = Maasha::Biopieces::get_record( $in ) )
3614 foreach $key ( @{ $options->{ "keys" } } )
3616 if ( $record->{ $key } ) {
3617 $sum_hash{ $key } += $record->{ $key };
3621 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3624 $fh = write_stream( $options->{ "data_out" } );
3626 foreach $key ( @{ $options->{ "keys" } } ) {
3627 Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
3634 sub script_mean_vals
3636 # Martin A. Hansen, August 2007.
3638 # Calculate the mean of values of given keys.
3640 my ( $in, # handle to in stream
3641 $out, # handle to out stream
3642 $options, # options hash
3647 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
3649 while ( $record = Maasha::Biopieces::get_record( $in ) )
3651 foreach $key ( @{ $options->{ "keys" } } )
3653 if ( $record->{ $key } )
3655 $sum_hash{ $key } += $record->{ $key };
3656 $count_hash{ $key }++;
3660 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3663 $fh = write_stream( $options->{ "data_out" } );
3665 foreach $key ( @{ $options->{ "keys" } } )
3667 if ( $count_hash{ $key } ) {
3668 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
3673 Maasha::Biopieces::put_record( { $key . "_MEAN" => $mean } , $fh );
3680 sub script_median_vals
3682 # Martin A. Hansen, March 2008.
3684 # Calculate the median values of given keys.
3686 my ( $in, # handle to in stream
3687 $out, # handle to out stream
3688 $options, # options hash
3693 my ( $record, $key, %median_hash, $median, $fh );
3695 while ( $record = Maasha::Biopieces::get_record( $in ) )
3697 foreach $key ( @{ $options->{ "keys" } } ) {
3698 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
3701 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3704 $fh = write_stream( $options->{ "data_out" } );
3706 foreach $key ( @{ $options->{ "keys" } } )
3708 if ( $median_hash{ $key } ) {
3709 $median = Maasha::Calc::median( $median_hash{ $key } );
3714 Maasha::Biopieces::put_record( { $key . "_MEDIAN" => $median } , $fh );
3723 # Martin A. Hansen, February 2008.
3725 # Determine the maximum values of given keys.
3727 my ( $in, # handle to in stream
3728 $out, # handle to out stream
3729 $options, # options hash
3734 my ( $record, $key, $fh, %max_hash, $max_record );
3736 while ( $record = Maasha::Biopieces::get_record( $in ) )
3738 foreach $key ( @{ $options->{ "keys" } } )
3740 if ( $record->{ $key } )
3742 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
3746 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3749 $fh = write_stream( $options->{ "data_out" } );
3751 foreach $key ( @{ $options->{ "keys" } } )
3753 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
3756 Maasha::Biopieces::put_record( $max_record, $fh );
3764 # Martin A. Hansen, February 2008.
3766 # Determine the minimum values of given keys.
3768 my ( $in, # handle to in stream
3769 $out, # handle to out stream
3770 $options, # options hash
3775 my ( $record, $key, $fh, %min_hash, $min_record );
3777 while ( $record = Maasha::Biopieces::get_record( $in ) )
3779 foreach $key ( @{ $options->{ "keys" } } )
3781 if ( defined $record->{ $key } )
3783 if ( exists $min_hash{ $key } ) {
3784 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
3786 $min_hash{ $key } = $record->{ $key };
3791 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3794 $fh = write_stream( $options->{ "data_out" } );
3796 foreach $key ( @{ $options->{ "keys" } } )
3798 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
3801 Maasha::Biopieces::put_record( $min_record, $fh );
3807 sub script_upload_to_ucsc
3809 # Martin A. Hansen, August 2007.
3811 # Calculate the mean of values of given keys.
3813 # This routine has developed into the most ugly hack. Do something!
3815 my ( $in, # handle to in stream
3816 $out, # handle to out stream
3817 $options, # options hash
3822 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
3824 $options->{ "short_label" } ||= $options->{ 'table' };
3825 $options->{ "long_label" } ||= $options->{ 'table' };
3826 $options->{ "group" } ||= $ENV{ "LOGNAME" };
3827 $options->{ "priority" } ||= 1;
3828 $options->{ "visibility" } ||= "pack";
3829 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
3830 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
3832 $file = "$BP_TMP/ucsc_upload.tmp";
3837 $fh_out = Maasha::Common::write_open( $file );
3839 while ( $record = Maasha::Biopieces::get_record( $in ) )
3841 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3843 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
3847 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
3848 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
3851 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3855 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
3856 Maasha::UCSC::psl_put_entry( $record, $fh_out );
3860 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
3862 # chrom chromStart chromEnd name score strand size secStr conf
3866 print $fh_out join ( "\t",
3868 $record->{ "CHR_BEG" },
3869 $record->{ "CHR_END" } + 1,
3870 $record->{ "Q_ID" },
3871 $record->{ "SCORE" },
3872 $record->{ "STRAND" },
3873 $record->{ "SIZE" },
3874 $record->{ "SEC_STRUCT" },
3875 $record->{ "CONF" },
3878 elsif ( $record->{ "REC_TYPE" } eq "BED" )
3881 $columns = $record->{ "BED_COLS" };
3883 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
3884 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
3887 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
3892 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
3893 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
3896 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
3901 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
3903 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
3904 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
3907 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
3912 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
3913 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
3917 if ( $i == $options->{ "chunk_size" } )
3921 if ( $format eq "BED" ) {
3922 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
3923 } elsif ( $format eq "PSL" ) {
3924 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
3933 $fh_out = Maasha::Common::write_open( $file );
3941 if ( exists $options->{ "database" } and $options->{ "table" } )
3943 if ( $format eq "BED" )
3945 $type = "bed $columns";
3947 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
3949 elsif ( $format eq "BED_SS" )
3951 $type = "type bed 6 +";
3953 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
3955 elsif ( $format eq "PSL" )
3959 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
3961 elsif ( $format eq "WIGGLE" )
3963 $options->{ "visibility" } = "full";
3965 $wig_file = "$options->{ 'table' }.wig";
3966 $wib_file = "$options->{ 'table' }.wib";
3968 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
3970 Maasha::Common::dir_create_if_not_exists( $wib_dir );
3972 if ( $options->{ 'verbose' } ) {
3973 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
3975 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
3978 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
3986 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
3991 Maasha::UCSC::ucsc_update_config( $options, $type );
3996 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<