Answers.Body: <p>In response to question 1, save the following as a python3 script (e.g., <code>aln_parser.py</code>):</p>
  
  <pre><code>#!/usr/bin/env python3    
  
  from Bio import AlignIO
  import sys
  
  aln = sys.argv[1]
  
  algnmnt = AlignIO.read(open(aln, 'r'), 'clustal')
  
  for col in range(0, algnmnt.get_alignment_length()):
      proteins = set(algnmnt[:,col])
      if len(proteins) == 1:
          print(f"Postion {col}: protein {''.join(proteins)}")
  </code></pre>
  
  <p>Run the script to query your alignment (<code>clustal.aln</code>).</p>
  
  <p><code>python3 aln_parser.py clustal.aln</code></p>
  
  <p>To save the output to file:</p>
  
  <p><code>python3 aln_parser.py clustal.aln &gt; results.txt</code></p>
  
  <p>Requires <code>python3</code> and <code>Biopython</code> (<code>pip3 install biopython</code>).  </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-26T04:10:37.763Z
Answers.Text: @SidraYounas I would try to help, but I don't fully understand the question.
Answers.UserDisplayName: user3479780
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-26T05:42:21.613Z
Answers.Text: @user3479780 You can see the code sample above, that I have used. In this, I am iterating over the columns of the files. All I was trying to use the way if I can search for the presence of some particular amino acids for each column.
Answers.UserDisplayName: Sidra Younas
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-03T08:21:24.523Z
Answers.Text: @user3479780 I have been trying to delete these columns which have a single amino acid, but I am not being successful. Can you help?
Answers.UserDisplayName: Sidra Younas
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-02T15:17:44.810Z
Answers.Text: Looks good. You could also add a hash-bang `#!/bin/env python3` to make it executable.
Answers.UserDisplayName: Kamil S Jaron
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-04T07:52:33.763Z
Answers.Text: question 2 has still been the problem, if anyone can help.!
Answers.UserDisplayName: Sidra Younas
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-02T02:06:36.147Z
Answers.LastActivityDate: 2018-10-11T04:50:03.577Z
Answers.LastEditDate: 2018-10-11T04:50:03.577Z
Answers.UserDisplayName: user3479780
Body: <p>I was working on certain program using python and I have been using a protein aligned sequence file in two formats, phylip (.phy) and clustal (.aln).
  Example clustal file:</p>
  
  <pre><code>CLUSTAL 2.1 multiple sequence alignment
  
  
      Homo            -------------------------------------------MEMQDLTSPHSRLSGSS
      Macaca          -------------------------------------------MEMQDLTSPHSRLSGSS
      Canis           REWSSARPERSKGRRKPVDAAAVSAVQTSQTSSDVAVSSSCRSMEMQDLTSPHSRLSGSS
      Mus             -------------------------------------------MEMQDLTSPHSRLSGSS
      Rattus          -------------------------------------------MEMQDLTSPHSRLSGSS
                                                                 *****************
  </code></pre>
  
  <p>Example phylip file:</p>
  
  <pre><code>     3   120
  Apis       MPWIKCLGGG GGKARRGKPL SVSQPIHLLV KSEFEPQCHV FRTKQLRKPR 
  Branchiost ---------- ---------- ---------- ---------- ---------- 
  Nematostel ---------- ---------- ---------- ---------- ---------- 
  
             PCHLCHQAVI KQASCCRVCK YICHKTCEDK GARFRDTSQQ IHKQWQADPA 
             ---------- ---------- ---------- ---------- ---------- 
             ---------- ---------- ---------- ---------- ---------- 
  </code></pre>
  
  <p>I want to navigate through the file in a way that ;</p>
  
  <ol>
  <li>If the columns of the file have same amino acid, it saves and prints the amino acid that exists in the column.</li>
  <li>I need to know how can I search the columns by using the amino acids, if particular group of amino acids exist in that column or not. For example, if 'STA'or 'HY' or 'FVLIM' or 'NDEQHK' (with no '-') exists in any of the columns, then put a '@' in the file at the end of that column. 
  Following is the code I have been trying to manipulate the file with the following program:</li>
  </ol>
  
  <p>Code below</p>
  
  <pre><code>alignment = open ("example.aln")
  a = b = c = 0 
  for col in (alignment): 
      num = len(set(col)) 
          if num == 1 and col[0] != '-':
              print (num)
          a += 1
          elif num &gt; 1 and '-' not in col:
          print (num)
          b += 1 elif '-' in col:  # assumes 1 or more dashes
          c += 1
          print('Number of columns with the same amino acid: {}\n'
          'Number of columns with at least 2 amino acids (no gaps): {}\n'
          'Number of columns with one/more gaps: {}'
          .format(a, b, c))
  </code></pre>
  
  <p>These variables return the occurrences of amino acids in the columns of the file but I don't understand how to search using the amino acids.
  What is the best way to iterate over the file and write into a new file with results using python? Should I use python switch statement?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-08T15:08:26.347Z
