-
Notifications
You must be signed in to change notification settings - Fork 1
/
bedNameRewrite
executable file
·49 lines (37 loc) · 1.15 KB
/
bedNameRewrite
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
#!/usr/bin/env python
from sys import *
import sys
from textwrap import *
import re
from optparse import *
# === command line interface, options and help ===
parser = OptionParser("usage: %prog [options] \n change bed names, run with no options to see example")
(options, args) = parser.parse_args()
if len(argv)<=3:
print "change the bed name fields to something else"
print " SYNTAX: "
print " fastareformatname bed-infile bed-outfile <regex-source> <regex-target>"
print " EXAMPLE:"
print " bedNameRewrite jgi-exons.bed jgi-exons2.bed '(.*_exon_[0-9]*).*' '\1'"
print " ... will replace anycharacters_range=<string> by <string>"
exit()
fin = argv[1]
fout = argv[2]
regex1 = argv[3]
regex2 = argv[4]
if fin=="stdin":
lines = sys.stdin
else:
lines = open(fin, "r")
if fout=="stdout":
out = sys.stdout
else:
out = open(fout, "w")
for l in lines:
if l.startswith("#") or l.startswith("track") or l.startswith("browser") or l.startswith("\n") or l.startswith("\r"):
out.write(l)
continue
f = l.split()
newname = re.sub(regex1, regex2, f[3])
f[3] = newname
out.write ("\t".join(f)+"\n")