text: user14503: 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.
  
  user3967: 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.
  
  user3848: 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
  
  
original_text: 14503: <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>
   3967: <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>
   3848: <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>
  