Python statistics have many obstacles in our use. The problems related to the DNA sequence need to be learned continuously. Next we will introduce the relevant issues and hope to gain some gains in the usage of Python statistics in the future.
Given A bunch of DNA sequences, that is, A string consisting of characters A, C, G, and T, count the occurrence frequency of all subsequences with A length of n. For example, ACGTACGT, the sub-sequence length is 2, so AC = 2, CG = 2, GT = 2, TA = 1, and the sub-sequence frequency of the remaining length is 0.
The first thing that comes to mind is to create a dictionary. The key is all possible subsequences, and the value is the frequency at which this subsequence appears. However, when the sub-sequence is long, for example, n = 8, a dictionary with 65536 (4 to the power of 8) key-value pair is required, the length of each key is 8 characters. In this case, ms is a waste of memory ..
As a result, all subsequences whose lengths are n are ordered and continuous, so they can be mapped to a list with 4 n ing power. Make A = 0, C = 1, G = 2, T = 3, then, the sub-sequence ACGT is converted to 0*4 ^ 3 + 1*4 ^ 2 + 2*4 + 3 = 27 and mapped to the 27th-bit list. In this way, the index of the list corresponds to the subsequence, and the index position of the list stores the occurrence frequency of the subsequence.
Therefore, we need to create two dictionaries. Python statistics indicate the one-to-one correspondence between ACGT and 0123:
- i2mD = {0:'A', 1:'C', 2:'G', 3:'T'}
- m2iD = dict(A=0,C=1,G=2,T=3)
- # This is just another way to initialize a dictionary
And the following subsequences are mapped to integer functions:
- def motif2int(motif):
- '''convert a sub-sequence/motif to a non-negative integer'''
- total = 0
- for i, letter in enumerate(motif):
- total += m2iD[letter]*4**(len(motif)-i-1)
- return total
- Test:
- >>> motif2int('ACGT')
- 27
The above is an introduction to Python statistics. Although we use subsequences as positive integers to store them internally, actually, this integer is not stored in memory, but represented by its index in the list.) to make it easier for scientists to see, it is better to convert the data back to the subsequence during output.