-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscanpy_test_for_gtex.py
189 lines (109 loc) · 4.18 KB
/
scanpy_test_for_gtex.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
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
"""ScanPy_Test for GTEx
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1jxoNQSHMgeo3oFFstjTJvAT6Of17uGIc
# Installs
"""
!pip install scanpy
!pip3 install leidenalg
!pip install MulticoreTSNE
"""# Imports"""
import scanpy as sc
import numpy as np
import pandas as pd
import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt
sc.settings.verbosity = 3 # verbosity: errors (0), warnings (1), info (2), hints (3)
sc.logging.print_header()
sc.settings.set_figure_params(dpi=80, facecolor='white')
data = 'https://github.com/josoga2/dataset-repos/blob/main/finOutput.txt?raw=true'
lnc = pd.read_csv(data, index_col='transcript_id')
lnc = lnc.drop('lncRNA', axis=1)
lnc.head(1)
adata = sc.AnnData(X= lnc)
adata = adata.T
adata.var_names_make_unique()
adata
"""# Preprocessing
Selection of highly variable genes per cell
"""
sc.pl.highest_expr_genes(adata, n_top=10)
sc.pp.filter_cells(adata, min_genes=500)
sc.pp.filter_genes(adata, min_cells=500)
adata.var['mt'] = adata.var_names.str.startswith('MT-') # annotate the group of mitochondrial genes as 'mt'
sc.pp.calculate_qc_metrics(adata, qc_vars=['mt'], percent_top=None, log1p=False, inplace=True)
adata
sc.pl.scatter(adata, x='total_counts', y='pct_counts_mt')
sc.pl.scatter(adata, x='total_counts', y='n_genes_by_counts')
#Removing unwanted data of interest (mitochondiral genes and bad cells)
#adata = adata[adata.obs.n_genes_by_counts < 20, :]
#adata = adata[adata.obs.pct_counts_mt < 0.01, :]
"""# Select Highly Variable Data"""
#normalize total count data btw 1E0 - 1E4
sc.pp.normalize_total(adata, target_sum=10000)
#log of counts
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata, min_mean=0.0125, max_mean=3, min_disp=0.5)
import seaborn as sns
sns.set('paper', color_codes='ENST00000577364.1')
sc.pl.highly_variable_genes(adata)
adata
adata.raw = adata
"""# Visualization & Clustering"""
sc.pp.neighbors(adata, n_neighbors=10, n_pcs=40)
sc.tl.pca(adata, svd_solver='arpack')
sc.pl.pca(adata, color=['ENST00000577364.1'])
sc.tl.umap(adata)
sc.pl.umap(adata, color=['ENST00000577364.1'], use_raw=False)
sc.tl.leiden(adata)
#sc.pl.umap(adata, color=['leiden'])
adata.write('results_file')
"""# Using Highly Variable Marker genes to profile individual cells"""
sc.tl.rank_genes_groups(adata, 'leiden', method='t-test')
sc.pl.rank_genes_groups(adata, n_genes=5, sharey=False)
adata.write('results_file')
# USING tSNE
sc.tl.tsne(adata, perplexity=30, learning_rate=1000, random_state=0)
adata.write('results_file')
adata
sc.pl.tsne(adata, color='leiden')
#I don't understand how this data was extracted
marker_genes = ['IL7R', 'CD79A', 'MS4A1', 'CD8A', 'CD8B', 'LYZ', 'CD14',
'LGALS3', 'S100A8', 'GNLY', 'NKG7', 'KLRB1']
# 'FCGR3A', 'MS4A7', 'FCER1A', 'CST3', 'PPBP']
#adata = sc.read(results_file)
pd.DataFrame(adata.uns['rank_genes_groups']['names']).head()
result = adata.uns['rank_genes_groups']
groups = result['names'].dtype.names
pd.DataFrame(
{group + '_' + key[:1]: result[key][group]
for group in groups for key in ['names', 'pvals']}).head()
sc.tl.rank_genes_groups(adata, 'leiden', groups=['0'], reference='1', method='wilcoxon')
sc.pl.rank_genes_groups(adata, groups=['0'], n_genes=10)
sc.pl.rank_genes_groups_tracksplot(adata, groupby='leiden')
new_cluster_names = [
'CD4 T', 'CD14 Monocytes',
'B', 'CD8 T',
'NK', 'FCGR3A Monocytes','UNK',
'Dendritic', 'Megakaryocytes']
adata.rename_categories('leiden', new_cluster_names)
sc.pl.dotplot(adata, marker_genes, groupby='leiden')
sc.pl.stacked_violin(adata, marker_genes, groupby='leiden', rotation=90)
adata.uns.
gtexDF = adata.transpose().to_df()
gtexDF.head()
gtexDF.to_csv('cleanData.tsv', sep='\t', index=None)
import pandas as pd
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import numpy as np
lnc.head(5)
from scipy.cluster import hierarchy
Z = hierarchy.linkage(gtexDF)
hierarchy.dendrogram(Z, leaf_rotation=90, leaf_font_size=8, labels=gtexDF.index)
import sys
print(sys.getrecursionlimit())
sys.setrecursionlimit(2681)
print(sys.getrecursionlimit())