def main(go_file, hp_file, terms_file, preds_file, gene): go = Ontology(go_file, with_rels=True) print('GO loaded') hp = Ontology(hp_file, with_rels=True) print('HP loaded') terms_df = pd.read_pickle(terms_file) global terms terms = terms_df['terms'].values.flatten() labels = terms_df['labels'].values.flatten() print('Phenotypes', len(terms)) global term_set term_set = set(terms) terms_dict = {v: i for i, v in enumerate(terms)} df = pd.read_pickle(preds_file) row = df.loc[df['genes'] == gene] with open(f'data/{gene}.deepgo_annotations.txt', 'w') as f: dg = [x.split('|') for x in row['deepgo_annotations'].values[0]] dg = sorted(dg, key=lambda x: float(x[1]), reverse=True) for go_id, score in dg: name = go.get_term(go_id)['name'] f.write(f'{go_id}\t{name}\t{score}\n') with open(f'data/{gene}.go_annotations.txt', 'w') as f: dg = [x for x in row['go_annotations'].values[0]] for go_id in dg: name = go.get_term(go_id)['name'] f.write(f'{go_id}\t{name}\n') with open(f'data/{gene}.deeppheno_annotations.txt', 'w') as f: dp = [(terms[i], score) for i, score in enumerate(row['preds'].values[0])] dp = sorted(dp, key=lambda x: x[1], reverse=True) for hp_id, score in dp: name = hp.get_term(hp_id)['name'] f.write(f'{hp_id}\t{name}\t{score}\n') if score < 0.01: break
def main(in_file, out_file, go_file, model_file, terms_file, annotations_file, chunk_size, diamond_file, threshold, batch_size, alpha): # Load GO and read list of all terms go = Ontology(go_file, with_rels=True) terms_df = pd.read_pickle(terms_file) terms = terms_df['terms'].values.flatten() # Read known experimental annotations annotations = {} df = pd.read_pickle(annotations_file) for row in df.itertuples(): annotations[row.proteins] = set(row.prop_annotations) go.calculate_ic(annotations.values()) diamond_preds = {} mapping = {} with gzip.open(diamond_file, 'rt') as f: for line in f: it = line.strip().split() if it[0] not in mapping: mapping[it[0]] = {} mapping[it[0]][it[1]] = float(it[2]) for prot_id, sim_prots in mapping.items(): annots = {} allgos = set() total_score = 0.0 for p_id, score in sim_prots.items(): allgos |= annotations[p_id] total_score += score allgos = list(sorted(allgos)) sim = np.zeros(len(allgos), dtype=np.float32) for j, go_id in enumerate(allgos): s = 0.0 for p_id, score in sim_prots.items(): if go_id in annotations[p_id]: s += score sim[j] = s / total_score for go_id, score in zip(allgos, sim): annots[go_id] = score diamond_preds[prot_id] = annots # Load CNN model model = load_model(model_file) # Alphas for the latest model alphas = {NAMESPACES['mf']: 0.55, NAMESPACES['bp']: 0.59, NAMESPACES['cc']: 0.46} # Alphas for the cafa2 model # alphas = {NAMESPACES['mf']: 0.63, NAMESPACES['bp']: 0.68, NAMESPACES['cc']: 0.48} start_time = time.time() total_seq = 0 w = gzip.open(out_file, 'wt') for prot_ids, sequences in read_fasta(in_file, chunk_size): total_seq += len(prot_ids) deep_preds = {} ids, data = get_data(sequences) preds = model.predict(data, batch_size=batch_size) assert preds.shape[1] == len(terms) for i, j in enumerate(ids): prot_id = prot_ids[j] if prot_id not in deep_preds: deep_preds[prot_id] = {} for l in range(len(terms)): if preds[i, l] >= 0.01: # Filter out very low scores if terms[l] not in deep_preds[prot_id]: deep_preds[prot_id][terms[l]] = preds[i, l] else: deep_preds[prot_id][terms[l]] = max( deep_preds[prot_id][terms[l]], preds[i, l]) # Combine diamond preds and deepgo for prot_id in prot_ids: annots = {} if prot_id in diamond_preds: for go_id, score in diamond_preds[prot_id].items(): annots[go_id] = score * alphas[go.get_namespace(go_id)] for go_id, score in deep_preds[prot_id].items(): if go_id in annots: annots[go_id] += (1 - alphas[go.get_namespace(go_id)]) * score else: annots[go_id] = (1 - alphas[go.get_namespace(go_id)]) * score # Propagate scores with ontology structure gos = list(annots.keys()) for go_id in gos: for g_id in go.get_anchestors(go_id): if g_id in annots: annots[g_id] = max(annots[g_id], annots[go_id]) else: annots[g_id] = annots[go_id] sannots = sorted(annots.items(), key=lambda x: x[1], reverse=True) for go_id, score in sannots: if score >= threshold: w.write(prot_id + '\t' + go_id + '\t' + go.get_term(go_id)['name'] + '\t%.2f' % go.get_ic(go_id) + '\t%.3f\n' % score) w.write('\n') w.close() total_time = time.time() - start_time print('Total prediction time for %d sequences is %d' % (total_seq, total_time))