2
$\begingroup$

I am using python 3 and biopython 1.72. I have been using a protein sequence file in clustal format.

CLUSTAL 2.1 multiple sequence alignment


Human           ------------------------------------------------------------
Chimpanzee      ------------------------------------------------------------
Dog             ------------------------------------------------------------
Mouse           ------------------------------------------------------------
Xenopus         ------------------------------------------------------------
Amphioxus       MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA


Human           ------------------------------------------------------------
Chimpanzee      ------------------------------------------------------------
Dog             ------------------------------------------------------------
Mouse           ------------------------------------------------------------
Xenopus         ------------------------------------------------------------
Amphioxus       HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK


Human           ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS
Chimpanzee      ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS
Dog             ------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS
Mouse           ------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS
Xenopus         ------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST
Amphioxus       INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD
                                  *:***::  :.  :::     *** **: *        * . 

Human           NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI
Chimpanzee      NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI
Dog             DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL
Mouse           DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL
Xenopus         EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ
Amphioxus       DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH
                :  * *:** :*.* **::.* . **:***** ::* .   .               *  

I need to iterate over the rows of my file so that I could count the occurrence of characters in each row. I tried using the code:

file_var = input("File: ")
count = 0
with open (file_var , 'r') as fo:
    for i in fo:
        count += 1
        print (i)
    print (count)

But this results just counting the total number of rows in a file(count variable). If I iterate over the file using biopython, it does'nt work also.

for seq_record in SeqIO.parse("sample.aln","clustal"):
    print (seq_record.id)
    print (repr(seq_record.seq))

seq_record.id results the names of the sequences used in the file. repr(seq_record.seq) results the sequences of each specie alongwith description.

While I need to

  1. get the rows only with amino acids (exclusing the symbols '*,.,:' line) and nothing else from the row and for each row I need to count the number of characters (exclusing '-' dashes)
  2. the number of characters of that row be placed at the end of each of the row.

for example: for above example file, the counter should work like;

------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
$\endgroup$
  • $\begingroup$ you can count for the character using 'length' function and before the counting character, use regular expression to remove the other character from the string. $\endgroup$ – Arijit Panda Sep 5 '18 at 10:37
  • $\begingroup$ yes it does, but it won't do for each row separately. Thankyou for the response. :) $\endgroup$ – Sidra Younas Sep 5 '18 at 18:16
  • $\begingroup$ I think biopython is not the right tool if you want the counts row-by-row: biopython treats the sequence of a taxon as one unit, regardless of the original layout in the alignment file. $\endgroup$ – bli Sep 7 '18 at 9:36
5
$\begingroup$

Here's one way with pure Python:

#!/usr/bin/env python3
import sys
import re

with open (sys.argv[1] , 'r') as fo:
    for line in fo:
        fields = line.split()
        if len(fields)==2:
            print(fields[1],len(fields[1].replace('-','')))

Running this on your file gives:

$ foo.py file.aln 
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
------------------------------------------------------------ 0
HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60

Some other options:

  1. awk

    $ awk 'NF==2{l=$2;gsub(/-/,"",$2); print l,length($2)}' file.aln 
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
    ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
    ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
    ------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
    ------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
    ------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
    INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
    NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
    NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
    DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
    DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
    EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
    DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60
    
  2. Perl

    $ perl -lane '$l=$F[1];$l=~s/-//g; $#F==1 && print "$F[1] ", length($l)' file.aln 
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    MQWTGFRVSMTTLMMIMGVVAVLIALLPAKAQQPHDKSLRTTSTLTDTGASADEADMGSA 60
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    ------------------------------------------------------------ 0
    HVELLDGDDDVGNGSDQMMVTLHLQSIFQCIRRPCEKVDRAIDPVTQRWRTANTRNDYQK 60
    ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSHLRQSLLCTWGYSS 42
    ------------------MRLRVRLLKRTWPLEVPETEPTLGHLRSRLRQSLLCTWGYSS 42
    ------------------MKLRVRLQKRTWPLDLPDAEPTLGQLRAHLSQALLPSWGFGS 42
    ------------------MKLRVRLQKRTQPLEVPESEPTLGQLRAHLSQVLLPTLGFSS 42
    ------------------MKLRVRVRKQTNRLELEAESPTLGDLRSKLSSVTLPALGYST 42
    INVCVVPAYDVSLSTGVRMKLRVKISGQKTRVDVGQDCHTLGTLRTLLAPVLGEQYGLGD 60
    NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
    NTRFTITLNYKDPLTGDEETLASYGIVSGDLICLILQDDIPA---------------PNI 45
    DTRFAITLNNKDALTGDEETLASYGIVSGDLICLILEDAIPA---------------PNL 45
    DTRFAITLNNKDALTGDEETLASYGIVSGDLICLVLEDDMPA---------------PNL 45
    EANFTITLNGKDALTGDQNTLESAGIISGDLIVVVLPDSQLP---------------PPQ 45
    DMPFEISLNGRDALLGDDKPLSDLGIVSGDLIHILLASVDQPSTNHNTQQQGQHPSSPEH 60
    
$\endgroup$
  • $\begingroup$ Well ,Thankyou for this much help, it really solved my problem. Would this also work for a phylip file format?? which is ; [phylip format example] (scikit-bio.org/docs/0.2.3/generated/skbio.io.phylip.html) $\endgroup$ – Sidra Younas Sep 5 '18 at 19:12
  • $\begingroup$ @SidraYounas I'm afraid not. This approach assumes that the lines of interest have two fields. Phylip has more. You might want to ask a new question about that. $\endgroup$ – terdon Sep 5 '18 at 19:15
  • $\begingroup$ ,alright. But how come I write up these countings into the file and not just printing them out.?? As I'm working with windows and not linux system $\endgroup$ – Sidra Younas Sep 5 '18 at 19:27
  • $\begingroup$ this code does not write the character numbers in my file. Do I need to use file handler to access writing permission to file?? $\endgroup$ – Sidra Younas Sep 6 '18 at 5:26
  • 1
    $\begingroup$ @SidraYounas yes, that's just normal python. Next time, please make sure to include all your requirements in your question so we don't need to do this sort of back and forth in the comments. $\endgroup$ – terdon Sep 6 '18 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.