@@ -7,91 +7,102 @@ assignReadClasstoTranscripts <- function(readClassList, annotations, isoreParame
77 returnDistTable = FALSE , trackReads = TRUE ) {
88 if (is.character(readClassList )) readClassList <- readRDS(file = readClassList )
99 metadata(readClassList )$ readClassDist <- calculateDistTable(readClassList , annotations , isoreParameters , verbose , returnDistTable )
10- readClassList = splitReadClassFiles(readClassList )
10+ readClassList <- splitReadClassFiles(readClassList )
1111 readClassDt <- genEquiRCs(metadata(readClassList )$ readClassDist , annotations , verbose )
1212 readClassDt $ eqClass.match = match(readClassDt $ eqClassById ,metadata(readClassList )$ eqClassById )
1313 readClassDt <- simplifyNames(readClassDt )
14- readClassDt = readClassDt %> % group_by(eqClassId , gene_sid ) %> %
15- mutate(multi_align = length(unique(txid ))> 1 ) %> % ungroup() %> % mutate(aval = 1 ) %> %
14+ readClassDt <- readClassDt %> % group_by(eqClassId , gene_sid ) %> %
15+ mutate(multi_align = length(unique(txid ))> 1 ) %> %
16+ ungroup() %> %
17+ mutate(aval = 1 ) %> %
1618 data.table()
1719 # return non-em counts
18- ColData = generateColData(colnames(metadata(readClassList )$ countMatrix ), clusters = NULL , demultiplexed , spatial )
20+ ColData <- generateColData(colnames(metadata(readClassList )$ countMatrix ), clusters = NULL , demultiplexed , spatial )
1921 quantData <- SummarizedExperiment(assays = SimpleList(
2022 counts = generateUniqueCounts(readClassDt , metadata(readClassList )$ countMatrix , annotations )),
2123 rowRanges = annotations ,
2224 colData = ColData )
23- colnames(quantData ) = ColData $ id
25+ colnames(quantData ) <- ColData $ id
2426 if (sum(metadata(readClassList )$ incompatibleCountMatrix )== 0 ){
25- metadata(quantData )$ incompatibleCounts = NULL
26- } else {
27- metadata(quantData )$ incompatibleCounts = generateIncompatibleCounts(metadata(readClassList )$ incompatibleCountMatrix , annotations )
28- }
29- metadata(quantData )$ nonuniqueCounts = generateNonUniqueCounts(readClassDt , metadata(readClassList )$ countMatrix , annotations )
30- metadata(quantData )$ readClassDt = readClassDt
31- metadata(quantData )$ countMatrix = metadata(readClassList )$ countMatrix
32- metadata(quantData )$ incompatibleCountMatrix = metadata(readClassList )$ incompatibleCountMatrix
33- metadata(quantData )$ sampleNames = metadata(readClassList )$ sampleNames
34- if (returnDistTable ){
35- metadata(quantData )$ distTable = metadata(metadata(readClassList )$ readClassDist )$ distTableOld
27+ metadata(quantData )$ incompatibleCounts <- NULL
28+ }else {
29+ metadata(quantData )$ incompatibleCounts <- generateIncompatibleCounts(metadata(readClassList )$ incompatibleCountMatrix , annotations )
3630 }
37- if (trackReads ){
38- metadata(quantData )$ readToTranscriptMap =
39- generateReadToTranscriptMap(readClassList , metadata(readClassList )$ readClassDist ,
40- annotations )
41- }
31+ metadata(quantData )$ nonuniqueCounts <- generateNonUniqueCounts(readClassDt , metadata(readClassList )$ countMatrix , annotations )
32+ metadata(quantData )$ readClassDt <- readClassDt
33+ metadata(quantData )$ countMatrix <- metadata(readClassList )$ countMatrix
34+ metadata(quantData )$ incompatibleCountMatrix <- metadata(readClassList )$ incompatibleCountMatrix
35+ metadata(quantData )$ sampleNames <- metadata(readClassList )$ sampleNames
36+ if (returnDistTable )
37+ metadata(quantData )$ distTable <- metadata(metadata(readClassList )$ readClassDist )$ distTableOld
38+
39+ if (trackReads )
40+ metadata(quantData )$ readToTranscriptMap <-
41+ generateReadToTranscriptMap(readClassList ,
42+ metadata(readClassList )$ readClassDist ,
43+ annotations )
44+
4245 return (quantData )
4346
4447}
4548
46-
49+ # ' Generate unique counts
50+ # ' @noRd
4751generateUniqueCounts <- function (readClassDt , countMatrix , annotations ){
48- x = readClassDt %> % filter(! multi_align & ! is.na(eqClass.match ))
49- uniqueCounts = countMatrix [x $ eqClass.match ,]
50- uniqueCounts.tx = sparse.model.matrix(~ factor (x $ txid ) - 1 )
51- uniqueCounts = t(uniqueCounts.tx ) %*% uniqueCounts
52- rownames(uniqueCounts ) = names(annotations )[match(as.numeric(levels(factor (x $ txid ))),mcols(annotations )$ txid )]
53- counts = sparseMatrix(length(annotations ), ncol(uniqueCounts ), x = 0 )
54- rownames(counts ) = names(annotations )
55- counts [rownames(uniqueCounts ),] = uniqueCounts
52+ x <- readClassDt %> % filter(! multi_align & ! is.na(eqClass.match ))
53+ uniqueCounts <- countMatrix [x $ eqClass.match ,]
54+ uniqueCounts.tx <- sparse.model.matrix(~ factor (x $ txid ) - 1 )
55+ uniqueCounts <- t(uniqueCounts.tx ) %*% uniqueCounts
56+ rownames(uniqueCounts ) <- names(annotations )[match(as.numeric(levels(factor (x $ txid ))),mcols(annotations )$ txid )]
57+ counts <- sparseMatrix(length(annotations ), ncol(uniqueCounts ), x = 0 )
58+ rownames(counts ) <- names(annotations )
59+ counts [rownames(uniqueCounts ),] <- uniqueCounts
5660 return (counts )
57-
58- counts.total = colSums(countMatrix ) + colSums(incompatibleCountMatrix )
59- counts.total [counts.total == 0 ] = 1
60- counts.CPM = counts / counts.total * 10 ^ 6
61+
62+ # these three lines appear after return, so it's not used, is this used for debug only?
63+ # counts.total = colSums(countMatrix) + colSums(incompatibleCountMatrix)
64+ # counts.total[counts.total==0] = 1
65+ # counts.CPM = counts/counts.total * 10^6
6166
6267}
6368
69+
70+ # ' Generate incompatible counts
71+ # ' @noRd
6472generateIncompatibleCounts <- function (incompatibleCountMatrix , annotations ){
65- genes = levels(factor (unique(mcols(annotations )$ GENEID )))
66- rownames(incompatibleCountMatrix ) = genes [as.numeric(rownames(incompatibleCountMatrix ))]
67- geneMat = sparseMatrix(length(genes ), ncol(incompatibleCountMatrix ), x = 0 )
68- rownames(geneMat ) = genes
69- geneMat [rownames(incompatibleCountMatrix ),] = incompatibleCountMatrix
73+ genes <- levels(factor (unique(mcols(annotations )$ GENEID )))
74+ rownames(incompatibleCountMatrix ) <- genes [as.numeric(rownames(incompatibleCountMatrix ))]
75+ geneMat <- sparseMatrix(length(genes ), ncol(incompatibleCountMatrix ), x = 0 )
76+ rownames(geneMat ) <- genes
77+ geneMat [rownames(incompatibleCountMatrix ),] <- incompatibleCountMatrix
7078 return (geneMat )
7179}
7280
81+
82+ # ' Generate non-unique counts
83+ # ' @noRd
7384generateNonUniqueCounts <- function (readClassDt , countMatrix , annotations ){
7485 # fuse multi align RCs by gene
75- x = readClassDt %> % filter(multi_align & ! is.na(eqClass.match ))
76- x = x %> % distinct(eqClassId , .keep_all = TRUE )
77- nonuniqueCounts = countMatrix [x $ eqClass.match ,, drop = FALSE ]
86+ x <- readClassDt %> % filter(multi_align & ! is.na(eqClass.match ))
87+ x <- x %> % distinct(eqClassId , .keep_all = TRUE )
88+ nonuniqueCounts <- countMatrix [x $ eqClass.match ,, drop = FALSE ]
7889 if (nrow(x )> 1 & length(unique(x $ gene_sid ))> 1 ){
79- nonuniqueCounts.gene = sparse.model.matrix(~ factor (x $ gene_sid ) - 1 )
80- nonuniqueCounts = t(nonuniqueCounts.gene ) %*% nonuniqueCounts
90+ nonuniqueCounts.gene <- sparse.model.matrix(~ factor (x $ gene_sid ) - 1 )
91+ nonuniqueCounts <- t(nonuniqueCounts.gene ) %*% nonuniqueCounts
8192 } else {
8293 warning(" The factor variable 'gene_sid' has only one level. Adjusting output." )
83- nonuniqueCounts.gene = Matrix(1 , nrow = nrow(x ), ncol = 1 , sparse = TRUE )
84- nonuniqueCounts = t(nonuniqueCounts.gene ) %*% nonuniqueCounts
94+ nonuniqueCounts.gene <- Matrix(1 , nrow = nrow(x ), ncol = 1 , sparse = TRUE )
95+ nonuniqueCounts <- t(nonuniqueCounts.gene ) %*% nonuniqueCounts
8596 }
8697 # covert ids into gene ids
87- geneids = as.numeric(levels(factor (x $ gene_sid )))
88- geneids = x $ txid [match(geneids , x $ gene_sid )]
89- geneids = mcols(annotations )$ GENEID [as.numeric(geneids )]
90- rownames(nonuniqueCounts ) = geneids
98+ geneids <- as.numeric(levels(factor (x $ gene_sid )))
99+ geneids <- x $ txid [match(geneids , x $ gene_sid )]
100+ geneids <- mcols(annotations )$ GENEID [as.numeric(geneids )]
101+ rownames(nonuniqueCounts ) <- geneids
91102 # create matrix for all annotated genes
92- genes = levels(factor (unique(mcols(annotations )$ GENEID )))
93- geneMat = sparseMatrix(length(genes ), ncol(nonuniqueCounts ), x = 0 )
94- rownames(geneMat ) = genes
95- geneMat [rownames(nonuniqueCounts ),] = nonuniqueCounts
103+ genes <- levels(factor (unique(mcols(annotations )$ GENEID )))
104+ geneMat <- sparseMatrix(length(genes ), ncol(nonuniqueCounts ), x = 0 )
105+ rownames(geneMat ) <- genes
106+ geneMat [rownames(nonuniqueCounts ),] <- nonuniqueCounts
96107 return (geneMat )
97108}
0 commit comments