-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoct-07-tpm.R
More file actions
185 lines (154 loc) · 6.77 KB
/
noct-07-tpm.R
File metadata and controls
185 lines (154 loc) · 6.77 KB
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
# Project/code: Noct, RNA-seq of NOCT overexpression
# Start date: Sun Oct 20 15:44:36 2019
# Objective: calculate TPM for genes for each sample
# --------------
# Author: diaorch
# Modification date: Sun Oct 20 15:44:36 2019
# Modification (if modified from another procjet/script):
# 0. original (not modified from other sources)
# --------------
# setwd('~/data/noct/humanOE/06-tpm/')
tpmPath <- '~/data/noct/humanOE/06-tpm/'
fragLenSuffix <- '.fistSegment.field9.csv'
filenameSet <- list.files(path = tpmPath, pattern = fragLenSuffix)
# print(filenameSet)
exonLengthFile <- '20191020-exonic_total_length.RData'
countPath <- '04-htseq/'
htseqSuffix <- '.stranded.htseq.count'
countFilenameSet <- list.files(path = countPath, pattern = htseqSuffix)
print(countFilenameSet)
deseqPath <- '05-deseq/' # path masked for code sharing
# deseqFile <- '20191001-volcano_plotting.RData'
deseqFile <- '20191223-noct-patched.deseq2_lfcShrink_ordered_table.csv'
deResTbl <- read.table(file = paste(deseqPath, deseqFile, sep = ''),
header = TRUE, sep = ',', quote = '"', stringsAsFactors = FALSE)
print(head(deResTbl))
#### Calculate median fragment length ####
medianFragLen <- rep(x = NA, length(filenameSet))
names(medianFragLen) <- gsub(pattern = fragLenSuffix, replacement = '', x = filenameSet)
print(medianFragLen)
for (i in 1:length(filenameSet)){
# i <- 1
sampleName <- gsub(pattern = fragLenSuffix, replacement = '', x = filenameSet[i])
fragLenFirstSegment <- read.table(
file = paste(tpmPath, filenameSet[i], sep = ''), sep = ',', header = FALSE)
# print(head(fragLenFirstSegment))
fragLenSet <- abs(fragLenFirstSegment$V1)
print(summary(fragLenSet))
print(median(fragLenSet))
medianFragLen[sampleName] <- median(fragLenSet)
}
save(medianFragLen,
file = paste(tpmPath, '20191020-median_fragment_lengths.RData', sep = ''))
# load(file = paste(tpmPath, '20191020-median_fragment_lengths.RData', sep = ''))
# print(medianFragLen)
#### Calculate TPM ####
# load median read length
load(paste(tpmPath, '20191020-median_fragment_lengths.RData', sep = ''))
# load gene exon-covered lengths
load(paste(tpmPath, exonLengthFile, sep = ''))
# load('20191020-exonic_info.RData')
# print(head(exonicInfo))
# print(any(exonicInfo92$ensembl_gene_id == 'ENSG00000130489'))
htseqAnnot <- c('__no_feature', '__ambiguous', '__too_low_aQual',
'__not_aligned', '__alignment_not_unique')
tpmSamples <- NULL
for (countFilename in countFilenameSet){
# countFilename <- countFilenameSet[1]
# print(countFilename)
sampleName <- gsub(pattern = htseqSuffix, replacement = '', x = countFilename)
fragLen <- medianFragLen[sampleName]
print(fragLen)
countTbl <- read.table(file = paste(countPath, countFilename, sep = ''),
sep = '\t', header = FALSE, stringsAsFactors = FALSE)
colnames(countTbl) <- c('gene_id', 'htseq_count')
# print(head(countTbl))
# print(tail(countTbl))
# print(dim(countTbl))
countTbl$exon_length <- totalExonLenAll[countTbl$gene_id]
# print(head(countTbl))
# print(tail(countTbl))
# print(nrow(countTbl))
# print(sum(is.na(countTbl$exon_length)))
# print(countTbl[is.na(countTbl$exon_length), ])
countTblClean <- countTbl[!is.na(countTbl$exon_length), ]
# print(countTbl[is.na(countTbl$exon_length), ])
countTblClean$transcript_quant <-
countTblClean$htseq_count * fragLen / countTblClean$exon_length
# print(head(countTblClean))
totalTranscript <- sum(countTblClean$transcript_quant)
sampleNameTpm <- paste(sampleName, '_TPM', sep = '')
countTblClean[sampleNameTpm] <-
countTblClean$transcript_quant / totalTranscript * 10 ^ 6
# print(head(countTblClean))
# tpmSamples[[sampleName]] <- countTblClean$tpm
if (is.null(tpmSamples)){
tpmSamples <- data.frame(countTblClean[, c('gene_id', sampleNameTpm)])
} else {
tpmSamples <- merge(x = tpmSamples,
y = countTblClean[, c('gene_id', sampleNameTpm)],
by = 'gene_id', all = TRUE)
}
# print(head(tpmSamples))
}
print(dim(tpmSamples))
print(head(tpmSamples))
# formating
tpmSamplesRes <- format(tpmSamples, digit = 2, scientific = FALSE, trim = TRUE)
print(head(tpmSamplesRes))
tpmSamplesRes <- cbind(
data.frame(gene_id = tpmSamplesRes$gene_id),
as.data.frame(sapply(X = tpmSamplesRes[2:ncol(tpmSamplesRes)], FUN = as.numeric)))
print(sapply(X = as.data.frame(tpmSamplesRes), FUN = mode))
print(head(tpmSamplesRes))
print(head(tpmSamplesRes$Del_3F_11_S5[1:6]))
# write.csv(x = tpmSamplesRes,
# file = paste(tpmPath, '20191020-TPM.alignedBySTAR.csv', sep = ''),
# # col.names = TRUE,
# row.names = FALSE, quote = FALSE)
#### annotate TPM table with gene info #####
# listMarts()
# ensembl <- useMart('ensembl', dataset = 'hsapiens_gene_ensembl')
# listAttributes(mart = ensembl, page = 'feature_page', what = 'description')
# head(listAttributes(mart = ensembl, page = 'feature_page'), 20)
ensembl92 = useEnsembl(biomart = 'ensembl', dataset = 'hsapiens_gene_ensembl',
version = 92)
gnMapping <- getBM(
attributes = c('ensembl_gene_id', 'hgnc_symbol', 'description'),
filters = 'ensembl_gene_id' , values = tpmSamplesRes$gene_id,
mart = ensembl92)
print(head(gnMapping))
tpmSamplesRes <- merge(x = tpmSamplesRes, y = gnMapping,
by.x = 'gene_id', by.y = 'ensembl_gene_id',
all.x = TRUE, all.y = FALSE)
print(head(tpmSamplesRes))
print(tpmSamplesRes[1300:1305, ])
# ENSG00000230417 has two HGNC symbols
write.csv(x = merge(x = tpmSamples, y = gnMapping,
by.x = 'gene_id', by.y = 'ensembl_gene_id',
all.x = TRUE, all.y = FALSE),
# x = tpmSamplesRes,
file = paste(tpmPath, '20191108-TPM.alignedBySTAR.csv', sep = ''),
# col.names = TRUE,
row.names = FALSE, quote = TRUE)
write.csv(x = tpmSamplesRes,
file = paste(tpmPath, '20191108-TPM.alignedBySTAR.rounded.csv', sep = ''),
# col.names = TRUE,
row.names = FALSE, quote = TRUE)
#### DESeq info ####
# load(paste(deseqPath, deseqFile, sep = ''))
print(dim(deResTbl))
print(head(deResTbl))
tpmWirttenRes <- read.table(file = paste(tpmPath, '20191108-TPM.alignedBySTAR.rounded.csv', sep = ''),
header = TRUE, sep = ',', stringsAsFactors = FALSE)
print(head(tpmWirttenRes))
tpmDeRes <- merge(x = tpmWirttenRes, y = deResTbl,
by.x = 'gene_id', by.y = 'X', all.x = TRUE, all.y = FALSE)
print(head(tpmDeRes))
print(dim(tpmDeRes))
tpmDeRes <- tpmDeRes[order(tpmDeRes$gene_id), ]
print(head(tpmDeRes))
write.csv(x = tpmDeRes,
file = paste(tpmPath, '20191223-TPM.alignedBySTAR.rounded.DESeqStats.csv', sep = ''),
# col.names = TRUE,
row.names = FALSE, quote = TRUE, na = '')