<p>The Python solution is here. IMO Bash definitely isn't a good solution for this calculation.</p>
<pre><code>from itertools import permutations, combinations

one2all ={'A': ('A', 'ALA', 'alanine'),
          'R': ('R', 'ARG', 'arginine'),
          'N': ('N', 'ASN', 'asparagine'),
          'D': ('D', 'ASP', 'aspartic acid'),
          'C': ('C', 'CYS', 'cysteine'),
          'Q': ('Q', 'GLN', 'glutamine'),
          'E': ('E', 'GLU', 'glutamic acid'),
          'G': ('G', 'GLY', 'glycine'),
          'H': ('H', 'HIS', 'histidine'),
          'I': ('I', 'ILE', 'isoleucine'),
          'L': ('L', 'LEU', 'leucine'),
          'K': ('K', 'LYS', 'lysine'),
          'M': ('M', 'MET', 'methionine'),
          'F': ('F', 'PHE', 'phenylalanine'),
          'P': ('P', 'PRO', 'proline'),
          'S': ('S', 'SER', 'serine'),
          'T': ('T', 'THR', 'threonine'),
          'W': ('W', 'TRP', 'tryptophan'),
          'Y': ('Y', 'TYR', 'tyrosine'),
          'V': ('V', 'VAL', 'valine'),
          'X': ('X', 'GLX', 'glutaminx'),
          'Z': ('Z', 'GLI', 'glycine'),
          'J': ('J', 'NLE', 'norleucine'),
          'U': ('U', 'CYC', 'cysteinc')}
myprotein = [x for x in one2all.keys()]        
tetrapeps = combinations(myprotein,4)
peptide = []
count = 0
for x in list(tetrapeps):
    peptide.append(''.join(x))
for tetra in peptide:    
    permpeptides = list(permutations (tetra))
    for i in permpeptides:
        count +=1
        print (''.join(i))
