Python statistics on how to sort DNA Sequences

Source: Internet
Author: User

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:

 
 
  1. i2mD = {0:'A', 1:'C', 2:'G', 3:'T'}  
  2. m2iD = dict(A=0,C=1,G=2,T=3)  
  3. # This is just another way to initialize a dictionary 

And the following subsequences are mapped to integer functions:

 
 
  1. def motif2int(motif):  
  2. '''convert a sub-sequence/motif to a non-negative integer'''  
  3. total = 0 
  4. for i, letter in enumerate(motif):  
  5. total += m2iD[letter]*4**(len(motif)-i-1)  
  6. return total  
  7. Test:  
  8. >>> motif2int('ACGT')  
  9. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.