Skip to content

Biostumblematic

A biophysicist teaches himself how to code

For some reason it seems that every program which will output a percentage of the identity between two proteins will also align them itself – therefore screwing up any alignment which you’ve already made. I knocked up a short script over the weekend which will read in a FASTA-formatted alignment and output the percent identity of all of the proteins in it to the first one in the file.

I couldn’t find a built-in way to do this all in BioPython, but I did use it to parse the seqences out of the alignment. The rest of the work is just brute force string crunching.

#!/usr/bin/env python
# https://biostumblematic.wordpress.com

import string
from Bio import AlignIO

# change input.fasta to match your alignment
input_handle = open("input.fasta", "rU")
alignment = AlignIO.read(input_handle, "fasta")

j=0 # counts positions in first sequence
i=0 # counts identity hits 
for record in alignment:
    for amino_acid in record.seq:
        if amino_acid == '-':
            pass
        else:
            if amino_acid == alignment[0].seq[j]:
                i += 1
        j += 1
    j = 0
    seq = str(record.seq)
    gap_strip = seq.replace('-', '')
    percent = 100*i/len(gap_strip)
    print record.id+' '+str(percent)
    i=0

I didn’t implement similarity here, but it gets the basic job done. This script is available on GitHub as seqhomology.py

I’m not sure if this behavior is normal, but I find that I learn a new system best by choosing a real-life problem that I need to solve and applying the new method in order to solve it. This inevitably means that I’ll probably be doing things in a non-efficient way (since I’m a noobie), but code can always be refined later.

Here is the problem I have in front of me today: I have a series of proteins from which I’d like to isolate (via cloning) a certain domain. The cDNA clones of the full length proteins are available from the IMAGE consortium. Unfortunately these aren’t completely “clean” cDNAs; there tends to be some extraneous sequence on both ends of the gene.

The plan of action goes something like this:
The starting materials are the cDNA sequence, the amino acid sequence of the protein, and the residue ranges of the domain of interest. So what I’d like to do is to check each frame of the cDNA to find the one matching the translated protein sequence, then extract just the cDNA coding for the domain I’d like to isolate. I can then (independently) design PCR primers for this domain.

You’re probably thinking that this could be done manually (and of course that’s true), but I find this painstaking work. Also it gives me a chance to play around with the SeqIO functions of BioPython a bit 🙂

Enough introduction, let’s get to work. The protein I’ll be using for this exercise is IKBKB. This is a 756 amino acid protein; I’ll be trying to get the cDNA for the protein kinase domain from residues 15-300. The IMAGE clone ID is 5784717.

Baby step 1 – find the ORF we’re interested in

#! /usr/bin/env python

# https://biostumblematic.wordpress.com

# Extraction of the cDNA
# for a given protein domain

import re
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

input_cdna = raw_input('Paste your cDNA sequence >> ')
input_search = raw_input('What are the first amino acids of the protein? >> ')

cdna = Seq(input_cdna, IUPAC.unambiguous_dna)

i=0
while i < 3:
    frame = cdna&#91;i:150&#93;
    trans = frame.translate()
    orf_find = re.search(input_search, str(trans))
    if orf_find:
        orf_frame = i+1
    else:
        pass
    i += 1
print 'The protein is coded in frame '+str(orf_frame)
&#91;/sourcecode&#93;
Given the input of the cDNA and the first 4 residues (MSWS), this outputs the right answer:
<pre>The protein is coded in frame 3</pre>
Note that I'm only checking the first 50 residues of the cDNA (see line 19) Hopefully this is enough to catch the protein of interest (it would be a lot of extraneous 5' sequence if not).

Obviously this is not enough sexy for Biopython.  It's cumbersome to have to type in the starting sequence of the protein you're interested in, so why don't we let SeqIO handle that for us via the SwissProt code?  Also, we'll change a couple of things to enable automation of the full list later.

First, I made a file called 'test.csv' which has a single line consisting of SwissProt ID,cDNA:
<pre>O14920,atagccccggg[...]</pre>
Then I modified the script like so:

import re, csv
from Bio import ExPASy, SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

reader = csv.reader(open('test.csv'))
for row in reader:
    input_prot = row[0]
    get_prot = ExPASy.get_sprot_raw(input_prot)
    prot_obj = SeqIO.read(get_prot, "swiss")
    get_prot.close()
    prot_seq = prot_obj.seq
    prot_start = prot_seq[0:4]

    cdna = Seq(row[1], IUPAC.unambiguous_dna)

    i=0
    while i < 3:
        frame = cdna&#91;i:150&#93;
        trans = frame.translate()
        orf_find = re.search(str(prot_start), str(trans))
        if orf_find:
            orf_frame = i+1
        else:
            pass
        i += 1
    print 'The protein is coded in frame '+str(orf_frame)
&#91;/sourcecode&#93;
Biopython grabs the protein sequence from the web using the SwissProt ID.  The prot_start variable takes just the first few residues and uses that as the search term for the regular expression later on.  Now there is no command line input, as everything is done via the CSV file.  This will iterate over lines in the CSV file to do multiple proteins.  Right now, however, we would just get a long list of "The protein is coded in frame X" lines, which is less than useful.  Time to take care of that.

In this case the domain is annotated in SwissProt already.  This means that I <em>could</em> use the <a href="http://biopython.org/DIST/docs/tutorial/Tutorial.html#chapter:swiss_prot">built-in parsing function</a> of BioPython to select the domain, however I have some custom annotations for other proteins in my list that make this not a good idea in this case.  Instead let's just make some minor modifications to our input CSV and existing script.  The new CSV includes the start and stop residues of interest:
<pre>O14920,15,300,atagccccgggttt[...]</pre>
Now I just modify the top of the script to take into account the new structure of the CSV:

reader = csv.reader(open('test.csv'))
for row in reader:
    input_prot = row[0]
    get_prot = ExPASy.get_sprot_raw(input_prot)
    prot_obj = SeqIO.read(get_prot, "swiss")
    get_prot.close()
    prot_seq = prot_obj.seq
    prot_domain = prot_seq[int(row[1])-1:int(row[2])]
    cdna = Seq(row[3], IUPAC.unambiguous_dna)

and adjust what happens if the script finds a match to the domain sequence:

        if orf_find:
            trans_split = re.split('('+str(prot_domain)+')', str(trans))
            cdna_start = len(trans_split[0])*3
            cdna_stop = cdna_start + len(trans_split[1])*3
            cdna_extracted = frame[cdna_start:cdna_stop]
            print cdna_extracted

And this gets the job done! This prints a cDNA sequence which, when translated back, matches the domain of interest.

The last part there feels sloppy. All I’m doing is counting the number of amino acids that come out of the translation before the start of the domain, then multiplying by three, and getting the cDNA start from this. I feel like there must should be a way to transition more effectively between protein and cDNA sequence.

The entire script, slightly modified to write out the results to a new CSV file, is available over on GitHub. I hope you found the post interesting and look forward to your comments.

Yeah, I’ve heard of it. Biopython is A python module package (thanks Chris) that’s written to help with doing computational biology. To my utter dismay I somewhat ignored it, being the “ll just brew it myself” type. What a mistake.

Today I was trying to wrangle some DNA and protein sequences and realized that this might be something covered by Biopython. It’s even better than that. You want tasty yum yums? How about a reverse complementer in 3 lines of code? I even formatted it so it looks nice on the terminal:

from Bio.Seq import Seq
sequence = Seq(raw_input('Paste your DNA sequence >> '))
print '\nReverse Complement\n------------------\n'+sequence.reverse_complement()

The very next bit of code in the tutorial replaced a ~50 line program I had cobbled together (and which still wasn’t working exactly the way I wanted) into this beauty:

#! /usr/bin/env python

# Biopython can automatically parse FASTA
# as well as many other "standard" biological formats

from Bio import SeqIO
inputfile = open('myproteins_fasta.txt')

for seq_record in SeqIO.parse(inputfile, 'fasta'):
    print seq_record.id
    print repr(seq_record.seq)
    print len(seq_record)
inputfile.close

BOOM, FASTA reader.

I’m just getting started on reading the documentation, but so far I’m really impressed (and not a little bit sheepish at my previous obstinance). Expect to see some Biopython examples in the coming days

