Most scalable pseudotime ordering algorithm What algorithms for linear Pseudotime trajectory construction (diffusion-based) are the most scalable to large datasets?

I'm currently using Slingshot based on the recommendation in this manuscript: https://www.biorxiv.org/content/biorxiv/early/2018/03/05/276907.full.pdf

Slingshot is nice and easy to use, but it turns out it is terrible at scaling to big datasets. 

For 2000 highest variable genes and a random sampling of a variable number of cells in my ~80% sparse expression matrix I get the following computation times in R (96 GB RAM):

#cells / Time (min)
100 / 0.7
1000 / 4.3
2000 / 12.3
5000 / 71.1


At this rate it would take nearly 1.5 days to process 200,000 cells and 4.5 months to process 2 million cells based on 2,000 HVG.

I feel like Slingshot is limited by a fundamentally unnecessary comparison of diffusion for all cells against all cells, rather than preclustering and then finer resolution of pseudotime across cluster edges.

What algorithms are better, but still implement a similar realization of pseudotime ordering?  I just need a single linear trajectory.
As it turns out, pseudotime algorithms are exceedingly difficult to parallelize. Pseudotime does not scale linearly to a subset of data compared to the full dataset, and as such a course-grained approach to parallelizing pseudotime calculations on random bins of the data, followed by bin merging, does not work. These values will never converge on the actual full-dataset solution.

I put in a pretty good effort trying to parallelize slingshot, but with no success. I realize my code may be of limited utility without more explanation, but I'm not putting more time into this. FWIW:

This code:


Finds computationally optimal bin size for slingshot based on a number of genes and features
Randomly splits the SingleCellExperiment object into that number of bins and runs slingshot on each bin
Orients each bin so that a cell with a pseudotime of 1 in the first bin is similar to a pseudotime of 1 in every bin. Pseudotime can get flipped depending on the sampled subset.
Merges all bins into a single matrix so each cell has a assigned pseudotime based on randomly sampled bins.
Repeats steps #2 to #5 with a different random seed for binning every time.
Measures convergence towards actual solution at three pseudotime values, where convergence is defined as correlation of cell indices along the trajectory.


I abandoned the project after observing that more iterations to not change the convergence towards the actual solution. While pseudotime values close to 0 recapitulated the actual solution quite well, pseudotime values towards 100 failed to correlate at all with the actual solution. There was no convergence with more iterations.

Maybe this will be helpful to somebody, which is why I'm posting.

# Parallel Iterative Sampling with Slingshot
# pissshot is a course-grained estimator of the actual slingshot solution, and iteratively converges towards the actual solution
# pissshot randomly partitions cells in a large database into bins, bin size is determined by finding the computationally optimal number of cells
# pissshot is run independently on each bin, pseudotime vectors are aligned across all bins, and then all bins are merged
# pissshot repeats this process and the average of pseudotime assignments for each cell is taken at the end of each iteration. 
# The convergence of the blingshot model at the end of each iteration is calculated by running slingshot on a small sample of adjacent cells at a random pseudotime interval and measuring correlation with the predicted model
# When satisfactory convergence is reached, blingshot returns a singlecellobject, just as slingshot would

