Mercurial > repos > yating-l > rename_scaffolds
diff rename.py @ 0:65c9ce351343 draft
planemo upload commit af6c4dc7f6d6795f2b2db7299eb14ae1d5622ac1
author | yating-l |
---|---|
date | Fri, 20 Jan 2017 17:06:52 -0500 |
parents | |
children | 0d13e4410c3d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rename.py Fri Jan 20 17:06:52 2017 -0500 @@ -0,0 +1,38 @@ +""" +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 +""" +import sys +from collections import OrderedDict + +def rename(inputfile, outputfile, indexfile): + namemap = OrderedDict() + with open(outputfile, 'w') as out: + with open(inputfile, 'r') as rf: + lines = rf.readlines() + i = 1 + for line in lines: + if ">" in line: + oldname = line[1:].rstrip() + newname = "scaffold_" + str(i) + line = ">" + newname + i = i+1 + namemap[oldname] = newname + #TODO: Add line breaks to chromosome sequences that are in a single line + #else: + #if (len(line) > 50): + #for + out.write(line.rstrip() + "\n") + with open(indexfile, 'w') as index: + for k in namemap: + index.write(k + "\t" + namemap[k] + "\n") + +def main(): + inputfile = str(sys.argv[1]) + outputfile = str(sys.argv[2]) + indexfile = str(sys.argv[3]) + rename(inputfile, outputfile, indexfile) + +if __name__ == "__main__": + main() + +