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:
- The
calculate_gc_contentfunction takes a DNA sequence as input. - It counts the occurrences of ‘G’ and ‘C’ in the sequence using the
countmethod. - It calculates the total length of the sequence.
- 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.
