简体   繁体   中英

Python String manipulation from a file and writing into a new file after checking the string

I'm trying to help my partner with his Python class, but I don't know much about Python. Here's the problem he has been stuck for hours, any help would be appreciated!

Question: The program will accept a text file of DNA sequences (1 sequence per line in the file) and determine if the sequence is valid when the following criteria is met: 1. length is a multiple of 3. 2. starts with ATG. 3. ends with TAG. The program will then write the DNA sequence followed by 'True' or 'False' to a new file (each output on a separate line). Example: Input sequence: 'ATGCGCCTGCGTCTGTACTAG' from input file. Output: 'ATGCGCCTGCGTCTGTACTAG True' to the output file.

def check_sequence(sequence):
    if len(sequence) % 3:
        return False
    if sequence[:3] != 'ATG':
        return False
    if sequence[-3:] != 'TAG':
        return False


def process_file(input_file_path, output_file_path):
    with open(input_file_path) as input_file:
        with open(output_file_path, 'w') as output_file:
            for map(str.rstrip, input_file):
                output_file.write(line)
                output_file.write(' ')
                output_file.write(str(check_sequence(line)))
                output_file.write('\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM