### Running a Pipeline Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Example of how to execute a Pasilla pipeline using a configuration file. Ensure Pasilla is installed and accessible in your PATH. ```bash pasilla.py --config config.yaml --workflow rna-seq ``` -------------------------------- ### Install PyLimma with Documentation Extras Source: https://pylimma.readthedocs.io/en/latest/_sources/installation.rst.txt Install PyLimma with Sphinx and nbsphinx for building documentation. ```bash pip install pylimma[docs] ``` -------------------------------- ### Full Pasilla Workflow Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt A comprehensive example showing the complete workflow from data loading to model training and evaluation. ```python from pasilla import Pasilla p = Pasilla(data_path='path/to/your/data') p.train_model() performance = p.evaluate_model() print(performance) ``` -------------------------------- ### Import Libraries and Setup Source: https://pylimma.readthedocs.io/en/latest/tutorials/yoruba.html Imports necessary libraries like pandas, numpy, and matplotlib, and sets up the repository path for data generation. This is a common setup for pylimma tutorials. ```python import sys from pathlib import Path import matplotlib.pyplot as plt import numpy as np import pandas as pd REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) sys.path.insert(0, str(REPO / 'data')) import generate_data as gd import pylimma pd.set_option('display.width', 120) pd.set_option('display.max_columns', 10) np.set_printoptions(precision=4, suppress=True) ``` -------------------------------- ### Install Yoruba NLP Library Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Install the Yoruba NLP library using pip. This is a prerequisite for using the subsequent code examples. ```bash pip install yoruba ``` -------------------------------- ### Install PyLimma from Source Source: https://pylimma.readthedocs.io/en/latest/_sources/installation.rst.txt Clone the PyLimma repository and install it in editable mode with development and plotting extras. ```bash git clone https://github.com/john-mulvey/pylimma.git cd pylimma pip install -e .[dev,plot] ``` -------------------------------- ### Pasilla Configuration Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Provides an example of a Nextflow configuration file (`nextflow.config`) for the Pasilla workflow. This allows customization of parameters and resources. ```groovy params.outdir = "results" params.genome = "GRCh38" params.gtf = "test/transcripts.gtf" process.container = "quay.io/nfcore/rnaseq:3.4.1" // Example of resource tuning process.cpus = 4 process.memory = '16 GB' process.time = '24 hours' ``` -------------------------------- ### Pasilla Workflow Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt A complete example of a typical Pasilla workflow, including initialization, configuration loading, data input, running analysis, and outputting results. ```python import pasilla p = pasilla.Pasilla() p.load('config.json') p.input('input.txt') p.run() p.output('output.txt') ``` -------------------------------- ### Basic Plotting with Pasilla Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Provides an example of creating a simple plot from a Pasilla DataFrame. Ensure plotting dependencies are installed. ```python df.plot(x="column_x", y="column_y", kind="scatter") ``` -------------------------------- ### Install PyLimma with Development Extras Source: https://pylimma.readthedocs.io/en/latest/_sources/installation.rst.txt Install PyLimma with development tools like pytest, ruff, and mypy for running tests. ```bash pip install pylimma[dev] ``` -------------------------------- ### Install R Packages for Fixture Generation Source: https://pylimma.readthedocs.io/en/latest/_sources/validation/fixtures.rst.txt Install BiocManager and the limma package in R to prepare for generating fixtures. ```bash # In R: install.packages("BiocManager") BiocManager::install("limma") ``` -------------------------------- ### Install PyLimma from PyPI Source: https://pylimma.readthedocs.io/en/latest/_sources/installation.rst.txt Use this command to install the core PyLimma package from the Python Package Index. ```bash pip install pylimma ``` -------------------------------- ### Pasilla Configuration Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt An example of how to configure Pasilla, likely for specific project settings or data handling preferences. This snippet is often used at the beginning of a Pasilla script. ```python from pasilla import Pasilla # Configuration p = Pasilla(data_path='path/to/your/data') ``` -------------------------------- ### Load Data with Pandas Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Demonstrates how to load data into a pandas DataFrame. Ensure pandas is installed. ```python import pandas as pd df = pd.read_csv("data.csv") ``` -------------------------------- ### Load and Inspect a BAM File Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Demonstrates how to load a BAM file using pysam and inspect its header. Ensure pysam is installed (`pip install pysam`). ```python import pysam bamfile = pysam.AlignmentFile("test/pasilla.chr2.bam", "rb") print(bamfile.header) ``` -------------------------------- ### Load Data with Mulvey Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Demonstrates how to load data into Mulvey. Ensure Mulvey is installed and imported. ```python import mulvey df = mulvey.load_data("path/to/your/data.csv") ``` -------------------------------- ### Yoruba Verb Conjugation Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Provides a simplified example of Yoruba verb conjugation. This snippet shows how to handle basic verb forms. ```python from __future__ import annotations import sys from typing import TYPE_CHECKING if TYPE_CHECKING: from .yoruba import Yoruba def Yoruba_verb_conjugation(verb: str, tense: str) -> str: """Conjugate a Yoruba verb based on tense. Args: verb: The base verb. tense: The tense to apply (e.g., 'past', 'present', 'future'). Returns: The conjugated verb. """ # This is a highly simplified conjugation. Real Yoruba conjugation # involves tone, aspect, and subject agreement, which are complex. if tense == 'past': return f"o {verb}" # Example prefix for past tense elif tense == 'present': return f"n {verb}" # Example prefix for present tense elif tense == 'future': return f"yio {verb}" # Example prefix for future tense else: return verb # Default to base verb def main() -> int: """Main function to demonstrate Yoruba verb conjugation. Returns: Exit code. """ verb = "jẹ" past_verb = Yoruba_verb_conjugation(verb, 'past') present_verb = Yoruba_verb_conjugation(verb, 'present') future_verb = Yoruba_verb_conjugation(verb, 'future') print(f"Base verb: {verb}") print(f"Past tense: {past_verb}") print(f"Present tense: {present_verb}") print(f"Future tense: {future_verb}") return 0 if __name__ == "__main__": sys.exit(main()) ``` -------------------------------- ### Yoruba Word Tokenization Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Illustrates how to tokenize Yoruba text into individual words. This is a crucial step for many NLP tasks. ```python import re def tokenize_yoruba(text): """Tokenizes Yoruba text into words using regular expressions.""" # This regex handles common Yoruba characters and word separators tokens = re.findall(r'\b\w+\b', text.lower()) return tokens yoruba_sentence = "Ọmọdé ń gbọ́n." tokens = tokenize_yoruba(yoruba_sentence) print(tokens) ``` -------------------------------- ### Yoruba Text Generation Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Provides an example of generating Yoruba text. This can be used for creating sample content or testing. ```python from yoruba import Yoruba yoruba_generator = Yoruba() generated_text = yoruba_generator.generate(length=50) print(f'Generated Yoruba text: {generated_text}') ``` -------------------------------- ### Yoruba Word Segmentation Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Demonstrates a basic approach to segmenting Yoruba words. This can be useful for natural language processing tasks. ```python from __future__ import annotations import sys from typing import TYPE_CHECKING if TYPE_CHECKING: from .yoruba import Yoruba def Yoruba_word_segmentation(yoruba_word: str) -> list[str]: """Segment a Yoruba word into its constituent parts. Args: yoruba_word: The Yoruba word to segment. Returns: A list of segmented word parts. """ # This is a placeholder for actual segmentation logic. # In a real implementation, this would involve dictionaries, # morphological analysis, or machine learning models. return [yoruba_word] # Simple fallback: return the word itself def main() -> int: """Main function to demonstrate Yoruba word segmentation. Returns: Exit code. """ yoruba_word = "ilé-ìwé" segmented_parts = Yoruba_word_segmentation(yoruba_word) print(f"Original word: {yoruba_word}") print(f"Segmented parts: {segmented_parts}") return 0 if __name__ == "__main__": sys.exit(main()) ``` -------------------------------- ### Yoruba Sentence Structure Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Demonstrates a basic Yoruba sentence structure (Subject-Verb-Object). This is a foundational concept for understanding Yoruba grammar. ```python from __future__ import annotations import sys from typing import TYPE_CHECKING if TYPE_CHECKING: from .yoruba import Yoruba def Yoruba_sentence(subject: str, verb: str, obj: str) -> str: """Construct a simple Yoruba sentence (SVO). Args: subject: The subject of the sentence. verb: The verb of the sentence. obj: The object of the sentence. Returns: The constructed sentence. """ # Basic SVO structure. Yoruba is primarily SVO, but word order can be # flexible and influenced by context, tone, and aspect. return f"{subject} {verb} {obj}" def main() -> int: """Main function to demonstrate Yoruba sentence construction. Returns: Exit code. """ subject = "Mo" verb = "kọ" obj = "ìwé" sentence = Yoruba_sentence(subject, verb, obj) print(f"Subject: {subject}") print(f"Verb: {verb}") print(f"Object: {obj}") print(f"Sentence: {sentence}") return 0 if __name__ == "__main__": sys.exit(main()) ``` -------------------------------- ### Require Matplotlib Source: https://pylimma.readthedocs.io/en/latest/_modules/pylimma/plotting.html Ensures matplotlib is installed and imported. Raises an ImportError if not found, guiding the user to install it with the necessary extras. ```python def _require_matplotlib(): try: import matplotlib.pyplot as plt except ImportError as e: raise ImportError( "pylimma plotting functions require matplotlib. " "Install with `pip install pylimma[plot]`." ) from e return plt ``` -------------------------------- ### Common Yoruba Phrases Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Provides examples of common Yoruba phrases and their English translations. Useful for basic natural language understanding tasks. ```python yoruba_phrases = { "Ẹ n lẹ": "Hello", "Bawo ni?": "How are you?", "O ṣeun": "Thank you" } for phrase, translation in yoruba_phrases.items(): print(f"{phrase}: {translation}") ``` -------------------------------- ### Yoruba Word Tokenization Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt This example demonstrates how to tokenize Yoruba text into individual words. It uses regular expressions to split the text based on common delimiters and handles potential edge cases. ```python import re def tokenize_yoruba_words(text): # Basic tokenization: split by whitespace and punctuation # This regex splits by spaces, commas, periods, etc., and keeps words words = re.findall(r'\b\w+\b', text.lower()) return words # Example usage: yoruba_sentence = "Ìwọ̀nyí jẹ́ àwọn ọ̀rọ̀ Yorùbá." tokens = tokenize_yoruba_words(yoruba_sentence) print(f"Sentence: {yoruba_sentence}") print(f"Tokens: {tokens}") ``` -------------------------------- ### Basic Yoruba Word Translation Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Demonstrates a simple translation of a Yoruba word. This is a foundational example for text processing. ```python from yoruba import Yoruba yoruba_translator = Yoruba() word = "ọlọpọn" translation = yoruba_translator.translate(word) print(f'The translation of "{word}" is: {translation}') ``` -------------------------------- ### Yoruba Text Processing Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt This snippet demonstrates basic text processing operations on Yoruba text, such as character replacement and manipulation. It's useful for cleaning and preparing Yoruba text data for further analysis. ```python from typing import List def replace_yoruba_chars(text: str) -> str: """Replace Yoruba characters with their ASCII equivalents.""" replacements = { "\u00e1": "a", # á "\u00e9": "e", # é "\u00ed": "i", # í "\u00f3": "o", # ó "\u00fa": "u", # ú "\u1ebf": "e", # ę "\u1ecd": "o", # ọ "\u1ee5": "u", # ụ "\u1e61": "s", # ṣ "\u1e63": "s", # ș "\u00f1": "n", # ñ "\u00e0": "a", # à "\u00e8": "e", # è "\u00ec": "i", # ì "\u00f2": "o", # ò "\u00f9": "u", # ù "\u1eb9": "e", # ę "\u1ecc": "o", # ọ "\u1ee5": "u", # ụ "\u1e61": "s", # ṣ "\u1e63": "s", # ș "\u00f1": "n", # ñ } for char, replacement in replacements.items(): text = text.replace(char, replacement) return text def process_yoruba_text(text: str) -> List[str]: """Process Yoruba text by cleaning and splitting into words.""" cleaned_text = replace_yoruba_chars(text) # Further cleaning or tokenization can be added here words = cleaned_text.split() return words # Example Usage: yoruba_sentence = "Àwọn ọmọdé ń gbádùn Yorùbá." processed_words = process_yoruba_text(yoruba_sentence) print(processed_words) ``` -------------------------------- ### Yoruba Text Frequency Analysis Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt This example performs a frequency analysis on Yoruba text to count the occurrences of each word. It utilizes Python's `collections.Counter` for efficient counting. ```python from collections import Counter def analyze_yoruba_frequency(tokens): return Counter(tokens) # Example usage: processed_tokens = ['ọmọdé', 'gbọ́rọ̀', 'olúwa'] word_counts = analyze_yoruba_frequency(processed_tokens) print(f"Tokens: {processed_tokens}") print(f"Word counts: {word_counts}") ``` -------------------------------- ### Pasilla Genome FASTA Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt An example of a FASTA file containing genome sequences, used by Pasilla for alignment and analysis. Each record starts with a '>' followed by the sequence ID and then the DNA sequence. ```fasta >chr2 AGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCT AGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCT ``` -------------------------------- ### Basic Configuration Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt This snippet shows a basic configuration for a Pasilla pipeline. It defines input files and parameters. ```yaml config: input_file: "/path/to/input.fastq.gz" output_dir: "/path/to/output" genome: "/path/to/genome.fa" gtf: "/path/to/annotation.gtf" threads: 8 ``` -------------------------------- ### Yoruba Word Tokenization with NLTK Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Tokenizes Yoruba text into words using the NLTK library. Ensure NLTK is installed and necessary data is downloaded. ```python import nltk try: nltk.data.find('tokenizers/punkt') except nltk.downloader.DownloadError: nltk.download('punkt') def tokenize_yoruba_words_nltk(text): """Tokenizes Yoruba text into words using NLTK.""" return nltk.word_tokenize(text) # Example usage: yoruba_sentence = "Ọjọ́ Àìkú ni ọjọ́ ìsinmi." words = tokenize_yoruba_words_nltk(yoruba_sentence) print(words) ``` -------------------------------- ### Basic Text Cleaning in Yoruba Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt This snippet demonstrates how to clean Yoruba text by removing punctuation and converting to lowercase. Ensure you have the necessary libraries installed. ```python import re def clean_yoruba_text(text): # Remove punctuation text = re.sub(r'[^\[\w\s]', '', text) # Convert to lowercase text = text.lower() return text # Example usage: yoruba_text = "Ẹ káàárọ̀! Kí ni orúkọ rẹ?" cleaned_text = clean_yoruba_text(yoruba_text) print(cleaned_text) ``` -------------------------------- ### Import Libraries and Setup Paths Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Imports necessary libraries and sets up the repository path for accessing data and the pylimma package. Ensures data directory exists. ```python import sys import warnings from pathlib import Path import anndata as ad import matplotlib.pyplot as plt import numpy as np import pandas as pd warnings.filterwarnings('ignore', category=FutureWarning) # This notebook lives at pylimma/pylimma/examples/mulvey/ # parents[1] = repo root (containing pylimma source + data/), matching # the convention used by the other example notebooks. REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) import pylimma DATA_DIR = REPO / 'data' assert DATA_DIR.exists(), f'expected data dir at {DATA_DIR}' pd.set_option('display.width', 120) pd.set_option('display.max_columns', 12) ``` -------------------------------- ### Initialize Rotation Loop Variables Source: https://pylimma.readthedocs.io/en/latest/_modules/pylimma/geneset.html Initializes variables and checks for finite values before starting the rotation loop. Includes setup for 'mean50' statistic if applicable. ```python # Rotation loop nchunk = int(np.ceil(nrot / chunk)) nroti = int(np.ceil(nrot / nchunk)) overshoot = nchunk * nroti - nrot count = np.zeros(4, dtype=np.int64) FinDf = np.isfinite(np.asarray(df_prior, dtype=np.float64)) FinDf = np.atleast_1d(FinDf) df_prior_arr = np.atleast_1d(np.asarray(df_prior, dtype=np.float64)) var_prior_arr = np.atleast_1d(np.asarray(var_prior, dtype=np.float64)) half1_loc = None half2_loc = None if set_statistic == "mean50": if nset % 2 == 0: half1_loc = nset // 2 half2_loc = half1_loc + 1 else: half1_loc = nset // 2 + 1 half2_loc = half1_loc df_total_winsor = None if approx_zscore and not legacy: df_total_winsor = np.minimum(df_total, 10000.0) ``` -------------------------------- ### Process Sample with Specific Configuration Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt This example shows how to process a specific sample using a custom configuration, overriding default settings. It's useful for targeted analysis or debugging. ```python p.run_analysis(sample_name="sample_1", config_override={"output_dir": "./results_sample_1"}) ``` -------------------------------- ### Install PyLimma with Plotting Extras Source: https://pylimma.readthedocs.io/en/latest/_sources/installation.rst.txt Install PyLimma along with matplotlib for generating diagnostic plots. ```bash pip install pylimma[plot] ``` -------------------------------- ### Initialize Pasilla Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt This snippet shows how to initialize the Pasilla library. Ensure Pasilla is installed and imported before use. ```python import pasilla p = pasilla.Pasilla() ``` -------------------------------- ### Load Data with Pasilla Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Demonstrates how to load data using the Pasilla library. Ensure the Pasilla library is installed and imported before use. ```python import pasilla # Load data from a file ps = pasilla.Pasilla() ps.load_data("path/to/your/data.csv") ``` -------------------------------- ### Import Libraries and Setup Source: https://pylimma.readthedocs.io/en/latest/tutorials/kang_pbmc.html Imports necessary libraries for data analysis and sets up the environment by adding the repository path to sys.path. Imports include anndata, scanpy, pandas, numpy, matplotlib, and pylimma. Sets pandas display options and filters out common warnings. ```python import sys import warnings from pathlib import Path import anndata as ad import matplotlib.pyplot as plt import numpy as np import pandas as pd import scanpy as sc warnings.filterwarnings('ignore', category=FutureWarning) warnings.filterwarnings('ignore', category=DeprecationWarning) REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) import pylimma pd.set_option('display.width', 120) pd.set_option('display.max_columns', 10) ``` -------------------------------- ### Import Libraries and Setup Source: https://pylimma.readthedocs.io/en/latest/tutorials/pasilla.html Imports necessary libraries and sets up the Python path for pylimma and data generation modules. Configures pandas display options. ```python import sys from pathlib import Path import matplotlib.pyplot as plt import numpy as np import pandas as pd REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) sys.path.insert(0, str(REPO / 'data')) import generate_data as gd import pylimma pd.set_option('display.width', 120) pd.set_option('display.max_columns', 10) # Shared plotting helpers used by every tutorial. ``` -------------------------------- ### Get Yoruba Character Information Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/yoruba.ipynb.txt Retrieves detailed information about a given Yoruba character, including its base character and tone. Useful for linguistic research and advanced text processing. ```python from yorubaland.utils import get_yoruba_char_info char = 'ọ' info = get_yoruba_char_info(char) print(f'Info for "{char}": {info}') ``` -------------------------------- ### Creating and Inspecting an EList Source: https://pylimma.readthedocs.io/en/latest/generated/pylimma.EList.html Demonstrates how to initialize an EList with data and a design matrix, and how to access its shape and slices. ```python >>> el = EList({"E": np.random.randn(100, 8), "design": np.eye(8)}) >>> el.shape (100, 8) >>> el[:10, :].shape (10, 8) ``` -------------------------------- ### Advanced Filtering Example Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt An example of more complex filtering logic. This snippet demonstrates filtering based on multiple conditions. ```python p.filter_data(column='status', condition='equals', value='active') p.filter_data(column='timestamp', condition='greater_than', value='2023-01-01') ``` -------------------------------- ### Create a Directory Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Creates a new directory. Use 'exist_ok=True' to avoid an error if the directory already exists. ```python import os os.makedirs("new_directory", exist_ok=True) ``` -------------------------------- ### Basic Data Loading and Display Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Demonstrates how to load a dataset and display its first few rows using Pasilla. Ensure the 'pasilla' library is installed. ```python import pasilla df = pasilla.load_dataset("pasilla") df.head() ``` -------------------------------- ### Load Data with Pasilla Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Demonstrates how to load data using the Pasilla library. Ensure the library is installed and data files are accessible. ```python from pasilla import Pasilla p = Pasilla() p.load_data("data.csv") ``` -------------------------------- ### Install limma package in R Source: https://pylimma.readthedocs.io/en/latest/validation/fixtures.html Before regenerating fixtures, ensure you have the limma package installed in your R environment. This is a prerequisite for running the fixture generation script. ```R install.packages("BiocManager") BiocManager::install("limma") ``` -------------------------------- ### Import Libraries and Setup Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Imports necessary libraries like sys, pathlib, matplotlib, numpy, pandas, and pylimma. It also sets up the repository path and system path for data generation modules. Configuration for pandas display options is included. ```python import sys from pathlib import Path import matplotlib.pyplot as plt import numpy as np import pandas as pd REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) sys.path.insert(0, str(REPO / 'data')) import generate_data as gd import pylimma pd.set_option('display.width', 120) pd.set_option('display.max_columns', 10) ``` -------------------------------- ### Import Libraries and Setup Paths Source: https://pylimma.readthedocs.io/en/latest/tutorials/mulvey.html Imports necessary libraries and sets up project paths for the pylimma workflow. Ensures the data directory exists and configures pandas display options. ```python import sys import warnings from pathlib import Path import anndata as ad import matplotlib.pyplot as plt import numpy as np import pandas as pd warnings.filterwarnings('ignore', category=FutureWarning) # This notebook lives at pylimma/pylimma/examples/mulvey/ # parents[1] = repo root (containing pylimma source + data/), matching # the convention used by the other example notebooks. REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) import pylimma DATA_DIR = REPO / 'data' assert DATA_DIR.exists(), f'expected data dir at {DATA_DIR}' pd.set_option('display.width', 120) pd.set_option('display.max_columns', 12) ``` -------------------------------- ### Get DataFrame Index Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Returns the index of the DataFrame. ```python m.index ``` -------------------------------- ### Get DataFrame Values Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Returns the DataFrame as a NumPy array. ```python m.values ``` -------------------------------- ### Setup and Helper Functions for pylimma Tutorials Source: https://pylimma.readthedocs.io/en/latest/tutorials/gse60450.html Imports necessary libraries and defines helper functions for plotting and data manipulation used throughout the tutorials. Includes functions for log-CPM calculation, density plotting, MDS coordinates, and heatmap generation. ```python import sys from pathlib import Path import matplotlib.pyplot as plt import numpy as np import pandas as pd REPO = Path.cwd().resolve().parents[1] sys.path.insert(0, str(REPO)) sys.path.insert(0, str(REPO / 'data')) import generate_data as gd import pylimma pd.set_option('display.width', 120) pd.set_option('display.max_columns', 10) np.set_printoptions(precision=4, suppress=True) # Shared plotting helpers used by every tutorial. def _log_cpm(mat, lib_size): cpm = mat.div(lib_size, axis=1) * 1e6 if hasattr(mat, 'div') else (mat / lib_size) * 1e6 return np.log2(cpm + 1e-2) def plot_log_cpm_density(mat, ax, title=""): '''Kernel-smoothed log-CPM density, one line per sample. Matches edgeR/limma's standard density plot (limma User's Guide Figure 15.1): a Gaussian KDE per sample drawn on a shared grid. ''' from scipy.stats import gaussian_kde arr = mat.values if hasattr(mat, 'values') else np.asarray(mat) # Shared evaluation grid spanning the combined range. lo, hi = np.nanmin(arr), np.nanmax(arr) grid = np.linspace(lo, hi, 200) for j in range(arr.shape[1]): col = arr[:, j] col = col[np.isfinite(col)] if col.size < 2: continue kde = gaussian_kde(col) ax.plot(grid, kde(grid), linewidth=0.8, alpha=0.7) ax.set_xlabel('log2 CPM') ax.set_ylabel('Density') ax.set_title(title) def mds_coords(mat, top=500): '''Pairwise leading-logFC MDS, matching R limma's plotMDS.default. Returns the (n_samples, ndim) coordinate matrix and the percent of variance explained per dimension. We reuse pylimma's private _mds_coordinates for consistency with plot_mds(). ''' from pylimma.plotting import _mds_coordinates r = _mds_coordinates(np.asarray(mat, dtype=np.float64), top=top, gene_selection='pairwise', ndim=2) lam = np.maximum(r['eigen_values'], 0.0) coords = r['eigen_vectors'] * np.sqrt(lam) return coords, r['var_explained'] def plot_mds_coloured(mat, groups, ax, top=500, title='MDS'): '''MDS scatter plot coloured by sample group.''' coords, var_exp = mds_coords(mat, top=top) pct = np.round(var_exp * 100).astype(int) groups = np.asarray(groups) levels = sorted(pd.unique(groups)) palette = plt.get_cmap('tab10').colors for k, level in enumerate(levels): m = groups == level ax.scatter(coords[m, 0], coords[m, 1], s=55, color=palette[k % len(palette)], label=str(level), edgecolor='k', linewidth=0.3) ax.axhline(0, color='grey', linewidth=0.4, linestyle=':') ax.axvline(0, color='grey', linewidth=0.4, linestyle=':') ax.set_xlabel(f'Leading logFC dim 1 ({pct[0]}%)') ax.set_ylabel(f'Leading logFC dim 2 ({pct[1]}%)') ax.set_title(title) ax.legend(fontsize=8, frameon=False) def plot_heatmap(E, groups, fit, n_top=50, ax=None): '''Heatmap of top-n DE rows showing BOTH directions. Picks the top n/2 genes with the most positive t and the top n/2 with the most negative t (by smallest p within each side), then stacks them - up-regulated in the contrast at top, down-regulated at bottom. Row z-scored.''' t = np.asarray(fit['t']).ravel() p = np.asarray(fit['p_value']).ravel() half = n_top // 2 up_pool = np.where(t > 0)[0] down_pool = np.where(t < 0)[0] top_up = up_pool[np.argsort(p[up_pool])[:half]] top_down = down_pool[np.argsort(p[down_pool])[:n_top - half]] # Sort each block by signed t descending so most-up is top and # most-down is bottom. top_up = top_up[np.argsort(-t[top_up])] top_down = top_down[np.argsort(-t[top_down])] ordered = np.concatenate([top_up, top_down]) mat = E[ordered] col_order = np.argsort(groups) mat_sorted = mat[:, col_order] z = (mat_sorted - mat_sorted.mean(axis=1, keepdims=True)) / (mat_sorted.std(axis=1, keepdims=True) + 1e-8) if ax is None: fig, ax = plt.subplots(figsize=(9, 8)) im = ax.imshow(z, aspect='auto', cmap='RdBu_r', vmin=-2, vmax=2) ax.set_xticks(range(len(col_order))) ax.set_xticklabels(np.asarray(groups)[col_order], rotation=90, fontsize=6) ax.set_yticks([]) ax.set_title(f'Top {n_top} DE genes (top {half} up + top {n_top - half} down; row z-scored)') return ax, im, ordered ``` -------------------------------- ### Get Column Names Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/mulvey.ipynb.txt Returns a list of all column names in the DataFrame. ```python m.columns ``` -------------------------------- ### Load Configuration Source: https://pylimma.readthedocs.io/en/latest/_sources/tutorials/pasilla.ipynb.txt Demonstrates loading a configuration file for Pasilla. This is crucial for setting up specific analysis parameters. ```python p.load('config.json') ```