Mercurial > repos > yating-l > rename_scaffolds
changeset 4:e35a3509c160 draft
planemo upload commit 3675b4447d11fb1cd75d505886e1bf693f9d07f5
author | yating-l |
---|---|
date | Thu, 26 Jul 2018 15:46:24 -0400 |
parents | 9529a207d704 |
children | 7c8b327f298c |
files | rename.py rename_scaffold.xml test-data/fixed_reference_with_tab.fasta test-data/sequence_with_tab.fa |
diffstat | 4 files changed, 40 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/rename.py Thu Jul 26 13:02:23 2018 -0400 +++ b/rename.py Thu Jul 26 15:46:24 2018 -0400 @@ -1,6 +1,6 @@ """ 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 -Call truncate to truncate the scaffold names that are more than 31 characters. Replace non-ASCII character with '_' +Call truncate to truncate the scaffold names that are more than 31 characters. Replace each invalid character (non-ASCII, '\t', '\n', '\x0b', '\x0c', '\r') with '_' """ import sys import csv @@ -21,7 +21,7 @@ writer.writerow([oldname.encode('utf-8'), newname]) out.write(line) -def truncate(inputFile, outputFile): +def truncate(inputFile, outputFile, valid_characters): names = [] with codecs.open(outputFile, 'w', encoding='utf-8') as out: with codecs.open(inputFile, 'r', encoding='utf-8') as rf: @@ -30,7 +30,7 @@ if ">" in l: print l.encode('utf-8') name = l[1:].rstrip() - name = substituteNonAscii(name) + name = substituteNonAscii(name, valid_characters) if len(name) > 31: name = name[:31] print "\tTruncate the scaffold name to less than 31 characters: %s" % name @@ -41,11 +41,11 @@ print "======================\n" out.write(l) -def substituteNonAscii(str): +def substituteNonAscii(str, valid_charaters): l = [] for c in str: - if c not in string.printable: - print "\tSubstitute Non-ASCII character %s with _" % c.encode('utf-8') + if c not in valid_charaters: + print "\tSubstitute invalid character %s with _" % c.encode('utf-8') c = '_' l.append(c) return "".join(l) @@ -54,13 +54,14 @@ inputfile = str(sys.argv[1]) manipulate = str(sys.argv[2]) outputfile = str(sys.argv[3]) + valid_characters = string.letters + string.punctuation + string.digits + ' ' if manipulate == "rename": indexfile = str(sys.argv[4]) csvfile = open(indexfile, 'w') writer = csv.writer(csvfile) rename(inputfile, outputfile, writer) elif manipulate == "truncate": - truncate(inputfile, outputfile) + truncate(inputfile, outputfile, valid_characters) if __name__ == "__main__": main()
--- a/rename_scaffold.xml Thu Jul 26 13:02:23 2018 -0400 +++ b/rename_scaffold.xml Thu Jul 26 15:46:24 2018 -0400 @@ -1,4 +1,4 @@ -<tool id="rename_scaffold" name="rename the scaffolds" version="2.0"> +<tool id="rename_scaffold" name="rename the scaffolds" version="2.1"> <description>a Galaxy tool to rename or truncate the scaffold names in the reference genome so that they won't exceed 31 characters</description> <stdio> <exit_code range="1:" /> @@ -55,11 +55,17 @@ <param name="input" value="sequence_with_noascii_name_conflict.fa" /> <param name="manipulate_selector" value="truncate" /> </test> + <test> + <!-- Test input with tab --> + <param name="input" value="sequence_with_tab.fa" /> + <param name="manipulate_selector" value="truncate" /> + <output name="output" file="fixed_reference_with_tab.fasta" /> + </test> </tests> <help><![CDATA[ This tool is to rename scaffolds in the reference genome so that the sequence names are less than 31 characters. Rename all scaffolds to scaffold_1, scaffold_2, ..., scaffold_N and also output a name mapping file. -Or truncate the scaffold names that are more than 31 characters and replace each non-ASCII character with '_' +Or truncate the scaffold names that are more than 31 characters and replace each invalid character (non-ASCII, '\\t', '\\n', '\\x0b', '\\x0c', '\\r') with '_' ]]></help> <citations> </citations>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/fixed_reference_with_tab.fasta Thu Jul 26 15:46:24 2018 -0400 @@ -0,0 +1,12 @@ +>contig1'abcd__* _ {/& +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT +>contig2_| 757_763.ii215_ii225St +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT +>contig3 | prediction on sequenc +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT +>contig4 | 757_763.ii215_ii225St +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sequence_with_tab.fa Thu Jul 26 15:46:24 2018 -0400 @@ -0,0 +1,12 @@ +>contig1'abcdé * _ {/& +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT +>contig2 | 757â763.ii215âii225Stanke, M. and Waack +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT +>contig3 | prediction on sequence number 1 (length = 1992969, name = scaffold1|size1992969) ----- # # Constraints/Hints: # (none) # Predicted genes for sequence number 1 on both strands # start gene g1 scaffold1|size1992969 AUGUSTUS gene 17453 19382 0.11 + . g6 scaffold1|size1992969 AUGUSTUS transcript 17453 19382 0.11 + . g6.t1 scaffold1|size1992969 AUGUSTUS start_codon 17453 17455 +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT +>contig4 | 757£763.ii215ôii225Stanke, M. and Waack +AAAACTAATTTTATCAAAATCGGACAACTATATCATATAGCTGCCATACG +AACGATCGGAAAATTGGTAAGTAAATAATTAAAAATATTATATCTTTGGT