Example #1
0
    def __init__(self, data=None, Info=None, GeneticCode=None, \
        Mask=None, ValueMask=None, Constraint=None):
        """Initializes new CodonUsage with Info and frequency data.
        
        Note: Mask, ValueMask and Constraint are ignored, but must be present
        to support copy() because of the ConstrainedContainer interface.
        """
        #check if we have a sequence: if so, take it 3 bases at a time
        #this will (properly) fail on lists of tuples or anything else where
        #the items don't behave like strings.
        try:
            codons = [''.join(data[i:i + 3]) for i in xrange(0, len(data), 3)]
        except:
            codons = data or {}
        UnsafeFreqs.__init__(self, codons)
        #set required keys
        for k in self.RequiredKeys:
            if k not in self:
                self[k] = 0.0
        #flatten Info onto self directly for lookups
        if Info:
            self.__dict__.update(Info)
        self.Info = Info or {}

        if GeneticCode:
            if isinstance(GeneticCode, GenCodeClass):
                curr_code = GeneticCode
            else:
                curr_code = GeneticCodes[GeneticCode]
        else:
            curr_code = self._default_code
        self.GeneticCode = curr_code
Example #2
0
 def __init__(self, data=None, Info=None, GeneticCode=None, \
     Mask=None, ValueMask=None, Constraint=None):
     """Initializes new CodonUsage with Info and frequency data.
     
     Note: Mask, ValueMask and Constraint are ignored, but must be present
     to support copy() because of the ConstrainedContainer interface.
     """
     #check if we have a sequence: if so, take it 3 bases at a time
     #this will (properly) fail on lists of tuples or anything else where 
     #the items don't behave like strings.
     try:
         codons = [''.join(data[i:i+3]) for i in xrange(0, len(data), 3)]
     except:
         codons = data or {}
     UnsafeFreqs.__init__(self, codons)
     #set required keys
     for k in self.RequiredKeys:
         if k not in self:
             self[k] = 0.0
     #flatten Info onto self directly for lookups
     if Info:
         self.__dict__.update(Info)
     self.Info = Info or {}
     
     if GeneticCode:
         if isinstance(GeneticCode, GenCodeClass):
             curr_code = GeneticCode
         else:
             curr_code = GeneticCodes[GeneticCode]
     else:
         curr_code = self._default_code
     self.GeneticCode = curr_code
Example #3
0
    def _first_order_frequency_calculation(self):
        """Handles single-character calculations, which are independent.

        Specifically, don't need to take into account any other characters, and
        can just feed the whole thing into a single Freqs.
        """
        freqs = Freqs('')
        for line in self.Text:
            freqs += line
        #get rid of line breaks if necessary
        if not self.Linebreaks:
            for badkey in ['\r', '\n']:
                try:
                    del freqs[badkey]
                except KeyError:
                    pass    #don't care if there weren't any
        #if order is negative, equalize the frequencies
        if self.Order < 0:
            for key in freqs:
                freqs[key] = 1
        self.RawCounts= {'':deepcopy(freqs)}
        freqs.normalize()
        self.Frequencies = {'':freqs}