Tags:

I’ve written about ElementTree before, and it really is a handy tool. I took the output NCBI GI numbers from my previous post and used them in concert with the ID mapper at UniProt to get a listing of the proteins. UniProt kindly allows you to download this subset in XML, which I did in order to quickly extract the information I was interested in.

Here’s what a bit of the XML looks like:

Continue reading this article ›

I managed to find a paper in which some of the analysis I’ve been working on had been done. Unfortunately the raw results of the analysis were just that – raw. Specifically they had been dumped into a 6.8 MB text file as a supplement to the paper.

In order to extract the information I was interested in, and to prove to people who read this that I don’t solve all of my problems with Python, I thought I’d share the quick code I used.

First of all, I wanted all of the lines that reported proteins from humans. This turned out to be workable by running:

cat infile.txt | grep 'Homo sapiens' > oufile.txt

This gave me a long list which helpfully had each line starting with the NCBI GI number for the protein of interest. To extract the GI numbers alone involved:

cat outfile.txt | cut -c 1-11 > GI_list.txt

then to trim the whitespace:

sed 's/^[ \t]*//;s/[ \t]*$//' GI_list.txt > GI_list.txt

(this last one took some help from the handy sed one-liners page)

The entire process took about 1/4 of the time I’ve just taken writing it up, and I now have a nicely-formatted 11 kb file which I can use as input to my next round of tasks.

I still don’t claim to understand inheritance well (although it should be simple), but I think that part of my trouble was stemming from differences between Django and Python. Regardless, I managed to solve my problem from the last post with something like this:

class Protein(models.Model):
    ...
class Domain(models.Model):
    protein = models.ForeignKey(Protein)
    ...
    def domain_percent(self):
        m = re.match('^RING', self.domain)
        if m:
            p = Protein.objects.get(pk=self.protein_id)
            p_clean = re.sub("\s+", "", p.sequence)
            p_len = float(len(p_clean))
            domain_l = float(self.domain_stop - self.domain_start)
            perc = int(100*(domain_l/p_len))
            return perc
        else:
            pass

the key line is

p = Protein.objects.get(pk=self.protein_id)

. This says “get all of the objects from the related class “Protein” which have a primary key (pk) equal to the protein_id of this domain. Whew.

As usual, the key information I needed was in the part of the tutorial that I skimmed. I’ve spent most of the day today struggling with querysets, but I’m still a long way from maximizing them as well. I’ve managed to get things working “well enough” for the time being – I need to stop tinkering with the backend of this for now and actually populate/extract data if I want to be able to wow my advisor at our individual meeting tomorrow.

Tags: , ,

<insert ramble about how long it’s been since the last post here>

So I’m back to working with Django, and once again something that seems simple is killing me.  I simply want to be able to define the various conserved domains in the protein, then be able to automatically pull out the amino acid sequence of a given domain.  The problem (which I’ve dealt with before) is getting the python classes to talk to one another.

Here is some simple code to illustrate:

class Protein(models.Model):
    uniprot = models.CharField('Uniprot accession code', max_length=6, blank=True)
    sequence = models.TextField('Amino acid sequence')

class Domain(models.Model):
    protein = models.ForeignKey(Protein)
    domain = models.CharField('Domain Name', max_length=25, blank=True)
    domain_start = models.IntegerField('Domain start residue', blank=True)
    domain_stop = models.IntegerField('Domain end residue', blank=True)

So what I want to do looks like this in my head, but doesn’t work:

class Domain(models.Model):
...
    def domain_FASTA(self):
        amino_clean = re.sub("\s+", "", protein.sequence)
        domain_sstart = self.domain_start - 1 # lists start from 0
        domain_sstop = self.domain_stop - 1
        domain_seq = amino_clean[domain_sstart:domain_sstop]
        fasta = '> '+protein.uniprot+' - '+self.domain+' '+str(self.domain_start)+'-'+str(self.domain_stop)+'\n'+domain_seq
        return fasta

I’ve been banging on this for hours, and have managed to fairly thoroughly break the things that I did have working. Fortunately I’m older and wiser than I once was, so I managed to restore them. I’m pretty well stumped on how to solve this one, though.

Tags: , ,