# HG changeset patch # User devteam # Date 1583065402 0 # Node ID ff4751ce764da9d8f4815c9205e2ee247e0efcd7 # Parent 5cabbe4cfaf4e1d758a977dc663e6265ac24706b "planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_to_tabular commit cd1ed08574b749eee2a3f6e6151dbb0c8ca15bbf" diff -r 5cabbe4cfaf4 -r ff4751ce764d fasta_to_tabular.py --- a/fasta_to_tabular.py Tue Oct 13 12:20:15 2015 -0400 +++ b/fasta_to_tabular.py Sun Mar 01 12:23:22 2020 +0000 @@ -6,52 +6,53 @@ format convert: fasta to tabular """ -import sys, os +import sys + -def stop_err( msg ): - sys.stderr.write( msg ) - sys.exit() +def stop_err(msg): + sys.exit(msg) + def __main__(): if len(sys.argv) != 5: stop_err("Wrong number of argument. Expect four (fasta, tabular, truncation, columns)") infile = sys.argv[1] outfile = sys.argv[2] - keep_first = int( sys.argv[3] ) - descr_split = int( sys.argv[4] ) - fasta_title = fasta_seq = '' + keep_first = int(sys.argv[3]) + descr_split = int(sys.argv[4]) if keep_first == 0: keep_first = None elif descr_split == 1: - #Added one for the ">" character - #(which is removed if using descr_split > 1) + # Added one for the ">" character + # (which is removed if using descr_split > 1) keep_first += 1 if descr_split < 1: stop_err("Bad description split value (should be 1 or more)") - out = open( outfile, 'w' ) - for i, line in enumerate( open( infile ) ): - line = line.rstrip( '\r\n' ) - if not line or line.startswith( '#' ): - continue - if line.startswith( '>' ): - #Don't want any existing tabs to trigger extra columns: - line = line.replace('\t', ' ') - if i > 0: - out.write('\n') - if descr_split == 1: - out.write(line[1:keep_first]) + with open(outfile, 'w') as out, open(infile) as in_fh: + for i, line in enumerate(in_fh): + line = line.rstrip('\r\n') + if not line or line.startswith('#'): + continue + if line.startswith('>'): + # Don't want any existing tabs to trigger extra columns: + line = line.replace('\t', ' ') + if i > 0: + out.write('\n') + if descr_split == 1: + out.write(line[1:keep_first]) + else: + words = line[1:].split(None, descr_split - 1) + # apply any truncation to first word (the id) + words[0] = words[0][0:keep_first] + # pad with empty columns if required + words += [""] * (descr_split - len(words)) + out.write("\t".join(words)) + out.write('\t') else: - words = line[1:].split(None, descr_split-1) - #apply any truncation to first word (the id) - words[0] = words[0][0:keep_first] - #pad with empty columns if required - words += [""]*(descr_split-len(words)) - out.write("\t".join(words)) - out.write('\t') - else: - out.write(line) - if i > 0: - out.write('\n') - out.close() + out.write(line) + if i > 0: + out.write('\n') -if __name__ == "__main__" : __main__() + +if __name__ == "__main__": + __main__() diff -r 5cabbe4cfaf4 -r ff4751ce764d fasta_to_tabular.xml --- a/fasta_to_tabular.xml Tue Oct 13 12:20:15 2015 -0400 +++ b/fasta_to_tabular.xml Sun Mar 01 12:23:22 2020 +0000 @@ -1,64 +1,67 @@ - - converter - fasta_to_tabular.py $input $output $keep_first $descr_columns - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + converter + + python + + +python '$__tool_directory__/fasta_to_tabular.py' '$input' '$output' $keep_first $descr_columns + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + + + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - + + + + + + + + + EYKX4VC02EQLO5 length=108 xy=1826_0455 region=2 run=R_2007_11_07_16_15_57_ TCCGCGCCGAGCATGCCCATCTTGGATTCCGGCGCGATGACCATCGCCCGCTCCACCACG TTCGGCCGGCCCTTCTCGTCGAGGAATGACACCAGCGCTTCGCCCACG - >EYKX4VC02D4GS2 length=60 xy=1573_3972 region=2 run=R_2007_11_07_16_15_57_ + >EYKX4VC02D4GS2 length=60 xy=1573_3972 region=2 run=R_2007_11_07_16_15_57_ AATAAAACTAAATCAGCAAAGACTGGCAAATACTCACAGGCTTATACAATACAAATGTAA Running this tool with the default settings will produce this (2 column output): @@ -124,5 +127,5 @@ Note the sequences have been truncated for display purposes in the above tables. - + ]]>