Use this to run the Harmony algorithm on gene expression or PCA matrix.
HarmonyMatrix(data_mat, meta_data, vars_use, do_pca = TRUE, npcs = 20, theta = NULL, lambda = NULL, sigma = 0.1, nclust = NULL, tau = 0, block.size = 0.05, max.iter.harmony = 10, max.iter.cluster = 200, epsilon.cluster = 1e-05, epsilon.harmony = 1e-04, plot_convergence = FALSE, return_object = FALSE, verbose = TRUE, reference_values = NULL, cluster_prior = NULL)
Matrix of normalized gene expession (default) or PCA embeddings (see do_pca). Cells can be rows or columns.
Either (1) Dataframe with variables to integrate or (2) vector with labels.
If meta_data is dataframe, this defined which variable(s) to remove (character vector).
Whether to perform PCA on input matrix.
If doing PCA on input matrix, number of PCs to compute.
Diversity clustering penalty parameter. Specify for each variable in vars_use Default theta=2. theta=0 does not encourage any diversity. Larger values of theta result in more diverse clusters.
Ridge regression penalty parameter. Specify for each variable in vars_use. Default lambda=1. Lambda must be strictly positive. Smaller values result in more aggressive correction.
Width of soft kmeans clusters. Default sigma=0.1. Sigma scales the distance from a cell to cluster centroids. Larger values of sigma result in cells assigned to more clusters. Smaller values of sigma make soft kmeans cluster approach hard clustering.
Number of clusters in model. nclust=1 equivalent to simple linear regression.
Protection against overclustering small datasets with large ones. tau is the expected number of cells per cluster.
What proportion of cells to update during clustering. Between 0 to 1, default 0.05. Larger values may be faster but less accurate
Maximum number of rounds to run Harmony. One round of Harmony involves one clustering and one correction step.
Maximum number of rounds to run clustering at each round of Harmony.
Convergence tolerance for clustering round of Harmony. Set to -Inf to never stop early.
Convergence tolerance for Harmony. Set to -Inf to never stop early.
Whether to print the convergence plot of the clustering objective function. TRUE to plot, FALSE to suppress. This can be useful for debugging.
(Advanced Usage) Whether to return the Harmony object or only the corrected PCA embeddings.
Whether to print progress messages. TRUE to print, FALSE to suppress.
(Advanced Usage) Defines reference dataset(s). Cells that have batch variables values matching reference_values will not be moved.
(Advanced Usage) Provides user defined clusters for cluster initialization. If the number of provided clusters C is less than K, Harmony will initialize K-C clusters with kmeans. C cannot exceed K.
By default, matrix with corrected PCA embeddings. If return_object is TRUE, returns the full Harmony object (R6 reference class type).
## By default, Harmony inputs a normalized gene expression matrix# NOT RUN { harmony_embeddings <- HarmonyMatrix(exprs_matrix, meta_data, 'dataset') # }## Harmony can also take a PCA embeddings matrix data(cell_lines_small) pca_matrix <- cell_lines_small$scaled_pcs meta_data <- cell_lines_small$meta_data harmony_embeddings <- HarmonyMatrix(pca_matrix, meta_data, 'dataset', do_pca=FALSE)#>#>#>#>#>#>#>#>#>#> [1] 300 20#> X1 X2 X3 X4 X5 #> [1,] 0.008448274 -0.0018134832 -1.552202e-03 -0.0077926057 0.0001466930 #> [2,] -0.011292669 0.0003581516 -1.989622e-05 -0.0014619744 0.0034635288 #> [3,] 0.010883023 -0.0050407453 1.579927e-03 -0.0043506676 -0.0006763035 #> [4,] 0.009927078 -0.0014537998 1.159716e-03 -0.0080219594 -0.0014029851 #> [5,] -0.010156191 0.0027586043 -3.074632e-03 -0.0000108491 0.0009655249## Finally, we can return an object with all the underlying data structures harmony_object <- HarmonyMatrix(pca_matrix, meta_data, 'dataset', do_pca=FALSE, return_object=TRUE)#>#>#>#>#>#>#>#>#>#>#> [1] 20 10#> [1] 10 300#> [1] 20 300#> [,1] [,2] [,3] #> [1,] 6.197592 9.983329e+00 8.725975e-05 #> [2,] 11.953170 7.462688e-06 2.536973e+01 #> [3,] 7.495340 1.563652e+01 6.057042e-07 #> [4,] 10.170492 1.752684e+01 7.977053e-07 #> [5,] 10.999486 1.901373e-05 2.325515e+01 #> [6,] 10.060248 1.894319e+01 1.766110e-06