{"id":9208,"library":"pybigwig","title":"pyBigWig: Accessing BigWig Files","description":"pyBigWig is a Python package providing efficient access to bigWig files, leveraging the underlying C library libBigWig. It enables reading genomic data stored in bigWig format and also supports creating and writing new bigWig files. The current version is 0.3.25, with frequent updates addressing bug fixes and build improvements.","status":"active","version":"0.3.25","language":"en","source_language":"en","source_url":"https://github.com/deeptools/pyBigWig","tags":["bioinformatics","genomics","data-analysis","bigwig","file-format"],"install":[{"cmd":"pip install pyBigWig","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for handling numerical data arrays used in bigWig operations.","package":"numpy","optional":false}],"imports":[{"symbol":"pyBigWig","correct":"import pyBigWig"}],"quickstart":{"code":"import pyBigWig\nimport os\n\n# Define a temporary bigWig file name\ntemp_bw_file = \"temp_quickstart.bigWig\"\n\n# --- Writing a new bigWig file for demonstration ---\n# Chromosome sizes are required for writing\nchrom_sizes = {\"chr1\": 100000, \"chr2\": 50000}\nbw_write = pyBigWig.open(temp_bw_file, \"w\")\nbw_write.addHeader([(name, size) for name, size in chrom_sizes.items()])\n\n# Add some dummy entries\nbw_write.addEntries(\n    [\"chr1\"] * 5,\n    [0, 100, 200, 300, 400],\n    [99, 199, 299, 399, 499],\n    [0.1, 0.2, 0.3, 0.4, 0.5]\n)\nbw_write.addEntries(\"chr1\", 5000, 5010, [1.0]*10, step=1, span=1)\nbw_write.addEntries(\"chr2\", 1000, 1005, [2.0]*5, step=1, span=1)\nbw_write.close()\nprint(f\"Created dummy bigWig file: {temp_bw_file}\\n\")\n\n# --- Reading from the bigWig file ---\nbw = pyBigWig.open(temp_bw_file)\n\nprint(f\"Chromosomes found: {bw.chroms()}\\n\")\n\n# Get values from a specific region on chr1\nvalues_chr1 = bw.values(\"chr1\", 0, 500)\nprint(f\"Values on chr1 (0-500, first 10): {values_chr1[:10]}...\\n\")\n\n# Get summary statistics for a region\nsummary_chr1 = bw.stats(\"chr1\", 0, 1000, type=\"mean\")\nprint(f\"Mean value on chr1 (0-1000): {summary_chr1[0]}\\n\")\n\n# Get intervals (start, end, value) for a region\nintervals_chr1 = bw.intervals(\"chr1\", 0, 1000)\nprint(f\"Intervals on chr1 (0-1000, first 3): {intervals_chr1[:3]}...\\n\")\n\n# Close the bigWig file to release resources\nbw.close()\n\n# Clean up the temporary file\nif os.path.exists(temp_bw_file):\n    os.remove(temp_bw_file)\n    print(f\"Cleaned up temporary file: {temp_bw_file}\")","lang":"python","description":"This quickstart demonstrates how to create a simple bigWig file with dummy data, then open it in read mode to extract values, summary statistics, and intervals from specific genomic regions. It also shows the importance of closing the file handles."},"warnings":[{"fix":"Always call `bw.close()` for opened bigWig files. This issue was largely addressed in v0.3.17 but good practice remains.","message":"Explicitly close bigWig file objects using `bw.close()` after use to prevent potential memory leaks, especially when processing many files in a single session.","severity":"gotcha","affected_versions":"<0.3.17"},{"fix":"Ensure the file is opened in read mode (`'r'`, which is the default) for reading, or create a separate `pyBigWig` object for reading if simultaneous read/write-like logic is required.","message":"Attempting to perform read operations (e.g., `values()`, `stats()`) on a `pyBigWig` object opened in write mode (`'w'`) will result in an error.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update to `pyBigWig` version 0.3.25 or later to ensure correct handling of empty files and files with zero summary values.","message":"Versions prior to 0.3.25 could incorrectly report a single random entry for a genuinely empty bigWig file, and versions prior to 0.3.18 might misinterpret bigWig files with all zero summary values as empty.","severity":"breaking","affected_versions":"<0.3.25, <0.3.18"},{"fix":"Update to `pyBigWig` version 0.3.24 or later, which includes a fix to handle such cases correctly.","message":"BigWig files where the chromosome order in the header differs from the data sections could lead to issues.","severity":"gotcha","affected_versions":"<0.3.24"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify the file path is correct and the file exists. Use an absolute path or ensure the relative path is accurate from where the script is run.","cause":"The bigWig file specified in `pyBigWig.open()` does not exist at the provided path.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'non_existent.bigWig'"},{"fix":"If you intend to read data, open the file in read mode: `bw = pyBigWig.open('my.bigWig', 'r')` (or simply `pyBigWig.open('my.bigWig')` as 'r' is default).","cause":"You are calling a read-only method (e.g., `values()`, `stats()`, `intervals()`) on a `pyBigWig` object that was opened with the mode `'w'` (write mode).","error":"ValueError: Function is not allowed on a file opened for writing."},{"fix":"Convert your dictionary of chromosome sizes to the required list of tuples: `bw_write.addHeader([(name, size) for name, size in chrom_sizes_dict.items()])`.","cause":"The `addHeader()` method expects chromosome sizes as a list of `(chromosome_name, size)` tuples, but a dictionary was provided.","error":"TypeError: Expected a list of tuples for addHeader, got <class 'dict'>"},{"fix":"For genuinely empty files, handle this condition in your code (e.g., check `bw.is_empty()`). If you suspect a library issue, ensure you are on `pyBigWig` version 0.3.25 or later.","cause":"The bigWig file you are trying to query is genuinely empty, or in older versions (pre-0.3.25) there might have been misinterpretation of empty files.","error":"Exception: There are no entries in the bigWig file."}]}