# HG changeset patch # User earlhaminst # Date 1488543773 18000 # Node ID dd268de3a10794c16380cdf1a618a5bdf93bc7b7 # Parent 7ea4df039a535e9c06e19311cffe2147b0f1e11f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/TreeBest commit 988b1fc1cb8739e45648465adbf099f3fdaf87f8 diff -r 7ea4df039a53 -r dd268de3a107 fasta_header_converter.py --- a/fasta_header_converter.py Wed Feb 22 05:48:02 2017 -0500 +++ b/fasta_header_converter.py Fri Mar 03 07:22:53 2017 -0500 @@ -1,7 +1,28 @@ from __future__ import print_function +import collections import json import optparse +import sys + +Sequence = collections.namedtuple('Sequence', ['header', 'sequence']) + + +def FASTAReader_gen(fasta_filename): + with open(fasta_filename) as fasta_file: + line = fasta_file.readline() + while True: + if not line: + return + assert line.startswith('>'), "FASTA headers must start with >" + header = line.rstrip() + sequence_parts = [] + line = fasta_file.readline() + while line and line[0] != '>': + sequence_parts.append(line.rstrip()) + line = fasta_file.readline() + sequence = "\n".join(sequence_parts) + yield Sequence(header, sequence) def read_gene_info(gene_info): @@ -17,23 +38,25 @@ help='Gene feature information in JSON format') parser.add_option('-f', '--fasta', dest="input_fasta_filename", help='Sequences in FASTA format') +parser.add_option('-o', '--output', dest="output_fasta_filename", + help='Output FASTA file name') options, args = parser.parse_args() if options.input_gene_filename is None: raise Exception('-j option must be specified') - if options.input_fasta_filename is None: raise Exception('-f option must be specified') +if options.output_fasta_filename is None: + raise Exception('-o option must be specified') with open(options.input_gene_filename) as json_fh: gene_info = json.load(json_fh) transcript_species_dict = read_gene_info(gene_info) -with open(options.input_fasta_filename) as fasta_fh: - for line in fasta_fh: - line = line.rstrip() - if line.startswith(">"): - name = line[1:].lstrip() - print(">" + name + "_" + transcript_species_dict[name]) - else: - print(line) +with open(options.output_fasta_filename, 'w') as output_fasta_file: + for entry in FASTAReader_gen(options.input_fasta_filename): + name = entry.header[1:].lstrip() + if name not in transcript_species_dict: + print("Transcript '%s' not found in the gene feature information" % name, file=sys.stderr) + continue + output_fasta_file.write(">%s_%s\n%s\n" % (name, transcript_species_dict[name], entry.sequence)) diff -r 7ea4df039a53 -r dd268de3a107 fasta_header_converter.xml --- a/fasta_header_converter.xml Wed Feb 22 05:48:02 2017 -0500 +++ b/fasta_header_converter.xml Fri Mar 03 07:22:53 2017 -0500 @@ -1,11 +1,11 @@ to append species information - + '$outputFile' +-o '$outputFile' ]]>