58

2015 bioinformatics bio_python

Embed Size (px)

Citation preview

Page 1: 2015 bioinformatics bio_python
Page 2: 2015 bioinformatics bio_python

FBW

27-10-2015

Wim Van Criekinge

Page 3: 2015 bioinformatics bio_python

Bioinformatics.be

Page 4: 2015 bioinformatics bio_python

GitHub: Hosted GIT

• Largest open source git hosting site

• Public and private options

• User-centric rather than project-centric

• http://github.ugent.be (use your Ugent

login and password)

– Accept invitation from Bioinformatics-I-

2015

URI:

– https://github.ugent.be/Bioinformatics-I-

2015/Python.git

Page 5: 2015 bioinformatics bio_python

Control Structures

if condition:

statements

[elif condition:

statements] ...

else:

statements

while condition:

statements

for var in sequence:

statements

break

continue

Page 6: 2015 bioinformatics bio_python

Lists

• Flexible arrays, not Lisp-like linked

lists• a = [99, "bottles of beer", ["on", "the",

"wall"]]

• Same operators as for strings• a+b, a*3, a[0], a[-1], a[1:], len(a)

• Item and slice assignment• a[0] = 98

• a[1:2] = ["bottles", "of", "beer"]

-> [98, "bottles", "of", "beer", ["on", "the", "wall"]]

• del a[-1] # -> [98, "bottles", "of", "beer"]

Page 7: 2015 bioinformatics bio_python

Dictionaries

• Hash tables, "associative arrays"• d = {"duck": "eend", "water": "water"}

• Lookup:• d["duck"] -> "eend"

• d["back"] # raises KeyError exception

• Delete, insert, overwrite:• del d["water"] # {"duck": "eend", "back": "rug"}

• d["back"] = "rug" # {"duck": "eend", "back":

"rug"}

• d["duck"] = "duik" # {"duck": "duik", "back":

"rug"}

Page 8: 2015 bioinformatics bio_python

Regex.py

text = 'abbaaabbbbaaaaa'

pattern = 'ab'

for match in re.finditer(pattern, text):

s = match.start()

e = match.end()

print ('Found "%s" at %d:%d' % (text[s:e], s, e))

m = re.search("^([A-Z]) ",line)

if m:

from_letter = m.groups()[0]

Page 9: 2015 bioinformatics bio_python

Question 3. Swiss-Knife.py

• Using a database as input ! Parse

the entire Swiss Prot collection

– How many entries are there ?

– Average Protein Length (in aa and

MW)

– Relative frequency of amino acids

• Compare to the ones used to construct

the PAM scoring matrixes from 1978 –

1991

Page 10: 2015 bioinformatics bio_python

Question 3: Getting the database

Uniprot_sprot.dat.gz – 528Mb

(save on your network drive H:\)

Unzipped 2.92 Gb !

http://www.ebi.ac.uk/uniprot/download-center

Page 11: 2015 bioinformatics bio_python

Amino acid frequencies

1978 1991

L 0.085 0.091

A 0.087 0.077

G 0.089 0.074

S 0.070 0.069

V 0.065 0.066

E 0.050 0.062

T 0.058 0.059

K 0.081 0.059

I 0.037 0.053

D 0.047 0.052

R 0.041 0.051

P 0.051 0.051

N 0.040 0.043

Q 0.038 0.041

F 0.040 0.040

Y 0.030 0.032

M 0.015 0.024

H 0.034 0.023

C 0.033 0.020

W 0.010 0.014

Second step: Frequencies of Occurence

Page 12: 2015 bioinformatics bio_python

Extra Questions

• How many records have a sequence of length 260?

• What are the first 20 residues of 143X_MAIZE?

• What is the identifier for the record with the

shortest sequence? Is there more than one record

with that length?

• What is the identifier for the record with the

longest sequence? Is there more than one record

with that length?

• How many contain the subsequence "ARRA"?

• How many contain the substring "KCIP-1" in the

description?

Page 13: 2015 bioinformatics bio_python

Perl / Python 00

• A class is a package

• An object is a reference to a data

structure (usually a hash) in a class

• A method is a subroutine in the class

Page 14: 2015 bioinformatics bio_python
Page 15: 2015 bioinformatics bio_python
Page 16: 2015 bioinformatics bio_python

Biopython functionality and tools

• The ability to parse bioinformatics files into Python utilizable data structures

• Support the following formats:– Blast output

– Clustalw

– FASTA

– PubMed and Medline

– ExPASy files

– SCOP

– SwissProt

– PDB

• Files in the supported formats can be iterated over record by record or indexed and accessed via a dictionary interface

Page 17: 2015 bioinformatics bio_python

Biopython functionality and tools