</code></pre>
<p>I get 255024 permutations so more than the estimate of 160000, however the code is correct (please see <strong>Validation</strong> below).</p>
<p>Truncated output</p>
<blockquote>
<p>....
JUXV
UVXJ
UVJX
UXVJ
UXJV
UJVX
UJXV
VZJU
VZUJ
VJZU
VJUZ
VUZJ
VUJZ
ZVJU
ZVUJ
ZJVU
ZJUV
ZUVJ
ZUJV
JVZU
JVUZ
JZVU
JZUV
JUVZ
JUZV
UVZJ
UVJZ
UZVJ
UZJV
UJVZ
UJZV
XZJU
XZUJ
XJZU
XJUZ
XUZJ
XUJZ
ZXJU
ZXUJ
ZJXU
ZJUX
ZUXJ
ZUJX
JXZU
JXUZ
JZXU
JZUX
JUXZ
JUZX
UXZJ
UXJZ
UZXJ
UZJX
UJXZ
UJZX</p>
</blockquote>
<p>I cut and paste the amino dictionary from <a href="http://aria.pasteur.fr/example-files/modified-residues/aria-files/AminoAcid.py/view" rel="nofollow noreferrer">here</a> (I wasn't going to write it by hand), <code>AminoAcid.py</code> by Pasteur.</p>
<hr />
<p><strong>Validation</strong></p>
<p>The theoretical number of permutations from 4 samples of 20 amino acids is</p>
<blockquote>
<p><strong>116280</strong></p>
</blockquote>
<p>However, the amino acid chart I used comprised <strong>24</strong> amino acids (I just copied and pasted it). The theoretical number for 24 amino acids is therefore:</p>
<blockquote>
<p><strong>255024</strong></p>
</blockquote>
<p>This was the number observed in the output of the above code and validated the calculation.</p>
<p>A permutations calutation can be performed online <a href="https://www.calculatorsoup.com/calculators/discretemathematics/permutations.php" rel="nofollow noreferrer">here</a></p>
<p>I am not sure how the expected output of 160000 was derived but suspect it was a guess.</p>
<hr />
<p>The permutation equation is as follows, the output of this equation and the code above coincide,</p>
<p><span class="math-container">$$ P_{n, r}=\frac{n !}{(n-r) !}$$</span></p>
<p>n = no. amino acids</p>
<p>r = size of oligopeptide</p>
<hr />
<h4>Permutation by repetition</h4>
<p>If the permutation by repetition equation  is sought (comment below). The code is:</p>
<pre><code>from itertools import product
myprotein = [x for x in one2all.keys()]
print(len([p for p in product(myprotein, repeat=4)]))
</code></pre>
<p>for 20 amino acids this is:</p>
<blockquote>
<p>160000</p>
</blockquote>
<p>for 24 amino acids:</p>
<blockquote>
<p>331776</p>
</blockquote>

CC BY-SA 4.0
2022-08-06T20:34:49.907Z
I think your calculations assume permutation without replacement. If you assume replacement occurs (i.e. an amino acid can occur more than once in a tetrapeptide), then the expected number of unique tetrapeptides is 20^4 = 160,000.
acvill
CC BY-SA 4.0
2022-08-07T02:11:13.113Z
Firstly thanks for your answer @acvill. However, the equation you have used is permutation with repetition not the permutation equation. The question asked for just permutations. I've added the permutation equation below the answer and the permutation by repetition code below the equation.
M__
CC BY-SA 4.0
2022-08-07T13:00:07.560Z
you are correct that I did not use the permutation equation. From what I can tell, your approach is also more efficient and readily understood. However, it seems that there is a disconnect between the language that OP used ("permutations") and the question they are actually asking, which I read as "*How to generate all 160,000 tetrapeptides*?" It seems odd that a peptide library built for an enzymatic screen would exclude elements that have repeated residues, though, since this question is now months old, I don't expect we'll get any clarification from OP
acvill
CC BY-SA 4.0
2022-08-07T18:00:30.013Z
Thanks @acvill. Now you raise the biological perspective its makes sense. I approached this from stats theory (*ad infinitum*) and the 'permutation equation' means only one thing. However, from a molecular biological perspective the permutation equation also means only one thing, as you've pointed out, but its a completely different equation. The OP's lecturers should have known this difference however. Epidemiology was one field where the perspective between biologists and maths/stats resulted in some very famous errors (I will avoid details). Anyway upvoted, thanks.
M__
CC BY-SA 4.0
2022-08-06T14:12:30.873Z
2022-08-07T02:09:01.507Z
2022-08-07T02:09:01.507Z
M__
<p>Here's a bash solution using brace expansion and the <a href="https://biology.stackexchange.com/questions/105321">single-letter codes for the 20 common amino acids</a>:</p>
<pre><code>aalist=D,E,K,R,H,N,Q,S,T,P,G,A,V,I,L,M,C,F,Y,W
eval printf '&quot;%s\n&quot;' {&quot;$aalist&quot;}{&quot;$aalist&quot;}{&quot;$aalist&quot;}{&quot;$aalist&quot;}
</code></pre>
<p>This prints 160,000 lines, one for each tetrapeptide.</p>

CC BY-SA 4.0
2022-08-06T18:09:44.563Z
2022-08-06T18:09:44.563Z
acvill
<p>I have an assignment for a project to build a tetrapeptides library to screen against some target enzymes <em>in silicon</em>.</p>
<p>I am looking for a script to construct about 160000 tetrapeptide from the 20Amino acids permutations.</p>
<p>I am quite familiar with Bash, but am struggling to get a function to working.</p>

CC BY-SA 4.0
2021-11-02T16:39:51.780Z
No "about" 20^4 (permutation with repetition) is 160_000, no approximation.
Matteo Ferla
CC BY-SA 4.0
2021-11-02T17:20:19.107Z
Hi @Ruthy and welcome to Bioinformatics.SE! We try always to be a helpful community forum, but we're most eager to help those willing to help themselves. Could you please update your question to describe *why* you need to solve this problem? What is the context? Is this for a homework assignment? Or a new research project? What have you tried so far? This forum is not designed to teach the very basics of programming, but if you have *specific* questions about a bioinformatics program you're trying to write we might be able to point you in the right direction.
Daniel Standage
CC BY-SA 4.0
2021-11-02T18:20:32.990Z
Noted, @Matteo. And thank you, Daniel. It's an assignment for a project. I am building a tetrapeptides library to screen virtually against some target enzymes.  I am quite familiar with Bash. But, writing a script to achieve that has just been a problem. I hope I can just get a function to work with, then...  Thank you, once again.
Ruthy
CC BY-SA 4.0
2021-11-03T12:56:15.083Z
Please post example of code that you tried. See also [help] and [ask].
Timur Shtatland
CC BY-SA 4.0
2021-11-06T17:39:20.993Z
has to be necessarily in bash? can't be python for example?
JRodrigoF
<python><phylogenetics><phylogeny><protein-structure><bash>
Permutations of 4 amino acid sequence oligopeptide
# Permutations of 4 amino acid sequence oligopeptide

(Asked by: username_0 on 2021-11-02)

I have an assignment for a project to build a tetrapeptides library to screen against some target enzymes *in silicon*.

I am looking for a script to construct about 160000 tetrapeptide from the 20Amino acids permutations.

I am quite familiar with Bash, but am struggling to get a function to working.

**Tags:** python, phylogenetics, phylogeny, protein-structure, bash
> **Comments:**
> **username_1**: No "about" 20^4 (permutation with repetition) is 160_000, no approximation.  
> **username_2**: Hi @Ruthy and welcome to Bioinformatics.SE! We try always to be a helpful community forum, but we're most eager to help those willing to help themselves. Could you please update your question to describe *why* you need to solve this problem? What is the context? Is this for a homework assignment? Or a new research project? What have you tried so far? This forum is not designed to teach the very basics of programming, but if you have *specific* questions about a bioinformatics program you're trying to write we might be able to point you in the right direction.  
> **username_0**: Noted, @Matteo. And thank you, Daniel. It's an assignment for a project. I am building a tetrapeptides library to screen virtually against some target enzymes.  I am quite familiar with Bash. But, writing a script to achieve that has just been a problem. I hope I can just get a function to work with, then...  Thank you, once again.  
> **username_3**: Please post example of code that you tried. See also [help] and [ask].  
> **username_4**: has to be necessarily in bash? can't be python for example?  

---

## 2 answers

### Answer by username_5 on 2022-08-06. Score: 2

The Python solution is here. IMO Bash definitely isn't a good solution for this calculation.
from itertools import permutations, combinations

one2all ={'A': ('A', 'ALA', 'alanine'),
          'R': ('R', 'ARG', 'arginine'),
          'N': ('N', 'ASN', 'asparagine'),
          'D': ('D', 'ASP', 'aspartic acid'),
          'C': ('C', 'CYS', 'cysteine'),
          'Q': ('Q', 'GLN', 'glutamine'),
          'E': ('E', 'GLU', 'glutamic acid'),
          'G': ('G', 'GLY', 'glycine'),
          'H': ('H', 'HIS', 'histidine'),
          'I': ('I', 'ILE', 'isoleucine'),
          'L': ('L', 'LEU', 'leucine'),
          'K': ('K', 'LYS', 'lysine'),
          'M': ('M', 'MET', 'methionine'),
          'F': ('F', 'PHE', 'phenylalanine'),
          'P': ('P', 'PRO', 'proline'),
          'S': ('S', 'SER', 'serine'),
          'T': ('T', 'THR', 'threonine'),
          'W': ('W', 'TRP', 'tryptophan'),
          'Y': ('Y', 'TYR', 'tyrosine'),
          'V': ('V', 'VAL', 'valine'),
          'X': ('X', 'GLX', 'glutaminx'),
          'Z': ('Z', 'GLI', 'glycine'),
          'J': ('J', 'NLE', 'norleucine'),
          'U': ('U', 'CYC', 'cysteinc')}
myprotein = [x for x in one2all.keys()]        
tetrapeps = combinations(myprotein,4)
peptide = []
count = 0
for x in list(tetrapeps):
    peptide.append(''.join(x))
for tetra in peptide:    
    permpeptides = list(permutations (tetra))
    for i in permpeptides:
        count +=1
        print (''.join(i))

I get 255024 permutations so more than the estimate of 160000, however the code is correct (please see Validation below).
Truncated output

....
JUXV
UVXJ
UVJX
UXVJ
UXJV
UJVX
UJXV
VZJU
VZUJ
VJZU
VJUZ
VUZJ
VUJZ
ZVJU
ZVUJ
ZJVU
ZJUV
ZUVJ
ZUJV
JVZU
JVUZ
JZVU
JZUV
JUVZ
JUZV
UVZJ
UVJZ
UZVJ
UZJV
UJVZ
UJZV
XZJU
XZUJ
XJZU
XJUZ
XUZJ
XUJZ
ZXJU
ZXUJ
ZJXU
ZJUX
ZUXJ
ZUJX
JXZU
JXUZ
JZXU
JZUX
JUXZ
JUZX
UXZJ
UXJZ
UZXJ
UZJX
UJXZ
UJZX

I cut and paste the amino dictionary from here (I wasn't going to write it by hand), AminoAcid.py by Pasteur.

Validation
The theoretical number of permutations from 4 samples of 20 amino acids is

116280

However, the amino acid chart I used comprised 24 amino acids (I just copied and pasted it). The theoretical number for 24 amino acids is therefore:

255024

This was the number observed in the output of the above code and validated the calculation.
A permutations calutation can be performed online here
I am not sure how the expected output of 160000 was derived but suspect it was a guess.

The permutation equation is as follows, the output of this equation and the code above coincide,
$$ P_{n, r}=\frac{n !}{(n-r) !}$$
n = no. amino acids
r = size of oligopeptide

Permutation by repetition
If the permutation by repetition equation  is sought (comment below). The code is:
from itertools import product
myprotein = [x for x in one2all.keys()]
print(len([p for p in product(myprotein, repeat=4)]))

for 20 amino acids this is:

160000

for 24 amino acids:

331776

> **Comments:**
> **username_6**: I think your calculations assume permutation without replacement. If you assume replacement occurs (i.e. an amino acid can occur more than once in a tetrapeptide), then the expected number of unique tetrapeptides is 20^4 = 160,000.  
> **username_5**: Firstly thanks for your answer @acvill. However, the equation you have used is permutation with repetition not the permutation equation. The question asked for just permutations. I've added the permutation equation below the answer and the permutation by repetition code below the equation.  
> **username_6**: you are correct that I did not use the permutation equation. From what I can tell, your approach is also more efficient and readily understood. However, it seems that there is a disconnect between the language that OP used ("permutations") and the question they are actually asking, which I read as "*How to generate all 160,000 tetrapeptides*?" It seems odd that a peptide library built for an enzymatic screen would exclude elements that have repeated residues, though, since this question is now months old, I don't expect we'll get any clarification from OP  
> **username_5**: Thanks @acvill. Now you raise the biological perspective its makes sense. I approached this from stats theory (*ad infinitum*) and the 'permutation equation' means only one thing. However, from a molecular biological perspective the permutation equation also means only one thing, as you've pointed out, but its a completely different equation. The OP's lecturers should have known this difference however. Epidemiology was one field where the perspective between biologists and maths/stats resulted in some very famous errors (I will avoid details). Anyway upvoted, thanks.  

---

### Answer by username_6 on 2022-08-06. Score: 1

Here's a bash solution using brace expansion and the single-letter codes for the 20 common amino acids:
aalist=D,E,K,R,H,N,Q,S,T,P,G,A,V,I,L,M,C,F,Y,W
eval printf '"%s\n"' {"$aalist"}{"$aalist"}{"$aalist"}{"$aalist"}

This prints 160,000 lines, one for each tetrapeptide.


---

