def calc_thresholds(rbh, file_name, thresholds = [False, False, False, False], stdevs = <NUM_LIT:2>):
calc_threshold = thresholds[-<NUM_LIT:1>]<EOL>norm_threshold = {}<EOL>for pair in itertools.permutations([i for i in rbh], <NUM_LIT:2>):<EOL><INDENT>if pair[<NUM_LIT:0>] not in norm_threshold:<EOL><INDENT>norm_threshold[pair[<NUM_LIT:0>]] = {}<EOL><DEDENT>norm_threshold[pair[<NUM_LIT:0>]][pair[<NUM_LIT:1>]] = {}<EOL><DEDENT>out = open(file_name, '<STR_LIT:w>')<EOL>print('<STR_LIT>', file=out)<EOL>comparisons = []<EOL>for genome in rbh:<EOL><INDENT>for compare in rbh[genome]:<EOL><INDENT>pair = '<STR_LIT>'.join(sorted([genome, compare]))<EOL>if pair in comparisons:<EOL><INDENT>continue<EOL><DEDENT>comparisons.append(pair)<EOL>scores = {'<STR_LIT>': [], '<STR_LIT>': [], '<STR_LIT>': [], '<STR_LIT>': [], '<STR_LIT>': []}<EOL>print('<STR_LIT>' % (genome, compare), file=out)<EOL>for id in rbh[genome][compare]:<EOL><INDENT>pident, length_fraction, e, bit, norm_bit = rbh[genome][compare][id][<NUM_LIT:3>:]<EOL>scores['<STR_LIT>'].append(pident)<EOL>scores['<STR_LIT>'].append(length_fraction)<EOL>scores['<STR_LIT>'].append(e)<EOL>scores['<STR_LIT>'].append(bit)<EOL>scores['<STR_LIT>'].append(norm_bit)<EOL><DEDENT>if calc_threshold is True:<EOL><INDENT>norms = scores['<STR_LIT>']<EOL>average = numpy.average(norms) <EOL>std = numpy.std(norms)<EOL>normal_thresh = average - (std * stdevs)<EOL>print('<STR_LIT>' % average, file=out)<EOL>print('<STR_LIT>' % std, file=out)<EOL>print('<STR_LIT>' % (normal_thresh), file=out)<EOL>norm_threshold[genome][compare], norm_threshold[compare][genome] = normal_thresh, normal_thresh<EOL><DEDENT>for score in scores:<EOL><INDENT>print('<STR_LIT>' % (score), file=out)<EOL>if len(scores[score]) > <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>' % numpy.average(scores[score]), file=out)<EOL><INDENT>hist = histogram(scores[score], [])<EOL>for line in hist:<EOL><INDENT>print >> out, line<EOL><DEDENT><DEDENT><DEDENT>print('<STR_LIT>', file=out)<EOL><DEDENT><DEDENT><DEDENT>out.close()<EOL>if calc_threshold is True:<EOL><INDENT>return thresholds[<NUM_LIT:0>:-<NUM_LIT:1>] + [norm_threshold]<EOL><DEDENT>else:<EOL><INDENT>return thresholds<EOL><DEDENT>
if thresholds are not specififed, calculate based on the distribution of normalized bit scores