You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询基于Python的DNA序列潜在基因判定程序的功能与实现

Python Script (gene.py) to Identify Potential DNA Genes

Alright, let's build this program step by step. We need to validate a user-provided DNA sequence against four specific criteria to determine if it's a potential gene. Here's how to approach each requirement and put it all together.

Breakdown of the Checks

First, let's map each criterion to a concrete code check:

  • Start with ATG: Verify the first 3 characters of the sequence are exactly ATG.
  • Length is a multiple of 3: Use the modulo operator (%) to check if len(sequence) % 3 == 0.
  • End with a stop codon: The last 3 characters must be one of TAG, TAA, or TGA—using a set for these makes lookups quick.
  • No intermediate stop codons: Iterate through the sequence between the start codon and the final stop codon, checking every 3-character chunk to ensure none are stop codons.

Complete Code

Save this as gene.py:

def is_potential_gene(dna_sequence):
    # Define the valid stop codons
    stop_codons = {'TAG', 'TAA', 'TGA'}
    
    # Check 1: Sequence starts with the start codon ATG
    if not dna_sequence.startswith('ATG'):
        return False
    
    # Check 2: Length is a multiple of 3
    if len(dna_sequence) % 3 != 0:
        return False
    
    # Check 3: Sequence ends with a valid stop codon
    if dna_sequence[-3:] not in stop_codons:
        return False
    
    # Check 4: No stop codons in the middle (between start and final stop)
    for i in range(3, len(dna_sequence) - 3, 3):
        current_codon = dna_sequence[i:i+3]
        if current_codon in stop_codons:
            return False
    
    # All checks passed
    return True

def main():
    # Get and clean user input (convert to uppercase to handle lowercase inputs)
    dna_input = input("Enter your DNA sequence: ").strip().upper()
    
    # Optional: Validate that all characters are valid DNA bases
    valid_bases = {'A', 'C', 'G', 'T'}
    for base in dna_input:
        if base not in valid_bases:
            print("Error: Invalid DNA base detected. Only A, C, G, T are allowed.")
            return
    
    # Run the check and output the result
    if is_potential_gene(dna_input):
        print("This sequence is a potential gene!")
    else:
        print("This sequence is NOT a potential gene.")

if __name__ == "__main__":
    main()

How It Works

  • The is_potential_gene function handles all four validation steps in order—we fail fast as soon as any check fails, which is efficient.
  • The main function handles user input: it cleans the input (trims whitespace, converts to uppercase), validates that all characters are valid DNA bases, then calls the validation function and prints the result.
  • For the intermediate stop codon check, we start at index 3 (right after the start codon) and stop at len(dna_sequence) - 3 (right before the final stop codon), stepping by 3 each time to grab each codon in between.

Test Examples

Let's test a few cases to make sure it works:

  • Valid sequence: ATGCGTAAATGA → Output: "This sequence is a potential gene!"
  • Missing start codon: CGTAAATGA → Output: "This sequence is NOT a potential gene."
  • Length not divisible by 3: ATGCGTAAATG → Output: "This sequence is NOT a potential gene."
  • Wrong stop codon: ATGCGTAAAATG → Output: "This sequence is NOT a potential gene."
  • Intermediate stop codon: ATGTAGAAATGA → Output: "This sequence is NOT a potential gene."

内容的提问来源于stack exchange,提问作者Gngowi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:36:12