Mercurial > repos > yating-l > rename_scaffolds
comparison rename.py @ 1:0d13e4410c3d draft
planemo upload commit 68b7e251486853b77396932b588c20f389c366d2
| author | yating-l |
|---|---|
| date | Mon, 18 Jun 2018 18:36:00 -0400 |
| parents | 65c9ce351343 |
| children | 8fdd3e06e1ec |
comparison
equal
deleted
inserted
replaced
| 0:65c9ce351343 | 1:0d13e4410c3d |
|---|---|
| 1 """ | 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 | 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 """ | 3 """ |
| 4 import sys | 4 import sys |
| 5 import csv | |
| 5 from collections import OrderedDict | 6 from collections import OrderedDict |
| 6 | 7 |
| 7 def rename(inputfile, outputfile, indexfile): | 8 def rename(inputfile, outputfile, writer): |
| 8 namemap = OrderedDict() | 9 namemap = OrderedDict() |
| 9 with open(outputfile, 'w') as out: | 10 with open(outputfile, 'w') as out: |
| 10 with open(inputfile, 'r') as rf: | 11 with open(inputfile, 'r') as rf: |
| 11 lines = rf.readlines() | 12 lines = rf.readlines() |
| 12 i = 1 | 13 i = 1 |
| 14 if ">" in line: | 15 if ">" in line: |
| 15 oldname = line[1:].rstrip() | 16 oldname = line[1:].rstrip() |
| 16 newname = "scaffold_" + str(i) | 17 newname = "scaffold_" + str(i) |
| 17 line = ">" + newname | 18 line = ">" + newname |
| 18 i = i+1 | 19 i = i+1 |
| 19 namemap[oldname] = newname | 20 writer.writerow([oldname, newname]) |
| 20 #TODO: Add line breaks to chromosome sequences that are in a single line | 21 #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") | 22 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 | 23 |
| 29 def main(): | 24 def main(): |
| 30 inputfile = str(sys.argv[1]) | 25 inputfile = str(sys.argv[1]) |
| 31 outputfile = str(sys.argv[2]) | 26 outputfile = str(sys.argv[2]) |
| 32 indexfile = str(sys.argv[3]) | 27 indexfile = str(sys.argv[3]) |
| 33 rename(inputfile, outputfile, indexfile) | 28 csvfile = open(indexfile, 'w') |
| 29 fieldnames = ['Original sequence name', 'Renamed sequence name'] | |
| 30 writer = csv.writer(csvfile) | |
| 31 writer.writerow(fieldnames) | |
| 32 rename(inputfile, outputfile, writer) | |
| 34 | 33 |
| 35 if __name__ == "__main__": | 34 if __name__ == "__main__": |
| 36 main() | 35 main() |
| 37 | 36 |
| 38 | 37 |
