def list_seqs_from_regex(self, regex_seq, print_warnings = True, raise_overload_warning = True):
        """List sequences that match regular expression template.
    
        This function parses a limited regular expression vocabulary, and 
        lists all the sequences consistent with the regular expression. Supported 
        regex syntax: [] and {}. Cannot have two {} in a row. Note we can't use 
        Kline star (*) as this is the symbol for a stop codon --- use {}.
    
        Parameters
        ----------
        regex_seq : str
            The regular expression string that represents the sequences to be 
            listed.
        print_warnings : bool
            Determines whether warnings are printed or not. Default ON.
        raise_overload_warning : bool
            A flag to warn of more than 10000 seqs corresponding to the regex_seq
    
        Returns
        -------
        CDR3_seqs : list
            A list of CDR3 sequences that correspond to the regex_seq
        
        Examples
        --------
        >>> generation_probability.list_seqs_from_regex('CASS[AGR]SARPEQFF')
        ['CASSGSARPEQFF', 'CASSRSARPEQFF', 'CASSASARPEQFF']
        >>> generation_probability.list_seqs_from_regex('CASSAX{0,5}SARPEQFF')
        ['CASSASARPEQFF',
         'CASSAXXXXSARPEQFF',
         'CASSAXXSARPEQFF',
         'CASSAXXXXXSARPEQFF',
         'CASSAXXXSARPEQFF',
         'CASSAXSARPEQFF']
            
        """
    
        aa_symbols = ''.join(self.codons_dict)
        
        default_max_reps = 40
    
        #Check to make sure that expression is of the right form/symbols
        
        
        #Identify bracket expressions
        bracket_ex = [x for x in re.findall('\[[' + aa_symbols + ']*?\]|\{\d+,{0,1}\d*\}', regex_seq)]
        
        
        split_seq  = re.split('\[[' + aa_symbols + ']*?\]|\{\d+,{0,1}\d*\}', regex_seq)
        #Check that all remaining characters are in the codon dict
        for aa in ''.join(split_seq):
            if aa not in aa_symbols:
                if print_warnings:
                    print 'Unfamiliar symbol representing a codon:' + aa + ' --- check codon dictionary or the regex syntax'
                return []
        
        
        regex_list = [split_seq[i/2] if i%2 == 0 else bracket_ex[i/2] for i in range(len(bracket_ex) + len(split_seq)) if not (i%2 == 0 and len(split_seq[i/2]) ==  0)]
        
        max_num_seqs = 1
        for l, ex in enumerate(regex_list[::-1]):
            i = len(regex_list) - l - 1
            if ex[0] == '[': #bracket expression
                #check characters
                for aa in ex.strip('[]'):
                    if aa not in aa_symbols:
                        if print_warnings:
                            print 'Unfamiliar symbol representing a codon:' + aa + ' --- check codon dictionary'
                        return []
                max_num_seqs *= len(ex) - 2
            elif ex[0] == '{': #curly bracket
                if i == 0:
                    if print_warnings:
                        print "Can't have {} expression at start of sequence"
                    return []
                elif isinstance(regex_list[i-1], list):
                    if print_warnings:
                        print "Two {} expressions in a row is not supported"
                    return []
                elif regex_list[i-1][0] == '[':
                    syms = regex_list[i-1].strip('[]')
                    regex_list[i-1] = ''                
                else:
                    syms = regex_list[i-1][-1]
                    regex_list[i-1] = regex_list[i-1][:-1]
                if ',' not in ex:
                    new_expression = [int(ex.strip('{}')), int(ex.strip('{}')), syms]
                    max_num_seqs *= len(syms)**new_expression[0]
                else:
                    try:
                        new_expression = [int(ex.strip('{}').split(',')[0]), int(ex.strip('{}').split(',')[1]), syms]
                    except ValueError: #No max limit --- use default
                        new_expression = [int(ex.strip('{}').split(',')[0]), default_max_reps, syms]
                    if new_expression[0] > new_expression[1]:
                        if print_warnings:
                            print 'Check regex syntax --- should be {min,max}'
                        return []
                    max_num_seqs *= sum([len(syms)**n for n in range(new_expression[0], new_expression[1]+1)])/len(syms)
                #print new_expression
                regex_list[i] = new_expression
                
        if max_num_seqs > 10000 and raise_overload_warning:
            if print_warnings:
                answer = raw_input('Warning large number of sequences (estimated ' + str(max_num_seqs) + ' seqs) match the regular expression. Possible memory and time issues. Continue? (y/n)')
                if not answer == 'y':
                    print 'Canceling...'
                    return []
            else:
                return []
        #print regex_list
        CDR3_seqs = ['']
        for l, ex in enumerate(regex_list[::-1]):
            i = len(regex_list) - l - 1
            if isinstance(ex, list): #curly bracket case
                c_seqs = ['']
                f_seqs = []
                for j in range(ex[1] + 1):
                    if j in range(ex[0], ex[1]+1):
                        f_seqs += c_seqs
                    c_seqs = [aa + c_seq for aa in ex[2] for c_seq in c_seqs]
                CDR3_seqs = [f_seq + CDR3_seq for f_seq in f_seqs for CDR3_seq in CDR3_seqs]
            elif len(ex) == 0:
                pass
            elif ex[0] == '[': #square bracket case
                CDR3_seqs = [aa + CDR3_seq for aa in ex.strip('[]') for CDR3_seq in CDR3_seqs]
            else:
                CDR3_seqs = [ex + CDR3_seq for CDR3_seq in CDR3_seqs]
    
    
        return list(set(CDR3_seqs))
