Skip to content
scipython.com

P2.3.1GC-content of a nucleotide sequence

(a) Given a string representing a base-pair sequence (i.e. containing only the letters A, G, C and T), determine the fraction of G and C bases in the sequence.

[Hint: strings have a count method, returning the number of occurences of a substring]

(b) Using only string methods, devise a way to determine if a nucleotide sequence is a palindrome in the sense that it is equal to the its own complementary sequence read backwards. For example, the sequence TGGATCCA is palindromic because its complement is ACCTAGGT which is the same as the original sequence backwards. The complementary base pairs are (A, T) and (C, G).

Show solution

(a) For example,

>>> seq = 'GATTACCTGAGCCTAC'
>>> (seq.count('G') + seq.count('C')) / len(seq)
0.5

(b) One solution is to create the complementary sequence using replace:

>>> seq = 'ACCTAGGT'
>>> seqc = seq.replace('A', 't').replace('C', 'g').replace('G', 'c')
              .replace('T', 'a').upper()
>>> seqc
'TGGATCCA'
>>> seqc == seq[::-1]
True

Note that this would not be a suitable method for long sequences because each replace operation generates an entirely new string in memory (recall that str is an immutable type).