API Reference¶
This page documents all public functions in microscape. Each function's docstrings are rendered directly from the source.
microscape.filter -- Sequence Table Filtering¶
Quality-control filtering of the ASV sequence table by length, prevalence, abundance, and sample depth.
filter_seqtab¶
filter_seqtab(df, min_length=50, min_samples=2, min_seqs=2, min_reads=1000)
¶
Apply cascading quality filters to a long-format sequence table.
Parameters¶
df : pd.DataFrame
Long-format DataFrame with columns sample, sequence, count.
min_length : int
Minimum ASV sequence length in base-pairs. ASVs shorter than this are
removed because they cannot be reliably assigned taxonomy.
min_samples : int
Minimum number of samples an ASV must appear in to be retained
(prevalence filter). ASVs below this threshold are considered orphans.
min_seqs : int
Minimum total read count for an ASV across all samples (abundance
filter). ASVs below this are considered singletons/doubletons.
min_reads : int
Minimum total reads per sample (depth filter). Samples with fewer
reads are removed as insufficiently sequenced.
Returns¶
dict
"filtered" -- pd.DataFrame of retained observations.
"orphans" -- pd.DataFrame of ASVs removed by the prevalence
filter.
"small_samples" -- pd.DataFrame of samples removed by the depth
filter.
"stats" -- pd.DataFrame summarising each filter step.
Source code in microscape/filter.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
plot_filter_summary¶
plot_filter_summary(df, output=None)
¶
Generate diagnostic rank-abundance plots for a filtered sequence table.
Parameters¶
df : pd.DataFrame
Long-format DataFrame with columns sample, sequence, count.
Typically the "filtered" value returned by :func:filter_seqtab.
output : str or None
Path to a PDF file to save the plot. If None, the matplotlib Figure
is returned instead of being saved.
Returns¶
matplotlib.figure.Figure or None The figure object when output is None; otherwise None (the figure is saved and closed).
Source code in microscape/filter.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
microscape.metadata -- Metadata Loading¶
Load and validate MIMARKS-compliant sample metadata.
load_metadata¶
load_metadata(path, seqtab=None, id_col='sample_name')
¶
Load sample metadata from a TSV or CSV file.
Parameters¶
path : str
Path to the metadata file. Files with .tsv or .txt
extensions are read as tab-separated; everything else as
comma-separated.
seqtab : pd.DataFrame or None
Optional long-format sequence table (columns: sample, sequence,
count). When provided, the returned DataFrame includes additional
_match_status information so users can see which samples exist
only in the pipeline output, only in the metadata, or in both.
id_col : str
Name of the column containing sample identifiers. If this column
is not found, the first column of the file is used instead.
Returns¶
pd.DataFrame
Metadata indexed by sample identifier. The DataFrame has an
attribute match_stats (a dict) summarising the overlap between
the metadata and the sequence table when seqtab is provided.
Source code in microscape/metadata.py
microscape.renormalize -- Taxonomic Renormalization¶
Split ASVs into taxonomic groups and compute within-group proportional abundances.
renormalize¶
renormalize(df, taxa)
¶
Group ASVs by taxonomy and compute within-group proportions.
Parameters¶
df : pd.DataFrame
Long-format count table with columns sample, sequence,
count.
taxa : pd.DataFrame
Taxonomy table indexed by sequence. Expected to contain columns
for at least one of Kingdom/Domain, Order, and Family (case-
insensitive matching is applied).
Returns¶
dict A dictionary with the following keys:
* One key per biological group (e.g. ``"prokaryote"``,
``"eukaryote"``, ``"chloroplast"``, ``"mitochondria"``,
``"unknown"``), each mapping to a long-format pd.DataFrame
with an added ``proportion`` column representing within-group
relative abundance.
* ``"group_assignments"`` -- a pd.DataFrame mapping each sequence
to its assigned group.
* ``"stats"`` -- a pd.DataFrame with per-group summary statistics.
Source code in microscape/renormalize.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
microscape.phylogeny -- Phylogenetic Tree Construction¶
Multiple sequence alignment (MAFFT) and neighbor-joining tree construction.
build_phylogeny¶
build_phylogeny(sequences, output_newick=None, cpus=1, min_coverage=0.6)
¶
Build a multiple sequence alignment and neighbor-joining tree.
Parameters¶
sequences : list of str DNA sequences (ASVs) to align and build into a tree. output_newick : str or None If provided, the Newick tree string is also written to this file path. cpus : int Number of CPU threads to pass to MAFFT. min_coverage : float Minimum fraction of sequences that must have a non-gap character at a given alignment column for it to be retained during trimming.
Returns¶
dict
"tree_newick" -- str, the Newick-format tree.
"distance_matrix" -- np.ndarray, pairwise distance matrix.
"alignment" -- str, trimmed alignment in FASTA format.
"seq_map" -- dict mapping ASV identifiers to original
sequences.
"asv_ids" -- list of ASV identifier strings.
Source code in microscape/phylogeny.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
microscape.ordination -- Ordination¶
Bray-Curtis dissimilarity with t-SNE or PCA dimensionality reduction.
ordinate¶
ordinate(df, method='tsne', metric='braycurtis', perplexity=30)
¶
Compute ordination coordinates for samples and ASVs.
Parameters¶
df : pd.DataFrame
Long-format count table with columns sample, sequence,
count.
method : str
Dimensionality reduction method. Currently supports "tsne"
(PCA followed by t-SNE) and "pca" (PCA only).
metric : str
Distance metric passed to :func:scipy.spatial.distance.pdist.
Common choices: "braycurtis", "euclidean", "jaccard".
perplexity : float
Perplexity parameter for t-SNE. Automatically adjusted downwards
when the number of entities is small.
Returns¶
dict
"sample_coords" -- pd.DataFrame with columns label,
dim1, dim2 for samples.
"asv_coords" -- pd.DataFrame with columns label,
dim1, dim2 for ASVs.
"distances" -- np.ndarray, sample-by-sample distance matrix.
Source code in microscape/ordination.py
microscape.network -- Co-occurrence Networks¶
SparCC-style compositional correlation networks.
sparcc_network¶
sparcc_network(df, min_prevalence=0.1, min_correlation=0.1)
¶
Compute a CLR-based co-occurrence network (SparCC approximation).
Parameters¶
df : pd.DataFrame
Long-format count table with columns sample, sequence,
count.
min_prevalence : float
Minimum fraction of samples in which an ASV must be present to be
included in the network (0 to 1).
min_correlation : float
Minimum absolute Pearson correlation (on CLR-transformed data) for
an edge to be retained in the output.
Returns¶
pd.DataFrame
Edge list with columns node1, node2, correlation,
weight (absolute correlation), and color ("positive" or
"negative"). Sorted by descending weight.
Source code in microscape/network.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
microscape.viz -- Visualization Export¶
Export all analysis results as a JSON bundle for web-based visualization.
export_viz¶
export_viz(results_dir, output_dir)
¶
Convert pipeline outputs to JSON files for the Svelte frontend.
Parameters¶
results_dir : str Directory containing pipeline pickle outputs. Expected files:
* ``seqtab_final.pkl`` -- filtered long-format count table
* ``renorm_merged.pkl`` or ``renorm_by_group.pkl`` -- renormalized data
* ``sample_bray_tsne.pkl`` -- sample t-SNE coordinates
* ``seq_bray_tsne.pkl`` -- ASV t-SNE coordinates
* ``sparcc_correlations.pkl`` -- network edge list
* ``metadata.pkl`` (optional) -- sample metadata
* ``*_taxonomy.pkl`` (optional) -- taxonomy assignments
str
Directory where JSON files will be written. Created if it does not exist.
Notes¶
Produces the following files in output_dir:
samples.json-- sample metadata with t-SNE coordinatesasvs.json.gz-- ASV info with t-SNE coordinates and taxonomycounts.json.gz-- sparse count matrixnetwork.json-- correlation edge listtaxonomy.json-- per-database taxonomy assignmentsrenorm_stats.json-- group-level summary statistics
Source code in microscape/viz.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |