88adb3f4a121ba595b63aba89b57261e52e66c4d max Fri May 2 08:17:38 2025 -0700 adding quickgenes to cbImportSeurat diff --git src/cbPyLib/cellbrowser/seurat.py src/cbPyLib/cellbrowser/seurat.py index b8f81e3..60c8576 100644 --- src/cbPyLib/cellbrowser/seurat.py +++ src/cbPyLib/cellbrowser/seurat.py @@ -1,22 +1,24 @@ # a wrapper around the R library Seurat import logging, optparse, sys, glob, os, datetime, shutil from os.path import join, basename, dirname, isfile, isdir, relpath, abspath, getsize, getmtime, expanduser, splitext from .cellbrowser import copyPkgFile, writeCellbrowserConf, pipeLog, makeDir, maybeLoadConfig, errAbort, popen from .cellbrowser import setDebug, build, isDebugMode, generateHtmls, runCommand, copyFileIfDiffSize +from .cellbrowser import generateQuickGenes + def parseArgs(): " setup logging, parse command line arguments and options. -h shows auto-generated help page " parser = optparse.OptionParser("""usage: %prog [options] -e matrixFile -o outDir - run Seurat and output .tsv files If exceptions occur, will automatically start the debugger. """) parser.add_option("", "--init", dest="init", action="store_true", help="copy sample seurat.conf to current directory") parser.add_option("-e", "--exprMatrix", dest="exprMatrix", action="store", help="gene-cell expression matrix file, possible formats: .mtx, .txt.gz, .csv.gz, .rds. For .mtx, specify the directory where the matrix.mtx file is stored, e.g. filtered_gene_bc_matrices/hg19/") parser.add_option("-o", "--outDir", dest="outDir", action="store", @@ -41,39 +43,31 @@ if (options.exprMatrix is None and options.outDir is None) and not options.init: parser.print_help() exit(1) setDebug(options.debug) return args, options def runRscript(scriptFname, logFname): """ run an R script given as a string through Rscript """ logging.info("running %s through Rscript" % scriptFname) #ofh = open(logFname, "w") # removed subprocess. Multiprocessing is notoriously tricky to get right. cmd = "time Rscript %s 2>&1 | tee %s" % (scriptFname, logFname) - #cmd = ["time","Rscript", scriptFname, "&"] - #proc, stdout = popen(cmd, shell=True) - #for line in stdout: - #print(line), - #ofh.write(line) err = os.system(cmd) - #proc.stdout.close() - #stat = os.waitpid(proc.pid, 0) - #err = stat[1] assert(err==0) logging.info("Wrote logfile of R run to %s" % logFname) def checkRVersion(): " make sure that we have > 3.4" logging.debug("Checking R version") cmd = ["Rscript", "-e", "ver = R.Version(); if (ver$major<3 || (ver$major==3 && ver$minor < 4)) { stop('Sorry, Seurat requires at least R 3.4') } "] runCommand(cmd) def getSeuratVersion(): " return current Seurat version " logging.debug("Checking Seurat version") cmd = ["Rscript", "-e", "message(packageVersion('Seurat'))"] proc, stdout = popen(cmd, useStderr=True) if proc is None: @@ -323,30 +317,32 @@ confArgs = {"clusterField":"Cluster"} if inConf.get("skipMatrixExport", False): confArgs["exprMatrix"] = inMatrix matrixPath = None writeCbSeuratScript(inConf, inMatrix, tsnePath, clusterPath, markerPath, rdsPath, matrixPath, scriptPath, datasetName, outDir, threadCount) runRscript(scriptPath, logPath) if not isfile(markerPath): errAbort("R script did not complete successfully. Check %s and analysisLog.txt." % scriptPath) coords = [{'shortLabel':'t-SNE', 'file':'tsne.coords.tsv'}] + generateQuickGenes(outDir) + confArgs['quickGenesFile'] = "quickGenes.tsv" writeCellbrowserConf(datasetName, coords, cbConfPath, args=confArgs) generateHtmls(datasetName, outDir, desc = {"supplFiles": [{"label":"Seurat RDS", "file":basename(rdsPath)}]}) copyPkgFile("sampleConfig/desc.conf", outDir) def cbImportSeurat_parseArgs(showHelp=False): " setup logging, parse command line arguments and options. -h shows auto-generated help page " parser = optparse.OptionParser("""usage: %prog [options] -i input.rds -o outDir [-n datasetName] - convert Seurat object to cellbrowser Example: - %prog -i pbmc3k.rds -o pbmc3kSeurat - convert pbmc3k to directory of tab-separated files """) parser.add_option("-d", "--debug", dest="debug", action="store_true",