Occurrence Frequency of the Python DNA sequence neutron Sequence

Source: Internet
Author: User

There are many things we need to pay attention to when using Python DNA sequences. In fact, there are many problems in constant learning. Next we will look at in detail how to conduct related technical schools. Ms is my brother's rotation project: Given A bunch of Python DNA sequences, that is, A string consisting of characters A, C, G, T, count the occurrence frequency of all subsequences whose lengths are 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. Python DNA sequences, but when the sub-sequences are relatively long, such as n = 8, A 65536 (4 to the power of 8) key-value pair dictionary 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.

So we need to create two dictionaries to 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') 

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.

Therefore, the following integer is mapped to a sub-Sequence function, and another function baseN () is called. The source is here. Thanks to the author ~

 
 
  1. def baseN(n,b):  
  2. '''convert non-negative decimal integer n to  
  3. equivalent in another base b (2-36)'''  
  4. return ((n == 0) and '0' ) or ( baseN(n // b, b).lstrip('0') + \  
  5. "0123456789abcdefghijklmnopqrstuvwxyz"[n % b])  
  6. def int2motif(n, motifLen):  
  7. '''convert non-negative integer n to a sub-sequence/motif with length motifLen'''  
  8. intBase4 = baseN(n,4)  
  9. return ''.join(map(lambda x: i2mD[int(x)],'0'*(motifLen-len(intBase4))+intBase4))  
  10. Test:  
  11. >>> int2motif(27,4)  
  12. 'ACGT'  

The following code reads a fasta file containing a DNA sequence from the command line and the length of the sub-sequence, and outputs the sub-sequence and frequency. Note that the following code requires the Biopython module.

 
 
  1. if __name__ == '__main__':  
  2. import sys  
  3. from Bio import SeqIO  
  4. # read in the fasta file name and motif length  
  5. # from command line parameters  
  6. fastafile = sys.argv[1]  
  7. motifLen = int(sys.argv[2])  
  8. # list to store subsequence frequency  
  9. frequencyL = [0]*4**motifLen  
  10. # go over each DNA sequence in the fasta file  
  11. # and count the frequency of subsequences  
  12. it = SeqIO.parse(open(fastafile),'fasta')  
  13. for rec in it:  
  14. chrom = rec.seq.tostring()  
  15. for i in range(len(chrom)-motifLen+1):  
  16. motif = chrom[i:i+motifLen]  
  17. frequencyL[motif2int(motif)] += 1  
  18. # print frequency result to screen  
  19. for i, frequency in enumerate(frequencyL):  
  20. print int2motif(i, motifLen), frequency  

The above describes the Python DNA sequence.

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.