|
| 1 | +import argparse |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import rpy2_api |
| 6 | +from como_utilities import stringlist_to_list |
| 7 | +from project import Config |
| 8 | + |
| 9 | +# read and translate R functions |
| 10 | +configs = Config() |
| 11 | +r_file_path = Path(configs.code_dir, "rscripts", "cluster_samples.R") |
| 12 | + |
| 13 | + |
| 14 | +def main() -> None: |
| 15 | + """ |
| 16 | + Cluster RNA-seq Data |
| 17 | + """ |
| 18 | + |
| 19 | + parser = argparse.ArgumentParser( |
| 20 | + prog="cluster_rnaseq.py", |
| 21 | + description="Cluster RNA-seq Data using Multiple Correspondence Analysis or UMAP. Clusters at the replicate, " |
| 22 | + "batch/study, and context levels.", |
| 23 | + epilog="For additional help, please post questions/issues in the MADRID GitHub repo at " |
| 24 | + "https://github.com/HelikarLab/MADRID or email [email protected]", |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "-n", |
| 28 | + "--context-names", |
| 29 | + type=str, |
| 30 | + required=True, |
| 31 | + dest="context_names", |
| 32 | + help="""Tissue/cell name of models to generate. If making multiple models in a batch, then |
| 33 | + use the format: \"['context1', 'context2', ... etc]\". Note the outer double-quotes and the |
| 34 | + inner single-quotes are required to be interpreted. This a string, not a python list""", |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "-t", |
| 38 | + "--filt-technique", |
| 39 | + type=str, |
| 40 | + required=True, |
| 41 | + dest="technique", |
| 42 | + help="'zfpkm', 'quantile', or 'cpm'", |
| 43 | + ) |
| 44 | + parser.add_argument( |
| 45 | + "-a", |
| 46 | + "--cluster-algorithm", |
| 47 | + type=str, |
| 48 | + required=False, |
| 49 | + default="umap", |
| 50 | + dest="clust_algo", |
| 51 | + help="""Clustering algorithm to use. 'mca' or 'umap'.""", |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "-l", |
| 55 | + "--label", |
| 56 | + type=str, |
| 57 | + required=False, |
| 58 | + default=True, |
| 59 | + dest="label", |
| 60 | + help="""True to label replicate/batch/context names on the plots. May be ugly for large sets""", |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "-d", |
| 64 | + "--min-dist", |
| 65 | + type=float, |
| 66 | + required=False, |
| 67 | + default=0.01, |
| 68 | + dest="min_dist", |
| 69 | + help="""Minimum distance for UMAP clustering. Must be between 0 and 1""", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "-r", |
| 73 | + "--replicate-ratio", |
| 74 | + type=str, |
| 75 | + required=False, |
| 76 | + default=0.9, |
| 77 | + dest="rep_ratio", |
| 78 | + help="""Ratio of genes active in replicates for a batch/study to be active""", |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "-b", |
| 82 | + "--batch-ratio", |
| 83 | + type=str or float, |
| 84 | + required=False, |
| 85 | + default=0.9, |
| 86 | + dest="batch_ratio", |
| 87 | + help="""Ratio of genes active in a batch/study to be active in the context""", |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "-nr", |
| 91 | + "--n-neighbors-rep", |
| 92 | + type=str or float, |
| 93 | + required=False, |
| 94 | + default="default", |
| 95 | + dest="n_neigh_rep", |
| 96 | + help="""N nearest neighbors for replicate clustering, 'default' is total number of replicates""", |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + "-nb", |
| 100 | + "--n-neighbors-batch", |
| 101 | + type=str or float, |
| 102 | + required=False, |
| 103 | + default="default", |
| 104 | + dest="n_neigh_batch", |
| 105 | + help="""N nearest neighbors for batch clustering, 'default' is total number of batches""", |
| 106 | + ) |
| 107 | + parser.add_argument( |
| 108 | + "-nc", |
| 109 | + "--n-neighbors-context", |
| 110 | + type=str or float, |
| 111 | + required=False, |
| 112 | + default="default", |
| 113 | + dest="n_neigh_cont", |
| 114 | + help="""N nearest neighbors for context clustering, 'default' is total number of contexts""", |
| 115 | + ) |
| 116 | + parser.add_argument( |
| 117 | + "-c", |
| 118 | + "--min-count", |
| 119 | + type=str or int, |
| 120 | + required=False, |
| 121 | + default="default", |
| 122 | + dest="min_count", |
| 123 | + help="""Ratio of active genes in a batch/study to be active in the context""", |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + "-q", |
| 127 | + "--quantile", |
| 128 | + type=str or int, |
| 129 | + required=False, |
| 130 | + default=0.5, |
| 131 | + dest="quantile", |
| 132 | + help="""Ratio of active genes in a batch/study to be active in the context""", |
| 133 | + ) |
| 134 | + parser.add_argument( |
| 135 | + "-s", |
| 136 | + "--seed", |
| 137 | + type=int, |
| 138 | + required=False, |
| 139 | + default=-1, |
| 140 | + dest="seed", |
| 141 | + help="""Random seed for clustering algorithm initialization""", |
| 142 | + ) |
| 143 | + args = parser.parse_args() |
| 144 | + |
| 145 | + context_names = stringlist_to_list(args.context_names) |
| 146 | + technique = args.technique.lower() |
| 147 | + clust_algo = args.clust_algo.lower() |
| 148 | + label = args.label |
| 149 | + rep_ratio = args.rep_ratio |
| 150 | + batch_ratio = args.batch_ratio |
| 151 | + min_count = args.min_count |
| 152 | + quantile = args.quantile |
| 153 | + min_dist = args.min_dist |
| 154 | + n_neigh_rep = args.n_neigh_rep |
| 155 | + n_neigh_batch = args.n_neigh_batch |
| 156 | + n_neigh_cont = args.n_neigh_cont |
| 157 | + |
| 158 | + # Set a random seed if none provided |
| 159 | + if int(args.seed) == -1: |
| 160 | + seed = np.random.randint(0, 100000) |
| 161 | + else: |
| 162 | + seed = args.seed |
| 163 | + |
| 164 | + if isinstance(min_count, str) and min_count.lower() == "default": |
| 165 | + try: |
| 166 | + min_count = int(min_count) |
| 167 | + except ValueError: |
| 168 | + raise ValueError("--min-count must be either 'default' or an integer > 0") |
| 169 | + if not isinstance(min_count, str) and min_count < 0: |
| 170 | + raise ValueError("--min-count must be either 'default' or an integer > 0") |
| 171 | + |
| 172 | + if isinstance(quantile, str) and not quantile.lower() == "default": |
| 173 | + try: |
| 174 | + quantile = int(quantile) |
| 175 | + except ValueError: |
| 176 | + raise ValueError("--quantile must be either 'default' or an integer between 0 and 100") |
| 177 | + if not isinstance(quantile, str) and 0 > quantile > 100: |
| 178 | + raise ValueError("--quantile must be either 'default' or an integer between 0 and 100") |
| 179 | + |
| 180 | + if isinstance(rep_ratio, str) and not rep_ratio.lower() == "default": |
| 181 | + try: |
| 182 | + rep_ratio = float(rep_ratio) |
| 183 | + except ValueError: |
| 184 | + raise ValueError("--rep-ratio must be 'default' or a float between 0 and 1") |
| 185 | + if not isinstance(rep_ratio, str) and 0 > rep_ratio > 1.0: |
| 186 | + raise ValueError("--rep-ratio must be 'default' or a float between 0 and 1") |
| 187 | + |
| 188 | + if isinstance(batch_ratio, str) and not batch_ratio.lower() == "default": |
| 189 | + try: |
| 190 | + batch_ratio = float(batch_ratio) |
| 191 | + except ValueError: |
| 192 | + raise ValueError("--batch-ratio must be 'default' or a float between 0 and 1") |
| 193 | + if not isinstance(batch_ratio, str) and 0 > batch_ratio > 1.0: |
| 194 | + raise ValueError("--batch-ratio must be 'default' or a float between 0 and 1") |
| 195 | + |
| 196 | + if technique.lower() not in ["quantile", "tpm", "cpm", "zfpkm"]: |
| 197 | + raise ValueError("--technique must be either 'quantile', 'tpm', 'cpm', 'zfpkm'") |
| 198 | + |
| 199 | + if technique.lower() == "tpm": |
| 200 | + technique = "quantile" |
| 201 | + |
| 202 | + if clust_algo.lower() not in ["mca", "umap"]: |
| 203 | + raise ValueError("--clust_algo must be either 'mca', 'umap'") |
| 204 | + |
| 205 | + if not isinstance(min_dist, str) and 0 > min_dist > 1.0: |
| 206 | + raise ValueError("--min_dist must be a float between 0 and 1") |
| 207 | + |
| 208 | + if isinstance(n_neigh_rep, str) and not n_neigh_rep.lower() == "default": |
| 209 | + try: |
| 210 | + n_neigh_rep = int(n_neigh_rep) |
| 211 | + except ValueError: |
| 212 | + raise ValueError( |
| 213 | + "--n_neigh_rep must be either 'default' or an integer greater than 1 and less than or equal to " |
| 214 | + "the total number of replicates being clustered across all contexts." |
| 215 | + ) |
| 216 | + if not isinstance(n_neigh_rep, str) and n_neigh_rep < 2: |
| 217 | + raise ValueError( |
| 218 | + "--n_neigh_rep must be either 'default' or an integer greater than 1 and less than or equal to " |
| 219 | + "the total number of replicates being clustered across all contexts." |
| 220 | + ) |
| 221 | + |
| 222 | + if isinstance(n_neigh_batch, str) and not n_neigh_batch.lower() == "default": |
| 223 | + try: |
| 224 | + n_neigh_batch = int(n_neigh_batch) |
| 225 | + except ValueError: |
| 226 | + raise ValueError( |
| 227 | + "--n_neigh_batch must be either 'default' or an integer greater than 1 and less than or equal to " |
| 228 | + "the total number of batches being clustered across all contexts." |
| 229 | + ) |
| 230 | + if not isinstance(n_neigh_batch, str) and n_neigh_batch < 2: |
| 231 | + raise ValueError( |
| 232 | + "--n_neigh_batch must be either 'default' or an integer greater than 1 and less than or equal to " |
| 233 | + "the total number of batches being clustered across all contexts." |
| 234 | + ) |
| 235 | + |
| 236 | + if isinstance(n_neigh_cont, str) and not n_neigh_cont.lower() == "default": |
| 237 | + try: |
| 238 | + n_neigh_cont = int(n_neigh_cont) |
| 239 | + except ValueError: |
| 240 | + raise ValueError( |
| 241 | + "--n_neigh_batch must be either 'default' or an integer greater than 1 and less than or equal to " |
| 242 | + "the total number of batches being clustered across all contexts." |
| 243 | + ) |
| 244 | + if not isinstance(n_neigh_cont, str) and n_neigh_cont < 2: |
| 245 | + raise ValueError( |
| 246 | + "--n_neigh_context must be either 'default' or an integer greater than 1 and less than or equal to " |
| 247 | + "the total number of contexts being clustered." |
| 248 | + ) |
| 249 | + |
| 250 | + cluster_samples = rpy2_api.Rpy2( |
| 251 | + r_file_path=r_file_path, |
| 252 | + wd=configs.result_dir, |
| 253 | + context_names=context_names, |
| 254 | + technique=technique, |
| 255 | + clust_algo=clust_algo, |
| 256 | + label=label, |
| 257 | + min_dist=min_dist, |
| 258 | + n_neigh_rep=n_neigh_rep, |
| 259 | + n_neigh_batch=n_neigh_batch, |
| 260 | + n_neigh_cont=n_neigh_cont, |
| 261 | + rep_ratio=rep_ratio, |
| 262 | + batch_ratio=batch_ratio, |
| 263 | + quantile=quantile, |
| 264 | + min_count=min_count, |
| 265 | + seed=seed, |
| 266 | + ) |
| 267 | + cluster_samples.call_function("cluster_samples_main") |
| 268 | + |
| 269 | + |
| 270 | +if __name__ == "__main__": |
| 271 | + main() |
0 commit comments