Comments.Text: Hi Sidra, as it was the block of code was unreadable since it seemed like many of the lines had been merged together. Could you edit your post to make sure that the code displayed above exactly matches your program? It would also help if you make your code a working minimal example where you provide a small testcase "example.aln". https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks
Comments.UserDisplayName: conchoecia
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-10T18:46:55.137Z
Comments.Text: Cross posted at Biostars: https://www.biostars.org/p/336237/#337089
Comments.UserDisplayName: Joe Healey
Tags: <python>
Title: How to iterate protein sequences using amino acids?
ThreadText: # How to iterate protein sequences using amino acids?
  
  (Asked by: username_0 on 2018-09-07)
  
  I was working on certain program using python and I have been using a protein aligned sequence file in two formats, phylip (.phy) and clustal (.aln).
  Example clustal file:
  
  ```
  CLUSTAL 2.1 multiple sequence alignment
  
      Homo            -------------------------------------------MEMQDLTSPHSRLSGSS
      Macaca          -------------------------------------------MEMQDLTSPHSRLSGSS
      Canis           REWSSARPERSKGRRKPVDAAAVSAVQTSQTSSDVAVSSSCRSMEMQDLTSPHSRLSGSS
      Mus             -------------------------------------------MEMQDLTSPHSRLSGSS
      Rattus          -------------------------------------------MEMQDLTSPHSRLSGSS
                                                                 *****************
  
  ```
  
  Example phylip file:
  
  ```
       3   120
  Apis       MPWIKCLGGG GGKARRGKPL SVSQPIHLLV KSEFEPQCHV FRTKQLRKPR 
  Branchiost ---------- ---------- ---------- ---------- ---------- 
  Nematostel ---------- ---------- ---------- ---------- ---------- 
  
             PCHLCHQAVI KQASCCRVCK YICHKTCEDK GARFRDTSQQ IHKQWQADPA 
             ---------- ---------- ---------- ---------- ---------- 
             ---------- ---------- ---------- ---------- ---------- 
  
  ```
  
  I want to navigate through the file in a way that ;
  
  1. If the columns of the file have same amino acid, it saves and prints the amino acid that exists in the column.
  2. I need to know how can I search the columns by using the amino acids, if particular group of amino acids exist in that column or not. For example, if 'STA'or 'HY' or 'FVLIM' or 'NDEQHK' (with no '-') exists in any of the columns, then put a '@' in the file at the end of that column.
     Following is the code I have been trying to manipulate the file with the following program:
  
  Code below
  
  ```
  alignment = open ("example.aln")
  a = b = c = 0 
  for col in (alignment): 
      num = len(set(col)) 
          if num == 1 and col[0] != '-':
              print (num)
          a += 1
          elif num > 1 and '-' not in col:
          print (num)
          b += 1 elif '-' in col:  # assumes 1 or more dashes
          c += 1
          print('Number of columns with the same amino acid: {}\n'
          'Number of columns with at least 2 amino acids (no gaps): {}\n'
          'Number of columns with one/more gaps: {}'
          .format(a, b, c))
  
  ```
  
  These variables return the occurrences of amino acids in the columns of the file but I don't understand how to search using the amino acids.
  What is the best way to iterate over the file and write into a new file with results using python? Should I use python switch statement?
  
  **Tags:** python
  
  > **Comments:**
  > **username_1**: Hi username_0, as it was the block of code was unreadable since it seemed like many of the lines had been merged together. Could you edit your post to make sure that the code displayed above exactly matches your program? It would also help if you make your code a working minimal example where you provide a small testcase "example.aln". https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks  
  > **username_2**: Cross posted at Biostars: https://www.biostars.org/p/336237/#337089  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-10-02. Score: 3 | Accepted answer
  
  In response to question 1, save the following as a python3 script (e.g., `aln_parser.py`):
  
  ```
  #!/usr/bin/env python3    
  
  from Bio import AlignIO
  import sys
  
  aln = sys.argv[1]
  
  algnmnt = AlignIO.read(open(aln, 'r'), 'clustal')
  
  for col in range(0, algnmnt.get_alignment_length()):
      proteins = set(algnmnt[:,col])
      if len(proteins) == 1:
          print(f"Postion {col}: protein {''.join(proteins)}")
  
  ```
  
  Run the script to query your alignment (`clustal.aln`).
  
  `python3 aln_parser.py clustal.aln`
  
  To save the output to file:
  
  `python3 aln_parser.py clustal.aln > results.txt`
  
  Requires `python3` and `Biopython` (`pip3 install biopython`).
  
  > **Comments:**
  > **username_4**: Looks good. You could also add a hash-bang `#!/bin/env python3` to make it executable.  
  > **username_0**: question 2 has still been the problem, if anyone can help.!  
  > **username_3**: @username_0 I would try to help, but I don't fully understand the question.  
  > **username_0**: @username_3 You can see the code sample above, that I have used. In this, I am iterating over the columns of the files. All I was trying to use the way if I can search for the presence of some particular amino acids for each column.  
  > **username_0**: @username_3 I have been trying to delete these columns which have a single amino acid, but I am not being successful. Can you help?  
  
  ---
  
  