-
Notifications
You must be signed in to change notification settings - Fork 1
/
OONMFmetadata.py
205 lines (159 loc) · 7.43 KB
/
OONMFmetadata.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
ClusterMode = True
from datetime import date
import numpy as np
today = str(date.today())
if (ClusterMode):
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from scipy.stats import mannwhitneyu
import pandas as pd
'''
functions:
make_significance_plot - make the signature system-wide association plots that we use to show statistical associations between components and metadata categories.
make_significance_plot_homogeneity - make a version of these metadata associations, but test for homogeneity among NMF components
get_rosetta - this is an important one. It figures out how to reorder the names of samples from metadata so that it matches up to the order of sample names from the masterlist matrix. Uses regular expressions.
'''
def make_significance_plot(X, Basis, Category_str, my_rosetta, thecmap='viridis', NMFCOMPS=16, save=True, filename_addon='', PCAmode=False, write_mode=False):
CategoryType = X[Category_str].value_counts().keys()[0:20]
CategoryCats = X[Category_str].values[my_rosetta]
list_of_sig = []
for i, cat in enumerate(CategoryType[0:20]):
growthlist = [cat]
print('*****************')
print('Category ',i,cat)
CatCut = (CategoryCats==cat)
growthlist+=[len(CatCut[CatCut])]
for i in range(NMFCOMPS):
car = mannwhitneyu(Basis[:,i][CatCut], Basis[:,i][~CatCut],alternative='greater')
#print(car)
Ncats = min(20, len(CategoryType[0:20]))
adjustedp = (car[1]+1e-30)*NMFCOMPS*Ncats
growthlist += [-1*np.log10(adjustedp)]
list_of_sig.append(growthlist)
colnames = [Category_str.replace(" ", ""), 'Count'] + ['Comp'+str(i+1) for i in range(NMFCOMPS)]
CategoryChart = pd.DataFrame(list_of_sig, columns=colnames)
if write_mode:
CategoryChart.to_csv(filename_addon + Category_str+'MWmatrix.csv', sep='\t')
CategoryChartMatrix = CategoryChart.values[:,2:].astype(float)
plt.clf()
myfs = 45
plt.figure(figsize=(35,len(CategoryType[0:20])*2))
plt.imshow(CategoryChartMatrix, cmap=thecmap, vmin=-3, vmax=30)
if (PCAmode):
plt.xlabel('Principal Component',fontsize=myfs)
else:
plt.xlabel('NMF component',fontsize=myfs)
plt.ylabel(Category_str,fontsize=myfs)
plt.yticks(np.arange(Ncats), CategoryType[0:20], rotation='horizontal',fontsize=myfs)
plt.xticks(np.arange(NMFCOMPS), (np.arange(NMFCOMPS)+1).astype(str), rotation='vertical',fontsize=myfs)
cbar = plt.colorbar(fraction=0.046, pad=0.04)
cbar.set_label(r'- $\log_{10} (p*$'+str(NMFCOMPS)+r'$*$'+str(Ncats)+r'$)$',fontsize=myfs)
cbar_ax = cbar.ax
cbar_ax.tick_params(labelsize=myfs)
for i in cbar_ax.get_yticklabels():
i.set_fontsize(myfs)
if(save):
plt.savefig(filename_addon + Category_str+'MWplot.pdf', bbox_inches='tight')
else:
plt.show()
return (CategoryChartMatrix, CategoryType[0:20])
def make_significance_plot_WSO(X, Basis, Category_str, my_rosetta, thecmap='binary', NMFCOMPS=16, save=True, filename_addon='', PCAmode=False, write_mode=False):
CategoryType = X[Category_str].value_counts().keys()[0:20]
CategoryCats = X[Category_str].values[my_rosetta]
CategoryType = np.sort(CategoryType)
list_of_sig = []
for i, cat in enumerate(CategoryType[0:20]):
growthlist = [cat]
print('*****************')
print('Category ',i,cat)
CatCut = (CategoryCats==cat)
growthlist+=[len(CatCut[CatCut])]
for i in range(NMFCOMPS):
car = mannwhitneyu(Basis[:,i][CatCut], Basis[:,i][~CatCut],alternative='greater')
#print(car)
Ncats = min(20, len(CategoryType[0:20]))
adjustedp = (car[1]+1e-30)*NMFCOMPS*Ncats
growthlist += [-1*np.log10(adjustedp)]
list_of_sig.append(growthlist)
colnames = [Category_str.replace(" ", ""), 'Count'] + ['Comp'+str(i+1) for i in range(NMFCOMPS)]
CategoryChart = pd.DataFrame(list_of_sig, columns=colnames)
if write_mode:
CategoryChart.to_csv(filename_addon + Category_str+'MWmatrix.csv', sep='\t')
CategoryChartMatrix = CategoryChart.values[:,2:].astype(float)
plt.clf()
myfs = 45
plt.figure(figsize=(35,len(CategoryType[0:20])*2))
plt.imshow(CategoryChartMatrix, cmap=thecmap, vmin=-3, vmax=30)
if (PCAmode):
plt.xlabel('Principal Component',fontsize=myfs)
else:
plt.xlabel('NMF component',fontsize=myfs)
plt.ylabel(Category_str,fontsize=myfs)
plt.yticks(np.arange(Ncats), CategoryType[0:20], rotation='horizontal',fontsize=myfs)
plt.xticks(np.arange(NMFCOMPS), (np.arange(NMFCOMPS)+1).astype(str), rotation='vertical',fontsize=myfs)
cbar = plt.colorbar(fraction=0.046, pad=0.04)
cbar.set_label(r'- $\log_{10} (p*$'+str(NMFCOMPS)+r'$*$'+str(Ncats)+r'$)$',fontsize=myfs)
cbar_ax = cbar.ax
cbar_ax.tick_params(labelsize=myfs)
for i in cbar_ax.get_yticklabels():
i.set_fontsize(myfs)
if(save):
plt.savefig(filename_addon + Category_str+'MWplot.pdf', bbox_inches='tight')
else:
plt.show()
return (CategoryChartMatrix, CategoryType[0:20])
def make_significance_plot_homogeneity(X, homogeneity, Category_str, my_rosetta, thecmap='viridis', NMFCOMPS=16, save=True,filename_addon=''):
CategoryType = X[Category_str].value_counts().keys()[0:20]
CategoryCats = X[Category_str].values[my_rosetta]
list_of_sig = []
for i, cat in enumerate(CategoryType[0:20]):
growthlist = [cat]
print('*****************')
print('Category ',i,cat)
CatCut = (CategoryCats==cat)
growthlist+=[len(CatCut[CatCut])]
majorcount = 0
semimajorcount = 0
bigcount=0
minorcount = 0
noncount = 0
car = mannwhitneyu(homogeneity[CatCut], homogeneity[~CatCut],alternative='greater')
print ('mean homogeneity of ',cat,np.mean(homogeneity[CatCut]))
print ('mean homogeneity of anti-',cat,np.mean(homogeneity[~CatCut]))
#print(car)
Ncats = min(20, len(CategoryType[0:20]))
adjustedp = (car[1]+1e-30)*1*Ncats
growthlist += [-1*np.log10(adjustedp)]
list_of_sig.append(growthlist)
colnames = [Category_str.replace(" ", ""), 'Count'] +['A']
CategoryChart = pd.DataFrame(list_of_sig, columns=colnames)
CategoryChartMatrix = CategoryChart.values[:,2:].astype(float)
plt.clf()
plt.figure(figsize=(len(CategoryType[0:20])*2,6))
plt.imshow(CategoryChartMatrix.T, cmap=thecmap, vmin=-3, vmax=15)
plt.xlabel(Category_str,fontsize=25)
plt.xticks(np.arange(Ncats), CategoryType[0:20], rotation='vertical',fontsize=25)
cbar = plt.colorbar(fraction=0.046, pad=0.04, ticklabel_size=24)
cbar.set_label(r'- $\log_{10} (p*$'+str(Ncats)+r'$)$',fontsize=25)
cbar_ax = cbar.ax
cbar_ax.tick_params(labelsize=35)
for i in cbar_ax.get_yticklabels():
i.set_fontsize(35)
if(save):
plt.savefig(filename_addon + Category_str+'MWhom_plot.pdf')
else:
plt.show()
return (CategoryChartMatrix, CategoryType[0:20])
def get_rosetta(MetaDataMat, names):
import re
DSnos = []
DSnos_naked = []
for name in names:
p = re.search('(.)(DS\w+)', name)
DSnos.append(p.group(2))
DSnos_naked.append(p.group(2)[2:])
rosetta = []
for i in DSnos:
rosetta.append(np.argwhere(i == MetaDataMat['DS_plus'].values)[0][0])
return rosetta