Finding probability of word in string
<p>If I have a longer string, how do I calculate the probability of finding a word of a given length within that string?</p>

<p>So far I have this:</p>

<pre class="lang-python prettyprint-override"><code>import math
from scipy import stats

alphabet = list("ATCG")  # This is the alphabet I am working with
string = "AATCAGTAGATCG"  # Here are two example strings
string2 = "TGTAAACCTTGGTTTATCG"
word = "ATCG"  # This is my word

n_substrings = len(string) - len(word)  # The number of possible substrings
n_substrings2 = len(string2) - len(word)

prob_match = math.pow(len(alphabet), - len(word))  # The probability of randomly choosing the word from the alphabet

# Get the probability from a binomial test?
print stats.binom_test(1, n_substrings, p=prob_match)  # (Number of successes, number of trials, prob of success)
print stats.binom_test(1, n_substrings2, p=prob_match)

&gt;&gt;&gt;0.0346119111615
   0.0570183821615
</code></pre>

<p>Is this a suitable way to do this or am I missing something? </p>

<p>I think you should do:</p>

<pre><code>n_substrings = len(string) - len(word) +1
</code></pre>

<p>In a 5 letter string, with a 4 letter substring you have 2 options:
ATCGA can hold ATCG and TCGA</p>

python statistics