This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Biopython How to generate random Position-Weight Matrices

Hi,

I am looking for a way to generate random Position-Weight Matrices (PWM) using Biopython.

I have to use Biopython for various reasons (fast motif finding for instance) but seem to be unable to create a Motif or PWM object by hand.

Is there any method to create PWMs from either numpy arrays or by specifying the 'counts' variable explicitly?

Thanks for any suggestions.

sequence biopython

1 answer

Okay, I already solved it myself.

For anyone interested, here is how I did it:

def createRandomMotif (motifLength, alphabet):
    counts = {}
    for letter in alphabet.letters:
        counts[letter] = [random.randint(0,100) for x in xrange(motifLength)]
    return mat.PositionWeightMatrix(alphabet, counts)

x = createRandomMotif(6, alphabet)
print x

Console:

        0      1      2      3      4      5
A:   0.20   0.22   0.12   0.31   0.27   0.23
C:   0.03   0.33   0.34   0.28   0.39   0.38
G:   0.33   0.00   0.24   0.26   0.02   0.23
T:   0.44   0.45   0.29   0.16   0.32   0.16

This generates a random motif matrix for any given alphabet and motif length (k-mer length) k.

I am also using biopython and I am looking for a way to generate 'WAG' matrix from aligned protein sequence file. What kind of matrix is this one? How does the formula is applied to calculate frequencies of amino acids?

Log in to answer this question.