Allow specifying window size
- User
Allow specifying window size
Assistant
Here’s the updated script with a
--windowargument to specify the window size (default: 101 bp). The window size must be odd (so it can be centered on the position), and the script will now adjust the half‐window size accordingly.#!/usr/bin/env python3 import argparse, math, sys import pandas as pd from collections import Counter from Bio import SeqIO def parse_args(): p = argparse.ArgumentParser(description="Build PWM centered on positions") p.add_argument("-f", "--fasta", default="sega007.fasta") p.add_argument("-p", "--positions", default="positions.tsv") p.add_argument("-o", "--out", default="matrix.tsv") p.add_argument("-w", "--window", default=101, type=int, help="Window size (must be odd, default: 101)") return p.parse_args() def load_genome(fasta_path): rec = next(SeqIO.parse(fasta_path, "fasta"), None) if not rec: sys.exit(f"No seq in {fasta_path}") return str(rec.seq).upper() def load_positions(tsv_path): df = pd.read_csv(tsv_path, sep="\t") if "Positions" not in df: sys.exit("Need a 'Positions' column") return df["Positions"].astype(int).tolist() def extract_windows(genome, positions, half): """ Circular extraction of windows around each 0-based position. """ L = len(genome) windows = [] for pos in positions: win = ''.join(genome[(pos + i) % L] for i in range(-half, half + 1)) if len(win) == 2*half + 1: windows.append(win) if not windows: sys.exit("Error: no windows extracted (check your inputs).") return windows def build_count_matrix(windows): npos = len(windows[0]) counts = {nt: [0]*npos for nt in "ACGT"} for w in windows: for i,ch in enumerate(w): if ch in counts: counts[ch][i] += 1 return counts def counts_to_log_odds(counts, total, bg={'A':.246,'C':.254,'G':.254,'T':.246}): pwm = {nt: [] for nt in "ACGT"} for i in range(len(next(iter(counts.values())))): for nt in "ACGT": freq = counts[nt][i] / total val = math.log2(freq/bg[nt]) if freq>0 else float("-inf") pwm[nt].append(val) return pwm def write_pwm(pwm, out_path): n = len(next(iter(pwm.values()))) rows = [] for i in range(n): pos = i - (n//2) rows.append({ "Position": pos, "A": pwm["A"][i], "C": pwm["C"][i], "G": pwm["G"][i], "T": pwm["T"][i] }) pd.DataFrame(rows).to_csv(out_path, sep="\t", index=False, float_format="%.4f") print(f"Wrote PWM to {out_path}") def main(): args = parse_args() if args.window % 2 == 0: sys.exit("Error: window size must be odd") half = args.window // 2 genome = load_genome(args.fasta) positions = load_positions(args.positions) windows = extract_windows(genome, positions, half=half) print(f"Extracted {len(windows)} windows of size {args.window}") counts = build_count_matrix(windows) pwm = counts_to_log_odds(counts, total=len(windows)) write_pwm(pwm, args.out) if __name__=="__main__": main()Key changes:
- Added
--windowargument (default: 101) - Check that window size is odd
- Compute
half = window // 2for use inextract_windows - Updated print message to show window size
Now you can run it like this for a 51 bp window:
python build_pwm.py --window 51- Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.