Jump to content

Optimização de uma função: dicionários e módulo optparser


JoaoRodrigues
 Share

Recommended Posts

Recebo do utlizador ( opcionalmente ) alguns parâmetros. Quero que esses parâmetros sejam incluídos num dicionário e que caso falte algum este seja automaticamente determinado. A função abaixo faz o que quero, mas haverá maneira de optimizar isto?

    def adapt_BLAST_parameters(self, opts, sequence):
    
        sequenceLength = len(sequence)
        
        BLAST_parameters = {}
        
        # Matrix Choice. As per: http://www.ncbi.nlm.nih.gov/blast/html/sub_matrix.html
        if opts.b_matrix:
            BLAST_parameters['matrix'] = opts.b_matrix
        elif sequenceLength <35:
            BLAST_parameters['matrix'] = 'PAM30'
        elif sequenceLength <50:
            BLAST_parameters['matrix'] = 'PAM70'
        elif sequenceLength <85:
            BLAST_parameters['matrix'] = 'BLOSUM80'
        else:
            BLAST_parameters['matrix'] = 'BLOSUM62'

        if sequenceLength < 30: # Short Sequence Parameters (as the BLAST server)
            if opts.b_word_size: BLAST_parameters['word_size'] = opts.b_word_size
            else: BLAST_parameters['word_size'] = "2"
            if opts.b_gap_cost: BLAST_parameters['gap_cost'] = opts.b_gap_cost
            else: BLAST_parameters['gap_cost'] = '9 1'
            if opts.b_cb_stats: BLAST_parameters['cb_stats'] = opts.b_cb_stats
            else: BLAST_parameters['cb_stats'] = False
            if opts.b_e_threshold: BLAST_parameters['e_threshold'] = opts.b_e_threshold
            else: BLAST_parameters['e_threshold'] = 200000
        else:
            if opts.b_word_size: BLAST_parameters['word_size'] = opts.b_word_size
            else: BLAST_parameters['word_size'] = "3" 
            if opts.b_gap_cost: BLAST_parameters['gap_cost'] = opts.b_gap_cost
            else: BLAST_parameters['gap_cost'] = '10 1'
            if opts.b_cb_stats: BLAST_parameters['cb_stats'] = opts.b_cb_stats
            else: BLAST_parameters['cb_stats'] = True
            if opts.b_e_threshold: BLAST_parameters['e_threshold'] = opts.b_e_threshold
            else: BLAST_parameters['e_threshold'] = 10
        
        return BLAST_parameters

O argumento opts é o resultado do módulo optparser. Informação:

print opts
{'b_e_threshold': None, 'b_gap_cost': None, 'b_cb_stats': None, 'b_word_size': '7', 'b_matrix': 'BLOSUM65', 'b_results': '5'}
print type(opts)
<type 'instance'>
print dir(opts)
['__cmp__', '__doc__', '__init__', '__module__', '__repr__', '__str__', '_update', '_update_careful', '_update_loose', 'b_cb_stats', 'b_e_threshold', 'b_gap_cost', 'b_matrix', 'b_results', 'b_word_size', 'ensure_value', 'read_file', 'read_module']

Alguma maneira de optimizar isto? É que vejo muitos ifs/elses por ali...

Link to comment
Share on other sites

E que tal assim? (Não foi testado)

    def adapt_BLAST_parameters(self, opts, sequence):

        _matrix = [(0, 35): 'PAM30', (35, 50): 'PAM70', (50, 85): 'BLOSUM80', (85, 2**64): 'BLOSUM62']
        _params = {'word_size': ("2", "3"), 'gap_cost': ('9 1', '10 1'), 'cb_stats': (False, True), 'e_threshold': (200000, 10)}
    
        sequenceLength = len(sequence)

        BLAST_parameters = {}

        # Matrix Choice. As per: http://www.ncbi.nlm.nih.gov/blast/html/sub_matrix.html
        if opts.b_matrix:
            BLAST_parameters['matrix'] = opts.b_matrix
        else:
            index = filter( lambda rng: rng[0] <= sequenceLength and sequenceLength < rng[1], _matrix.keys() )[0]
            BLAST_parameters['matrix'] = _matrix[index]
        
        
        for param in _params.keys():
            if eval("opts.b_" + param):
                BLAST_parameters[param] = eval("opts.b_" + param)
            else:
                if sequenceLength < 30:
                    BLAST_parameters[param] = _params[param][0]
                else:
                    BLAST_parameters[param] = _params[param][1]

        return BLAST_parameters
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.