forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateSeuratPrototype.R
More file actions
71 lines (57 loc) · 2.4 KB
/
UpdateSeuratPrototype.R
File metadata and controls
71 lines (57 loc) · 2.4 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
netRc <- paste0(Sys.getenv('USER_HOME'), '/.netrc')
if (!file.exists(netRc)) {
print(list.files(Sys.getenv('USER_HOME')))
stop(paste0('Unable to find file: ', netRc))
}
invisible(Rlabkey::labkey.setCurlOptions(NETRC_FILE = netRc))
Rdiscvr::SetLabKeyDefaults(baseUrl = serverBaseUrl, defaultFolder = defaultLabKeyFolder)
if (Sys.getenv('SEURAT_MAX_THREADS') != '') {
nCores <- Sys.getenv('SEURAT_MAX_THREADS')
} else {
nCores <- 1
}
for (datasetId in names(seuratObjects)) {
printName(datasetId)
seuratObj <- readSeuratRDS(seuratObjects[[datasetId]])
if (reapplyMetadata) {
seuratObj <- Rdiscvr::QueryAndApplyCdnaMetadata(seuratObj)
}
if (runRira) {
seuratObj <- RIRA::Classify_ImmuneCells(seuratObj, maxBatchSize = maxBatchSize, retainProbabilityMatrix = retainProbabilityMatrix)
seuratObj <- RIRA::Classify_TNK(seuratObj, maxBatchSize = maxBatchSize, retainProbabilityMatrix = retainProbabilityMatrix)
seuratObj <- RIRA::Classify_Myeloid(seuratObj, maxBatchSize = maxBatchSize, retainProbabilityMatrix = retainProbabilityMatrix)
}
if (applyTCR) {
seuratObj <- Rdiscvr::DownloadAndAppendTcrClonotypes(seuratObj, allowMissing = allowMissingTcr)
}
if (runTNKClassification) {
# ClassifyTNKByExpression will fail without this, so ignore allowMissingTcr
if (!'HasCDR3Data' %in% names(seuratObj@meta.data)) {
seuratObj <- Rdiscvr::DownloadAndAppendTcrClonotypes(seuratObj)
}
seuratObj <- Rdiscvr::ClassifyTNKByExpression(seuratObj)
}
if (saveRepertoireStats) {
seuratObj <- readSeuratRDS(seuratObjects[[datasetId]])
outputFile <- gsub(seuratObjects[[datasetId]], pattern = '.rds', replacement = '.tcrStats.txt')
df <- Rdiscvr::CalculateAndStoreTcrRepertoireStats(seuratObj, outputFile = outputFile)
}
if (scoreActivation) {
# Drop existing columns:
toDrop <- grep(names(seuratObj@meta.data), pattern = "sPLS", value = TRUE)
if (length(toDrop) > 0) {
print(paste0('Dropping pre-existing columns: ', paste0(toDrop, collapse = ', ')))
for (colName in toDrop) {
seuratObj[[toDrop]] <- NULL
}
}
seuratObj <- RIRA::PredictTcellActivation(seuratObj)
}
if (recalculateUCells) {
seuratObj <- RIRA::CalculateUCellScores(seuratObj, storeRanks = FALSE, assayName = 'RNA', forceRecalculate = TRUE, ncores = nCores, dropAllExistingUcells = TRUE)
}
saveData(seuratObj, datasetId)
# Cleanup
rm(seuratObj)
gc()
}