Mercurial > repos > yating-l > rename_scaffolds
comparison rename.py @ 3:9529a207d704 draft
planemo upload commit fb7ce0e9f333b753a39ff171a8fbf39c3b3a069c
| author | yating-l |
|---|---|
| date | Thu, 26 Jul 2018 13:02:23 -0400 |
| parents | 8fdd3e06e1ec |
| children | e35a3509c160 |
comparison
equal
deleted
inserted
replaced
| 2:8fdd3e06e1ec | 3:9529a207d704 |
|---|---|
| 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 Call truncate to truncate the scaffold names that are more than 31 characters. Replace non-ASCII character with '_' | |
| 3 """ | 4 """ |
| 4 import sys | 5 import sys |
| 5 import csv | 6 import csv |
| 7 import codecs | |
| 8 import string | |
| 6 | 9 |
| 7 def rename(inputfile, outputfile, writer): | 10 def rename(inputfile, outputfile, writer): |
| 8 with open(outputfile, 'w') as out: | 11 with open(outputfile, 'w') as out: |
| 9 with open(inputfile, 'r') as rf: | 12 with codecs.open(inputfile, 'r', encoding='utf-8') as rf: |
| 10 lines = rf.readlines() | 13 lines = rf.readlines() |
| 11 i = 1 | 14 i = 1 |
| 12 for line in lines: | 15 for line in lines: |
| 13 if ">" in line: | 16 if ">" in line: |
| 14 oldname = line[1:].rstrip() | 17 oldname = line[1:].rstrip() |
| 15 newname = "scaffold_" + str(i) | 18 newname = "scaffold_" + str(i) |
| 16 line = ">" + newname + "\n" | 19 line = ">" + newname + "\n" |
| 17 i = i+1 | 20 i = i+1 |
| 18 writer.writerow([oldname, newname]) | 21 writer.writerow([oldname.encode('utf-8'), newname]) |
| 19 out.write(line) | 22 out.write(line) |
| 23 | |
| 24 def truncate(inputFile, outputFile): | |
| 25 names = [] | |
| 26 with codecs.open(outputFile, 'w', encoding='utf-8') as out: | |
| 27 with codecs.open(inputFile, 'r', encoding='utf-8') as rf: | |
| 28 lines = rf.readlines() | |
| 29 for l in lines: | |
| 30 if ">" in l: | |
| 31 print l.encode('utf-8') | |
| 32 name = l[1:].rstrip() | |
| 33 name = substituteNonAscii(name) | |
| 34 if len(name) > 31: | |
| 35 name = name[:31] | |
| 36 print "\tTruncate the scaffold name to less than 31 characters: %s" % name | |
| 37 if name in names: | |
| 38 sys.exit("Name conflict! Name " + name + " already exist.") | |
| 39 names.append(name) | |
| 40 l = ">" + name + "\n" | |
| 41 print "======================\n" | |
| 42 out.write(l) | |
| 43 | |
| 44 def substituteNonAscii(str): | |
| 45 l = [] | |
| 46 for c in str: | |
| 47 if c not in string.printable: | |
| 48 print "\tSubstitute Non-ASCII character %s with _" % c.encode('utf-8') | |
| 49 c = '_' | |
| 50 l.append(c) | |
| 51 return "".join(l) | |
| 20 | 52 |
| 21 def main(): | 53 def main(): |
| 22 inputfile = str(sys.argv[1]) | 54 inputfile = str(sys.argv[1]) |
| 23 outputfile = str(sys.argv[2]) | 55 manipulate = str(sys.argv[2]) |
| 24 indexfile = str(sys.argv[3]) | 56 outputfile = str(sys.argv[3]) |
| 25 csvfile = open(indexfile, 'w') | 57 if manipulate == "rename": |
| 26 writer = csv.writer(csvfile) | 58 indexfile = str(sys.argv[4]) |
| 27 rename(inputfile, outputfile, writer) | 59 csvfile = open(indexfile, 'w') |
| 60 writer = csv.writer(csvfile) | |
| 61 rename(inputfile, outputfile, writer) | |
| 62 elif manipulate == "truncate": | |
| 63 truncate(inputfile, outputfile) | |
| 28 | 64 |
| 29 if __name__ == "__main__": | 65 if __name__ == "__main__": |
| 30 main() | 66 main() |
| 31 | 67 |
| 32 | 68 |