• Code to deal with on-line bioinformatics destinations (NCBI, ExPASy)

• Interface to common bioinformatics programs (Blast, ClustalW)

• A sequence obj dealing with seqs, seq IDs, seq features

• Tools for operations on sequences

• Tools for dealing with alignments

• Tools to manage protein structures

• Tools to run applications

Page 18: 2015 bioinformatics bio_python

Install Biopython

The Biopython module name is Bio

It must be downloaded and installed (http://biopython.org/wiki/Download)

You need to install numpy first

>>>import Bio

Page 19: 2015 bioinformatics bio_python

Install Biopython

pip is the preferred installer program.

Starting with Python 3.4, it is included

by default with the Python binary

installers.

pip3.5 install Biopython

#pip3.5 install yahoo_finance

from yahoo_finance import Share

yahoo = Share('AAPL')

print (yahoo.get_open())

Page 20: 2015 bioinformatics bio_python

Run Install.py (is BioPython installed ?)

import pip

import sys

import platform

import webbrowser

print ("Python " + platform.python_version()+ " installed

packages:")

installed_packages = pip.get_installed_distributions()

installed_packages_list = sorted(["%s==%s" % (i.key, i.version)

for i in installed_packages])

print(*installed_packages_list,sep="\n")

Page 21: 2015 bioinformatics bio_python

BioPython

• Make a histogram of the MW (in kDa) of all proteins in Swiss-Prot

• Find the most basic and most acidic protein in Swiss-Prot?

• Biological relevance of the results ?

From AAIndex

H ZIMJ680104

D Isoelectric point (Zimmerman et al., 1968)

R LIT:2004109b PMID:5700434

A Zimmerman, J.M., Eliezer, N. and Simha, R.

T The characterization of amino acid sequences in proteins by statistical

methods

J J. Theor. Biol. 21, 170-201 (1968)

C KLEP840101 0.941 FAUJ880111 0.813 FINA910103 0.805

I A/L R/K N/M D/F C/P Q/S E/T G/W H/Y I/V

6.00 10.76 5.41 2.77 5.05 5.65 3.22 5.97 7.59 6.02

5.98 9.74 5.74 5.48 6.30 5.68 5.66 5.89 5.66 5.96

Page 22: 2015 bioinformatics bio_python

• Introduction to Biopython

– Sequence objects (I)

– Sequence Record objects (I)

– Protein structures (PDB module) (II)

• Working with DNA and protein sequences

– Transcription and Translation

• Extracting information from biological resources

– Parsing Swiss-Prot files (I)

– Parsing BLAST output (I)

– Accessing NCBI’s Entrez databases (II)

– Parsing Medline records (II)

• Running external applications (e.g. BLAST) locally and from a script

– Running BLAST over the Internet

– Running BLAST locally

• Working with motifs

– Parsing PROSITE records

– Parsing PROSITE documentation records

Page 23: 2015 bioinformatics bio_python

Introduction to Biopython (I)

• Sequence objects

• Sequence Record objects

Page 24: 2015 bioinformatics bio_python

Sequence Object

• Seq objects vs Python strings:

– They have different methods

– The Seq object has the attribute alphabet

(biological meaning of Seq)

>>> import Bio

>>> from Bio.Seq import Seq

>>> my_seq = Seq("AGTACACTGGT")

>>> my_seq

Seq('AGTACACTGGT', Alphabet())

>>> print my_seq

Seq('AGTACACTGGT', Alphabet())

>>> my_seq.alphabet

Alphabet()

>>>

Page 25: 2015 bioinformatics bio_python

The alphabet attribute

• Alphabets are defined in the Bio.Alphabet module

• We will use the IUPAC alphabets

(http://www.chem.qmw.ac.uk/iupac)

• Bio.Alphabet.IUPAC provides definitions for DNA, RNA and

proteins + provides extension and customization of basic

definitions:

– IUPACProtein (IUPAC standard AA)

– ExtendedIUPACProtein (+ selenocysteine, X,

etc)

– IUPACUnambiguousDNA (basic GATC letters)

– IUPACAmbiguousDNA (+ ambiguity letters)

– ExtendedIUPACDNA (+ modified bases)

– IUPACUnambiguousRNA

– IUPACAmbiguousRNA

Page 26: 2015 bioinformatics bio_python

>>> import Bio

>>> from Bio.Seq import Seq

>>> from Bio.Alphabet import IUPAC

>>> my_seq = Seq("AGTACACTGGT", IUPAC.unambiguous_dna)

>>> my_seq

Seq('AGTACACTGGT', IUPACUnambiguousDNA())

>>> my_seq.alphabet

IUPACUnambiguousDNA()

>>> my_seq = Seq("AGTACACTGGT", IUPAC.protein)

>>> my_seq

Seq('AGTACACTGGT', IUPACProtein())

>>> my_seq.alphabet

IUPACProtein()

>>>

The alphabet attribute

Page 27: 2015 bioinformatics bio_python

>>> my_seq = Seq("AGTAACCCTTAGCACTGGT", IUPAC.unambiguous_dna)

>>> for index, letter in enumerate(my_seq):

... print index, letter

...

0 A

1 G

2 T

3 A

4 A

5 C

...etc

>>> print len(my_seq)

19

>>> print my_seq[0]

A

>>> print my_seq[2:10]

Seq('TAACCCTT', IUPACProtein())

>>> my_seq.count('A')

5

>>> 100*float(my_seq.count('C')+my_seq.count('G'))/len(my_seq)

47.368421052631582

Sequences act like strings

Page 28: 2015 bioinformatics bio_python

>>> my_seq = Seq("AGTAACCCTTAGCACTGGT", IUPAC.unambiguous_dna)

>>>>>> str(my_seq)

'AGTAACCCTTAGCACTGGT'

>>> print my_seq

AGTAACCCTTAGCACTGGT

>>> fasta_format_string = ">DNA_id\n%s\n"% my_seq

>>> print fasta_format_string

>DNA_id

AGTAACCCTTAGCACTGGT

# Biopython 1.44 or older

>>>my_seq.tostring()

'AGTAACCCTTAGCACTGGT'

Turn Seq objects into strings

You may need the plain sequence string (e.g. to write to a file or to insert

into a database)

Page 29: 2015 bioinformatics bio_python

>>> dna_seq = Seq("AGTAACCCTTAGCACTGGT", IUPAC.unambiguous_dna)

>>> protein_seq = Seq("KSMKPPRTHLIMHWIIL", IUPAC.IUPACProtein())

>>> protein_seq + dna_seq

Traceback (most recent call last):

File "<stdin>", line 1, in ?

File "/home/abarbato/biopython-1.53/build/lib.linux-x86_64-

2.4/Bio/Seq.py", line 216, in __add__

raise TypeError("Incompatable alphabets %s and %s" \

TypeError: Incompatable alphabets IUPACProtein() and

IUPACUnambiguousDNA()

BUT, if you give generic alphabet to dna_seq and protein_seq:>>> from Bio.Alphabet import generic_alphabet

>>> dna_seq.alphabet = generic_alphabet

>>> protein_seq.alphabet = generic_alphabet

>>> protein_seq + dna_seq

Seq('KSMKPPRTHLIMHWIILAGTAACCCTTAGCACTGGT', Alphabet())

Concatenating sequences

You can’t add sequences with incompatible alphabets (protein sequence

and DNA sequence)

Page 30: 2015 bioinformatics bio_python

>>> from Bio.Alphabet import generic_dna

>>> dna_seq = Seq("acgtACGT", generic_dna)

>>> dna_seq.upper()

Seq('ACGTACGT', DNAAlphabet())

>>> dna_seq.lower()

Seq('acgtacgt', DNAAlphabet())

>>>

Changing case

Seq objects have upper() and lower() methods

Note that the IUPAC alphabets are for upper case only

Page 31: 2015 bioinformatics bio_python

>>> from Bio.Seq import Seq

>>> from Bio.Alphabet import IUPAC

>>> dna_seq = Seq("AGTAACCCTTAGCACTGGT", IUPAC.unambiguous_dna)

>>> dna_seq.complement()

Seq('TCATTGGGAATCGTGACCA', IUPACUnambiguousDNA())

>>> dna_seq.reverse_complement()

Seq('ACCAGTGCTAAGGGTTACT', IUPACUnambiguousDNA())

Nucleotide sequences and (reverse) complements

Seq objects have upper() and lower() methods

Note that these operations are not allowed with protein

alphabets

Page 32: 2015 bioinformatics bio_python

Transcription

Page 33: 2015 bioinformatics bio_python

Transcription

>>> coding_dna =

Seq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG",

IUPAC.unambiguous_dna)

>>> template_dna = coding_dna.reverse_complement()

>>> template_dna

Seq('CTATCGGGCACCCTTTCAGCGGCCCATTACAATGGCCAT',

IUPACUnambiguousDNA())

>>> messenger_rna = coding_dna.transcribe()

>>> messenger_rna

Seq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG',

IUPACUnambiguousRNA())

>>> messenger_rna.back_transcribe()

Seq('ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG',

IUPACUnambiguousDNA())

Note: all this does is a switch T --> U and adjust the alphabet.

The Seq object also includes a back-transcription method:

Page 34: 2015 bioinformatics bio_python

Translation

>>> from Bio.Seq import Seq

>>> from Bio.Alphabet import IUPAC

>>> messenger_rna = Seq('AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG',

IUPAC.unambiguous_rna)

>>> messenger_rna.translate()

Seq('MAIVMGR*KGAR*', HasStopCodon(IUPACProtein(), '*'))

>>>

You can also translate directly from the coding strand DNA sequence

>>> from Bio.Seq import Seq

>>> from Bio.Alphabet import IUPAC

>>> coding_dna = Seq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG",

IUPAC.unambiguous_dna)

>>> coding_dna.translate()

Seq('MAIVMGR*KGAR*', HasStopCodon(IUPACProtein(), '*'))

>>>

Page 35: 2015 bioinformatics bio_python

Translation with different translation tables

>>> coding_dna.translate(table="Vertebrate Mitochondrial")

Seq('MAIVMGRWKGAR*', HasStopCodon(IUPACProtein(), '*'))

>>> coding_dna.translate(table=2)

Seq('MAIVMGRWKGAR*', HasStopCodon(IUPACProtein(), '*'))

>>> coding_dna.translate(to_stop = True)

Seq('MAIVMGR', IUPACProtein())

>>> coding_dna.translate(table=2,to_stop = True)

Seq('MAIVMGRWKGAR', IUPACProtein())

Translation tables available in Biopython are based on those from the NCBI.

By default, translation will use the standard genetic code (NCBI table id 1)

If you deal with mitochondrial sequences:

If you want to translate the nucleotides up to the first in frame stop, and

then stop (as happens in nature):

Page 36: 2015 bioinformatics bio_python

Translation tables

>>> from Bio.Data import CodonTable

>>> standard_table =

CodonTable.unambiguous_dna_by_name["Standard"]

>>> mito_table =

CodonTable.unambiguous_dna_by_name["Vertebrate Mitochondrial"]

#Using the NCB table ids:

>>>standard_table = CodonTable.unambiguous_dna_by_id[1]

>>> mito_table = CodonTable.unambiguous_dna_by_id[2]

Translation tables available in Biopython are based on those from the NCBI.

By default, translation will use the standard genetic code (NCBI table id 1)

http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi

Page 37: 2015 bioinformatics bio_python

Translation tables

>>>print standard_table

Table 1 Standard, SGC0

| T | C | A | G |

--+---------+---------+---------+---------+--

T | TTT F | TCT S | TAT Y | TGT C | T

T | TTC F | TCC S | TAC Y | TGC C | C

T | TTA L | TCA S | TAA Stop| TGA Stop| A

T | TTG L(s)| TCG S | TAG Stop| TGG W | G

--+---------+---------+---------+---------+--

C | CTT L | CCT P | CAT H | CGT R | T

C | CTC L | CCC P | CAC H | CGC R | C

C | CTA L | CCA P | CAA Q | CGA R | A

C | CTG L(s)| CCG P | CAG Q | CGG R | G

--+---------+---------+---------+---------+--

A | ATT I | ACT T | AAT N | AGT S | T

A | ATC I | ACC T | AAC N | AGC S | C

A | ATA I | ACA T | AAA K | AGA R | A

A | ATG M(s)| ACG T | AAG K | AGG R | G

--+---------+---------+---------+---------+--

G | GTT V | GCT A | GAT D | GGT G | T

G | GTC V | GCC A | GAC D | GGC G | C

G | GTA V | GCA A | GAA E | GGA G | A

G | GTG V | GCG A | GAG E | GGG G | G

--+---------+---------+---------+---------+--

Page 38: 2015 bioinformatics bio_python

Translation tables

>>> print mito_table

Table 2 Vertebrate Mitochondrial, SGC1

| T | C | A | G |

--+---------+---------+---------+---------+--

T | TTT F | TCT S | TAT Y | TGT C | T

T | TTC F | TCC S | TAC Y | TGC C | C

T | TTA L | TCA S | TAA Stop| TGA W | A

T | TTG L | TCG S | TAG Stop| TGG W | G

--+---------+---------+---------+---------+--

C | CTT L | CCT P | CAT H | CGT R | T

C | CTC L | CCC P | CAC H | CGC R | C

C | CTA L | CCA P | CAA Q | CGA R | A

C | CTG L | CCG P | CAG Q | CGG R | G

--+---------+---------+---------+---------+--

A | ATT I(s)| ACT T | AAT N | AGT S | T

A | ATC I(s)| ACC T | AAC N | AGC S | C

A | ATA M(s)| ACA T | AAA K | AGA Stop| A

A | ATG M(s)| ACG T | AAG K | AGG Stop| G

--+---------+---------+---------+---------+--

G | GTT V | GCT A | GAT D | GGT G | T

G | GTC V | GCC A | GAC D | GGC G | C

G | GTA V | GCA A | GAA E | GGA G | A

G | GTG V(s)| GCG A | GAG E | GGG G | G

--+---------+---------+---------+---------+--

Page 39: 2015 bioinformatics bio_python

MutableSeq objects

>>> from Bio.Seq import Seq

>>> from Bio.Alphabet import IUPAC

>>> my_seq =

Seq('CGCGCGGGTTTATGATGACCCAAATATAGAGGGCACAC',

IUPAC.unambiguous_dna)

>>> my_seq[5] = 'A'

Traceback (most recent call last):

File "<stdin>", line 1, in ?

TypeError: object does not support item assignment

>>>

Like Python strings, Seq objects are immutable

However, you can convert it into a mutable sequence (a MutableSeq object)

>>> mutable_seq = my_seq.tomutable()

>>> mutable_seq

MutableSeq('CGCGCGGGTTTATGATGACCCAAATATAGAGGGCACAC',

IUPACUnambiguousDNA())

Page 40: 2015 bioinformatics bio_python

MutableSeq objects

>>> from Bio.Seq import Seq

>>> from Bio.Alphabet import IUPAC

>>> mutable_seq =

MutableSeq('CGCGCGGGTTTATGATGACCCAAATATAGAGGGCACAC',

IUPAC.unambiguous_dna)

>>> mutable_seq[5] = 'A'

>>> mutable_seq

MutableSeq('CGCGCAGGTTTATGATGACCCAAATATAGAGGGCACAC',

IUPACUnambiguousDNA())

You can create a mutable object directly

A MutableSeq object can be easily converted into a read-only sequence:

>>> new_seq = mutable_seq.toseq()

>>> new_seq

Seq('CGCGCAGGTTTATGATGACCCAAATATAGAGGGCACAC',

IUPACUnambiguousDNA())

Page 41: 2015 bioinformatics bio_python

Sequence Record objects

The SeqRecord class is defined in the Bio.SeqRecord module

This class allows higher level features such as identifiers and features to be

associated with a sequence

>>> from Bio.SeqRecord import SeqRecord

>>> help(SeqRecord)

Page 42: 2015 bioinformatics bio_python

class SeqRecord(__builtin__.object)

A SeqRecord object holds a sequence and information about it.

Main attributes:id - Identifier such as a locus tag (string)

seq - The sequence itself (Seq object or similar)

Additional attributes:name - Sequence name, e.g. gene name (string)

description - Additional text (string)

dbxrefs - List of db cross references (list of strings)

features - Any (sub)features defined (list of SeqFeature objects)

annotations - Further information about the whole sequence (dictionary)

Most entries are strings, or lists of strings.

letter_annotations -

Per letter/symbol annotation (restricted dictionary). This holds

Python sequences (lists, strings or tuples) whose length

matches that of the sequence. A typical use would be to hold a

list of integers representing sequencing quality scores, or a string representing the secondary structure.

Page 43: 2015 bioinformatics bio_python

>>> from Bio.Seq import Seq

>>> from Bio.SeqRecord import SeqRecord

>>> TMP = Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF’)