def list_seqs_from_regex ( self , regex_seq , print_warnings = True , raise_overload_warning = True ) : aa_symbols = '' . join ( self . codons_dict ) default_max_reps = 40 bracket_ex = [ x for x in re . findall ( '\[[' + aa_symbols + ']*?\]|\{\d+,{0,1}\d*\}' , regex_seq ) ] split_seq = re . split ( '\[[' + aa_symbols + ']*?\]|\{\d+,{0,1}\d*\}' , regex_seq ) for aa in '' . join ( split_seq ) : if aa not in aa_symbols : if print_warnings : print 'Unfamiliar symbol representing a codon:' + aa + ' --- check codon dictionary or the regex syntax' return [ ] regex_list = [ split_seq [ i / 2 ] if i % 2 == 0 else bracket_ex [ i / 2 ] for i in range ( len ( bracket_ex ) + len ( split_seq ) ) if not ( i % 2 == 0 and len ( split_seq [ i / 2 ] ) == 0 ) ] max_num_seqs = 1 for l , ex in enumerate ( regex_list [ : : - 1 ] ) : i = len ( regex_list ) - l - 1 if ex [ 0 ] == '[' : for aa in ex . strip ( '[]' ) : if aa not in aa_symbols : if print_warnings : print 'Unfamiliar symbol representing a codon:' + aa + ' --- check codon dictionary' return [ ] max_num_seqs *= len ( ex ) - 2 elif ex [ 0 ] == '{' : if i == 0 : if print_warnings : print "Can't have {} expression at start of sequence" return [ ] elif isinstance ( regex_list [ i - 1 ] , list ) : if print_warnings : print "Two {} expressions in a row is not supported" return [ ] elif regex_list [ i - 1 ] [ 0 ] == '[' : syms = regex_list [ i - 1 ] . strip ( '[]' ) regex_list [ i - 1 ] = '' else : syms = regex_list [ i - 1 ] [ - 1 ] regex_list [ i - 1 ] = regex_list [ i - 1 ] [ : - 1 ] if ',' not in ex : new_expression = [ int ( ex . strip ( '{}' ) ) , int ( ex . strip ( '{}' ) ) , syms ] max_num_seqs *= len ( syms ) ** new_expression [ 0 ] else : try : new_expression = [ int ( ex . strip ( '{}' ) . split ( ',' ) [ 0 ] ) , int ( ex . strip ( '{}' ) . split ( ',' ) [ 1 ] ) , syms ] except ValueError : new_expression = [ int ( ex . strip ( '{}' ) . split ( ',' ) [ 0 ] ) , default_max_reps , syms ] if new_expression [ 0 ] > new_expression [ 1 ] : if print_warnings : print 'Check regex syntax --- should be {min,max}' return [ ] max_num_seqs *= sum ( [ len ( syms ) ** n for n in range ( new_expression [ 0 ] , new_expression [ 1 ] + 1 ) ] ) / len ( syms ) regex_list [ i ] = new_expression if max_num_seqs > 10000 and raise_overload_warning : if print_warnings : answer = raw_input ( 'Warning large number of sequences (estimated ' + str ( max_num_seqs ) + ' seqs) match the regular expression. Possible memory and time issues. Continue? (y/n)' ) if not answer == 'y' : print 'Canceling...' return [ ] else : return [ ] CDR3_seqs = [ '' ] for l , ex in enumerate ( regex_list [ : : - 1 ] ) : i = len ( regex_list ) - l - 1 if isinstance ( ex , list ) : c_seqs = [ '' ] f_seqs = [ ] for j in range ( ex [ 1 ] + 1 ) : if j in range ( ex [ 0 ] , ex [ 1 ] + 1 ) : f_seqs += c_seqs c_seqs = [ aa + c_seq for aa in ex [ 2 ] for c_seq in c_seqs ] CDR3_seqs = [ f_seq + CDR3_seq for f_seq in f_seqs for CDR3_seq in CDR3_seqs ] elif len ( ex ) == 0 : pass elif ex [ 0 ] == '[' : CDR3_seqs = [ aa + CDR3_seq for aa in ex . strip ( '[]' ) for CDR3_seq in CDR3_seqs ] else : CDR3_seqs = [ ex + CDR3_seq for CDR3_seq in CDR3_seqs ] return list ( set ( CDR3_seqs ) )
List sequences that match regular expression template.
    
        This function parses a limited regular expression vocabulary, and 
        lists all the sequences consistent with the regular expression. Supported 
        regex syntax: [] and {}. Cannot have two {} in a row. Note we can't use 
        Kline star (*) as this is the symbol for a stop codon --- use {}.
    
        Parameters
        ----------
        regex_seq : str
            The regular expression string that represents the sequences to be 
            listed.
        print_warnings : bool
            Determines whether warnings are printed or not. Default ON.
        raise_overload_warning : bool
            A flag to warn of more than 10000 seqs corresponding to the regex_seq
    
        Returns
        -------
        CDR3_seqs : list
            A list of CDR3 sequences that correspond to the regex_seq
        
        Examples
        --------
        >>> generation_probability.list_seqs_from_regex('CASS[AGR]SARPEQFF')
        ['CASSGSARPEQFF', 'CASSRSARPEQFF', 'CASSASARPEQFF']
        >>> generation_probability.list_seqs_from_regex('CASSAX{0,5}SARPEQFF')
        ['CASSASARPEQFF',
         'CASSAXXXXSARPEQFF',
         'CASSAXXSARPEQFF',
         'CASSAXXXXXSARPEQFF',
         'CASSAXXXSARPEQFF',
         'CASSAXSARPEQFF']