Skip to content

Commit

Permalink
Merge branch 'master' of github.com:maximilianh/cellBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianh committed Dec 7, 2018
2 parents 247cce3 + bba1be0 commit f8ad01c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion sampleData/pbmc_small/scanpy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ louvainRes = 1.0
# Tilford tree layout).
# You can also set doLayouts="all" to generate all possible layouts mentioned above.
#doLayouts = ["fa", "umap"]
doLayouts = "all"
#doLayouts = "all"
doLayouts = ["umap", "pagaFa", "fa", "fr", "kk", "lgl", "drl", "rt"]

# find top marker genes per cluster?
doMarkers = True
Expand Down
32 changes: 16 additions & 16 deletions src/cbPyLib/cellbrowser/cellbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,6 @@ def getAbsPath(conf, key):

def popen(cmd, shell=False):
" run command and return proc object with its stdout attribute "

if isPy3:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, encoding="utf8", shell=shell)
else:
Expand Down Expand Up @@ -2638,7 +2637,11 @@ def anndataToTsv(ad, matFname, usePandas=False):
ofh.write("\n")

ofh.close()
os.rename(tmpFname, matFname)

if matFname.endswith(".gz"):
runGzip(tmpFname, matFname)
else:
os.rename(tmpFname, matFname)

def makeDictDefaults(inVar, defaults):
" convert inVar to dict if necessary, defaulting to our default labels "
Expand Down Expand Up @@ -2684,9 +2687,8 @@ def scanpyToCellbrowser(adata, path, datasetName, metaFields=["louvain", "percen
import anndata

if not skipMatrix:
matFname = join(path, 'exprMatrix.tsv')
matFname = join(path, 'exprMatrix.tsv.gz')
anndataToTsv(adata, matFname)
matFname = runGzip(matFname)

if coordFields=="all" or coordFields is None:
coordFields = coordLabels
Expand Down Expand Up @@ -3146,8 +3148,9 @@ def checkLayouts(conf):

return doLayouts

def cbScanpy(matrixFname, confFname, figDir, logFname):
" run expr matrix through scanpy, output a cellbrowser.conf, a matrix and the meta data "
def cbScanpy(matrixFname, confFname, figDir, logFname, outMatrixFname):
""" run expr matrix through scanpy, output a cellbrowser.conf, a matrix and the meta data.
Return an adata object. Write raw matrix to outMatrixFname """
import scanpy.api as sc
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -3176,6 +3179,9 @@ def cbScanpy(matrixFname, confFname, figDir, logFname):
start = timeit.default_timer()
adata = readMatrixAnndata(matrixFname, samplesOnRows=options.samplesOnRows)

if outMatrixFname is not None:
anndataToTsv(adata, outMatrixFname)

pipeLog("Data has %d samples/observations" % len(adata.obs))
pipeLog("Data has %d genes/variables" % len(adata.var))

Expand Down Expand Up @@ -3427,21 +3433,15 @@ def cbScanpyCli():
makeDir(outDir)

figDir = join(outDir, "figs")

adFname = join(outDir, "anndata.h5ad")
matrixOutFname = join(outDir, "exprMatrix.tsv.gz")

#if not isfile(adFname):
logFname = join(outDir, "cbScanpy.log")
adata = cbScanpy(matrixFname, confFname, figDir, logFname)
#except:
# on an exception, automatically bring up the debugger - avoids having to reload the data file
#extype, value, tb = sys.exc_info()
#import traceback, pdb
#traceback.print_tb(tb)
#pdb.post_mortem(tb)
# write the expr matrix before doing any processing, otherwise scanpy will destroy the original matrix
adata = cbScanpy(matrixFname, confFname, figDir, logFname, matrixOutFname)
logging.info("Writing final result as an anndata object to %s" % adFname)
adata.write(adFname)
scanpyToCellbrowser(adata, outDir, datasetName=options.name)
scanpyToCellbrowser(adata, outDir, datasetName=options.name, skipMatrix=True)

def mtxToTsvGz(mtxFname, geneFname, barcodeFname, outFname):
" convert mtx to tab-sep without scanpy. gzip if needed "
Expand Down

0 comments on commit f8ad01c

Please sign in to comment.