annotate rename.py @ 0:1a0e39acb62f draft

planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
author yating-l
date Mon, 19 Dec 2016 17:14:55 -0500
parents
children 148e353417e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
1 """
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
2 Call rename to rename scaffolds in reference genome so that the sequence names are less than 31 characters. Rename all scaffolds to scaffold_1, scaffold_2, ..., scaffold_N and provide a name mapping file
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
3 """
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
4 import sys
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
5 from collections import OrderedDict
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
6
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
7 def rename(inputfile, outputfile, indexfile):
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
8 namemap = OrderedDict()
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
9 with open(outputfile, 'w') as out:
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
10 with open(inputfile, 'r') as rf:
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
11 lines = rf.readlines()
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
12 i = 1
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
13 for line in lines:
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
14 if ">" in line:
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
15 oldname = line[1:].rstrip()
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
16 newname = "scaffold_" + str(i)
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
17 line = ">" + newname + "\n"
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
18 i = i+1
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
19 namemap[oldname] = newname
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
20 out.write(line)
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
21 with open(indexfile, 'w') as index:
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
22 for k in namemap:
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
23 index.write(k + "=>" + namemap[k] + "\n")
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
24
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
25 def main():
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
26 inputfile = str(sys.argv[1])
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
27 outputfile = str(sys.argv[2])
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
28 indexfile = str(sys.argv[3])
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
29 rename(inputfile, outputfile, indexfile)
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
30
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
31 if __name__ == "__main__":
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
32 main()
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
33
1a0e39acb62f planemo upload commit 3431b38459716fd36c193c1035ede040f7ca2e87-dirty
yating-l
parents:
diff changeset
34