### Basic LEfSe Workflow: Data Formatting to Visualization Source: https://context7.com/segatalab/lefse/llms.txt Complete LEfSe workflow demonstrating biomarker discovery from raw data to publication-ready visualizations. Uses example data with oxygen availability as class and body site as subclass. Outputs include LDA bar plots, taxonomic cladograms, and individual feature abundance plots. Requires LEfSe installation and Python dependencies. ```bash # Finding biomarkers for oxygen availability across body sites # Step 1: Download example data wget https://github.com/biobakery/biobakery/raw/master/test_suite/biobakery_tests/data/lefse/input/hmp_small_aerobiosis.txt # Step 2: Format input data # -c 1: oxygen availability (aerobic/anaerobic) as class # -s 2: body site (gut/oral/skin) as subclass # -u 3: subject ID # -o 1000000: normalize to 1 million lefse_format_input.py hmp_small_aerobiosis.txt hmp_aerobiosis.in \ -c 1 -s 2 -u 3 -o 1000000 # Step 3: Run LEfSe statistical analysis # -a 0.05: Kruskal-Wallis alpha # -w 0.05: Wilcoxon alpha # -l 2.0: LDA threshold # -b 30: bootstrap iterations lefse_run.py hmp_aerobiosis.in hmp_aerobiosis.res \ -a 0.05 -w 0.05 -l 2.0 -b 30 # Step 4: Create LDA bar plot lefse_plot_res.py hmp_aerobiosis.res hmp_aerobiosis_lda.png \ --format png --dpi 300 --width 10 --height 6 # Step 5: Create taxonomic cladogram lefse_plot_cladogram.py hmp_aerobiosis.res hmp_aerobiosis_cladogram.svg \ --format svg --clade_sep 1.5 --title "HMP Aerobiosis Biomarkers" # Step 6: Create individual feature plots mkdir biomarker_plots lefse_plot_features.py hmp_aerobiosis.in hmp_aerobiosis.res biomarker_plots/ \ -f diff --format pdf --archive zip # Results: # - hmp_aerobiosis.in: formatted data (pickle) # - hmp_aerobiosis.res: significant biomarkers with LDA scores # - hmp_aerobiosis_lda.png: bar plot of effect sizes # - hmp_aerobiosis_cladogram.svg: circular taxonomic tree # - biomarker_plots/: individual abundance plots # - biomarker_plots.zip: archive of all plots ``` -------------------------------- ### Running LEfSe with Bootstrap and Wilcoxon Options Source: https://context7.com/segatalab/lefse/llms.txt These bash commands execute LEfSe analysis on input data, adjusting bootstrap iterations for robust LDA estimation, skipping Wilcoxon tests, and enabling verbose output. Purpose is to perform biomarker discovery with customizable statistical rigor. Inputs: formatted data file (e.g., data.in); Outputs: results file (e.g., results.res); Dependencies: LEfSe installed; Limitations: Requires pre-formatted input, default alpha=0.05. ```bash # Increase bootstrap iterations for more robust LDA estimation # -b: number of bootstrap iterations (default 30) # -f: fraction of samples to use in each bootstrap (default 0.67) lefse_run.py data.in results.res -b 100 -f 0.75 # Skip Wilcoxon test (only use Kruskal-Wallis) lefse_run.py data.in results.res --wilc 0 # Verbose output showing each feature tested lefse_run.py data.in results.res --verbose 1 ``` -------------------------------- ### QIIME Data Conversion and LEfSe Analysis Pipeline Source: https://context7.com/segatalab/lefse/llms.txt Complete pipeline converting QIIME OTU tables and metadata to LEfSe format for biomarker discovery. Uses qiime2lefse.py for data conversion and standard LEfSe workflow for analysis. Outputs publication-ready visualizations including LDA plots and cladograms. Requires QIIME and LEfSe installations with proper metadata mapping. ```bash #!/bin/bash # Convert and analyze QIIME data # Step 1: Convert QIIME OTU table to LEfSe format qiime2lefse.py \ --in otu_table.txt \ --md sample_metadata.txt \ --out lefse_formatted.txt \ -c treatment \ -s timepoint \ -u subject_id # Step 2: Format for LEfSe (add normalization) lefse_format_input.py lefse_formatted.txt lefse_data.in \ -c 1 -s 2 -u 3 -o 1000000 # Step 3: Run analysis lefse_run.py lefse_data.in results.res -a 0.05 -w 0.05 -l 2.0 # Step 4: Generate all visualizations lefse_plot_res.py results.res lda_plot.png lefse_plot_cladogram.py results.res cladogram.png --format png lefse_plot_features.py lefse_data.in results.res feature_plots/ ``` -------------------------------- ### Initializing LEfSe Python API and Loading Data Source: https://context7.com/segatalab/lefse/llms.txt Python code to initialize the LEfSe environment and load formatted data from a pickle or input file. Purpose: Set up R integration and access feature abundances, classes, and hierarchies. Inputs: input file (e.g., hmp_aerobiosis.in); Outputs: Dictionaries for feats, cls, etc.; Dependencies: lefse module, R libraries; Limitations: Assumes pre-formatted data, prints structure info. ```python #!/usr/bin/env python3 import sys import pickle from lefse.lefse import * # Initialize R environment with required libraries init() # Load formatted data from pickle file feats, cls, class_sl, subclass_sl, class_hierarchy = load_data("hmp_aerobiosis.in") # Display loaded data structure print(f"Number of features: {len(feats)}") print(f"Number of classes: {len(cls)}") print(f"Classes: {list(cls.keys())}") print(f"First feature name: {list(feats.keys())[0]}") print(f"Feature values shape: {len(list(feats.values())[0])}") # Output: # Number of features: 127 # Number of classes: 2 # Classes: ['aerobic', 'anaerobic'] # First feature name: Bacteria # Feature values shape: 45 ``` -------------------------------- ### Run Complete LEfSe Analysis (Python) Source: https://context7.com/segatalab/lefse/llms.txt This script outlines the complete LEfSe analysis pipeline. It loads data, performs Kruskal-Wallis and Wilcoxon tests, calculates LDA effect sizes, and saves the results. The function `run_complete_lefse_analysis` takes input and output file paths, alpha levels, LDA threshold, and bootstrap iterations as arguments. ```Python #!/usr/bin/env python3 from lefse.lefse import * import sys def run_complete_lefse_analysis(input_file, output_file, alpha_kw=0.05, alpha_wilc=0.05, lda_threshold=2.0, n_boots=30): """ Complete LEfSe analysis pipeline Args: input_file: formatted .in file path output_file: results output path alpha_kw: Kruskal-Wallis alpha alpha_wilc: Wilcoxon alpha lda_threshold: LDA score threshold n_boots: bootstrap iterations Returns: Dictionary with results """ # Initialize R environment init() # Load data print("Loading data...") feats, cls, class_sl, subclass_sl, class_hierarchy = load_data(input_file) # Calculate class means print("Calculating class means...") kord, cls_means = get_class_means(class_sl, feats) # Kruskal-Wallis test print(f"Testing {len(feats)} features with Kruskal-Wallis...") kw_passed = {} kw_pvalues = {} for fname, fvals in feats.items(): is_sig, pval = test_kw_r(cls, fvals, alpha_kw, sorted(cls.keys())) kw_pvalues[fname] = pval if is_sig: kw_passed[fname] = fvals print(f" {len(kw_passed)} features passed Kruskal-Wallis") # Wilcoxon test print("Performing pairwise Wilcoxon tests...") wilc_passed = {} for fname, fvals in kw_passed.items(): # Simplified - full version uses test_rep_wilcoxon_r res = test_rep_wilcoxon_r( subclass_sl, class_hierarchy, fvals, alpha_wilc, 0, 0, fname, 10, 0, False ) if res: wilc_passed[fname] = fvals print(f" {len(wilc_passed)} features passed Wilcoxon") # LDA analysis if len(wilc_passed) > 0: print("Computing LDA effect sizes...") lda_res, lda_res_th = test_lda_r( cls, wilc_passed, class_sl, n_boots, 0.67, lda_threshold, 1e-10, 3 ) print(f" {len(lda_res_th)} features with LDA > {lda_threshold}") else: print("No features passed filtering") lda_res, lda_res_th = {}, {} # Save results results = { 'lda_res_th': lda_res_th, 'lda_res': lda_res, 'cls_means': cls_means, 'cls_means_kord': kord, 'wilcoxon_res': kw_pvalues } save_res(results, output_file) print(f"Results saved to {output_file}") return results # Run analysis if __name__ == '__main__': results = run_complete_lefse_analysis( 'hmp_aerobiosis.in', 'results.res', alpha_kw=0.05, alpha_wilc=0.05, lda_threshold=2.0, n_boots=30 ) print(f"\nFinal biomarkers: {len(results['lda_res_th'])}") for feat in results['lda_res_th']: print(f" {feat}") ``` -------------------------------- ### Load PCL format abundance files Source: https://context7.com/segatalab/lefse/llms.txt Load and process tab-delimited PCL (text) format abundance files with metadata detection and occurrence filtering. Automatically distinguishes metadata rows from feature rows based on column headers. Supports filtering features by minimum abundance thresholds. ```python from lefsebiom.AbundanceTable import AbundanceTable # Load tab-delimited PCL file # First row: sample IDs # First column: feature names # Remaining cells: abundance values abundance_table = AbundanceTable.funcMakeFromFile( xInputFile='abundance.pcl', cDelimiter='\t', sMetadataID='ID', # first column header sLastMetadata='sample1', # last metadata column before data strFormat='txt' ) # The table automatically detects metadata rows vs. feature rows samples = abundance_table.funcGetSampleNames() features = abundance_table.funcGetFeatureNames() print(f"Loaded {len(features)} features across {len(samples)} samples") # Filter by occurrence # Keep features with abundance >= 5.0 in at least 3 samples abundance_table = AbundanceTable.funcMakeFromFile( xInputFile='abundance.pcl', cDelimiter='\t', strFormat='txt', lOccurenceFilter=[5.0, 3.0] ) features_filtered = abundance_table.funcGetFeatureNames() print(f"After filtering: {len(features_filtered)} features remain") ``` -------------------------------- ### BIOM Format Analysis with Stringent Parameters Source: https://context7.com/segatalab/lefse/llms.txt LEfSe analysis workflow for BIOM-formatted microbiome data with strict statistical parameters. Uses disease status and body site metadata from BIOM file headers. Implements bootstrap resampling and more conservative significance thresholds. Suitable for large microbiome datasets requiring robust statistical validation. ```bash #!/bin/bash # Analyze BIOM-format microbiome data # Step 1: Format BIOM file # Specify metadata fields for class and subclass lefse_format_input.py microbiome.biom microbiome.in \ -biom_c disease_status \ -biom_s body_site \ -o 1000000 # Step 2: Run LEfSe with stringent parameters # More strict settings for large datasets lefse_run.py microbiome.in microbiome.res \ -a 0.01 \ -w 0.01 \ -l 3.0 \ -b 100 \ -y 1 \ -s 1 \ --min_c 15 # Step 3: Visualize results lefse_plot_res.py microbiome.res results.pdf \ --format pdf --dpi 300 --otu_only lefse_plot_cladogram.py microbiome.res cladogram.pdf \ --format pdf --dpi 300 --max_lev 6 ``` -------------------------------- ### Process hierarchical taxonomic data with CClade Source: https://context7.com/segatalab/lefse/llms.txt Work with hierarchical taxonomic data using CClade for tree structure operations. Supports creating taxonomic trees from hierarchical feature names, imputing parent abundances from children, and extracting features at specific taxonomic levels. Handles Kingdom|Phylum|Class|Order|Family format. ```python from lefsebiom.CClade import CClade # Create taxonomic tree from hierarchical features root = CClade() # Add features with hierarchical names # Format: Kingdom|Phylum|Class|Order|Family features = { 'Bacteria': [1000, 1200, 900], 'Bacteria|Firmicutes': [450, 520, 380], 'Bacteria|Firmicutes|Clostridia': [250, 290, 210], 'Bacteria|Bacteroidetes': [350, 420, 310], 'Bacteria|Proteobacteria': [200, 260, 210] } # Build tree structure for feature_name, abundances in features.items(): # Split hierarchical name taxonomy = feature_name.split('|') # Navigate/create nodes clade = root.get(taxonomy) # Set abundance values clade.set(abundances) # Impute parent abundances from children # Parents should equal sum of children root.impute() # Extract features at specific taxonomic level # iTarget=2 gets Class level (0=root, 1=Kingdom, 2=Phylum, 3=Class, etc.) phylum_level = {} root.freeze(phylum_level, iTarget=2, fLeaves=False) print("Phylum-level features:") for name, values in phylum_level.items(): print(f" {name}: {values}") ``` -------------------------------- ### Process BIOM format files with LEfSe Source: https://context7.com/segatalab/lefse/llms.txt Load and process BIOM format microbiome data files with occurrence filtering and normalization. Supports loading BIOM files with metadata, accessing sample and feature information, and normalizing abundance data. Requires lefsebiom library. ```python #!/usr/bin/env python3 from lefsebiom.AbundanceTable import AbundanceTable # Load BIOM file with occurrence filtering abundance_table = AbundanceTable.funcMakeFromFile( xInputFile='microbiome.biom', cDelimiter='\t', sMetadataID=None, sLastMetadata=None, lOccurenceFilter=[2.0, 2.0], # min abundance 2.0, in min 2 samples strFormat='biom' ) # Access sample information sample_names = abundance_table.funcGetSampleNames() print(f"Samples: {len(sample_names)}") print(f"Sample names: {sample_names[:5]}") # Access feature information feature_names = abundance_table.funcGetFeatureNames() print(f"\nFeatures: {len(feature_names)}") print(f"First features: {feature_names[:5]}") # Get metadata metadata = abundance_table.funcGetMetadata() print(f"\nMetadata keys: {list(metadata.funcGetMetadataCopy().keys())[:5]}") # Get abundance data abundances = abundance_table.funcGetAbundanceCopy() print(f"\nAbundance matrix shape: {abundances.shape}") # Normalize the table abundance_table.funcNormalize() normalized = abundance_table.funcGetAbundanceCopy() print(f"Sum after normalization: {normalized.sum(axis=0)[0]}") ``` -------------------------------- ### Convert LEfSe results to Circlader format Source: https://context7.com/segatalab/lefse/llms.txt Converts LEfSe analysis results to Circlader input format for circular visualizations. Takes a LEfSe results file and outputs a tab-delimited file with feature names, abbreviated labels, and class assignments. Useful for creating circular visualization inputs. ```bash # Convert LEfSe results to Circlader format lefse2circlader.py results.res circlader_input.txt # The output is a simple tab-delimited format: # feature_name abbreviated_name class_assignment ``` -------------------------------- ### Using SVM for Effect Size Calculation in LEfSe Source: https://context7.com/segatalab/lefse/llms.txt Bash command to replace LDA with SVM for ranking features in LEfSe analysis. Purpose: Alternative machine learning approach for effect sizes, with data normalization. Inputs: data file; Outputs: results.res; Dependencies: LEfSe with SVM support; Limitations: SVM may perform differently on sparse microbiome data. ```bash # Use Support Vector Machine for effect size calculation # -r svm: use SVM instead of LDA # --svm_norm 1: normalize data for SVM (recommended) lefse_run.py data.in results.res -r svm --svm_norm 1 ``` -------------------------------- ### Generate LEfSe Plots (Bash) Source: https://context7.com/segatalab/lefse/llms.txt These bash commands demonstrate how to generate different types of LEfSe plots using `lefse_plot_res.py`. Options include specifying output format (PNG, PDF, SVG), DPI, plot dimensions, orientation, and reporting features to stdout. ```Bash # Generate PNG bar plot with default settings lefse_plot_res.py results.res lda_scores.png # Generate high-resolution PDF for publication lefse_plot_res.py results.res lda_scores.pdf --format pdf --dpi 300 # Generate SVG (vector graphics) lefse_plot_res.py results.res lda_scores.svg --format svg # Horizontal bar plot with custom size lefse_plot_res.py results.res plot.png \ --format png \ --dpi 150 \ --width 10.0 \ --height 6.0 \ --orientation h ``` ```Bash # Vertical orientation instead of horizontal lefse_plot_res.py results.res vertical_plot.png --orientation v # Adjust font size for feature labels lefse_plot_res.py results.res plot.png --feature_font_size 10 # White background instead of transparent lefse_plot_res.py results.res plot.png --background_color w # Black background lefse_plot_res.py results.res plot.png --background_color k # Show only leaf-level features (OTUs) lefse_plot_res.py results.res otu_only.png --otu_only # Display specific number of taxonomic levels # --subclades 1: show only deepest level # --subclades -1: show all levels lefse_plot_res.py results.res plot.png --subclades 2 ``` ```Bash # Print feature names to console and save plot lefse_plot_res.py results.res plot.png --report_features # Output can be redirected to file lefse_plot_res.py results.res plot.png --report_features > biomarkers.txt # Example output: # Bacteria.Firmicutes.Clostridia 4.123 # Bacteria.Bacteroidetes.Bacteroidia 3.892 ``` -------------------------------- ### Advanced Multiclass Analysis Options in LEfSe Source: https://context7.com/segatalab/lefse/llms.txt Bash commands for multiclass strategies in LEfSe, including one-vs-one comparisons, multiple testing corrections, and minimum sample adjustments. Purpose: Handle complex class structures for stricter or broader biomarker detection. Inputs: data file; Outputs: results with adjusted p-values; Dependencies: LEfSe tool; Limitations: Higher min_c may reduce detections in small datasets. ```bash # One-against-one comparison strategy (more strict) # Tests each class pair independently # -y 1: one-vs-one strategy lefse_run.py data.in results.res -y 1 # One-against-all comparison (less strict, default) # -y 0: one-vs-all strategy lefse_run.py data.in results.res -y 0 # Multiple testing correction for independent comparisons # -s 0: no correction (default, more features detected) # -s 1: correction for independent comparisons # -s 2: correction for dependent comparisons lefse_run.py data.in results.res -s 1 # Adjust minimum samples required per subclass for testing # --min_c: minimum sample count (default 10) lefse_run.py data.in results.res --min_c 15 ``` -------------------------------- ### Performing Kruskal-Wallis Statistical Tests in LEfSe Python API Source: https://context7.com/segatalab/lefse/llms.txt Python code to test features for significance using Kruskal-Wallis in LEfSe. Purpose: Identify differentially abundant features across classes. Inputs: feats dict, cls dict; Outputs: Significant features and p-values; Dependencies: lefse module, numpy; Limitations: Uses alpha=0.05 threshold, single test per feature. ```python from lefse.lefse import * import numpy as np # Initialize init() # Load data feats, cls, class_sl, subclass_sl, class_hierarchy = load_data("data.in") # Test single feature with Kruskal-Wallis feature_name = 'Bacteria.Firmicutes.Clostridia' feature_values = feats[feature_name] # Perform Kruskal-Wallis test # Returns: (is_significant, p_value) is_significant, p_value = test_kw_r( cls, # class dictionary feature_values, # feature abundance values 0.05, # alpha threshold sorted(cls.keys()) # list of class names ) print(f"Feature: {feature_name}") print(f"Significant: {is_significant}") print(f"P-value: {p_value:.6f}") # Test all features and collect passing ones significant_features = {} for fname, fvals in feats.items(): is_sig, pval = test_kw_r(cls, fvals, 0.05, sorted(cls.keys())) if is_sig: significant_features[fname] = fvals print(f"{fname}: p={pval:.6f}") print(f"\nTotal significant features: {len(significant_features)}") # Output: # Feature: Bacteria.Firmicutes.Clostridia # Significant: True # P-value: 0.003421 # ... # Total significant features: 34 ``` -------------------------------- ### Calculating LDA Effect Sizes in LEfSe Python API Source: https://context7.com/segatalab/lefse/llms.txt Python code to filter significant features and compute LDA scores with bootstrapping. Purpose: Rank biomarkers by effect size after statistical filtering. Inputs: feats, cls from load_data; Outputs: lda_res dict with scores; Dependencies: lefse module; Limitations: Bootstrapping (default 30) adds computation time, threshold=2.0 filters results. ```python from lefse.lefse import * # Initialize and load init() feats, cls, class_sl, subclass_sl, class_hierarchy = load_data("data.in") # Filter to significant features first (Kruskal-Wallis) significant_feats = {} for fname, fvals in feats.items(): is_sig, pval = test_kw_r(cls, fvals, 0.05, sorted(cls.keys())) if is_sig: significant_feats[fname] = fvals # Perform LDA with bootstrapping lda_res, lda_res_th = test_lda_r( cls, # class dictionary significant_feats, # filtered feature dictionary class_sl, # class slices boots=30, # bootstrap iterations fract_sample=0.67, # subsample fraction per bootstrap lda_th=2.0, # LDA score threshold (log10) tol_min=1e-10, # tolerance for LDA nlogs=3 # max log influence ) # lda_res: all features with LDA scores # lda_res_th: only features passing threshold print(f"Features tested: {len(lda_res)}") print(f"Features with LDA > 2.0: {len(lda_res_th)}") print("\nTop biomarkers:") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.