# Can I get the graph generated by cellranger

I’ve run the cellranger analysis pipeline on single cell RNASeq datasets. I can import the matrix and graph-based clusters into R. Doing this I can optimise the dimension reduction and plot cells with coloured by clusters generated by cellranger. I wish to optimise the graph-based clustering as well.

Is it possible to either:

• obtain the graph generated by cellranger from the files generated. (I can only find the final clusters in the analysis output)

• reproduce generating a similar graph (used for clustering) from the output matrix (preferably with R or Python.

With either approach, I am looking for a list of edges for nearest neighbors in the gene-barcode UMI matrix to use as input for a clustering algorithm.

• Could you provide some data as an example or pick the data from the vignette to answer you would work for you? What have you tried to get the graph or reproduce it?
– llrs
Nov 13 '18 at 11:26
• I’ve tried several ways to reproduce this graph in R but I’m unsure that I understand well enough what cellranger is doing (based on Python code in the GitHub repo) to generate inputs for the Louvian algorithm. I’m now unsure that this is necessary since any cellranger run generates various output files for the analysis in a specific directory structure. I think this question requires someone with familiar with this software already and has run it themselves. I cannot provide the data that I’m working with currently and the files are too large even if I could. Nov 13 '18 at 13:39
• Hi @TomKelly. I don't know if I understood exactly the question. However, you may find useful to use scanpy for importing the data, run the Louvain clustering, and plot the data with the clusters. That is a Python module.
– gc5
Nov 13 '18 at 15:34
• @TomKelly Could you provide your attempts and why they fail? This would make it easier for me to avoid this pitfalls (if I try to answer your question)
– llrs
Nov 13 '18 at 16:43
• I don't understand what plot you want. You know where the T-SNE coordinates are, right? Nov 13 '18 at 20:10

Cell Ranger

You can't download the tSNE coordinates for cells directly from the Analysis tab of the fancy, polished .html document that Cell Ranger produces. If you have access to the machine on which the pipeline was run, you can grab the intermediate files. Everything is well documented by 10X. They explain defaults etc. on that page. The tSNE coordinates are available at:

analysis/tsne/2_components/projection.csv


and the contents look like:

\$ head -5 analysis/tsne/2_components/projection.csv
Barcode,TSNE-1,TSNE-2
AAACATACAACGAA-1,-13.5494,1.4674
AAACATACTACGCA-1,-2.7325,-10.6347
AAACCGTGTCTCGC-1,12.9590,-1.6369
AAACGCACAACCAC-1,-9.3585,-6.7300


Seurat

And the second point: Cell Ranger is replicating the Seurat workflow. Seurat uses the Louvain method for community detection by default. Check out the intro tutorial and see if you can follow along. This tutorial works with 10X data, in fact the first function call in the tutorial reads in the 10X matrices. If you are already plotting in R you'll speed through this tutorial. Seurat is great to work with, you'll have tSNEs and clusters in no time. And there's so much more you can do from there!

• Yes I’m familiar with the Seurat workflow. I don’t understand the Python source code well enough to know which parameters were used by cellranger. Nov 13 '18 at 23:31
• If you're unwilling to use a different software package, why mention that as a possibility in your second point? The matrices are very lightly analyzed data. The unfiltered matrices are counts for all cell barcodes. The filtered matrices are count for cell arcodes determined to come from real cells. You still have to normalize and perform quality control filtering on this data efore it's ready for its first round of dimensional reduction and clustering. Nov 14 '18 at 5:21
• If you want information about their algorithms, see here. There is also info on that page about the matrices. This should help with your understanding of how Cell Ranger is massaging the data. Nov 14 '18 at 5:23
• I’m considering whether it is necessary to compute the graph again of it has already been done by cellranger. If it has not been saved by this program, it will be needed to compute it as you’ve suggested. In that case, more detailed information will be helpful. I have (and will continue) to do it this way but it would be reassuring to know I’m on the right track and not reinventing a graph structure that I already have saved to disk somewhere. Nov 14 '18 at 5:27
• Ah, I was thinking you just just had the final product. If you have access to the machine where the pipeline was run you can get the tSNE coordinates. Nov 14 '18 at 21:50

### Files in a cellranger run

Cellranger removes the intermediary files after the clusters have been computed. This can be observed in the following path from the cellranger project directory. This contains empty directories.

SC_RNA_AGGREGATOR_CS/SC_RNA_ANALYZER/RUN_GRAPH_CLUSTERING


However, the clusters generated by cellranger can be found in the file:

outs/analysis/clustering/graphclust/clusters.csv


Clusters can also be "exported" from outs/cloupe.cloupe file into a CSV using the Cloupe Browser. These CSVs can be imported and used for plotting in R or Python.

The graph structure used to compute these (with the Louvian algorithm) cannot be found in the files generated by cellranger.

### Reproducing graph-based clustering

The Suerat R package performs graph-based clustering. The shared nearest neighbour (SNN) modularity optimisation uses the same parameters as the Python scripts called by cellranger (k=30). This generates an SNN matrix which can be used to generate a graph structure from an adjacency matrix. For a Seurat object test the SNN sparse matrix can be extract with:

FindClusters(test, dims.use = 1:20, algorithm = 1,
resolution = 0.6, k.param = 30, save.SNN = TRUE,
reduction.use = "pca", metric = "euclidean")
SNN_Matrix <- as.matrix(ceiling(as.matrix(test@snn)

FindClusters from the Seurat package requires a Seurat object that has already has a dimension reduction run on it such as RunPCA. The algorithm = 1 is the Louvian algorithm as performed by cellranger. The algorithm, reduction.use and metric can be changed as mentioned in the Suerat package documentation. Clustering algorithms not yet supported by Seurat can be run on the graph_structure resulting from extracting the SNN matrix. It is not necessary to compute the edges separately, the Seurat functions will compute and return these.
devtools:install_github("TomKellyGenetics/leiden")