-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
177 lines (137 loc) · 5.42 KB
/
render.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from jinja2 import Template
import scanpy as sc
import numpy as np
import pandas as pd
import math
import os
import sys
import json
# import subprocess
# import pandas as pd
# import sys
# import os
# import json
# import shutil
PTHRESH = 0.01
FCTHRESH = 0.01
TOP_N = 20
COLUMNS = ['phenotype', 'clone_id']
def open_file(filepath):
adata = sc.read(filepath)
adata = adata[adata.obs["clone_id"].notnull()]
sc.tl.umap(adata)
return adata
def get_data(filepath):
adata = open_file(filepath)
metadata = get_metadata(adata).to_dict(orient="records")
degs = get_degs(adata).to_dict(orient="records")
filters = get_filter(adata)
# probabilities = get_probabilities(adata).to_dict(orient="records")
return {
"metadata": [clean_record(record) for record in metadata],
"degs": [clean_record(record) for record in degs],
"filters": filters
# "probabilities": [clean_record(record) for record in probabilities]
}
def clean_record(record):
floats = [field for field in record if isinstance(record[field], float)]
for field in floats:
if np.isnan(record[field]):
record[field] = "None"
return record
def get_metadata(adata):
umap = pd.DataFrame(adata.obsm['X_umap'])
umap.columns = ['UMAP_1', 'UMAP_2']
add_columns = list(adata.uns['viz_columns'])
df = adata.obs[COLUMNS + add_columns + ['pgen']]
df = df.reset_index()
df = df.merge(umap, left_index=True, right_index=True)
df = df.rename(columns={'index': 'cell_id',
'pgen': 'log10_probability', 'phenotype': 'subtype'})
df = df.replace(to_replace="nan", value="None")
return df
def get_degs(adata):
subtypes = adata.uns['rank_genes_groups']["names"].dtype.names
genes = pd.DataFrame(adata.uns['rank_genes_groups']['names'].tolist(
), columns=adata.uns['rank_genes_groups']['names'].dtype.names)
adjpvals = pd.DataFrame(adata.uns['rank_genes_groups']['pvals_adj'].tolist(
), columns=adata.uns['rank_genes_groups']['pvals_adj'].dtype.names)
logfc = pd.DataFrame(adata.uns['rank_genes_groups']['logfoldchanges'].tolist(
), columns=adata.uns['rank_genes_groups']['logfoldchanges'].dtype.names)
data = pd.DataFrame()
for subtype in subtypes:
df = pd.DataFrame()
df['gene'] = genes[subtype]
df['adj_pval'] = adjpvals[subtype]
df['log_fc'] = logfc[subtype]
df['subtype'] = subtype
df = df[(df['adj_pval'] < PTHRESH) & (df['log_fc'] > FCTHRESH)]
df = df.reset_index(drop=True)
df = df.sort_values('log_fc', ascending=False)
df = df[:TOP_N]
data = pd.concat([data, df], ignore_index=True)
return data
def get_filter(adata):
columns = list(adata.uns['viz_columns']) + COLUMNS
records = []
for column in columns:
record = {
"name": column if column != "phenotype" else "subtype",
"values": list(adata.obs[column].unique())
}
records.append(record)
return records
def get_probabilities(adata):
df = adata.obs[["clone_id", "pgen", "subtype"]]
df = df[df['clone_id'].notnull() & df['pgen'].notnull()]
# df["log10_probability"] = [math.log10(float(prob)) for prob in df["pgen"].tolist()]
df = df.rename(columns={'pgen': 'log10_probability'})
df = df.reset_index(drop=True)
return df
# metadata = pd.read_csv(os.path.join(data_dir, "metadata.tsv"), sep="\t")
# metadata = metadata.to_dict('records')
#
# probabilities = pd.read_csv(os.path.join(data_dir, "probabilities.tsv"), sep="\t")
# probabilities = probabilities.to_dict("records")
#
# degs = pd.read_csv(os.path.join(data_dir, "degs.tsv"), sep="\t")
# degs = degs.to_dict("records")
# data = {
# "metadata": metadata,
# "probabilities": probabilities,
# "degs": degs
# }
#return df
def output_data(filepath, output):
adata = sc.read(filepath)
#get_metadata(adata).to_csv(os.path.join(output, "metadata.tsv"),
# sep="\t", index=False, na_rep='None')
#get_degs(adata).to_csv(os.path.join(output, "degs.tsv"),
# sep="\t", index=False, na_rep='None')
f = get_filter(adata)
print(f)
with open('filters.json', 'w') as file:
json.dumps(f)
#get_probabilities(adata).to_csv(os.path.join(
# output, "probabilities.tsv"), sep="\t", index=False, na_rep='None')
if __name__ == "__main__":
filename = sys.argv[1]
#output = sys.argv[2]
#output_data(filename, output)
data = get_data(filename)
data = json.dumps(data, indent=4)
app_dir = os.path.dirname(os.path.abspath(__file__))
# app_dir = os.path.abspath(os.path.join(app_dir, "../..", "build"))
index_template = os.path.join(app_dir, "build", "index.html")
template = Template(open(index_template, "r").read())
html = template.render(data=data)
output_html = os.path.join(app_dir, "build", "pictcr.html")
output = open(output_html, "w")
js_txt = open(os.path.join(app_dir, "build", "main.js"), 'r').read()
css_txt = open(os.path.join(app_dir, "build", "main.css"), 'r').read()
# html = html.replace('<script src="./main.js"></script>', f"<script>{js_txt}</script>")
# html = html.replace('<link href="./main.css" rel="stylesheet">', f"<style>{css_txt}</style>")
output.write(html)
output.close()
# datalake_build=os.path.join(sys.argv[1], "build")
# shutil.copytree(build_folder, datalake_build)