Mercurial > repos > yating-l > rename_scaffolds
comparison rename.py @ 0:65c9ce351343 draft
planemo upload commit af6c4dc7f6d6795f2b2db7299eb14ae1d5622ac1
author | yating-l |
---|---|
date | Fri, 20 Jan 2017 17:06:52 -0500 |
parents | |
children | 0d13e4410c3d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:65c9ce351343 |
---|---|
1 """ | |
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 | |
3 """ | |
4 import sys | |
5 from collections import OrderedDict | |
6 | |
7 def rename(inputfile, outputfile, indexfile): | |
8 namemap = OrderedDict() | |
9 with open(outputfile, 'w') as out: | |
10 with open(inputfile, 'r') as rf: | |
11 lines = rf.readlines() | |
12 i = 1 | |
13 for line in lines: | |
14 if ">" in line: | |
15 oldname = line[1:].rstrip() | |
16 newname = "scaffold_" + str(i) | |
17 line = ">" + newname | |
18 i = i+1 | |
19 namemap[oldname] = newname | |
20 #TODO: Add line breaks to chromosome sequences that are in a single line | |
21 #else: | |
22 #if (len(line) > 50): | |
23 #for | |
24 out.write(line.rstrip() + "\n") | |
25 with open(indexfile, 'w') as index: | |
26 for k in namemap: | |
27 index.write(k + "\t" + namemap[k] + "\n") | |
28 | |
29 def main(): | |
30 inputfile = str(sys.argv[1]) | |
31 outputfile = str(sys.argv[2]) | |
32 indexfile = str(sys.argv[3]) | |
33 rename(inputfile, outputfile, indexfile) | |
34 | |
35 if __name__ == "__main__": | |
36 main() | |
37 | |
38 |