-
Notifications
You must be signed in to change notification settings - Fork 10
/
rename_fasta.pl
168 lines (94 loc) · 2.87 KB
/
rename_fasta.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl -w
#
# rename_fasta.pl -- rename sequences ids in fasta format file.
#
# Author: Nowind
# Created: 2012-02-21
# Updated: 2014-08-25
# Version: 1.0.1
#
# Change logs:
# Version 1.0.0 13/05/30: The initial version.
# Version 1.0.1 14/08/25: Add support for paired ids in ref_list.
use strict;
use Data::Dumper;
use Getopt::Long;
use MyPerl::FileIO qw(:all);
##################### Main ####################
my $CMDLINE = "perl $0 @ARGV";
my $VERSION = '1.0.1';
my $HEADER = "##$CMDLINE\n##Version: $VERSION\n";
my $SOURCE = (scalar localtime()) . " Version: $VERSION";
my $word_wrap = 0;
my ($fasta_file, $ref_list, $output, $show_help);
GetOptions(
"i|fasta=s" => \$fasta_file,
"refer=s" => \$ref_list,
"output=s" => \$output,
"word-wrap=i" => \$word_wrap,
"help|?" => \$show_help,
);
unless( $fasta_file && $ref_list && !$show_help ) {
print <<EOF;
$0 -- rename sequences ids in fasta format file.
Version: $VERSION
Usage: perl $0 [options]
Options:
-i, --fasta <filename>
sequence file to be sorted in fasta format, required
-r, --refer <filename>
rename sequence name by related ids listed in this file
-o, --output <filename>
output fasta file, default to STDOUT
-w, --word-wrap <int>
maximum length of sequence to write per line, default each sequence
per line
-?, --help
show this help message
EOF
exit(1);
}
$|++;
if ($output) {
open (STDOUT, "> $output") || die $!;
}
print STDERR "# $0 v$VERSION\n# " . (scalar localtime()) . "\n";
my @new_ids = ();
my %new_ids = ();
if ($ref_list) {
print STDERR ">> Start parsing $ref_list ... ";
my $fh = getInputFilehandle($ref_list);
while (<$fh>)
{
next if (/#/ || /^\s+$/);
my ($id, $new_id) = (split /\s+/);
if ($new_id) {
$new_ids{$id} = $new_id;
}
else {
push @new_ids, $id;
}
}
print STDERR "done!\n";
}
print STDERR ">> Start reading $fasta_file ... ";
my @SEQs = ();
my @ids = parse_fasta_SEQs(\@SEQs, $fasta_file);
print STDERR "done!\n";
print STDERR ">> Start generating renamed file ... ";
for (my $i=0; $i<@ids; $i++)
{
my $id = $ids[$i];
unless($SEQs[$i]) {
print STDERR "Error: $fasta_file: no records found for $id!\n"; exit(2);
}
if (@new_ids > 0) {
print format_fasta_SEQs($new_ids[$i], \$SEQs[$i], $word_wrap);
}
else {
print format_fasta_SEQs($new_ids{$id}, \$SEQs[$i], $word_wrap);
}
}
print STDERR "done!\n";
print STDERR "# " . (scalar localtime()) . "\n";
######################### Sub #########################