Write a python script that makes a negative strand(so inverted atcg, backwards) out of…
- User
Write a python script that makes a negative strand(so inverted atcg, backwards) out of positive DNA strand FASTA sequence file.
Assistant
Here's a Python script that creates a negative strand (reverse complement) from a positive DNA strand in a FASTA file:
def reverse_complement(sequence): """Return the reverse complement of a DNA sequence.""" complement = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C', 'a': 't', 't': 'a', 'c': 'g', 'g': 'c', 'N': 'N', 'n': 'n'} return ''.join(complement.get(base, base) for base in reversed(sequence)) def process_fasta(input_file, output_file): """Read a FASTA file and write the reverse complement to a new file.""" with open(input_file, 'r') as infile, open(output_file, 'w') as outfile: header = "" sequence = "" for line in infile: line = line.strip() if line.startswith('>'): # If we've already collected a sequence, write it out if sequence: outfile.write(f"{header} [negative strand]\n") outfile.write(reverse_complement(sequence) + "\n") sequence = "" header = line else: sequence += line # Don't forget the last sequence if sequence: outfile.write(f"{header} [negative strand]\n") outfile.write(reverse_complement(sequence) + "\n") if __name__ == "__main__": import sys if len(sys.argv) != 3: print("Usage: python script.py input.fasta output.fasta") sys.exit(1) input_file = sys.argv[1] output_file = sys.argv[2] process_fasta(input_file, output_file) print(f"Negative strand written to {output_file}")Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.