Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions
Bio.SeqUtils.CodonUsage.CodonAdaptationIndex Class Reference

List of all members.

Public Member Functions

def __init__
def set_cai_index
def generate_index
def cai_for_gene
def print_index

Public Attributes

 index
 codon_count

Private Member Functions

def _count_codons

Detailed Description

A codon adaptation index (CAI) implementation.

Implements the codon adaptation index (CAI) described by Sharp and
Li (Nucleic Acids Res. 1987 Feb 11;15(3):1281-95).

NOTE - This implementation does not currently cope with alternative genetic
codes: only the synonymous codons in the standard table are considered.

Definition at line 47 of file CodonUsage.py.


Constructor & Destructor Documentation

Definition at line 57 of file CodonUsage.py.

00057 
00058     def __init__(self):
00059         self.index = {}
00060         self.codon_count = {}
    

Member Function Documentation

def Bio.SeqUtils.CodonUsage.CodonAdaptationIndex._count_codons (   self,
  fasta_file 
) [private]

Definition at line 132 of file CodonUsage.py.

00132 
00133     def _count_codons(self, fasta_file):
00134         handle = open(fasta_file, 'r')
00135         
00136         # make the codon dictionary local
00137         self.codon_count = CodonsDict.copy()
00138 
00139         # iterate over sequence and count all the codons in the FastaFile.
00140         for cur_record in SeqIO.parse(handle, "fasta"):
00141             # make sure the sequence is lower case
00142             if str(cur_record.seq).islower():
00143                 dna_sequence = str(cur_record.seq).upper()
00144             else:
00145                 dna_sequence = str(cur_record.seq)
00146             for i in range(0, len(dna_sequence), 3):
00147                 codon = dna_sequence[i:i+3]
00148                 if codon in self.codon_count:
00149                     self.codon_count[codon] += 1
00150                 else:
00151                     raise TypeError("illegal codon %s in gene: %s" % (codon, cur_record.id))
00152         handle.close()
    

Here is the call graph for this function:

Here is the caller graph for this function:

Calculate the CAI (float) for the provided DNA sequence (string).

This method uses the Index (either the one you set or the one you generated)
and returns the CAI for the DNA sequence.

Definition at line 105 of file CodonUsage.py.

00105 
00106     def cai_for_gene(self, dna_sequence):
00107         """Calculate the CAI (float) for the provided DNA sequence (string).
00108         
00109         This method uses the Index (either the one you set or the one you generated)
00110         and returns the CAI for the DNA sequence.
00111         """
00112         cai_value, cai_length = 0, 0
00113 
00114         # if no index is set or generated, the default SharpEcoliIndex will be used.
00115         if self.index == {}:
00116             self.set_cai_index(SharpEcoliIndex)
00117             
00118         if dna_sequence.islower():
00119             dna_sequence = dna_sequence.upper()
00120 
00121         for i in range (0, len(dna_sequence), 3):
00122             codon = dna_sequence[i:i+3]
00123             if codon in self.index:
00124                 if codon not in ['ATG', 'TGG']: # these two codons are always one, exclude them
00125                     cai_value += math.log(self.index[codon])
00126                     cai_length += 1
00127             elif codon not in ['TGA', 'TAA', 'TAG']: # some indices may not include stop codons
00128                 raise TypeError("illegal codon in sequence: %s.\n%s" % (codon, self.index))
00129 
00130         return math.exp(cai_value / (cai_length - 1.0))
00131         
            

Here is the call graph for this function:

Generate a codon usage index from a FASTA file of CDS sequences.

Takes a location of a Fasta file containing CDS sequences
(which must all have a whole number of codons) and generates a codon
usage index.

Definition at line 69 of file CodonUsage.py.

00069 
00070     def generate_index(self, fasta_file):
00071         """Generate a codon usage index from a FASTA file of CDS sequences.
00072         
00073         Takes a location of a Fasta file containing CDS sequences
00074         (which must all have a whole number of codons) and generates a codon
00075         usage index.
00076         """
00077         
00078         # first make sure we're not overwriting an existing index:
00079         if self.index != {} or self.codon_count != {}:
00080             raise ValueError("an index has already been set or a codon count has been done. cannot overwrite either.")
00081             
00082         # count codon occurrences in the file.
00083         self._count_codons(fasta_file)   
00084     
00085         # now to calculate the index we first need to sum the number of times
00086         # synonymous codons were used all together.
00087         for aa in SynonymousCodons:
00088             total = 0.0
00089             rcsu = [] # RCSU values are CodonCount/((1/num of synonymous codons) * sum of all synonymous codons)
00090             codons = SynonymousCodons[aa]
00091             
00092             for codon in codons:
00093                 total += self.codon_count[codon]
00094                 
00095             # calculate the RSCU value for each of the codons
00096             for codon in codons:
00097                 denominator = float(total) / len(codons)
00098                 rcsu.append(self.codon_count[codon] / denominator)
00099             
00100             # now generate the index W=RCSUi/RCSUmax:
00101             rcsu_max = max(rcsu)
00102             for i in range(len(codons)):
00103                 self.index[codons[i]] = rcsu[i] / rcsu_max
00104         
        

Here is the call graph for this function:

Prints out the index used.

Definition at line 154 of file CodonUsage.py.

00154 
00155     def print_index(self):
00156         """Prints out the index used."""
00157         for i in sorted(self.index):
00158             print "%s\t%.3f" % (i, self.index[i])
00159         
Sets up an index to be used when calculating CAI for a gene.
Just pass a dictionary similar to the SharpEcoliIndex in the
CodonUsageIndices module.

Definition at line 62 of file CodonUsage.py.

00062 
00063     def set_cai_index(self, index):
00064         """Sets up an index to be used when calculating CAI for a gene.
00065         Just pass a dictionary similar to the SharpEcoliIndex in the
00066         CodonUsageIndices module.
00067         """
00068         self.index = index  
    

Here is the caller graph for this function:


Member Data Documentation

Definition at line 59 of file CodonUsage.py.

Definition at line 58 of file CodonUsage.py.


The documentation for this class was generated from the following file: