Alternative content
def multi_to_single_line_fasta(input_filename, output_filename):
try:
with open(input_filename, 'r') as input_file:
with open(output_filename, 'w') as output_file:
current_sequence = ""
for line in input_file:
if line.startswith('>'):
# If a header line, write the previous sequence and then the new header
if current_sequence:
output_file.write(current_sequence + '\n')
output_file.write(line.strip() + '\n')
current_sequence = ""
else:
# If a sequence line, concatenate to the current sequence
current_sequence += line.strip()
# Write the last sequence
if current_sequence:
output_file.write(current_sequence + '\n')
print(f"Successfully converted {input_filename} to {output_filename} in single-line FASTA format.")
except FileNotFoundError:
print(f"Error: File '{input_filename}' not found.")
# Example usage:
# multi_to_single_line_fasta('multi_line.fasta', 'single_line.fasta')