<jupyter_start><jupyter_text>Post-processing of TRANSPIRE results***<jupyter_code>import TRANSPIRE

import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>1. Load data, define comparisons, and generate synthetic translocations<jupyter_code>f = 'mydata.csv'
df = TRANSPIRE.data.import_data.load_data(f)

comparisons = [('uninfected', 'infected')]

synthetic_translocations = TRANSPIRE.data.generate_translocations.make_translocations(df, comparisons)
mapping, mapping_r = TRANSPIRE.utils.get_mapping(df)

synthetic_translocations.head()<jupyter_output><empty_output><jupyter_text>2. Load TRANSPIRE predictions<jupyter_code>predictions = TRANSPIRE.data.import_data.load_predictions('results.csv')<jupyter_output><empty_output><jupyter_text>3. Perform GO enrichment analysis on translocating protiens **Make GOAnalyzer to perform enrichment analyses (facilitated by GOATOOLS)**<jupyter_code># define a background set of genes to assess enrichment against
background = df.index.get_level_values('accession').unique()
GO_analyzer = TRANSPIRE.go_enrichment.GOAnalyzer(background, species = '9606', alpha = 0.05, method = 'fdr_bh')<jupyter_output>EXISTS: go-basic.obo
  EXISTS: gene2go
go-basic.obo: fmt(1.2) rel(2020-05-02) 47,240 GO Terms
HMS:0:00:03.614330 336,356 annotations, 20,586 genes, 18,410 GOs, 1 taxids READ: gene2go 

Load BP Gene Ontology Analysis ...
fisher module not installed.  Falling back on scipy.stats.fisher_exact
 95%  3,051 of  3,196 population items found in association

Load CC Gene Ontology Analysis ...
fisher module not installed.  Falling back on scipy.stats.fisher_exact
 98%  3,147 of  3,196 population items found in association

Load MF Gene Ontology Analysis ...
fisher module not installed.  Falling back on scipy.stats.fisher_exact
 95%  3,032 of  3,196 population items found in association<jupyter_text>**Perform GO enrichment analysis for translocating proteins**<jupyter_code>translocation_accs = predictions.index.get_level_values('accession_A')[predictions['passes cutoff?']].unique()
translocation_enrichment = GO_analyzer.get_enrichment(translocation_accs)
translocation_enrichment<jupyter_output><empty_output><jupyter_text>**Plot the enriched terms and their values**<jupyter_code>import matplotlib.pyplot as plt

TRANSPIRE.visualization.result_visualization.plot_GO_enrichment_results(translocation_enrichment, orient='horizontal')
plt.show()<jupyter_output><empty_output><jupyter_text>4. Determine co-translocating proteins**Compute Mahalanobis distances between all proteins**<jupyter_code>possible_translocation_profiles =TRANSPIRE.data.generate_translocations.make_translocations(df, comparisons, synthetic=False).reset_index().set_index(predictions.index.names)

# get rid of index levels that aren't informative
possible_translocation_profiles = possible_translocation_profiles.reset_index(['localization_A', 'localization_B', 'accession_B', 'gene name_B'], drop=True)

possible_translocation_profiles.index.names = ['condition_A', 'condition_B', 'accession', 'gene name']
dists = possible_translocation_profiles.groupby(['condition_A', 'condition_B']).apply(lambda x: TRANSPIRE.cotranslocation.compute_distance(x.loc[x.name, :]))
dists = dists.reset_index().melt(dists.index.names).dropna().set_index(['condition_A', 'condition_B', 'accession_A', 'accession_B', 'gene name_A', 'gene name_B']).squeeze()
dists.sort_values()<jupyter_output><empty_output><jupyter_text>**Load CORUM complexes**<jupyter_code>corum, prot_to_complex, complex_to_prot = TRANSPIRE.data.import_data.load_CORUM()<jupyter_output><empty_output><jupyter_text>**Extract true postive (members of CORUM complexes) and true negative (marker proteins of distinct subcellular organelles) populations from the data**<jupyter_code>tn = TRANSPIRE.cotranslocation.extract_true_neg(dists, df)
tp = TRANSPIRE.cotranslocation.extract_true_pos(dists, complex_to_prot, prot_to_complex)<jupyter_output><empty_output><jupyter_text>**Compute a false-positive rate based on true negative and true positive populations**<jupyter_code>fpr = TRANSPIRE.cotranslocation.compute_fpr(tp, tn)<jupyter_output><empty_output><jupyter_text>**Determine a distance cutoff based on your desired fpr**<jupyter_code>fpr_cutoff = 0.05 # 5% FPR cutoff
dist_cutoff = fpr.groupby(['condition_A', 'condition_B']).apply(lambda x: x[x<=fpr_cutoff].idxmax()[0])<jupyter_output><empty_output><jupyter_text>**Identify cotranslocation based on distance cutoff**<jupyter_code>transloc_accs = predictions.index.get_level_values('accession_A')[predictions['passes cutoff?']].unique()
transloc_dists = dists[dists.index.get_level_values('accession_A').isin(transloc_accs)&dists.index.get_level_values('accession_B').isin(transloc_accs)]

cotransloc_dists = transloc_dists[transloc_dists <= dist_cutoff.loc[list(zip(transloc_dists.index.get_level_values('condition_A'), transloc_dists.index.get_level_values('condition_B')))].values]
cotransloc_accs = np.unique(cotransloc_dists.index.get_level_values('accession_A').values.tolist()+cotransloc_dists.index.get_level_values('accession_B').values.tolist())<jupyter_output><empty_output><jupyter_text>**Query STRINGdb for known interactions**<jupyter_code>gsi = TRANSPIRE.cotranslocation.GetSTRINGInteractions()
known_interactions = gsi.query(cotransloc_accs, '9606')
known_interactions.head()<jupyter_output><empty_output><jupyter_text>**Combine known interaction information with cotranslocation information**<jupyter_code>isknown = pd.Series(cotransloc_dists.reset_index([n for n in cotransloc_dists.index.names if not 'accession' in n]).index.isin(known_interactions.index), index = cotransloc_dists.index)

pd.concat([cotransloc_dists, isknown], axis=1, keys = ['Mahalanobis distance', 'known interaction?'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11251296232284826, 'fraction_numerical': 0.01659177324576564, 'mean_word_length': 9.677121771217712, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '314292', 'license': 'MIT', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1923, 'n_words': 340, 'path': 'docs/notebooks/post-processing (GO analysis, co-translocation analysis, etc.).ipynb', 'repo_name': 'mak4515/TRANSPIRE', 'size': '5786'}