Mercurial > repos > yating-l > rename_scaffolds
comparison rename.py @ 5:7c8b327f298c draft
planemo upload commit f565a59d3e28d34d1caf326fcee83d04a939c359
| author | yating-l |
|---|---|
| date | Tue, 31 Jul 2018 13:50:21 -0400 |
| parents | e35a3509c160 |
| children | 2d143f0ac727 |
comparison
equal
deleted
inserted
replaced
| 4:e35a3509c160 | 5:7c8b327f298c |
|---|---|
| 19 line = ">" + newname + "\n" | 19 line = ">" + newname + "\n" |
| 20 i = i+1 | 20 i = i+1 |
| 21 writer.writerow([oldname.encode('utf-8'), newname]) | 21 writer.writerow([oldname.encode('utf-8'), newname]) |
| 22 out.write(line) | 22 out.write(line) |
| 23 | 23 |
| 24 def truncate(inputFile, outputFile, valid_characters): | 24 def truncate(inputFile, outputFile, valid_characters, writer): |
| 25 names = [] | 25 names = [] |
| 26 with codecs.open(outputFile, 'w', encoding='utf-8') as out: | 26 with open(outputFile, 'w') as out: |
| 27 with codecs.open(inputFile, 'r', encoding='utf-8') as rf: | 27 with codecs.open(inputFile, 'r', encoding='utf-8') as rf: |
| 28 lines = rf.readlines() | 28 lines = rf.readlines() |
| 29 for l in lines: | 29 for l in lines: |
| 30 if ">" in l: | 30 if ">" in l: |
| 31 print l.encode('utf-8') | 31 print l.encode('utf-8') |
| 32 name = l[1:].rstrip() | 32 oldname = l[1:].rstrip() |
| 33 name = substituteNonAscii(name, valid_characters) | 33 name = substituteNonAscii(oldname, valid_characters) |
| 34 if len(name) > 31: | 34 if len(name) > 31: |
| 35 name = name[:31] | 35 name = name[:31] |
| 36 print "\tTruncate the scaffold name to less than 31 characters: %s" % name | 36 print "\tTruncate the scaffold name to less than 31 characters: %s" % name |
| 37 if name in names: | 37 if name in names: |
| 38 sys.exit("Name conflict! Name " + name + " already exist.") | 38 sys.exit("Name conflict! Name " + name + " already exist.") |
| 39 writer.writerow([oldname.encode('utf-8'), name]) | |
| 39 names.append(name) | 40 names.append(name) |
| 40 l = ">" + name + "\n" | 41 l = ">" + name + "\n" |
| 41 print "======================\n" | 42 print "======================\n" |
| 42 out.write(l) | 43 out.write(l) |
| 43 | 44 |
| 52 | 53 |
| 53 def main(): | 54 def main(): |
| 54 inputfile = str(sys.argv[1]) | 55 inputfile = str(sys.argv[1]) |
| 55 manipulate = str(sys.argv[2]) | 56 manipulate = str(sys.argv[2]) |
| 56 outputfile = str(sys.argv[3]) | 57 outputfile = str(sys.argv[3]) |
| 57 valid_characters = string.letters + string.punctuation + string.digits + ' ' | 58 indexfile = str(sys.argv[4]) |
| 59 csvfile = open(indexfile, 'w') | |
| 60 writer = csv.writer(csvfile) | |
| 58 if manipulate == "rename": | 61 if manipulate == "rename": |
| 59 indexfile = str(sys.argv[4]) | |
| 60 csvfile = open(indexfile, 'w') | |
| 61 writer = csv.writer(csvfile) | |
| 62 rename(inputfile, outputfile, writer) | 62 rename(inputfile, outputfile, writer) |
| 63 elif manipulate == "truncate": | 63 elif manipulate == "truncate": |
| 64 truncate(inputfile, outputfile, valid_characters) | 64 valid_characters = string.letters + string.punctuation + string.digits + ' ' |
| 65 truncate(inputfile, outputfile, valid_characters, writer) | |
| 65 | 66 |
| 66 if __name__ == "__main__": | 67 if __name__ == "__main__": |
| 67 main() | 68 main() |
| 68 | 69 |
| 69 | 70 |