pissshot &lt;- function(sce, num_iterations = 20){
    cat("Running blingshot on", dim(sce)[2],"cells and",dim(sce)[1],"features\n")
    cat("  Step 1/5: Finding computationally optimal increment sizes\n   ")
#    increment_size &lt;- FindOptimalIncrementSize(sce)
    increment_size &lt;- 688
    num_increments &lt;- ceiling(dim(sce)[2]/increment_size)-1
    cat("\n  ...Increment size of",increment_size,"cells is computationally optimal\n")
    cat("  Step 2/5: Course-grained pseudotime assignment across",num_increments,"increments")
    ps &lt;- list()
    convergenceArr &lt;- c()
    for(iteration in seq(from=1,to=num_iterations,by=1)){
        cat("\n   ... iteration",iteration,"...\n")
        cat("         randomly assigning cells to",num_increments,"bins\n         0%.")
        bins &lt;- PartitionBins(sce, increment_size = increment_size, seed = 7*iteration)
        cat("100%\n         running slingshot on each bin\n         0%")
        cell_pseudotimes &lt;- c()
        for(b in seq(from=1,to=length(bins),by=1)){
            bin_with_pseudotime &lt;- suppressMessages(slingshot(bins[[b]]))
            # Find right cell and left cell in pseudotime array (cell with highest/lowest pseudotime values)
            pcells &lt;- as.data.frame(colData(bin_with_pseudotime)$slingPseudotime_1)
            rownames(pcells) &lt;- colnames(bin_with_pseudotime)
            pcells &lt;- data.frame(lapply(pcells, function(x) as.numeric(as.character(x))), check.names = F, row.names = rownames(pcells))
            pcells.sorted &lt;- pcells[order(-pcells[,1]), , drop = FALSE]
            right_cell_ID &lt;- rownames(pcells.sorted)[dim(pcells.sorted)[1]]
            left_cell_ID &lt;- rownames(pcells.sorted)[1]
            right_cell_pos &lt;- match(right_cell_ID,colnames(bins[[b]]))
            left_cell_pos &lt;- match(left_cell_ID,colnames(bins[[b]]))
            right_cell_logcounts &lt;- assays(bins[[b]])$logcounts[,right_cell_pos]
            left_cell_logcounts &lt;- assays(bins[[b]])$logcounts[,left_cell_pos]
            if(b==1&amp;&amp;iteration==1){
                ref_right_cell_logcounts &lt;- right_cell_logcounts
                ref_left_cell_logcounts &lt;- left_cell_logcounts
            }
            # Figure out whether this pseudotime in this bin has flipped relative to reference (first bin), and if so, flip pseudotime values
            cor_opposite_ends &lt;- mean(cor(right_cell_logcounts,ref_left_cell_logcounts),cor(left_cell_logcounts,ref_right_cell_logcounts))
            cor_same_ends &lt;- mean(cor(right_cell_logcounts,ref_right_cell_logcounts),cor(left_cell_logcounts,ref_left_cell_logcounts))
            flipped &lt;- FALSE
            if(cor_opposite_ends &gt; cor_same_ends){
                flipped &lt;- TRUE
            }
            if(flipped==TRUE){
                # find max pseudotime in colData(bin_with_pseudotime)$slingPseudotime
                # Recalculate pseudotime in bin_with_pseudotime as max-value
                pseudotimes &lt;- colData(bin_with_pseudotime)$slingPseudotime_1
                colData(bin_with_pseudotime)$slingPseudotime_1 &lt;- max(pseudotimes)-pseudotimes 
            }
            cells_with_pseudotime &lt;- as.data.frame(colData(bin_with_pseudotime)$slingPseudotime_1,colnames(bin_with_pseudotime))
            cell_pseudotimes &lt;- rbind(cell_pseudotimes, cells_with_pseudotime)
            cat(".")
        }
        cat("100%\n")

        ps[[iteration]] &lt;- cell_pseudotimes[order(row.names(cell_pseudotimes)), , drop = FALSE]

        pss &lt;- data.frame(ps)
        if(iteration&gt;1){
            convergence10 &lt;- measureConvergence(pss, sce = sce, ptime = 10, increment_size = increment_size)
            convergence50 &lt;- measureConvergence(pss, sce = sce, ptime = 50, increment_size = increment_size)
            convergence99 &lt;- measureConvergence(pss, sce = sce, ptime = 99, increment_size = increment_size)
            cat("   convergence at 10:",convergence10,"... at 50:",convergence50,"...at 100:",convergence99,"\n")
            convergenceArr &lt;- rbind(convergenceArr,c(iteration,convergence10,convergence50,convergence99))
        }
        # Iterate until the model converges towards the actual slingshot solution, within an indicated fraction (i.e. convergence = 0.02)
        # The convergence measure compares how similar the model is to the actual slingshot solution, i.e. a convergence of 0.02 means that cell pseudotime values are within 2% of the actual solution
        # Assess this by selecting a pseudotime at random, analyzing the following increment_size cells, and comparing accuracy of pseudotime measurement across that trajectory with the composite of the course-grained analysis

        # need to study how convergence changes over pseudotime
    }

    # remove cells with really high standard deviations

    return(pss)
#    return(convergenceArr)
}


