-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobo-sed.pl
executable file
·102 lines (89 loc) · 1.8 KB
/
obo-sed.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
#!/usr/bin/perl -w
use strict;
my %tag_h=();
my $regexp = '';
my $noheader;
my $negate;
my $count;
my $del = "\n\n";
while ($ARGV[0] =~ /^\-.+/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
if ($opt eq '-r' || $opt eq '--regexp') {
$regexp = shift @ARGV;
}
if ($opt eq '-c' || $opt eq '--count') {
$count = 1;
}
if ($opt eq '--noheader') {
$noheader = 1;
}
if ($opt eq '--windowscr') {
$del = "\r\n\r\n";
}
if ($opt eq '-v' || $opt eq '--neg') {
$negate = 1;
}
}
my $sed = shift;
my $repl = eval "sub {$sed}";
my $n = 0;
if (!@ARGV) {
@ARGV=('-');
}
my $n_stanzas = 0;
while (@ARGV) {
my $f = pop @ARGV;
if ($f eq '-') {
*F=*STDIN;
}
else {
open(F,$f) || die $f;
}
my $hdr = 0;
my $s = '';
while(<F>) {
$s .= $_;
}
my @toks = split(/$del/,$s);
my $num = scalar(@toks);
foreach (@toks) {
$n_stanzas++;
if (!$hdr && $_ !~ /^\[/) {
print "$_$del" unless $noheader || $count;
$hdr = 1;
}
else {
if (!$regexp || /$regexp/) {
#$repl->($_);
&$repl;
}
$n++;
if (!$count) {
print;
print $del unless $n_stanzas == $num;
}
}
}
}
if ($count) {
print "$n\n";
}
print STDERR "num: $n_stanzas\n";
exit 0;
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn [--noheader] [--neg] [--r REGULAR-EXPRESSION] SED-EXPRESSION OBO-FILE
search an replace stanzas from obo files
Example:
$sn -r 'namespace: molecular_function' 's/ activity//' go.obo
EOM
}