-
Notifications
You must be signed in to change notification settings - Fork 0
/
spleenDataFormatting.R
171 lines (128 loc) · 5.96 KB
/
spleenDataFormatting.R
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
##this file formats the spleen data so it can be ingested into the format we need
library(readxl)
library(dplyr)
library(tidyr)
library(SingleCellExperiment)
library(org.Hs.eg.db)
source("synapseUtil.R")
syn<-loadSynapse()
swp.map<-as.data.frame(org.Hs.egUNIPROT)%>%
full_join(as.data.frame(org.Hs.egSYMBOL))
global.spleen<-read.table('../../Projects/HubMap/spleen/Vocanol_tip_Global.txt',sep='\t',header=T,comment.char = '')%>%
dplyr::select(Entry.Name,Gene,starts_with('sample'))%>%
#subset(Gene!='')%>%##TODO FIX THIS
tidyr::pivot_longer(cols=starts_with('sample'),names_to='Sample',values_to='LogRatio')
phospho.spleen<-readxl::read_xlsx('../../Projects/HubMap/spleen/Volcaono_tip_phospho_siteAnnotated.xlsx')
#read.table('../../Projects/HubMap/spleen/Vocanol_tip_phospho.txt',sep='\t',header=T,comment.char = '')
spleen.meta<-data.frame(Sample=c('sample.09','sample.10','sample.11','sample.12',
'sample.13','sample.14','sample.15','sample.16'),
pulpAnnotation=c('red','red','red','red','white','white','white','white'))%>%
mutate(source='Sorted')
md.file<-'../../Projects/HubMap/spleen/voxelMetadata.xlsx'
new.md.file<-'../../Projects/HubMap/spleen/2d manuscript/Voxel_metadata_histology.xlsx'
prot.file<-'../../Projects/HubMap/spleen/AfterBatchCorrection_global.txt'
phos.file<-'../../Projects/HubMap/spleen/Phospho_70percValidvalues.txt'
uncor.prot<-'../../Projects/HubMap/Spleen/ratio_gene_None.tsv'
uncor.phos<-'../../Projects/HubMap/spleen/ratio_single-site_None.tsv'
loadMetadata<-function(){
tab<-readxl::read_xlsx(new.md.file)
return(tab)
}
loadCrossTab<-function(ctfile,isPhospho=FALSE){
if(isPhospho)
sk=1
else
sk=0
cfile<-read.table(ctfile,sep='\t',header=T,skip=sk)%>%
tidyr::pivot_longer(cols=starts_with('TMT'),names_to='Sample',values_to='logRatio')%>%
separate(Sample,into=c('TMT','Channel Index'))%>%
mutate(`Channel Index`=as.numeric(`Channel Index`))%>%
mutate(`TMT set`=as.numeric(gsub('TMT','',TMT)))
return(cfile)
}
meta<-loadMetadata()
prot<-loadCrossTab(prot.file)%>%
full_join(meta,by=c('TMT set','Channel Index'))%>%
dplyr::rename(`Protein`='T..Index')
phos<-loadCrossTab(phos.file,TRUE)%>%
left_join(meta,by=c('TMT set','Channel Index'))%>%
dplyr::rename(`Phosphosite`='T..Index')
library(Matrix)
library(SingleCellExperiment)
counts<-prot%>%
dplyr::select(`Voxel Number`,Protein,logRatio)%>%
tidyr::pivot_wider(values_from=logRatio,names_from='Voxel Number',values_fn=list(logRatio=mean))%>%
subset(!is.na(Protein))%>%
tibble::column_to_rownames('Protein')%>%
as.matrix()
colData <- prot%>%
dplyr::select(`Voxel Number`,Xcoord,Ycoord,pulpAnnotation,`TMT set`,Histology)%>%
mutate(col=as.numeric(Xcoord),row=as.numeric(Ycoord))%>%
distinct()%>%
subset(!is.na(`Voxel Number`))%>%
tibble::column_to_rownames('Voxel Number')%>%
mutate(source='2D')
rowData <- data.frame(Protein=rownames(counts))
rownames(rowData)<-rownames(counts)
#cmat <- as.matrix(countdat[,rownames(i1_spots)])
#print(dim(cmat))
nas <- which(apply(counts,1,function(x) all(is.na(x))))
if(length(nas)>0)
counts <- counts[-nas,]
#print(dim(cmat))
rowD <- data.frame(Protein=rowData[rownames(counts),])
colD <- colData[colnames(counts),]
#create the ojbect for the protein data
spat.prot<- SingleCellExperiment(assays=list(logcounts=as(counts, "dgCMatrix")),
rowData=rowD,
colData=colD)
pcounts<-phos%>%
dplyr::select(`Voxel Number`,Phosphosite,logRatio)%>%
tidyr::separate(Phosphosite,into=c('uniprot_id','mod'))%>%
left_join(swp.map)%>%
tidyr::unite(col='Phosphosite',symbol,mod,sep='_')%>%
subset(!is.na(Phosphosite))%>%
dplyr::select(-c(uniprot_id,gene_id))%>%
tidyr::pivot_wider(values_from=logRatio,names_from='Voxel Number',values_fn=list(logRatio=mean))%>%
tibble::column_to_rownames('Phosphosite')%>%
as.matrix()
rowData <- data.frame(protein=rownames(pcounts))
rownames(rowData)<-rownames(pcounts)
prowD <- rowData[rownames(pcounts),]
pcolD <- colData[colnames(pcounts),]
#now create the same object for the phospho data
spat.phos<- SingleCellExperiment(assays=list(logcounts=as(pcounts, "dgCMatrix")),
rowData=prowD,
colData=pcolD)
pdat<-rowData(spat.phos)%>%
as.data.frame()%>%
tidyr::separate(X,into=c('Protein','Psite'),sep='_',remove=FALSE)%>%
tidyr::unite(Protein,Psite,col='Phosphosite',sep='-')
rowData(spat.phos)<-pdat
pcounts<-global.spleen%>%
dplyr::select(`Sample`,Gene,LogRatio)%>%
tidyr::pivot_wider(values_from=LogRatio,names_from='Sample',values_fn=list(logRatio=mean))%>%
subset(!is.na(Gene))%>%
tibble::column_to_rownames('Gene')%>%
as.matrix()
##nwo create the same object for global
global.sorted<-SingleCellExperiment(assays=list(logcounts=as(pcounts,'dgCMatrix')),
colData=tibble::column_to_rownames(spleen.meta,'Sample'),
rowData=rownames(pcounts))
##lastly let's process the full spleen phospho data
phcounts<-phospho.spleen%>%
dplyr::select(Index,Gene,starts_with('sample'))%>%#first get the columns
tidyr::separate(Index,into=c('prot','mod'),sep='_')%>% #separate swissprot
tidyr::unite(col='Site',Gene,mod,sep="_")%>%
dplyr::select(-prot)%>%
tidyr::pivot_longer(cols=starts_with('sample'),names_to='oldsample',values_to='logRatio')
phcounts$sample<-stringr::str_replace(phcounts$oldsample,'-','.')
phcounts<-phcounts%>%
dplyr::select(-oldsample)%>%
tidyr::pivot_wider(names_from='sample',values_from='logRatio')%>%
tibble::column_to_rownames('Site')%>%
as.matrix()
phospho.sorted<-SingleCellExperiment(assays=list(logcounts=as(phcounts,'dgCMatrix')),
colData=tibble::column_to_rownames(spleen.meta,'Sample'),
rowData=rownames(phcounts))
##lastly, let's normalize the phospho values by the matchced global values