def __init__(self, in_dict, dict_type, alphabet=None): """Initialize the class.""" self.alphabet = alphabet if dict_type == COUNT: self.count = in_dict self._freq_from_count() elif dict_type == FREQ: self.count = {} self.update(in_dict) else: raise ValueError("bad dict_type") if not alphabet: self.alphabet = Alphabet.Alphabet() self.alphabet.letters = self._alphabet_from_input()
def __init__(self, data=None, alphabet=None, mat_name='', build_later=0): # User may supply: # data: matrix itself # mat_name: its name. See below. # alphabet: an instance of Bio.Alphabet, or a subclass. If not # supplied, constructor builds its own from that matrix.""" # build_later: skip the matrix size assertion. User will build the # matrix after creating the instance. Constructor builds a half matrix # filled with zeroes. assert type(mat_name) == type('') # "data" may be: # 1) None --> then self.data is an empty dictionary # 2) type({}) --> then self takes the items in data # 3) An instance of SeqMat # This whole creation-during-execution is done to avoid changing # default values, the way Python does because default values are # created when the function is defined, not when it is created. if data: try: self.update(data) except ValueError: raise ValueError, "Failed to store data in a dictionary" if alphabet == None: alphabet = Alphabet.Alphabet() assert Alphabet.generic_alphabet.contains(alphabet) self.alphabet = alphabet # If passed alphabet is empty, use the letters in the matrix itself if not self.alphabet.letters: self._alphabet_from_matrix() # Assert matrix size: half or full if not build_later: N = len(self.alphabet.letters) assert len(self) == N**2 or len(self) == N * (N + 1) / 2 self.ab_list = list(self.alphabet.letters) self.ab_list.sort() # Names: a string like "BLOSUM62" or "PAM250" self.mat_name = mat_name if build_later: self._init_zero() else: # Convert full to half self._full_to_half() self._correct_matrix() self.sum_letters = {} self.relative_entropy = 0
def __init__(self, seq, alphaproperty=None, insertprob=None, deleteprob=None,mualphabet=None, muprob=None,mupos=None,delpos=None,inpos=None, verbose=False): try: self.occureddel=list() # This is to keep a history of chnges made to the reference self.occuredmu=list() # This is necessary for writing the haplotypes in the format self.occuredins=list() # of haplotyping software's. self.inserted_allele=list() # keeps track of the inserted allele to be able to get them back when needed! self.alt_allele=list() # keeps track of the substituted if not isinstance(verbose,bool): raise CustomException("ERROR: verbose must be set to either True or False. \ Default is to False") else: self.verbose=verbose if isinstance(seq,str): if alphaproperty is None: if self.verbose: print("WARNING: No alphabet type is specified for the sequence string!") else: pass self.alphaproperty=Alphabet() else: self.alphaproperty=alphaproperty self.seq=MutableSeq(seq, self.alphaproperty) elif isinstance(seq,Seq): self.alphaproperty=seq.__getattribute__('alphabet') self.seq=seq.tomutable() elif isinstance(seq,MutableSeq): self.alphaproperty=seq.__getattribute__('alphabet') self.seq=copy.deepcopy(seq) else: raise CustomException("ERROR: Should provide a Seq or MutableSeq object, \n \ or a string sequence!") self.alphabet=set(str(self.seq)) self.ref=str(self.seq) if not delpos: self.delpos=[] else: if set(delpos).issubset(set(range(len(self.ref)))): self.delpos=list(delpos) # Deletion by specifying the positions else: raise CustomException("ERROR: Deletion positions exceed the range of the reference or are not positive integers!") if not inpos: self.inpos=[] else: if set(inpos).issubset(set(range(len(self.ref)))): self.inpos=list(inpos) # Insertion by specifying the positions else: raise CustomException("ERROR: Insertion positions exceed the range of the reference or are not positive integers!") if not mupos: self.mupos=[] else: if set(mupos).issubset(set(range(len(self.ref)))): self.mupos=list(mupos) # Mutation by specifying the positions else: raise CustomException("ERROR: Mutation positions exceed the range of the reference or are not positive integers!") if not mualphabet: if self.verbose: print("WARNING: You have specified no mutation alphabet! Mutations are set to random \ letters!") self.mualphabet=dict() for key in self.alphabet: self.mualphabet[key]=''.join(self.alphabet-{key}) # Non-specified mutations could happen to any letter else: mualphabet=dict([(str(k), str(v)) for k, v in mualphabet.iteritems()]) for key, value in mualphabet.iteritems(): if len(key)!=1: raise CustomException("ERROR: the mutation alphabet deals with point mutations! Only single letters are\ allowed as keys!") elif key in set(''.join(value)): raise CustomException("ERROR: Wrong mutation values specified! A letter could just be substituted with a\ different letter for mutation!") if set(mualphabet.keys())==self.alphabet and set(''.join(mualphabet.values()))<=self.alphabet: self.mualphabet=copy.deepcopy(mualphabet) elif set(mualphabet.keys())<self.alphabet and set(''.join(mualphabet.values()))<self.alphabet: if self.verbose: print("WARNING: Mutation is not specified for some letters! Those mutations are set\ to random letters!") self.mualphabet=copy.deepcopy(mualphabet) # Whatever has been specified for mutation alphabet is kep intact for key in self.alphabet-set(mualphabet.keys()): self.mualphabet[key]=''.join(self.alphabet-{key}) # Non-specified mutations could happen to any letter else: if self.verbose: print("WARNING: Mutation alphabet is not compatible with sequence alphabet! Both alphabets are\ updated and\nunspecified mutations are set to random letters!") new_mualphabet=dict() # As mutation may introduce novel alleles in the sequence, alphabet is updated first for key, value in mualphabet.iteritems(): # Whatever has been specified for mutation alphabet is kep intact self.alphabet.add(key) # Only the alphabet is updated if necessary self.alphabet|=(set(''.join(value))-self.alphabet) new_mualphabet.update({key:value}) for key in self.alphabet-set(new_mualphabet.keys()): new_mualphabet[key]=''.join(self.alphabet-{key}) # Non-specified mutations could happen to any letter self.mualphabet=copy.deepcopy(new_mualphabet) if not insertprob: self.insertprob=dict() # If no insertprob is given, it is set to zero everywhere for key in self.alphabet: self.insertprob[key]=0 else: if set(list(insertprob.keys()))!=self.alphabet: if self.verbose: print("WARNING: Missing/Invalid letter(s) in insertion probability!\n\ Probabilities are set to zero for missing letters! Invalid letters are ignored!") new_insertprob=dict() for key, value in insertprob.iteritems(): if value>=0 and value<=1: new_insertprob.update({key:value}) else: raise CustomException("ERROR: Insertion probability must be >=0 and <=1!") for key in self.alphabet-set(new_insertprob.keys()): new_insertprob[key]=0 self.insertprob=copy.deepcopy(new_insertprob) if not deleteprob: # If no deleteprob is given, it is set to zero everywhere self.deleteprob=dict() for key in self.alphabet: self.deleteprob[key]=0 else: if set(list(deleteprob.keys()))!=self.alphabet: if self.verbose: print("WARNING: Missing/Invalid letter(s) in deletion probability!\n\ Probabilities are set to zero for missing letters! Invalid letters are ignored!") new_deleteprob=dict() for key, value in deleteprob.iteritems(): if value>=0 and value<=1: new_deleteprob.update({key:value}) else: raise CustomException("ERROR: Deletion probability must be >=0 and <=1!") for key in self.alphabet-set(new_deleteprob.keys()): new_deleteprob[key]=0 self.deleteprob=copy.deepcopy(new_deleteprob) if not muprob: self.muprob=dict() # If no muprob is given, it is set to zero everywhere for key in self.alphabet: self.muprob[key]=0 else: if set(list(muprob.keys()))!=self.alphabet: if self.verbose: print("WARNING: Missing/Invalid letter(s) in mutation probability!\n\ Probabilities are set to zero for missing letters! Invalid letters are ignored!") new_muprob=dict() for key, value in muprob.iteritems(): if value>=0 and value<=1: new_muprob.update({key:value}) else: raise CustomException("ERROR: Mutation probability must be >=0 and <=1!") for key in self.alphabet-set(new_muprob.keys()): new_muprob[key]=0 self.muprob=copy.deepcopy(new_muprob) except CustomException as instance: print(instance) sys.exit(2) else: if self.verbose: print("MuGen object successfully created.\nWARNING: MuGen sequence is case sensitive!")