measureConvergence &lt;- function(mat, sce = sce, ptime = 45, increment_size = 500){
    # this line is throwing an error
    mat[,"avg"] &lt;- apply(mat[,1:dim(mat)[2]],1,mean)
    mat &lt;- mat[order(mat[,"avg"]), , drop = FALSE]
    # select a random pseudotime increment
    mat &lt;- mat[mat[, "avg"] &gt; ptime,]
    mat &lt;- mat[1:increment_size,]
    cell_IDs &lt;- rownames(mat)
    # pull out this list of cell_IDs from the sce object and run slingshot
    sce2 &lt;- suppressMessages(slingshot(sce[,cell_IDs]))
    cpcells &lt;- as.data.frame(colData(sce2)$slingPseudotime_1)
    rownames(cpcells) &lt;- colnames(sce2)
    cpcells &lt;- data.frame(lapply(cpcells, function(x) as.numeric(as.character(x))), check.names = F, row.names = rownames(cpcells))
    # sort cpcells by increasing pseudotime
    cpcells &lt;- cpcells[order(cpcells[,1]), , drop = FALSE]
    ordered_cells &lt;- cbind(rownames(mat),rownames(cpcells))
    # get the index of each cell in the dataframe, run a correlation on how well the indices line up
    cell_indices &lt;- c()
    for(cell in cell_IDs){
        new_indices &lt;- c(which(ordered_cells[,1] == cell), which(ordered_cells[,2] == cell))
        cell_indices &lt;- rbind(cell_indices,new_indices)
    }
    convergence_measure &lt;- cor(cell_indices[,1],cell_indices[,2])
    plot(cell_indices[,1],cell_indices[,2])
    return(convergence_measure)
}

PartitionBins &lt;- function(sce, increment_size = 500, seed = 123){
    bins &lt;- c()
    while(dim(sce)[2]&gt;=increment_size){
        if(dim(sce)[2]&lt;increment_size*2){
            bins &lt;- c(bins, sce)
            sce &lt;- sce[,1]
        }
        else{
            set.seed(seed)
            train_ind &lt;- sample(seq_len(ncol(sce)), size = increment_size)
            bins &lt;- c(bins, sce[,train_ind])
            sce &lt;- sce[,-train_ind]
            cat(".")
        }
    }
    return(bins)
}

FindOptimalIncrementSize &lt;- function(sce){
    # Find the computationally optimal increment_size for grainy alignments using 5 increment points (100, 500, 1000, 2500)
    res &lt;- c(100,250,500,750,1000,1500,2500,5000)
    runtimes &lt;- c()
    for(i in res){
        runtimes &lt;- c(runtimes, suppressMessages(system.time(slingshot(sce[,0:i])))["elapsed"])
    }
    mat &lt;- as.data.frame(t(rbind(res,runtimes)))
    c &lt;- summary(lm(mat$runtimes ~ poly(mat$res, 2, raw=TRUE)))$coefficients[,"Estimate"]
    predicted_runtimes &lt;- c()
    for(i in seq(from=100, to=5000, by=1)){
        predicted_runtime &lt;- (c[1] + c[2]*i + c[3]*i^2)*(dim(sce)[2]/i)
        predicted_runtimes &lt;- rbind(predicted_runtimes,c(i,predicted_runtime))
        cat(".")
    }
    increment_size &lt;- predicted_runtimes[which.min(predicted_runtimes[,2]),1]
    return(increment_size)
}