BioUtil/Seq.pm

filter: Statementstmt 40.6% 78 / 192 Branchbran 26.0% 26 / 100 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 58.3% 14 / 24 Podpod 88.2% 15 / 17 totaltotal 39.3% 135 / 343 CC 57 SCAR 33.4
CC
57
Cov
36%
__ANON__
90.0
clean_fasta_header
90.0
rename_fasta_header
42.0
CRAP 28.3
↑ Summary BioUtil/Util.pm »
1 package BioUtil::Seq;
2  
3 1 require Exporter;
4 1 @ISA = (Exporter);
5 1 @EXPORT = qw(
6 FastaReader
7 read_sequence_from_fasta_file
8 write_sequence_to_fasta_file
9 format_seq
10  
11 validate_sequence
12 complement
13 revcom
14 base_content
15 degenerate_seq_to_regexp
16 degenerate_seq_match_sites
17 dna2peptide
18 codon2aa
19 generate_random_seqence
20  
21 shuffle_sequences
22 rename_fasta_header
23 clean_fasta_header
24 );
25  
26 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head1 NAME
35  
36 BioUtil::Seq - Utilities for sequence
37  
38 Some great modules like BioPerl provide many robust solutions.
39 However, it is not easy to install for someone in some platforms.
40 And for some simple task scripts, a lite module may be a good choice.
41 So I reinvented some wheels and added some useful utilities into this module,
42 hoping it would be helpful.
43  
44 =head1 VERSION
45  
46 Version 2015.0309
47  
48 =cut
49  
50 1 our $VERSION = 2015.0309;
51  
52 =head1 EXPORT
53  
54 FastaReader
55 read_sequence_from_fasta_file
56 write_sequence_to_fasta_file
57 format_seq
58  
59 validate_sequence
60 complement
61 revcom
62 base_content
63 degenerate_seq_to_regexp
64 degenerate_seq_match_sites
65 dna2peptide
66 codon2aa
67 generate_random_seqence
68  
69 shuffle_sequences
70 rename_fasta_header
71 clean_fasta_header
72  
73 =head1 SYNOPSIS
74  
75 use BioUtil::Seq;
76  
77  
78 =head1 SUBROUTINES/METHODS
79  
80  
81 =head2 FastaReader
82  
83 FastaReader is a fasta file parser using closure.
84 FastaReader returns an anonymous subroutine, when called, it
85 return a fasta record which is reference of an array
86 containing fasta header and sequence.
87  
88 FastaReader could also read from STDIN when the file name is "STDIN" or "stdin".
89  
90 A boolean argument is optional. If set as "true", spaces including blank, tab,
91 "return" ("\r") and "new line" ("\n") symbols in sequence will not be trimed.
92  
93 FastaReader speeds up by utilizing the special Perl variable $/ (set to "\n>"),
94 with kind help of Mario Roy, author of MCE
95 (https://code.google.com/p/many-core-engine-perl/). A lot of optimizations were
96 also done by him.
97  
98 Example:
99  
100 # do not trim the spaces and \n
101 # $not_trim = 1;
102 # my $next_seq = FastaReader("test.fa", $not_trim);
103
104 # read from STDIN
105 # my $next_seq = FastaReader('STDIN');
106
107 # read from file
108 my $next_seq = FastaReader("test.fa");
109  
110 while ( my $fa = &$next_seq() ) {
111 my ( $header, $seq ) = @$fa;
112  
113 print ">$header\n$seq\n";
114 }
115  
116 =cut
117  
118 sub FastaReader {
119 2 ▶ my ( $file, $not_trim ) = @_;
Subroutine FastaReader: called CC=7 SCAR=20.7
Pod documented
120  
121 2 my ( $open_flg, $finished ) = ( 0, 0 );
122 2 my ( $fh, $pos, $head ) = (undef) x 3;
123  
124 2 ▶ if ( $file =~ /^STDIN$/i ) { # from stdin
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /^STDIN$/i) { } 0 2 2
elsif (ref $file eq '' or ref $file eq 'SCALAR') { } 2 0 2
125 0 $fh = *STDIN;
126 }
127 ▶ elsif ( ref $file eq '' or ref $file eq 'SCALAR' ) { # from file
Condition33%1/3
ref $file eq "" or ref $file eq "SCALAR"
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $file eq "" or ref $file eq "SCALAR"
ref $file eq "" ref $file eq "SCALAR"
Truth table33%1/3
A or B
A= ref $file eq ""B= ref $file eq "SCALAR"
ABresult
1X1
011
000
128 2 ▶ open $fh, '<', $file or die "fail to open file: $file!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "fail to open file: $file!\n" 0 2 2
129 2 $open_flg = 1;
130 }
131 else { # glob, i.e. given file handler
132 0 $fh = $file;
133 }
134  
135 2 local $/ = \1; ## read one byte
136 2 ▶ while (<$fh>) { ## until reaching ">"
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2 0 2
137 2 ▶ last if $_ eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq ">" 2 0 2
138 }
139 return sub {
140 6 ▶ return if $finished;
Subroutine __ANON__: called CC=8 SCAR=21.2
Branch: 1 branch
BranchTrueFalseTotal
if $finished 0 6 6
141  
142 6 local $/ = "\n>"; ## set input record separator
143 6 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 4 2 6
144 ## trim trailing ">", part of $/. faster than s/\r?\n>$//
145 4 ▶ substr( $_, -1, 1, '' ) if substr( $_, -1, 1 ) eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, -1, 1) eq ">" 2 2 4
146  
147 ## extract header and sequence
148 # faster than ( $head, $seq ) = split( /\n/, $_, 2 );
149 4 $pos = index( $_, "\n" ) + 1;
150 4 $head = substr( $_, 0, $pos - 1 );
151  
152 # $_ becomes sequence, to save memory
153 # $seq = substr( $_, $pos );
154 4 substr( $_, 0, $pos, '' );
155  
156 ## trim trailing "\r" in header
157 4 ▶ chop $head if substr( $head, -1, 1 ) eq "\r";
Branch: 1 branch
BranchTrueFalseTotal
if substr($head, -1, 1) eq "\r" 0 4 4
158  
159 4 ▶ if ( length $head > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $head > 0) 4 0 4
160  
161 # faster than $seq =~ s/\s//g unless $not_trim;
162 # $seq =~ tr/\t\r\n //d unless $not_trim;
163 4 ▶ $_ =~ tr/\t\r\n //d unless $not_trim;
Branch: 1 branch
BranchTrueFalseTotal
unless $not_trim 4 0 4
164 4 return [ $head, $_ ];
165 }
166 }
167  
168 2 ▶ close $fh if $open_flg;
Branch: 1 branch
BranchTrueFalseTotal
if $open_flg 2 0 2
169 2 $finished = 1;
170 2 return;
171 2 };
172 }
173  
174 sub FastaReader_old {
175 0 ▶ my ( $file, $not_trim ) = @_;
Subroutine FastaReader_old: not called CC=3 SCAR=24.8
Pod undocumented
176  
177 0 my ( $last_header, $seq_buffer ) = ( '', '' ); # buffer for header and seq
178 0 my ( $header, $seq ) = ( '', '' ); # current header and seq
179 0 my $finished = 0;
180  
181 0 my ( $fh, $is_stdin ) = ( undef, 0 );
182 0 ▶ if ( $file =~ /^STDIN$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^STDIN$/i) { } 0 0 0
183 0 ( $fh, $is_stdin ) = ( *STDIN, 1 );
184 }
185 else {
186 0 ▶ open $fh, "<", $file or die "fail to open file: $file!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "fail to open file: $file!\n" 0 0 0
187 }
188  
189 return sub {
190 0 ▶ return undef if $finished; # end of file
Subroutine __ANON__: not called CC=9 SCAR=45.0
Branch: 1 branch
BranchTrueFalseTotal
if $finished 0 0 0
191  
192 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
193 0 s/^\s+//; # remove the space at the front of line
194  
195 0 ▶ if (/^>(.*)/) { # header line
Branch: 1 branch
BranchTrueFalseTotal
if (/^>(.*)/) { } 0 0 0
196 0 ( $header, $last_header ) = ( $last_header, $1 );
197 0 ( $seq, $seq_buffer ) = ( $seq_buffer, '' );
198  
199 # only output fasta records with non-blank header
200 0 ▶ if ( $header ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header ne "") 0 0 0
201 0 ▶ $seq =~ s/\s+//g unless $not_trim;
Branch: 1 branch
BranchTrueFalseTotal
unless $not_trim 0 0 0
202 0 return [ $header, $seq ];
203 }
204 }
205 else {
206 0 $seq_buffer .= $_; # append seq
207 }
208 }
209 0 ▶ close $fh unless $is_stdin;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_stdin 0 0 0
210 0 $finished = 1;
211  
212 # last record. only output fasta records with non-blank header
213 0 ▶ if ( $last_header ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_header ne "") 0 0 0
214 0 ▶ $seq_buffer =~ s/\s+//g unless $not_trim;
Branch: 1 branch
BranchTrueFalseTotal
unless $not_trim 0 0 0
215 0 return [ $last_header, $seq_buffer ];
216 }
217 0 };
218 }
219  
220 =head2 read_sequence_from_fasta_file
221  
222 Read all sequences from fasta file.
223  
224 Example:
225  
226 my $seqs = read_sequence_from_fasta_file($file);
227 for my $header (keys %$seqs) {
228 my $seq = $$seqs{$header};
229 print ">$header\n$seq\n";
230 }
231  
232 =cut
233  
234 sub read_sequence_from_fasta_file {
235 2 ▶ my ( $file, $not_trim ) = @_;
Subroutine read_sequence_from_fasta_file: called CC=2 SCAR=6.9
Pod documented
236 2 my $seqs = {};
237  
238 2 my $next_seq = FastaReader( $file, $not_trim );
239 2 ▶ while ( my $fa = &$next_seq() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fa = &$next_seq()) 4 2 6
240 # my ( $header, $seq ) = @$fa;
241 # $$seqs{$header} = $seq;
242 4 $$seqs{ $fa->[0] } = $fa->[1];
243 }
244  
245 2 return $seqs;
246 }
247  
248 =head2 write_sequence_to_fasta_file
249  
250 Example:
251  
252 my $seq = {"seq1" => "acgagaggag"};
253 write_sequence_to_fasta_file($seq, "seq.fa");
254  
255 =cut
256  
257 sub write_sequence_to_fasta_file {
258 1 ▶ my ( $seqs, $file, $n ) = @_;
Subroutine write_sequence_to_fasta_file: called CC=5 SCAR=17.8
Pod documented
259 1 ▶ unless ( ref $seqs eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $seqs eq "HASH") 0 1 1
260 0 warn "seqs should be reference of hash\n";
261 0 return 0;
262 }
263 1 ▶ $n = 70 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 0 1
264  
265 1 ▶ open my $fh2, ">$file" or die "failed to write to $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, ">$file" or die "failed to write to $file\n" 0 1 1
266 1 for ( keys %$seqs ) {
267 2 print $fh2 ">$_\n", format_seq( $$seqs{$_}, $n ), "\n";
268 }
269 1 close $fh2;
270 }
271  
272 =head2 format_seq
273  
274 Format sequence to readable text
275  
276 Example:
277  
278 printf ">%s\n%s", $head, format_seq($seq, 60);
279  
280 =cut
281  
282 sub format_seq {
283 2 ▶ my ( $s, $n ) = @_;
Subroutine format_seq: called CC=5 SCAR=17.4
Pod documented
284 2 ▶ $n = 70 unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 2 2
285 2 ▶ unless ( $n =~ /^\d+$/ and $n > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n =~ /^\d+$/ and $n > 0) 0 2 2
Condition33%1/3
$n =~ /^\d+$/ and $n > 0
!ll&&!rl&&r
002
MC/DC0%0/2
$n =~ /^\d+$/ and $n > 0
$n =~ /^\d+$/ $n > 0
Truth table33%1/3
A and B
A= $n =~ /^\d+$/B= $n > 0
ABresult
0X0
100
111
286 0 warn "n should be positive integer\n";
287 0 return $s;
288 }
289  
290 2 my $s2 = '';
291 2 my ( $j, $int );
292 2 $int = int( ( length $s ) / $n );
293 2 ▶ for ( $j = 0; $j <= $int; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $int) 2 2 4
294 2 $s2 .= substr( $s, $j * $n, $n ) . "\n";
295 }
296 2 return $s2;
297 }
298  
299 =head2 validate_sequence
300  
301 Validate a sequence.
302  
303 Legale symbols:
304  
305 DNA: ACGTRYSWKMBDHV
306 RNA: ACGURYSWKMBDHV
307 Protein: ACDEFGHIKLMNPQRSTVWY
308 gap and space: - *.
309  
310 Example:
311  
312 if (validate_sequence($seq)) {
313 # do some thing
314 }
315  
316 =cut
317  
318 sub validate_sequence {
319 2 ▶ my ($seq) = @_;
Subroutine validate_sequence: called CC=2 SCAR=6.9
Pod documented
320 2 ▶ return 0 if $seq =~ /[^\.\-\s_*ABCDEFGHIKLMNPQRSTUVWY]/i;
Branch: 1 branch
BranchTrueFalseTotal
if $seq =~ /[^\.\-\s_*ABCDEFGHIKLMNPQRSTUVWY]/i 1 1 2
321 1 return 1;
322 }
323  
324 =head2 complement
325  
326 Complement sequence
327  
328 IUPAC nucleotide code: ACGTURYSWKMBDHVN
329  
330 http://droog.gs.washington.edu/parc/images/iupac.html
331  
332 code base Complement
333 A A T
334 C C G
335 G G C
336 T/U T A
337  
338 R A/G Y
339 Y C/T R
340 S C/G S
341 W A/T W
342 K G/T M
343 M A/C K
344  
345 B C/G/T V
346 D A/G/T H
347 H A/C/T D
348 V A/C/G B
349  
350 X/N A/C/G/T X
351 . not A/C/G/T
352 or- gap
353  
354 my $comp = complement($seq);
355  
356 =cut
357  
358 sub complement {
359 1 ▶ my ($s) = @_;
Subroutine complement: called CC=1 SCAR=0.0
Pod documented
360 1 $s
361 =~ tr/ACGTURYMKSWBDHVNacgturymkswbdhvn/TGCAAYRKMSWVHDBNtgcaayrkmswvhdbn/;
362 1 return $s;
363 }
364  
365 =head2 revcom
366  
367 Reverse complement sequence
368  
369 my $recom = revcom($seq);
370  
371 =cut
372  
373 sub revcom {
374 1 ▶ my $rc = reverse complement( $_[0] );
Subroutine revcom: called CC=1 SCAR=0.0
Pod documented
375 1 return $rc;
376 }
377  
378 =head2 base_content
379  
380 Example:
381  
382 my $gc_cotent = base_content('gc', $seq);
383  
384 =cut
385  
386 sub base_content {
387 1 ▶ my ( $bases, $seq ) = @_;
Subroutine base_content: called CC=3 SCAR=11.4
Pod documented
388 1 ▶ if ( $seq eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq eq "") 0 1 1
389 0 return 0;
390 }
391  
392 1 my $sum = 0;
393 1 $sum += $seq =~ s/$_/$_/ig for split "", $bases;
394 1 return sprintf "%.4f", $sum / length $seq;
395 }
396  
397 =head2 degenerate_seq_to_regexp
398  
399 Translate degenerate sequence to regular expression
400  
401 =cut
402  
403 sub degenerate_seq_to_regexp {
404 0 ▶ my ($seq) = @_;
Subroutine degenerate_seq_to_regexp: not called CC=1 SCAR=6.9
Pod documented
405 0 my %bases = (
406 'A' => 'A',
407 'T' => 'T',
408 'U' => 'U',
409 'C' => 'C',
410 'G' => 'G',
411 'R' => '[AG]',
412 'Y' => '[CT]',
413 'M' => '[AC]',
414 'K' => '[GT]',
415 'S' => '[CG]',
416 'W' => '[AT]',
417 'H' => '[ACT]',
418 'B' => '[CGT]',
419 'V' => '[ACG]',
420 'D' => '[AGT]',
421 'N' => '[ACGT]',
422 );
423 0 return join '', map { $bases{$_} } split //, $seq;
424 }
425  
426 =head2 degenerate_seq_match_sites
427  
428 Find all sites matching degenerat subseq.
429  
430 See https://github.com/shenwei356/bio_scripts/blob/master/sequence/fasta_locate_motif.pl
431  
432 =cut
433  
434 sub degenerate_seq_match_sites {
435 0 ▶ my ( $r, $s ) = @_;
Subroutine degenerate_seq_match_sites: not called CC=2 SCAR=17.9
Pod documented
436  
437 # original regexp length
438 0 my $r2 = $r;
439 0 $r2 =~ s/\[[^\[\]]+?\]/_/g;
440 0 my $len = length $r2;
441  
442 0 my @sites = ();
443 0 my $pos = -1;
444 0 ▶ while ( $s =~ /($r)/ig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /($r)/gi) 0 0 0
445 0 $pos = pos $s;
446 0 push @sites, [ $pos - $len + 1, $pos, $1];
447 0 pos $s = $pos - $len + 1;
448 }
449 0 return \@sites;
450 }
451  
452 =head2 dna2peptide
453  
454 Translate DNA sequence into a peptide
455  
456 =cut
457  
458 sub dna2peptide {
459 0 ▶ my ($dna) = @_;
Subroutine dna2peptide: not called CC=2 SCAR=17.9
Pod documented
460 0 my $protein = '';
461  
462 # Translate each three-base codon to an amino acid, and append to a protein
463 0 ▶ for ( my $i = 0; $i < ( length($dna) - 2 ); $i += 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length($dna) - 2) 0 0 0
464 0 $protein .= codon2aa( substr( $dna, $i, 3 ) );
465 }
466 0 return $protein;
467 }
468  
469 =head2 codon2aa
470  
471 Translate a DNA 3-character codon to an amino acid
472  
473 =cut
474  
475 sub codon2aa {
476 0 ▶ my ($codon) = @_;
Subroutine codon2aa: not called CC=2 SCAR=17.9
Pod documented
477 0 $codon = uc $codon;
478 0 my %genetic_code = (
479 'TCA' => 'S', # Serine
480 'TCC' => 'S', # Serine
481 'TCG' => 'S', # Serine
482 'TCT' => 'S', # Serine
483 'TTC' => 'F', # Phenylalanine
484 'TTT' => 'F', # Phenylalanine
485 'TTA' => 'L', # Leucine
486 'TTG' => 'L', # Leucine
487 'TAC' => 'Y', # Tyrosine
488 'TAT' => 'Y', # Tyrosine
489 'TAA' => '_', # Stop
490 'TAG' => '_', # Stop
491 'TGC' => 'C', # Cysteine
492 'TGT' => 'C', # Cysteine
493 'TGA' => '_', # Stop
494 'TGG' => 'W', # Tryptophan
495 'CTA' => 'L', # Leucine
496 'CTC' => 'L', # Leucine
497 'CTG' => 'L', # Leucine
498 'CTT' => 'L', # Leucine
499 'CCA' => 'P', # Proline
500 'CCC' => 'P', # Proline
501 'CCG' => 'P', # Proline
502 'CCT' => 'P', # Proline
503 'CAC' => 'H', # Histidine
504 'CAT' => 'H', # Histidine
505 'CAA' => 'Q', # Glutamine
506 'CAG' => 'Q', # Glutamine
507 'CGA' => 'R', # Arginine
508 'CGC' => 'R', # Arginine
509 'CGG' => 'R', # Arginine
510 'CGT' => 'R', # Arginine
511 'ATA' => 'I', # Isoleucine
512 'ATC' => 'I', # Isoleucine
513 'ATT' => 'I', # Isoleucine
514 'ATG' => 'M', # Methionine
515 'ACA' => 'T', # Threonine
516 'ACC' => 'T', # Threonine
517 'ACG' => 'T', # Threonine
518 'ACT' => 'T', # Threonine
519 'AAC' => 'N', # Asparagine
520 'AAT' => 'N', # Asparagine
521 'AAA' => 'K', # Lysine
522 'AAG' => 'K', # Lysine
523 'AGC' => 'S', # Serine
524 'AGT' => 'S', # Serine
525 'AGA' => 'R', # Arginine
526 'AGG' => 'R', # Arginine
527 'GTA' => 'V', # Valine
528 'GTC' => 'V', # Valine
529 'GTG' => 'V', # Valine
530 'GTT' => 'V', # Valine
531 'GCA' => 'A', # Alanine
532 'GCC' => 'A', # Alanine
533 'GCG' => 'A', # Alanine
534 'GCT' => 'A', # Alanine
535 'GAC' => 'D', # Aspartic Acid
536 'GAT' => 'D', # Aspartic Acid
537 'GAA' => 'E', # Glutamic Acid
538 'GAG' => 'E', # Glutamic Acid
539 'GGA' => 'G', # Glycine
540 'GGC' => 'G', # Glycine
541 'GGG' => 'G', # Glycine
542 'GGT' => 'G', # Glycine
543 );
544  
545 0 ▶ if ( exists $genetic_code{$codon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $genetic_code{$codon}) { } 0 0 0
546 0 return $genetic_code{$codon};
547 }
548 else {
549 0 print STDERR "Bad codon \"$codon\"!!\n";
550 0 exit;
551 }
552 }
553  
554 =head2 generate_random_seqence
555  
556 Example:
557  
558 my @alphabet = qw/a c g t/;
559 my $seq = generate_random_seqence( \@alphabet, 50 );
560  
561 =cut
562  
563 sub generate_random_seqence {
564 0 ▶ my ( $alphabet, $length ) = @_;
Subroutine generate_random_seqence: not called CC=3 SCAR=24.8
Pod documented
565 0 ▶ unless ( ref $alphabet eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $alphabet eq "ARRAY") 0 0 0
566 0 warn "alphabet should be ref of array\n";
567 0 return 0;
568 }
569  
570 0 my $n = @$alphabet;
571 0 my $seq;
572 0 $seq .= $$alphabet[ int rand($n) ] for ( 1 .. $length );
573 0 return $seq;
574 }
575  
576 =head2 shuffle sequences
577  
578 Example:
579  
580 shuffle_sequences($file, "$file.shuf.fa");
581  
582 =cut
583  
584 sub shuffle_sequences {
585 0 ▶ my ( $file, $file_out, $not_trim ) = @_;
Subroutine shuffle_sequences: not called CC=4 SCAR=30.0
Pod undocumented
586 0 my $seqs = read_sequence_from_fasta_file( $file, $not_trim );
587 0 my @keys = shuffle( keys %$seqs );
588  
589 0 ▶ $file_out = "$file.shuffled.fa" unless defined $file_out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_out 0 0 0
590 0 ▶ open my $fh2, ">$file_out" or die "fail to write file $file_out\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, ">$file_out" or die "fail to write file $file_out\n" 0 0 0
591 0 print $fh2 ">$_\n$$seqs{$_}\n" for @keys;
592 0 close $fh2;
593  
594 0 return $file_out;
595 }
596  
597 =head2 rename_fasta_header
598  
599 Rename fasta header with regexp.
600  
601 Example:
602
603 # delete some symbols
604 my $n = rename_fasta_header('[^a-z\d\s\-\_\(\)\[\]\|]', '', $file, "$file.rename.fa");
605 print "$n records renamed\n";
606  
607 =cut
608  
609 sub rename_fasta_header {
610 0 ▶ my ( $regex, $repalcement, $file, $outfile ) = @_;
Subroutine rename_fasta_header: not called CC=6 SCAR=37.4
Pod documented
611  
612 0 ▶ open my $fh, "<", $file or die "fail to open file: $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "fail to open file: $file\n" 0 0 0
613 0 ▶ open my $fh2, ">", $outfile or die "fail to wirte file: $outfile\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, ">", $outfile or die "fail to wirte file: $outfile\n" 0 0 0
614  
615 0 my $head = '';
616 0 my $n = 0;
617 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
618 0 ▶ if (/^\s*>(.*)\r?\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*>(.*)\r?\n/) { } 0 0 0
619 0 $head = $1;
620 0 ▶ if ( $head =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head =~ /$regex/) 0 0 0
621 0 $head =~ s/$regex/$repalcement/g;
622 0 $n++;
623 }
624 0 print $fh2 ">$head\n";
625 }
626 else {
627 0 print $fh2 $_;
628 }
629 }
630 0 close $fh;
631 0 close $fh2;
632  
633 0 return $n;
634 }
635  
636 =head2 clean_fasta_header
637  
638 Rename given symbols to repalcement string.
639 Because, some symbols in fasta header will cause unexpected result.
640  
641 Example:
642  
643 my $file = "test.fa";
644 my $n = clean_fasta_header($file, "$file.rename.fa");
645 # replace any symbol in (\/:*?"<>|) with '', i.e. deleting.
646 # my $n = clean_fasta_header($file, "$file.rename.fa", '', '\/:*?"<>|');
647 print "$n records renamed\n";
648  
649 =cut
650  
651 sub clean_fasta_header {
652 0 ▶ my ( $file, $outfile, $replacement, $symbols ) = @_;
Subroutine clean_fasta_header: not called CC=9 SCAR=45.0
Pod documented
653 0 ▶ $replacement = "_" unless defined $replacement;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replacement 0 0 0
654  
655 0 my @default = split //, '\/:*?"<>|';
656 0 ▶ $symbols = \@default unless defined $symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $symbols 0 0 0
657 0 ▶ unless ( ref $symbols eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $symbols eq "ARRAY") 0 0 0
658 0 warn "symbols should be ref of array\n";
659 0 return 0;
660 }
661 0 my $re = join '', map { quotemeta $_ } @$symbols;
662 0 ▶ open my $fh, "<", $file or die "fail to open file: $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "fail to open file: $file\n" 0 0 0
663 0 ▶ open my $fh2, ">", $outfile or die "fail to wirte file: $outfile\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, ">", $outfile or die "fail to wirte file: $outfile\n" 0 0 0
664  
665 0 my $head = '';
666 0 my $n = 0;
667 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
668 0 ▶ if (/^\s*>(.*)\r?\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*>(.*)\r?\n/) { } 0 0 0
669 0 $head = $1;
670 0 ▶ if ( $head =~ /[$re]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head =~ /[$re]/) 0 0 0
671 0 $head =~ s/[$re]/$replacement/g;
672 0 $n++;
673 }
674 0 print $fh2 ">$head\n";
675 }
676 else {
677 0 print $fh2 $_;
678 }
679 }
680 0 close $fh;
681 0 close $fh2;
682  
683 0 return $n;
684 }
685  
686 1 1;
↑ Summary BioUtil/Util.pm »