You are currently viewing Calculating GC Content in DNA Sequences

Calculating GC Content in DNA Sequences

You can define a Python function to calculate the GC content (percentage of guanine and cytosine nucleotides) in a DNA sequence as follows:

def calculate_gc_content(sequence):
    # Count the number of 'G' and 'C' characters in the sequence
    gc_count = sequence.count('G') + sequence.count('C')
    
    # Calculate the total length of the sequence
    sequence_length = len(sequence)
    
    # Calculate the GC content as a percentage
    gc_content = (gc_count / sequence_length) * 100.0
    
    return gc_content

# Example usage:
dna_sequence = "ATGCGCTAGCTAGCTGCTAGCTGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCT"
gc_percentage = calculate_gc_content(dna_sequence)
print(f"GC Content: {gc_percentage:.2f}%")

In this code:

  1. The calculate_gc_content function takes a DNA sequence as input.
  2. It counts the occurrences of ‘G’ and ‘C’ in the sequence using the count method.
  3. It calculates the total length of the sequence.
  4. It calculates the GC content as a percentage by dividing the GC count by the sequence length and multiplying by 100.

You can use this function to calculate the GC content for any DNA sequence you have.

Leave a Reply