| 1 | =encoding utf8 | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Bio::BPWrapper::SeqManipulations - Functions for L<bioseq> | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Bio::BPWrapper::SeqManipulations; | ||
| 10 | # Set options hash ... | ||
| 11 | initialize(\%opts); | ||
| 12 | write_out(\%opts); | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Bio::BPWrapper::SeqManipulations; | ||
| 17 | |||
| 18 | use strict; # Still on 5.10, so need this for strict | ||
| 19 | use warnings; | ||
| 20 | use 5.010; | ||
| 21 | use Bio::Seq; | ||
| 22 | use Bio::SeqIO; | ||
| 23 | use File::Basename; | ||
| 24 | use Bio::Tools::CodonTable; | ||
| 25 | use Bio::DB::GenBank; | ||
| 26 | use Bio::Tools::SeqStats; | ||
| 27 | use Bio::SeqUtils; | ||
| 28 | use Scalar::Util; | ||
| 29 | use Exporter (); | ||
| 30 | |||
| 31 | if ($ENV{'DEBUG'}) { use Data::Dumper } | ||
| 32 | |||
| 33 | use vars qw(@ISA @EXPORT @EXPORT_OK); | ||
| 34 | |||
| 35 | @ISA = qw(Exporter); | ||
| 36 | |||
| 37 | @EXPORT = qw(initialize can_handle handle_opt write_out | ||
| 38 | print_composition filter_seqs retrieve_seqs remove_gaps | ||
| 39 | print_lengths print_seq_count make_revcom | ||
| 40 | print_subseq restrict_digest anonymize | ||
| 41 | shred_seq count_codons print_gb_gene_feats | ||
| 42 | count_leading_gaps hydroB linearize reloop_at | ||
| 43 | remove_stop parse_orders find_by_order | ||
| 44 | pick_by_order del_by_order find_by_id | ||
| 45 | pick_by_id del_by_id find_by_re | ||
| 46 | pick_by_re del_by_re | ||
| 47 | find_by_ambig pick_by_ambig del_by_ambig find_by_length | ||
| 48 | del_by_length); | ||
| 49 | |||
| 50 | # Package global variables | ||
| 51 | my ($in, $out, $seq, %opts, $filename, $in_format, $out_format); | ||
| 52 | use Bio::BPWrapper; | ||
| 53 | my $VERSION = '1.0'; | ||
| 54 | |||
| 55 | ## For new options, just add an entry into this table with the same key as in | ||
| 56 | ## the GetOpts function in the main program. Make the key be a reference to the handler subroutine (defined below), and test that it works. | ||
| 57 | my %opt_dispatch = ( | ||
| 58 | 'composition' => \&print_composition, | ||
| 59 | 'delete' => \&filter_seqs, | ||
| 60 | 'fetch' => \&retrieve_seqs, | ||
| 61 | 'nogaps' => \&remove_gaps, | ||
| 62 | 'length' => \&print_lengths, | ||
| 63 | 'numseq' => \&print_seq_count, | ||
| 64 | 'pick' => \&filter_seqs, | ||
| 65 | 'revcom' => \&make_revcom, | ||
| 66 | 'subseq' => \&print_subseq, | ||
| 67 | 'translate' => \&reading_frame_ops, | ||
| 68 | 'restrict' => \&restrict_digest, | ||
| 69 | 'anonymize' => \&anonymize, | ||
| 70 | 'break' => \&shred_seq, | ||
| 71 | 'count-codons' => \&count_codons, | ||
| 72 | 'feat2fas' => \&print_gb_gene_feats, | ||
| 73 | 'leadgaps' => \&count_leading_gaps, | ||
| 74 | 'hydroB' => \&hydroB, | ||
| 75 | 'linearize' => \&linearize, | ||
| 76 | 'reloop' => \&reloop_at, | ||
| 77 | 'removestop' => \&remove_stop, | ||
| 78 | 'split-cdhit' => \&split_cdhit, | ||
| 79 | # 'dotplot' => \&draw_dotplot, | ||
| 80 | # 'extract' => \&reading_frame_ops, | ||
| 81 | # 'longest-orf' => \&reading_frame_ops, | ||
| 82 | # 'prefix' => \&anonymize, | ||
| 83 | # 'rename' => \&rename_id, | ||
| 84 | # 'slidingwindow' => \&sliding_window, | ||
| 85 | # 'split' => \&split_seqs, | ||
| 86 | ); | ||
| 87 | |||
| 88 | my %filter_dispatch = ( | ||
| 89 | 'find_by_order' => \&find_by_order, | ||
| 90 | 'pick_by_order' => \&pick_by_order, | ||
| 91 | 'delete_by_order' => \&del_by_order, | ||
| 92 | 'find_by_id' => \&find_by_id, | ||
| 93 | 'pick_by_id' => \&pick_by_id, | ||
| 94 | 'delete_by_id' => \&del_by_id, | ||
| 95 | 'find_by_re' => \&find_by_re, | ||
| 96 | 'pick_by_re' => \&pick_by_re, | ||
| 97 | 'delete_by_re' => \&del_by_re, | ||
| 98 | 'find_by_ambig' => \&find_by_ambig, | ||
| 99 | 'pick_by_ambig' => \&pick_by_ambig, | ||
| 100 | 'delete_by_ambig' => \&del_by_ambig, | ||
| 101 | 'find_by_length' => \&find_by_length, | ||
| 102 | 'pick_by_length' => \&pick_by_length, | ||
| 103 | 'del_by_length' => \&del_by_length, | ||
| 104 | ); | ||
| 105 | |||
| 106 | ##################### initializer & option handlers ################### | ||
| 107 | |||
| 108 | ## TODO Function documentation! | ||
| 109 | ## TODO Formal testing! | ||
| 110 | |||
| 111 | =head1 SUBROUTINES | ||
| 112 | |||
| 113 | =head2 initialize() | ||
| 114 | |||
| 115 | Sets up most of the actions to be performed on an alignment. | ||
| 116 | |||
| 117 | Call this right after setting up an options hash. | ||
| 118 | |||
| 119 | Sets package variables: C<$in>, C<$in_format>, C<$filename>, C<$out_format>, and C<$out>. | ||
| 120 | |||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub initialize { | ||
| 125 | my $opts_ref = shift; | ||
| 126 | Bio::BPWrapper::common_opts($opts_ref); | ||
| 127 | %opts = %{$opts_ref}; | ||
| 128 | |||
| 129 | die "Option 'prefix' requires a value\n" if defined $opts{"prefix"} && $opts{"prefix"} =~ /^$/; | ||
| 130 | |||
| 131 | $filename = shift @ARGV || "STDIN"; # If no more arguments were given on the command line, assume we're getting input from standard input | ||
| 132 | |||
| 133 | $in_format = $opts{"input"} // 'fasta'; | ||
| 134 | |||
| 135 | $in = Bio::SeqIO->new(-format => $in_format, ($filename eq "STDIN")? (-fh => \*STDIN) : (-file => $filename)); | ||
| 136 | |||
| 137 | $out_format = $opts{"output"} // 'fasta'; | ||
| 138 | |||
| 139 | # A change in SeqIO, commit 0e04486ca4cc2e61fd72, means -fh or -file is required | ||
| 140 | $out = Bio::SeqIO->new(-format => $out_format, -fh => \*STDOUT) | ||
| 141 | } | ||
| 142 | |||
| 143 | sub can_handle { | ||
| 144 | my $option = shift; | ||
| 145 | return defined($opt_dispatch{$option}) | ||
| 146 | } | ||
| 147 | |||
| 148 | sub handle_opt { | ||
| 149 | my $option = shift; | ||
| 150 | $opt_dispatch{$option}->($option) # This passes option name to all functions | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 write_out() | ||
| 154 | |||
| 155 | Writes out the sequence file. | ||
| 156 | |||
| 157 | Call this after calling C<#initialize(\%opts)> and processing those options. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub write_out { | ||
| 162 | while ($seq = $in->next_seq()) { $out->write_seq($seq) } | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | ################### subroutine ######################## | ||
| 167 | |||
| 168 | sub split_cdhit { | ||
| 169 | my $cls_file = $opts{'split-cdhit'}; | ||
| 170 | open IN, "<" . $cls_file || die "cdhit clstr file not found: $cls_file\n"; | ||
| 171 | my %clusters; | ||
| 172 | my $cl_id; | ||
| 173 | my @mem; | ||
| 174 | while (<IN>) { | ||
| 175 | my $line = $_; | ||
| 176 | chomp $line; | ||
| 177 | if ($line =~ /^>(\S+)\s+(\d+)/) { | ||
| 178 | $cl_id = $1 . "_" . $2; | ||
| 179 | my @mem = (); | ||
| 180 | $clusters{$cl_id} = \@mem; | ||
| 181 | } else { | ||
| 182 | my $ref = $clusters{$cl_id}; | ||
| 183 | my @mems = @$ref; | ||
| 184 | my @els = split /\s+/, $line; | ||
| 185 | my $seq_id = $els[2]; | ||
| 186 | $seq_id =~ s/>//; | ||
| 187 | $seq_id =~ s/\.\.\.$//; | ||
| 188 | push @mems, $seq_id; | ||
| 189 | $clusters{$cl_id} = \@mems; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | # print Dumper(\%clusters); | ||
| 193 | my %seqs; | ||
| 194 | while ($seq = $in->next_seq()) { | ||
| 195 | my $id = $seq->display_id(); | ||
| 196 | $seqs{$id} = $seq; | ||
| 197 | } | ||
| 198 | |||
| 199 | foreach my $id (keys %clusters) { | ||
| 200 | my $out = Bio::SeqIO->new( -file => ">" . $filename . "-". $id . ".fas", -format => 'fasta'); | ||
| 201 | my @seqids = @{ $clusters{$id} }; | ||
| 202 | foreach my $seq_id (@seqids) { | ||
| 203 | $out->write_seq($seqs{$seq_id}); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | exit; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | sub print_composition { | ||
| 211 | while ($seq = $in->next_seq()) { | ||
| 212 | my $hash_ref = Bio::Tools::SeqStats->count_monomers($seq); | ||
| 213 | my $count; | ||
| 214 | $count += $hash_ref->{$_} foreach keys %$hash_ref; | ||
| 215 | print $seq->id(); | ||
| 216 | foreach (sort keys %$hash_ref) { | ||
| 217 | print "\t", $_, ":", $hash_ref->{$_}, "("; | ||
| 218 | printf "%.2f", $hash_ref->{$_}/$count*100; | ||
| 219 | print "%)" | ||
| 220 | } | ||
| 221 | print "\n" | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | # This sub calls all the del/pick subs above. Any option to filter input sequences by some criterion goes through here, and the appropriate filter subroutine is called. | ||
| 226 | sub filter_seqs { | ||
| 227 | my $action = shift; | ||
| 228 | my $match = $opts{$action}; | ||
| 229 | |||
| 230 | # matching to stop at 1st ':' so that ids using ':' as field delimiters are handled properly | ||
| 231 | $match =~ /^([^:]+):(\S+)$/ || die "Bad search format. Expecting a pattern of the form: tag:value.\n"; | ||
| 232 | |||
| 233 | my ($tag, $value) = ($1, $2); | ||
| 234 | my @selected = split(/,/, $value); | ||
| 235 | my $callsub = "find_by_" . "$tag"; | ||
| 236 | |||
| 237 | die "Bad tag or function not implemented. Tag was: $tag\n" if !defined($filter_dispatch{$callsub}); | ||
| 238 | |||
| 239 | if ($tag eq 'order') { | ||
| 240 | my $ct = 0; | ||
| 241 | my %order_list = parse_orders(\@selected); # Parse selected orders and create a hash | ||
| 242 | while (my $currseq = $in->next_seq) { $ct++; $filter_dispatch{$callsub}->($action, $ct, $currseq, \%order_list) } | ||
| 243 | foreach (keys %order_list) { print STDERR "No matches found for order number $_\n" if $_ > $ct } | ||
| 244 | } | ||
| 245 | elsif ($tag eq 'id') { | ||
| 246 | my %id_list = map { $_ => 1 } @selected; # create a hash from @selected | ||
| 247 | while (my $currseq = $in->next_seq) { $filter_dispatch{$callsub}->($action, $match, $currseq, \%id_list) } | ||
| 248 | foreach (keys %id_list) { warn "No matches found for '$_'\n" if $id_list{$_} == 1 } | ||
| 249 | } else { | ||
| 250 | while (my $currseq = $in->next_seq) { $filter_dispatch{$callsub}->($action, $currseq, $value) } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | =head2 retrieve_seqs() | ||
| 255 | |||
| 256 | Retrieves a sequence from GenBank using the provided accession | ||
| 257 | number. A wrapper for C<L<Bio::DB::GenBank>E<gt>#get_Seq_by_acc>. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | # To do: add fetch by gi | ||
| 262 | sub retrieve_seqs { | ||
| 263 | my $gb = Bio::DB::GenBank->new(); | ||
| 264 | my $seq = $gb->get_Seq_by_acc($opts{'fetch'}); # Retrieve sequence with Accession Number | ||
| 265 | $out->write_seq($seq) | ||
| 266 | } | ||
| 267 | |||
| 268 | =head2 remove_gaps() | ||
| 269 | |||
| 270 | Remove gaps | ||
| 271 | |||
| 272 | =cut | ||
| 273 | |||
| 274 | sub remove_gaps { | ||
| 275 | while ($seq = $in->next_seq()) { | ||
| 276 | my $string = $seq->seq(); | ||
| 277 | $string =~ s/-//g; | ||
| 278 | my $new_seq = Bio::Seq->new(-id => $seq->id(), -seq => $string); | ||
| 279 | $out->write_seq($new_seq) | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 print_lengths() | ||
| 284 | |||
| 285 | Print all sequence lengths. Wraps | ||
| 286 | L<Bio::Seq-E<gt>length|https://metacpan.org/pod/Bio::Seq#length>. | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub print_lengths { | ||
| 291 | while ($seq = $in->next_seq()) { print $seq->id(), "\t", $seq->length(), "\n" } | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | =head2 print_seq_count() | ||
| 296 | |||
| 297 | Print all sequence lengths. Wraps | ||
| 298 | L<Bio::Seq-E<gt>length|https://metacpan.org/pod/Bio::Seq#length>. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub print_seq_count { | ||
| 303 | my $count; | ||
| 304 | while ($seq = $in->next_seq()) { $count++ } | ||
| 305 | print $count, "\n" | ||
| 306 | } | ||
| 307 | |||
| 308 | =head2 make_revcom() | ||
| 309 | |||
| 310 | Reverse complement. Wraps | ||
| 311 | L<Bio::Seq-E<gt>revcom()|https://metacpan.org/pod/Bio::Seq#revcom>. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | |||
| 316 | sub make_revcom { # reverse-complement a sequence | ||
| 317 | while ($seq = $in->next_seq()) { | ||
| 318 | my $new = Bio::Seq->new(-id => $seq->id() . ":revcom", -seq => $seq->revcom()->seq()); | ||
| 319 | $out->write_seq($new) | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | =head2 print_subseq() | ||
| 324 | |||
| 325 | Select substring (of the 1st sequence). Wraps | ||
| 326 | L<Bio::Seq-E<gt>subseq()|https://metacpan.org/pod/Bio::Seq#subseq>. | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | |||
| 331 | sub print_subseq { | ||
| 332 | while ($seq = $in->next_seq()) { | ||
| 333 | my $id = $seq->id(); | ||
| 334 | my ($start, $end) = split /\s*,\s*/, $opts{"subseq"}; | ||
| 335 | $end = $seq->length() if $end eq '-'; # allow shorthand: -s'2,-' | ||
| 336 | die "end out of bound: $id\n" if $end > $seq->length(); | ||
| 337 | my $new = Bio::Seq->new(-id => $seq->id() . ":$start-$end", -seq => $seq->subseq($start, $end)); | ||
| 338 | $out->write_seq($new) | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | sub _internal_stop_or_x { | ||
| 343 | my $str = shift; | ||
| 344 | $str =~ s/\*$//; # remove last stop | ||
| 345 | return ($str =~ /[X\*]/) ? 1 : 0; # previously missed double ** | ||
| 346 | } | ||
| 347 | |||
| 348 | =head2 reading_frame_ops | ||
| 349 | |||
| 350 | Translate in 1, 3, or 6 frames based on the value of C<$opts> set via | ||
| 351 | L<C<#initilize(\%opts)>|/initialize>. Wraps | ||
| 352 | L<Bio::Seq-E<gt>translate()|https://metacpan.org/pod/Bio::Seq#translate>, | ||
| 353 | L<Bio::SeqUtils-E<gt>translate_3frames()|https://metacpan.org/pod/Bio::SeqUtils#translate_3frames>, | ||
| 354 | and | ||
| 355 | L<Bio::SeqUtils-E<gt>translate_6frames()|https://metacpan.org/pod/Bio::SeqUtils#translate_6frames>. | ||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | |||
| 360 | sub reading_frame_ops { | ||
| 361 | my $frame = $opts{"translate"}; | ||
| 362 | while ($seq = $in->next_seq()) { | ||
| 363 | if ($frame == 1) { | ||
| 364 | if (&_internal_stop_or_x($seq->translate()->seq())) { | ||
| 365 | warn "internal stop:\t" . $seq->id . "\tskip.\n" | ||
| 366 | } else { | ||
| 367 | $out->write_seq($seq->translate()) | ||
| 368 | } | ||
| 369 | } | ||
| 370 | elsif ($frame == 3) { | ||
| 371 | my @prots = Bio::SeqUtils->translate_3frames($seq); | ||
| 372 | $out->write_seq($_) foreach @prots | ||
| 373 | } elsif ($frame == 6) { | ||
| 374 | my @prots = Bio::SeqUtils->translate_6frames($seq); | ||
| 375 | $out->write_seq($_) foreach @prots | ||
| 376 | } else { warn "Accepted frame arguments: 1, 3, and 6\n"} | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | =head2 restrict_digest() | ||
| 381 | |||
| 382 | Predicted fragments from digestion by a specified restriction enzyme | ||
| 383 | specified in C<$opts{restrinct}> set via L<C<#initilize(\%opts)>|/initialize>. | ||
| 384 | |||
| 385 | An input file with a single sequence is expected. Wraps | ||
| 386 | L<Bio::Restriction::Analysis-E<gt>cut()|https://metacpan.org/pod/Bio::Restriction::Analysis#cut>. | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | |||
| 391 | sub restrict_digest { | ||
| 392 | my $enz = $opts{"restrict"}; | ||
| 393 | use Bio::Restriction::Analysis; | ||
| 394 | $seq = $in->next_seq(); | ||
| 395 | my $seq_str = $seq->seq(); | ||
| 396 | die "Not a DNA sequence\n" unless $seq_str =~ /^[ATCG]+$/i; | ||
| 397 | my $ra = Bio::Restriction::Analysis->new(-seq=>$seq); | ||
| 398 | foreach my $frag ($ra->fragment_maps($enz)) { | ||
| 399 | my $seq_obj = Bio::Seq->new( | ||
| 400 | -id=>$seq->id().'|'.$frag->{start}.'-'.$frag->{end}.'|'.($frag->{end}-$frag->{start}+1), | ||
| 401 | -seq=>$frag->{seq}); | ||
| 402 | $out->write_seq($seq_obj) | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | =head2 anonymize() | ||
| 407 | |||
| 408 | Replace sequence IDs with serial IDs I<n> characters long, as specified in | ||
| 409 | C<$opts{'anonymize'}> set via L<C<#initilize(\%opts)>|/initialize>. | ||
| 410 | For example if C<$opts{'anonymize'}>, the first ID will be C<S0001>. | ||
| 411 | leading 'S' The length of the serial idea | ||
| 412 | |||
| 413 | A sed script file is produced with a F<.sed> suffix that may be used | ||
| 414 | with sed's C<'-f'> argument. If the filename is F<'-'>, the sed file | ||
| 415 | is named C<STDOUT.sed> instead. A message containing the sed filename is | ||
| 416 | written to C<STDERR>. | ||
| 417 | |||
| 418 | =cut | ||
| 419 | |||
| 420 | sub anonymize { | ||
| 421 | my $char_len = $opts{"anonymize"} // die "Tried to use option 'preifx' without using option 'anonymize'. Exiting...\n"; | ||
| 422 | my $prefix = (defined($opts{"prefix"})) ? $opts{"prefix"} : "S"; | ||
| 423 | |||
| 424 | pod2usage(1) if $char_len < 1; | ||
| 425 | |||
| 426 | my $ct = 1; | ||
| 427 | my %serial_name; | ||
| 428 | my $length_warn = 0; | ||
| 429 | while ($seq = $in->next_seq()) { | ||
| 430 | my $serial = $prefix . sprintf "%0" . ($char_len - length($prefix)) . "s", $ct; | ||
| 431 | $length_warn = 1 if length($serial) > $char_len; | ||
| 432 | $serial_name{$serial} = $seq->id(); | ||
| 433 | $seq->id($serial); | ||
| 434 | $out->write_seq($seq); | ||
| 435 | $ct++ | ||
| 436 | } | ||
| 437 | |||
| 438 | _make_sed_file($filename, %serial_name); | ||
| 439 | warn "Anonymization map:\n"; | ||
| 440 | while (my ($k, $v) = each %serial_name) { warn "$k => $v\n" } | ||
| 441 | |||
| 442 | warn "WARNING: Anonymized ID length exceeded requested length: try a different length or prefix.\n" if $length_warn | ||
| 443 | } | ||
| 444 | |||
| 445 | =head2 shred_seq() | ||
| 446 | |||
| 447 | Break into individual sequences writing a FASTA file for each sequence. | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | |||
| 452 | sub shred_seq { | ||
| 453 | while ($seq = $in->next_seq()) { | ||
| 454 | my $newid = $seq->id(); | ||
| 455 | $newid =~ s/[\s\|]/_/g; | ||
| 456 | print $newid, "\n"; | ||
| 457 | my $newout = Bio::SeqIO->new(-format => $out_format, -file => ">" . $newid . ".fas"); | ||
| 458 | $newout->write_seq($seq) | ||
| 459 | } | ||
| 460 | exit | ||
| 461 | } | ||
| 462 | |||
| 463 | =head2 count_codons() | ||
| 464 | |||
| 465 | Count codons for coding sequences (e.g., a genome file consisting of | ||
| 466 | CDS sequences). Wraps | ||
| 467 | L<Bio::Tools::SeqStats-E<gt>count_codons()|https://metacpan.org/pod/Bio::Tools::SeqStats#count_codons>. | ||
| 468 | |||
| 469 | =cut | ||
| 470 | |||
| 471 | sub count_codons { | ||
| 472 | my $new_seq; | ||
| 473 | my $myCodonTable = Bio::Tools::CodonTable->new(); | ||
| 474 | while ($seq = $in->next_seq()) { $new_seq .= $seq->seq() } | ||
| 475 | my $hash_ref = Bio::Tools::SeqStats->count_codons(Bio::Seq->new(-seq=>$new_seq, -id=>'concat')); | ||
| 476 | my $count; | ||
| 477 | $count += $hash_ref->{$_} foreach keys %$hash_ref; | ||
| 478 | foreach (sort keys %$hash_ref) { | ||
| 479 | print $_, ":\t", $myCodonTable->translate($_), "\t", $hash_ref->{$_}, "\t"; | ||
| 480 | printf "%.2f", $hash_ref->{$_}/$count*100; | ||
| 481 | print "%\n" | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | =head2 print_gb_gene_feats() | ||
| 486 | |||
| 487 | print gene sequences in FASTA from a GenBank file of bacterial | ||
| 488 | genome. Won't work for a eukaryote genbank file. | ||
| 489 | |||
| 490 | =cut | ||
| 491 | |||
| 492 | sub print_gb_gene_feats { # works only for prokaryote genome | ||
| 493 | $seq = $in->next_seq(); | ||
| 494 | my $gene_count = 0; | ||
| 495 | foreach my $feat ($seq->get_SeqFeatures()) { | ||
| 496 | if ($feat->primary_tag eq 'gene') { | ||
| 497 | my $location = $feat->location(); | ||
| 498 | next if $location->isa('Bio::Location::Split'); | ||
| 499 | my $gene_tag = "gene_" . $gene_count++; | ||
| 500 | my $gene_symbol = 'na'; | ||
| 501 | foreach my $tag ($feat->get_all_tags()) { | ||
| 502 | ($gene_tag) = $feat->get_tag_values($tag) if $tag eq 'locus_tag'; | ||
| 503 | ($gene_symbol) = $feat->get_tag_values($tag) if $tag eq 'gene'; | ||
| 504 | } | ||
| 505 | my $gene = Bio::Seq->new(-id => (join "|", ($gene_tag, $feat->start, $feat->end, $feat->strand, $gene_symbol)), | ||
| 506 | -seq=>$seq->subseq($feat->start, $feat->end)); | ||
| 507 | if ($feat->strand() > 0) { $out->write_seq($gene) } else { $out->write_seq($gene->revcom())} | ||
| 508 | # print join "\t", | ||
| 509 | # ($feat->gff_string, $feat->start, $feat->end, | ||
| 510 | # $feat->strand); | ||
| 511 | # print "\n"; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | =head2 count_leading_gaps() | ||
| 517 | |||
| 518 | Count and print the number of leading gaps in each sequence. | ||
| 519 | |||
| 520 | =cut | ||
| 521 | |||
| 522 | sub count_leading_gaps { | ||
| 523 | while ($seq = $in->next_seq()) { | ||
| 524 | my $lead_gap = 0; | ||
| 525 | my $see_aa = 0; # status variable | ||
| 526 | my @mono = split //, $seq->seq(); | ||
| 527 | for (my $i = 0; $i < $seq->length(); $i++) { | ||
| 528 | $see_aa = 1 if $mono[$i] ne '-'; | ||
| 529 | $lead_gap++ if !$see_aa && $mono[$i] eq '-' | ||
| 530 | } | ||
| 531 | print $seq->id(), "\t", $lead_gap, "\n" | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | =head2 hydroB() | ||
| 536 | |||
| 537 | Return the mean Kyte-Doolittle hydropathicity for protein | ||
| 538 | sequences. Wraps | ||
| 539 | L<Bio::Tools::SeqStats-E<gt>hydropathicity()|https://metacpan.org/pod/Bio::Tools::SeqStats#hydropathicity>. | ||
| 540 | |||
| 541 | =cut | ||
| 542 | |||
| 543 | |||
| 544 | sub hydroB { | ||
| 545 | while ($seq = $in->next_seq()) { | ||
| 546 | my $pep_str = $seq->seq(); | ||
| 547 | $pep_str =~ s/\*//g; | ||
| 548 | $seq->seq($pep_str); | ||
| 549 | my $gravy = Bio::Tools::SeqStats->hydropathicity($seq); | ||
| 550 | printf "%s\t%.4f\n", $seq->id(), $gravy | ||
| 551 | } | ||
| 552 | } | ||
| 553 | |||
| 554 | =head2 linearize() | ||
| 555 | |||
| 556 | Linearize FASTA, print one sequence per line. | ||
| 557 | |||
| 558 | =cut | ||
| 559 | |||
| 560 | |||
| 561 | sub linearize { | ||
| 562 | while ($seq = $in->next_seq()) { print $seq->id(), "\t", $seq->seq(), "\n" } | ||
| 563 | } | ||
| 564 | |||
| 565 | =head2 reloop_at() | ||
| 566 | |||
| 567 | Re-circularize a bacterial genome by starting at a specified position | ||
| 568 | given in the C<$opts{"reloop"> set via | ||
| 569 | L<C<#initilize(\%opts)>|/initialize>. | ||
| 570 | |||
| 571 | For example for sequence "ABCDE". C<bioseq -R'2' ..> | ||
| 572 | would generate"'BCDEA". | ||
| 573 | |||
| 574 | =cut | ||
| 575 | sub reloop_at { | ||
| 576 | my $seq = $in->next_seq; # only the first sequence | ||
| 577 | my $break = $opts{"reloop"}; | ||
| 578 | my $new_seq = Bio::Seq->new(-id => $seq->id().":relooped_at_".$break, -seq => $seq->subseq($break, $seq->length()) . $seq->subseq(1, $break-1)); | ||
| 579 | $out->write_seq($new_seq) | ||
| 580 | } | ||
| 581 | |||
| 582 | =head2 remove_stop() | ||
| 583 | |||
| 584 | Remove stop codons. | ||
| 585 | |||
| 586 | =cut | ||
| 587 | |||
| 588 | sub remove_stop { | ||
| 589 | my $myCodonTable = Bio::Tools::CodonTable->new(); | ||
| 590 | while ($seq = $in->next_seq()) { | ||
| 591 | my $newstr = ""; | ||
| 592 | for (my $i = 1; $i <= $seq->length() / 3; $i++) { | ||
| 593 | my $codon = $seq->subseq(3 * ($i - 1) + 1, 3 * ($i - 1) + 3); | ||
| 594 | if ($myCodonTable->is_ter_codon($codon)) { warn "Found and removed stop codon\n"; next } | ||
| 595 | $newstr .= $codon | ||
| 596 | } | ||
| 597 | my $new = Bio::Seq->new(-id => $seq->id(), -seq => $newstr); | ||
| 598 | $out->write_seq($new) | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | |||
| 603 | ####################### internal subroutine ########################### | ||
| 604 | |||
| 605 | sub parse_orders { | ||
| 606 | my @selected = @{shift()}; | ||
| 607 | |||
| 608 | my @orders; | ||
| 609 | # Parse if $value contains ranges: allows mixing ranges and lists | ||
| 610 | foreach my $val (@selected) { | ||
| 611 | if ($val =~ /^(\d+)-(\d+)$/) { # A numerical range | ||
| 612 | my ($first, $last) = ($1, $2); | ||
| 613 | die "Invalid seq range: $first, $last\n" unless $last > $first; | ||
| 614 | push @orders, ($first .. $last) | ||
| 615 | } else { push @orders, $val } # Single value | ||
| 616 | } | ||
| 617 | return map { $_ => 1 } @orders | ||
| 618 | } | ||
| 619 | |||
| 620 | sub _make_sed_file { | ||
| 621 | my $filename = shift @_; | ||
| 622 | my (%serial_names) = @_; | ||
| 623 | |||
| 624 | $filename = "STDOUT" if $filename eq '-'; | ||
| 625 | |||
| 626 | my $sedfile = basename($filename) . ".sed"; | ||
| 627 | open(my $sedout, ">", $sedfile) or die $!; | ||
| 628 | |||
| 629 | print $sedout "# usage: sed -f $filename.sed <anonymized file>\n"; | ||
| 630 | |||
| 631 | foreach (keys %serial_names) { | ||
| 632 | my $real_name = $serial_names{$_}; | ||
| 633 | my $sed_cmd = "s/$_/" . $real_name . "/g;\n"; | ||
| 634 | print $sedout $sed_cmd | ||
| 635 | } | ||
| 636 | close $sedout; | ||
| 637 | |||
| 638 | print STDERR "\nCreated $filename.sed\tusage: sed -f $filename.sed <anonymized file>\n\n" | ||
| 639 | } | ||
| 640 | |||
| 641 | ################### pick/delete filters ##################### | ||
| 642 | |||
| 643 | sub find_by_order { | ||
| 644 | my ($action, $ct, $currseq, $order_list) = @_; # say join "\t", @_; | ||
| 645 | $filter_dispatch{ $action . "_by_order" }->($ct, $currseq, $order_list) | ||
| 646 | } | ||
| 647 | |||
| 648 | sub pick_by_order { | ||
| 649 | my ($ct, $currseq, $order_list) = @_; | ||
| 650 | $out->write_seq($currseq) if $order_list->{$ct} | ||
| 651 | } | ||
| 652 | |||
| 653 | sub del_by_order { | ||
| 654 | my ($ct, $currseq, $order_list) = @_; # say join "\t", @_; | ||
| 655 | if ($order_list->{$ct}) { warn "Deleted sequence: ", $currseq->id(), "\n" } | ||
| 656 | else { $out->write_seq($currseq) } | ||
| 657 | } | ||
| 658 | |||
| 659 | sub find_by_id { | ||
| 660 | my ($action, $match, $currseq, $id_list) = @_; | ||
| 661 | my $seq_id = $currseq->id(); | ||
| 662 | $filter_dispatch{$action . "_by_id"}->($match, $currseq, $id_list, $seq_id) | ||
| 663 | } | ||
| 664 | |||
| 665 | sub pick_by_id { | ||
| 666 | my ($match, $currseq, $id_list, $seq_id) = @_; | ||
| 667 | |||
| 668 | if ($id_list->{$seq_id}) { | ||
| 669 | $id_list->{$seq_id}++; | ||
| 670 | die "Multiple matches (" . $id_list->{$seq_id} - 1 . ") for $match found\n" if $id_list->{$seq_id} > 2; | ||
| 671 | $out->write_seq($currseq) | ||
| 672 | } | ||
| 673 | } | ||
| 674 | |||
| 675 | sub del_by_id { | ||
| 676 | my ($match, $currseq, $id_list, $seq_id) = @_; | ||
| 677 | |||
| 678 | if ($id_list->{$seq_id}) { | ||
| 679 | $id_list->{$seq_id}++; | ||
| 680 | warn "Deleted sequence: ", $currseq->id(), "\n" | ||
| 681 | } else { $out->write_seq($currseq) } | ||
| 682 | } | ||
| 683 | |||
| 684 | sub find_by_re { | ||
| 685 | my ($action, $currseq, $value) = @_; | ||
| 686 | my $regex = qr/$value/; | ||
| 687 | my $seq_id = $currseq->id(); | ||
| 688 | $filter_dispatch{ $action . "_by_re" }->($currseq, $regex, $seq_id) | ||
| 689 | } | ||
| 690 | |||
| 691 | sub pick_by_re { | ||
| 692 | my ($currseq, $regex, $seq_id) = @_; | ||
| 693 | $out->write_seq($currseq) if $seq_id =~ /$regex/ | ||
| 694 | } | ||
| 695 | |||
| 696 | sub del_by_re { | ||
| 697 | my ($currseq, $regex, $seq_id) = @_; | ||
| 698 | |||
| 699 | if ($seq_id =~ /$regex/) { warn "Deleted sequence: $seq_id\n" } | ||
| 700 | else { $out->write_seq($currseq) } | ||
| 701 | } | ||
| 702 | |||
| 703 | # TODO This needs better documentation | ||
| 704 | sub find_by_ambig { | ||
| 705 | my ($action, $currseq, $cutoff) = @_; | ||
| 706 | my $string = $currseq->seq(); | ||
| 707 | my $ct = ($string =~ s/n/n/gi); | ||
| 708 | my $percent_ambig = $ct / $currseq->length(); | ||
| 709 | $filter_dispatch{"$action" . "_by_ambig"}->($currseq, $cutoff, $ct, $percent_ambig) | ||
| 710 | } | ||
| 711 | |||
| 712 | # TODO Probably better to change behavior when 'picking'? | ||
| 713 | sub pick_by_ambig { | ||
| 714 | my ($currseq, $cutoff, $ct, $percent_ambig) = @_; | ||
| 715 | $out->write_seq($currseq) if $percent_ambig > $cutoff | ||
| 716 | } | ||
| 717 | |||
| 718 | sub del_by_ambig { | ||
| 719 | my ($currseq, $cutoff, $ct, $percent_ambig) = @_; | ||
| 720 | |||
| 721 | if ($percent_ambig > $cutoff) { warn "Deleted sequence: ", $currseq->id(), " number of N: ", $ct, "\n" } | ||
| 722 | else { $out->write_seq($currseq) } | ||
| 723 | } | ||
| 724 | |||
| 725 | sub find_by_length { | ||
| 726 | my ($action, $currseq, $value) = @_; | ||
| 727 | $filter_dispatch{$action . "_by_length"}->($currseq, $value) | ||
| 728 | } | ||
| 729 | |||
| 730 | sub pick_by_length { | ||
| 731 | my ($currseq, $value) = @_; | ||
| 732 | $out->write_seq($currseq) if $currseq->length() <= $value | ||
| 733 | } | ||
| 734 | |||
| 735 | sub del_by_length { | ||
| 736 | my ($currseq, $value) = @_; | ||
| 737 | |||
| 738 | if ($currseq->length() <= $value) { warn "Deleted sequence: ", $currseq->id(), " length: ", $currseq->length(), "\n" } | ||
| 739 | else { $out->write_seq($currseq) } | ||
| 740 | } | ||
| 741 | |||
| 742 | 1; | ||
| 743 | __END__ |