-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathordinalHeaders.pl
executable file
·53 lines (48 loc) · 1.43 KB
/
ordinalHeaders.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env perl
# Rewrite FASTA headers to increasing ordinals: S1, S2, S3..
#
# Samuel S. Shepard ([email protected])
# 2015, Centers for Disease Control & Prevention
use Getopt::Long;
GetOptions(
'keep-annotation|A' => \$keepAnnot,
'annot-with-header|H' => \$headerAnnot,
'name-annotation-format|N=s' => \$nameAnnotation,
'only-name-format|O=s' => \$onlyName,
'pipe-format|P' => \$pipeFormat
);
open( IN, '<', $ARGV[0] ) or die("$0 ERROR: Cannot open $ARGV[0].\n");
if ( scalar(@ARGV) != 1 ) {
die("Usage:\n\tperl $0 <input.fasta> [-A] [-H] [-N <STR>] [-O <STR>]\n");
}
# PROCESS fasta data
$/ = ">";
$i = 0;
while ( $record = <IN> ) {
chomp($record);
@lines = split( /\r\n|\n|\r/, $record );
$oldHeader = shift(@lines);
$ordinal = 'S' . ( $i++ );
$sequence = join( '', @lines );
if ($keepAnnot) {
if ( $oldHeader =~ /{(.+)}/ ) {
$ordinal .= "{$1}";
}
} elsif ($headerAnnot) {
$ordinal .= "{$oldHeader}";
} elsif ($nameAnnotation) {
$ordinal = $nameAnnotation . '{' . $ordinal . '}';
} elsif ($onlyName) {
$ordinal = $onlyName;
} elsif ($pipeFormat) {
$ordinal .= '|' . $oldHeader;
}
if ( length($sequence) == 0 ) {
next;
} else {
print '>', $ordinal, "\n";
print $sequence, "\n";
}
}
close(IN);
####################