>>> TMP_r = SeqRecord(TMP)

>>> TMP_r.id

'<unknown id>'

>>> TMP_r.id = 'YP_025292.1'

>>> TMP_r.description = 'toxic membrane protein'

>>> print TMP_r

ID: YP_025292.1

Name: <unknown name>

Description: toxic membrane protein

Number of features: 0

Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF',

Alphabet())

>>> TMP_r.seq

Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF',

Alphabet())

You will typically use Bio.SeqIO to read in sequences from files as

SeqRecord objects. However, you may want to create your own SeqRecord

objects directly:

Page 44: 2015 bioinformatics bio_python

>>> from Bio.Seq import Seq

>>> from Bio.SeqRecord import SeqRecord

>>> from Bio.Alphabet import IUPAC

>>> record

SeqRecord(seq=Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQ

TEVAVF', IUPACProtein()), id='YP_025292.1', name='HokC',

description='toxic membrane protein', dbxrefs=[])

>>> print record

ID: YP_025292.1

Name: HokC

Description: toxic membrane protein

Number of features: 0

Seq('MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF',

IUPACProtein())

>>>

You can also create your own SeqRecord objects as follows:

Page 45: 2015 bioinformatics bio_python

The format() method

It returns a string containing your cord formatted using one of the output file formats supported by Bio.SeqIO

