Last active
June 3, 2020 14:21
-
-
Save jodyphelan/d268d0fd86551003492b81cec3ad20d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_barcode_performance(csv_file, tree_file, lineage): | |
meta = [] | |
for row in csv.DictReader(open(args.csv)): | |
meta.append(row) | |
try: | |
t = ete3.Tree(args.tree,format=1) | |
except: | |
t = ete3.Tree(args.tree) | |
nodes_performance = [] | |
for node in tqdm(t.traverse()): | |
if node.is_leaf(): | |
continue | |
leaves = set(node.get_leaf_names()) | |
training_tp = len([s for s in meta if s["type"]=="training" and s["id"] in leaves and s["lineage"]==lineage]) | |
if training_tp==0: continue | |
training_fn = len([s for s in meta if s["type"]=="training" and s["id"] in leaves and s["lineage"]!=lineage]) | |
training_fp = len([s for s in meta if s["type"]=="training" and s["id"] not in leaves and s["lineage"]==lineage]) | |
training_tn = len([s for s in meta if s["type"]=="training" and s["id"] not in leaves and s["lineage"]!=lineage]) | |
training_sensitivity = training_tp/(training_tp+training_fn) | |
training_specificity = training_tn/(training_tn+training_fp) | |
if training_sensitivity>=0.95 and training_specificity>=0.95: | |
test_tp = len([s for s in meta if s["type"]=="test" and s["id"] in leaves and s["lineage"]==lineage]) | |
test_fn = len([s for s in meta if s["type"]=="test" and s["id"] in leaves and s["lineage"]!=lineage]) | |
test_fp = len([s for s in meta if s["type"]=="test" and s["id"] not in leaves and s["lineage"]==lineage]) | |
test_tn = len([s for s in meta if s["type"]=="test" and s["id"] not in leaves and s["lineage"]!=lineage]) | |
test_sensitivity = test_tp/(test_tp+test_fn) | |
test_specificity = test_tn/(test_tn+test_fp) | |
nodes_performance.append((node.name,training_sensitivity,training_specificity, test_sensitivity,test_specificity)) | |
return tuple(sorted(nodes_performance, key=lambda x: (x[1],x[2]))[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi :) Maybe something interesting for you https://codenpaste.com
would love to chat with you : drop me a line if you have time (using the link in the website for security reason). Thanks