-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathid_repl.pl
executable file
·51 lines (45 loc) · 1.17 KB
/
id_repl.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
#!/usr/bin/perl
use strict;
use Getopt::Std;
use FindBin;use lib $FindBin::Bin;
my $usage = q/Usage:
id_repl.pl <ID2replID.tab> ...
Replace IDs with new IDs based on a 2 column correspondence table
mapping the IDs to its replacement (tab, space or comma delimited).
Outputs the result of such replacement applied to the input data.
/;
umask 0002;
getopts('o:') || die($usage."\n");
my $outfile=$Getopt::Std::opt_o;
if ($outfile) {
open(OUTF, '>'.$outfile) || die("Error creating output file $outfile\n");
select(OUTF);
}
# --
my $rtab=shift(@ARGV);
die("${usage}Error: no ID mapping table provided!\n") unless $rtab && -f $rtab;
my %map; # oldID => newID
open(TAB, $rtab) || die("Error opening file $rtab!\n$!\n");
while (<TAB>) {
chomp;
next unless length>2;
my ($id, $nid)=split(/[ \t\,]/);
die("Error: invalid ID mapping on line:\n$_\n") unless length($id)>0;
die("Error: duplicate entry for ID $id \n") if exists($map{$id});
$map{$id}=$nid;
}
close(TAB);
my @ids=keys(%map);
while (<>) {
foreach my $id (@ids) {
my $nid=$map{$id};
s/\b$id\b/$nid/g;
}
print $_;
}
# --
if ($outfile) {
select(STDOUT);
close(OUTF);
}
#************ Subroutines **************