>>> from Bio.Seq import Seq

>>> from Bio.SeqRecord import SeqRecord

>>> from Bio.Alphabet import generic_protein

>>> rec =

SeqRecord(Seq("MGSNKSKPKDASQRRRSLEPSENVHGAGGAFPASQTPSKPASADGHRGPSA

AFVPPAAEPKLFGGFNSSDTVTSPQRAGALAGGVTTFVALYDYESRTETDLSFKKGERLQIVNNTR

KVDVREGDWWLAHSLSTGQTGYIPS", generic_protein), id = "P05480",

description = "SRC_MOUSE Neuronal proto-oncogene tyrosine-protein

kinase Src: MY TEST")

>>> print rec.format("fasta")

>P05480 SRC_MOUSE Neuronal proto-oncogene tyrosine-protein kinase

Src: MY TEST

MGSNKSKPKDASQRRRSLEPSENVHGAGGAFPASQTPSKPASADGHRGPSAAFVPPAAEP

KLFGGFNSSDTVTSPQRAGALAGGVTTFVALYDYESRTETDLSFKKGERLQIVNNTRKVD

VREGDWWLAHSLSTGQTGYIPS

Page 46: 2015 bioinformatics bio_python

INPUT FILE

SCRIPT.py

OUTPUT FILE

Seq1 “ACTGGGAGCTAGC”

Seq2 “TTGATCGATCGATCG”

Seq3 “GTGTAGCTGCT”

F = open(“input.txt”)

for line in F:

<parse line>

<get seq id>

<get description>

<get sequence>

<get other info>

from Bio.Seq import Seq

from Bio.SeqRecord import SeqRecord

from Bio.Alphabet import generic_protein

rec = SeqRecord(Seq(<sequence>, alphabet),id

= <seq_id>, description = <description>)

Format_rec = rec.format(“fasta”)

Out.write(Format_rec)

>P05480 SRC_MOUSE Neuronal proto-oncogene tyrosine-protein

kinase Src: MY TEST

MGSNKSKPKDASQRRRSLEPSENVHGAGGAFPASQTPSKPASADGHRGPSAAFVPPAAEP

KLFGGFNSSDTVTSPQRAGALAGGVTTFVALYDYESRTETDLSFKKGERLQIVNNTRKVD

Page 47: 2015 bioinformatics bio_python

Extracting information from biological resources:

parsing Swiss-Prot files, PDB files, ENSEMBLE records,

blast output files, etc.

• Sequence I/O– Parsing or Reading Sequences

– Writing Sequence Files

A simple interface for working with assorted file formats in a uniform way

>>>from Bio import SeqIO

>>>help(SeqIO)

Bio.SeqIO

Page 48: 2015 bioinformatics bio_python

Bio.SeqIO.parse()

• A handle to read the data form. It can be:

– a file opened for reading

– the output from a command line program

– data downloaded from the internet

• A lower case string specifying the sequence format (see

http://biopython.org/wiki/SeqIO for a full listing of supported

formats).

Reads in sequence data as SeqRecord objects.

It expects two arguments.

The object returned by Bio.SeqIO is an iterator which returns SeqRecord

objects

Page 49: 2015 bioinformatics bio_python

>>> from Bio import SeqIO

>>> handle = open("P05480.fasta")

>>> for seq_rec in SeqIO.parse(handle, "fasta"):

... print seq_rec.id

... print repr(seq_rec.seq)

... print len(seq_rec)

...

sp|P05480|SRC_MOUSE

Seq('MGSNKSKPKDASQRRRSLERGPSA...ENL', SingleLetterAlphabet())

541

>>> handle.close()

>>> for seq_rec in SeqIO.parse(handle, "genbank"):

... print seq_rec.id

... print repr(seq_rec.seq)

... print len(seq_rec)

...

U49845.1

Seq('GATCCTCCATATACAACGGTACGGAA...ATC', IUPACAmbiguousDNA())

5028

>>> handle.close()

Page 50: 2015 bioinformatics bio_python

>>> from Bio import SeqIO

>>> handle = open("AP006852.gbk")

>>> for seq_rec in SeqIO.parse(handle, "genbank"):

... print seq_rec.id

... print repr(seq_rec.seq)

... print len(seq_rec)

...

AP006852.1

Seq('CCACTGTCCAATACCCCCAACAGGAAT...TGT', IUPACAmbiguousDNA())

949626

>>>

>>>handle = open("AP006852.gbk")

>>>identifiers=[seq_rec.id for seq_rec in SeqIO.parse(handle,"genbank")]

>>>handle.close()

>>>identifiers

['AP006852.1']

>>>

Candida albicans genomic DNA, chromosome 7, complete sequence

Using list comprehension:

Page 51: 2015 bioinformatics bio_python

>>> from Bio import SeqIO

>>> handle = open("sprot_prot.fasta")

>>> ids = [seq_rec.id for seq_rec in SeqIO.parse(handle,"fasta")]

>>> ids

['sp|P24928|RPB1_HUMAN', 'sp|Q9NVU0|RPC5_HUMAN',

'sp|Q9BUI4|RPC3_HUMAN', 'sp|Q9BUI4|RPC3_HUMAN',

'sp|Q9NW08|RPC2_HUMAN', 'sp|Q9H1D9|RPC6_HUMAN',

'sp|P19387|RPB3_HUMAN', 'sp|O14802|RPC1_HUMAN',

'sp|P52435|RPB11_HUMAN', 'sp|O15318|RPC7_HUMAN',

'sp|P62487|RPB7_HUMAN', 'sp|O15514|RPB4_HUMAN',

'sp|Q9GZS1|RPA49_HUMAN', 'sp|P36954|RPB9_HUMAN',

'sp|Q9Y535|RPC8_HUMAN', 'sp|O95602|RPA1_HUMAN',

'sp|Q9Y2Y1|RPC10_HUMAN', 'sp|Q9H9Y6|RPA2_HUMAN',

'sp|P78527|PRKDC_HUMAN', 'sp|O15160|RPAC1_HUMAN',…,

'sp|Q9BWH6|RPAP1_HUMAN']

>>> ]

Here we do it using the sprot_prot.fasta file

Page 52: 2015 bioinformatics bio_python

Iterating over the records in a sequence file

Instead of using a for loop, you can also use the next() method of an

iterator to step through the entries

>>> handle = open("sprot_prot.fasta")

>>> rec_iter = SeqIO.parse(handle, "fasta")

>>> rec_1 = rec_iter.next()

>>> rec_1

SeqRecord(seq=Seq('MHGGGPPSGDSACPLRTIKRVQFGVLSPDELKRMSVTEGGIKYPET

TEGGRPKL...EEN', SingleLetterAlphabet()),

id='sp|P24928|RPB1_HUMAN', name='sp|P24928|RPB1_HUMAN',

description='sp|P24928|RPB1_HUMAN DNA-directed RNA polymerase II

subunit RPB1 OS=Homo sapiens GN=POLR2A PE=1 SV=2', dbxrefs=[])

>>> rec_2 = rec_iter.next()

>>> rec_2

SeqRecord(seq=Seq('MANEEDDPVVQEIDVYLAKSLAEKLYLFQYPVRPASMTYDDIPHLS

AKIKPKQQ...VQS', SingleLetterAlphabet()),

id='sp|Q9NVU0|RPC5_HUMAN', name='sp|Q9NVU0|RPC5_HUMAN',

description='sp|Q9NVU0|RPC5_HUMAN DNA-directed RNA polymerase III

subunit RPC5 OS=Homo sapiens GN=POLR3E PE=1 SV=1', dbxrefs=[])

>>> handle.close()

Page 53: 2015 bioinformatics bio_python

If your file has one and only one record (e.g. a GenBank file for a single chromosome), then use the Bio.SeqIO.read().

This will check there are no extra unexpected records present

Bio.SeqIO.read()

>>> rec_iter = SeqIO.parse(open("1293613.gbk"), "genbank")

>>> rec = rec_iter.next()

>>> print rec

ID: U49845.1

Name: SCU49845

Description: Saccharomyces cerevisiae TCP1-beta gene, partial cds, and Axl2p

(AXL2) and Rev7p (REV7) genes, complete cds.

Number of features: 6

/sequence_version=1

/source=Saccharomyces cerevisiae (baker's yeast)

/taxonomy=['Eukaryota', 'Fungi', 'Ascomycota', 'Saccharomycotina',

'Saccharomycetes', 'Saccharomycetales', 'Saccharomycetaceae', 'Saccharomyces']

/keywords=['']

/references=[Reference(title='Cloning and sequence of REV7, a gene whose function

is required for DNA damage-induced mutagenesis in Saccharomyces cerevisiae', ...),

Reference(title='Selection of axial growth sites in yeast requires Axl2p, a novel

plasma membrane glycoprotein', ...), Reference(title='Direct Submission', ...)]

/accessions=['U49845']

/data_file_division=PLN

/date=21-JUN-1999

/organism=Saccharomyces cerevisiae

/gi=1293613

Seq('GATCCTCCATATACAACGGTATCTCCACCTCAGGTTTAGATCTCAACAACGGAA...ATC',

IUPACAmbiguousDNA())

Page 54: 2015 bioinformatics bio_python

Sequence files as lists

Sequence files as dictionaries

>>> from Bio import SeqIO

>>> handle = open("ncbi_gene.fasta")

>>> records = list(SeqIO.parse(handle, "fasta"))

>>> >>> records[-1]

SeqRecord(seq=Seq('gggggggggggggggggatcactctctttcagtaacctcaac...c

cc', SingleLetterAlphabet()), id='A10421', name='A10421',

description='A10421 Synthetic nucleotide sequence having a human

IL-2 gene obtained from pILOT135-8. : Location:1..1000',

dbxrefs=[])

>>> handle = open("ncbi_gene.fasta")

>>> records = SeqIO.to_dict(SeqIO.parse(handle, "fasta"))

>>> handle.close()

>>> records.keys()

['M69013', 'M69012', 'AJ580952', 'J03005', 'J03004', 'L13858',

'L04510', 'M94539', 'M19650', 'A10421', 'AJ002990', 'A06663',

'A06662', 'S62035', 'M57424', 'M90035', 'A06280', 'X95521',

'X95520', 'M28269', 'S50017', 'L13857', 'AJ345013', 'M31328',

'AB038040', 'AB020593', 'M17219', 'DQ854814', 'M27543', 'X62025',

'M90043', 'L22075', 'X56614', 'M90027']

>>> seq_record = records['X95521']

'X95521 M.musculus mRNA for cyclic nucleotide phosphodiesterase :

Location:1..1000'

Page 55: 2015 bioinformatics bio_python

Parsing sequences from the net

Parsing GenBank records from the net

Parsing SwissProt sequence from the net

Handles are not always from files

>>>from Bio import Entrez

>>>from Bio import SeqIO

>>>handle = Entrez.efetch(db="nucleotide",rettype="fasta",id="6273291")

>>>seq_record = SeqIO.read(handle,”fasta”)

>>>handle.close()

>>>seq_record.description

>>>from Bio import ExPASy

>>>from Bio import SeqIO

>>>handle = ExPASy.get_sprot_raw("6273291")

>>>seq_record = SeqIO.read(handle,”swiss”)

>>>handle.close()

>>>print seq_record.id

>>>print seq_record.name

>>>prin seq_record.description

Page 56: 2015 bioinformatics bio_python

Indexing really large files

Bio.SeqIO.index() returns a dictionary without keeping

everything in memory.

It works fine even for million of sequences

The main drawback is less flexibility: it is read-only

>>> from Bio import SeqIO

>>> recs_dict = SeqIO.index("ncbi_gene.fasta", "fasta")>>> len(recs_dict)

34

>>> recs_dict.keys()

['M69013', 'M69012', 'AJ580952', 'J03005', 'J03004', 'L13858', 'L04510',

'M94539', 'M19650', 'A10421', 'AJ002990', 'A06663', 'A06662', 'S62035',

'M57424', 'M90035', 'A06280', 'X95521', 'X95520', 'M28269', 'S50017',

'L13857', 'AJ345013', 'M31328', 'AB038040', 'AB020593', 'M17219', 'DQ854814',

'M27543', 'X62025', 'M90043', 'L22075', 'X56614', 'M90027']

>>> print recs_dict['M57424']

ID: M57424

Name: M57424

Description: M57424 Human adenine nucleotide translocator-2 (ANT-2) gene,

complete cds. : Location:1..1000

Number of features: 0

Seq('gagctctggaatagaatacagtagaggcatcatgctcaaagagagtagcagatg...agc',

SingleLetterAlphabet())

Page 57: 2015 bioinformatics bio_python

Writing sequence files

Bio.SeqIO.write()

This function takes three arguments:

1. some SeqRecord objects

2. a handle to write to

3. a sequence format

from Bio.Seq import Seq

from Bio.SeqRecors import SeqRecord

from Bio.Alphabet import generic_protein

Rec1 = SqRecord(Seq(“ACCA…”,generic_protein), id=“1”, description=“”)

Rec1 = SqRecord(Seq(“CDRFAA”,generic_protein), id=“2”, description=“”)

Rec1 = SqRecord(Seq(“GRKLM”,generic_protein), id=“3”, description=“”)

My_records = [Rec1, Rec2, Rec3]

from Bio import SeqIO

handle = open(“MySeqs.fas”,”w”)

SeqIO.write(My_records, handle, “fasta”)

handle.close()

Page 58: 2015 bioinformatics bio_python

Converting between sequence file formats

We can do file conversion by combining Bio.SeqIO.parse()

and Bio.SeqIO.write()

from Bio import SeqIO

>>> In_handle = open ("AP006852.gbk", "r")

>>> Out_handle = open("AP006852.fasta", "w")

>>> records = SeqIO.parse(In_handle, "genbank")

>>> count = SeqIO.write(records, Out_handle, "fasta")

>>> count

1

>>>

>>> In_handle.close()

>>> Out_handle.close()