Skip to content

Commit 5da285e

Browse files
committed
avoid pivot_longer
1 parent e74f2fd commit 5da285e

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

R/bambu-extendAnnotations-utilityCombine.R

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ isore.combineTranscriptCandidates <- function(readClassList,
1717
combinedSplicedTranscripts <-
1818
combineSplicedTranscriptModels(readClassList, bpParameters,
1919
min.readCount, min.readFractionByGene,
20-
min.txScore.multiExon, min.txScore.singleExon, verbose) %>% data.table()
20+
min.txScore.multiExon, min.txScore.singleExon, verbose)
2121
combinedSplicedTranscripts[,confidenceType := "highConfidenceJunctionReads"]
2222
combinedUnsplicedTranscripts <-
2323
combineUnsplicedTranscriptModels(readClassList, bpParameters,
@@ -88,37 +88,24 @@ sequentialCombineFeatureTibble <- function(readClassList,
8888

8989
#' @noRd
9090
updateStartEndReadCount <- function(combinedFeatureTibble){
91-
combinedFeatureTibble <- combinedFeatureTibble %>%
92-
mutate(rowID = row_number())
93-
94-
startEndCountTibble <- combinedFeatureTibble %>%
95-
select(rowID, starts_with("start"),starts_with("end"),
96-
starts_with("readCount")) %>%
97-
tidyr::pivot_longer(c(starts_with("start"),starts_with("end"),
98-
starts_with("readCount")), names_to = c(".value","set"),
99-
names_pattern = "(.*)\\.(.)") %>%
100-
group_by(rowID) %>%
101-
mutate(sumReadCount = sum(readCount,na.rm = TRUE))
91+
setDT(combinedFeatureTibble)
92+
combinedFeatureTibble[, rowID := .I]
10293

103-
startTibble <- select(startEndCountTibble, rowID, start, readCount,
104-
sumReadCount) %>%
105-
arrange(start) %>%
106-
filter(cumsum(readCount)/sumReadCount>=0.5) %>%
107-
filter(row_number()==1)
108-
endTibble <- select(startEndCountTibble, rowID, end, readCount,
109-
sumReadCount) %>%
110-
arrange(end) %>%
111-
filter(cumsum(readCount)/sumReadCount>=0.5) %>%
112-
filter(row_number()==1)
94+
colNames <- colnames(combinedFeatureTibble)
95+
readCountCols <- sort(colNames[grep("^readCount", colNames)]) # to make sure it's ordered by sample name
96+
startCols <- sort(colNames[grep("^start", colNames)])
97+
endCols <- sort(colNames[grep("^end", colNames)])
11398

114-
combinedFeatureTibble <- combinedFeatureTibble %>%
115-
dplyr::select(intronStarts, intronEnds, chr, strand, maxTxScore,
116-
maxTxScore.noFit, NSampleReadCount, NSampleReadProp,
117-
NSampleTxScore, rowID) %>%
118-
full_join(select(startTibble, rowID, start), by = "rowID") %>%
119-
full_join(select(endTibble, rowID, end, readCount=sumReadCount),
120-
by = "rowID") %>%
121-
select(-rowID)
99+
startEndDt <- combinedFeatureTibble[,
100+
.(start = readCountWeightedMedian(.SD,x,y),
101+
end = readCountWeightedMedian(.SD,z,y),
102+
readCount = sum(.SD[,y], na.rm = TRUE)),
103+
by = rowID, env = I(list(x = startCols, y = readCountCols,z = endCols))]
104+
105+
combinedFeatureTibble <- startEndDt[combinedFeatureTibble[,.(intronStarts, intronEnds, chr, strand, maxTxScore,
106+
maxTxScore.noFit, NSampleReadCount, NSampleReadProp,
107+
NSampleTxScore, rowID)], by = rowID]
108+
combinedFeatureTibble[, rowID := NULL]
122109
return(combinedFeatureTibble)
123110
}
124111

0 commit comments

Comments
 (0)