def test_perfect_pcr(): db = PCRDatabase() logic = PCRLogic(db) primer1 = Aliquot(PRIMER, '1', 'Biowares') taq1 = Aliquot(TAQ, '1', 'Biowares') dntp1 = Aliquot(DNTP, '1', 'Biowares') buffer1 = Aliquot(BUFFER, '1', 'Biowares') pcr = PCR(True, False, [primer1, taq1, dntp1, buffer1]) culprits = logic.make_deductions(pcr) if (len(culprits) == 0): print 'Perfect PCR test successful!' else: print 'Perfect PCR test FAILED!'
def test_no_deduction_possible(): db = PCRDatabase() primer1 = Aliquot(PRIMER, '1', 'Biowares') taq1 = Aliquot(TAQ, '1', 'Biowares') dntp1 = Aliquot(DNTP, '1', 'Biowares') buffer1 = Aliquot(BUFFER, '1', 'Biowares') pcr = PCR(True, False, [primer1, taq1, dntp1, buffer1]) db.add_pcr(pcr) logic = PCRLogic(db) pcr2 = PCR(False, False, [primer1, taq1, dntp1, buffer1]) try: logic.make_deductions(pcr2) print 'No deduction possible test FAILED!' except: print 'No deduction possible test successful!'
def test_basic_contamination(): db = PCRDatabase() primer1 = Aliquot(PRIMER, '1', 'Biowares') taq1 = Aliquot(TAQ, '1', 'Biowares') dntp1 = Aliquot(DNTP, '1', 'Biowares') buffer1 = Aliquot(BUFFER, '1', 'Biowares') pcr = PCR(True, False, [primer1, taq1, dntp1, buffer1]) db.add_pcr(pcr) logic = PCRLogic(db) buffer2 = Aliquot(BUFFER, '2', 'Biowares') taq2 = Aliquot(TAQ, '2', 'Biowares') pcr2 = PCR(True, True, [primer1, taq2, dntp1, buffer2]) possible_culprits = logic.make_deductions(pcr2) if len(possible_culprits) == 2 and possible_culprits[ 0].id == '2' and possible_culprits[1].id == '2': print 'Basic contamination test successful!' else: print 'Basic contamination test FAILED!'
def main(): db = PCRDatabase() logic = PCRLogic(db) while True: command = get_user_command() if COMMANDS[command] == 'Quit': break db, logic = process_command(command, db, logic) print '\n'
def read_db_command(db, logic): try: filename = raw_input('Enter filename: ') db = PCRDatabase(filename) logic = PCRLogic(db) return db, logic except: print 'Could not open that file.' return db, logic
def simulate_pcrs_with_probabilities(): num_experiments = 100 probabilities = [0.01 * i for i in range(1, 10)] num_pcrs = 5 y = [] naive_y = [] for probability in probabilities: state_probabilities = { 'contaminated': probability, 'defective': probability, 'good': 1 - probability } errors = [] naive_errors = [] for i in range(num_experiments): db = PCRDatabase() logic = PCRLogic(db) curr_aliquots = [ Aliquot(reagent, 1, 'Biowares', choose(state_probabilities)) for reagent in REAGENT_MAP ] for j in range(num_pcrs): false_neg = any( map(lambda aliquot: aliquot.state == 'defective', curr_aliquots)) false_pos = any( map(lambda aliquot: aliquot.state == 'contaminated', curr_aliquots)) and not false_neg if false_neg: pcr = PCR(False, False, curr_aliquots) elif false_pos: pcr = PCR(True, True, curr_aliquots) else: pcr = PCR(True, False, curr_aliquots) db.add_pcr(pcr) # Randomly choose new aliquot for i in range(len(curr_aliquots)): old_aliquot = curr_aliquots[i] new_aliquot = Aliquot(old_aliquot.reagent, old_aliquot.id + 1, old_aliquot.manufacturer, choose(state_probabilities)) aliquot_probabilities = { old_aliquot: 0.8, new_aliquot: 0.2 } curr_aliquots[i] = choose(aliquot_probabilities) # Compute the defective and contaminated aliquot probabilities deductions = logic.make_probabilistic_deductions() all_aliquots = db.get_all_aliquots() errors.append(rmse(all_aliquots, deductions)) # Compute naive approach errors naive_deductions = dict(deductions) for error in naive_deductions: prob_list = naive_deductions[error] for i in range(len(prob_list)): prob_list[i] = (prob_list[i][0], 1.0) naive_errors.append(rmse(all_aliquots, naive_deductions)) print[(pcr.pos_control_result, pcr.negative_control_result) for pcr in db.pcrs] avg_error = sum(errors) / len(errors) avg_naive_error = sum(naive_errors) / len(naive_errors) y.append(avg_error) naive_y.append(avg_naive_error) print y print naive_y pyplot.plot(probabilities, y, label='Bayesian inference') pyplot.plot(probabilities, naive_y, label='Naive approach') pyplot.legend(loc=2) pyplot.xlabel('Probability of reagent being defective or contaminated') pyplot.ylabel('RMSE') pyplot.title( 'RMSE as a function of reagent defective/contaminated probability') pyplot.show()
def simulate_pcrs_with_heatmap(): num_pcrs = 10 state_probabilities = { 'contaminated': 0.04, 'defective': 0.04, 'good': 0.92 } db = PCRDatabase() logic = PCRLogic(db) curr_aliquots = [ Aliquot(reagent, 1, 'Biowares', choose(state_probabilities)) for reagent in REAGENT_MAP ] for j in range(num_pcrs): false_neg = any( map(lambda aliquot: aliquot.state == 'defective', curr_aliquots)) false_pos = any( map(lambda aliquot: aliquot.state == 'contaminated', curr_aliquots)) and not false_neg if false_neg: pcr = PCR(False, False, curr_aliquots) elif false_pos: pcr = PCR(True, True, curr_aliquots) else: pcr = PCR(True, False, curr_aliquots) db.add_pcr(pcr) # Randomly choose new aliquot for i in range(len(curr_aliquots)): old_aliquot = curr_aliquots[i] new_aliquot = Aliquot(old_aliquot.reagent, old_aliquot.id + 1, old_aliquot.manufacturer, choose(state_probabilities)) aliquot_probabilities = {old_aliquot: 0.8, new_aliquot: 0.2} curr_aliquots[i] = choose(aliquot_probabilities) # Compute the defective and contaminated aliquot probabilities deductions = logic.make_probabilistic_deductions() all_aliquots = db.get_all_aliquots() print 'defective', dict(deductions['defective']) print 'contaminated', dict(deductions['contaminated']) # Create heatmap actual_reagent_contaminations = np.array( [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)]) actual_reagent_defective = np.array( [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)]) predicted_reagent_contaminations = np.array( [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)]) predicted_reagent_defective = np.array( [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)]) for i in range(len(db.pcrs)): pcr = db.pcrs[i] for j in range(len(pcr.aliquots)): aliquot = pcr.aliquots[j] actual_reagent_contaminations[i][j] = float( aliquot.state == 'contaminated') actual_reagent_defective[i][j] = float( aliquot.state == 'defective') predicted_reagent_defective[i][j] = dict( deductions['defective']).get(aliquot, 0.0) predicted_reagent_contaminations[i][j] = dict( deductions['contaminated']).get(aliquot, 0.0) print[(pcr.pos_control_result, pcr.negative_control_result) for pcr in db.pcrs] # Plot heatmap for actual contaminations row_labels = [REAGENT_MAP[aliquot.reagent] for aliquot in curr_aliquots] column_labels = [i + 1 for i in range(num_pcrs)] plot_heatmap(actual_reagent_contaminations, row_labels, column_labels, matplotlib.cm.Reds, 'Actual contaminated reagents') plot_heatmap(predicted_reagent_contaminations, row_labels, column_labels, matplotlib.cm.Reds, 'Bayesian probabilities for contaminated reagents') plot_heatmap(actual_reagent_defective, row_labels, column_labels, matplotlib.cm.Blues, 'Actual defective reagents') plot_heatmap(predicted_reagent_defective, row_labels, column_labels, matplotlib.cm.Blues, 'Bayesian probabilities for defective reagents') pyplot.show()