|
| 1 | +#!/usr/bin/env lua |
| 2 | + |
| 3 | +-- Usage: |
| 4 | +-- lua make_primer_patterns.lua -i input.fasta [-o output_prefix] [-f forward_pattern] [-r reverse_pattern] |
| 5 | + |
| 6 | +local function parse_args() |
| 7 | + local opts = { |
| 8 | + input_fasta = nil, |
| 9 | + output_prefix = "primer_patterns", |
| 10 | + forward_pattern = "^(.*?)", |
| 11 | + reverse_pattern = "^(.*?)", |
| 12 | + } |
| 13 | + |
| 14 | + local i = 1 |
| 15 | + while i <= #arg do |
| 16 | + local a = arg[i] |
| 17 | + if a == "-i" or a == "--input_fasta" then |
| 18 | + i = i + 1 |
| 19 | + opts.input_fasta = arg[i] |
| 20 | + elseif a == "-o" or a == "--output_prefix" then |
| 21 | + i = i + 1 |
| 22 | + opts.output_prefix = arg[i] |
| 23 | + elseif a == "-f" or a == "--forward_pattern" then |
| 24 | + i = i + 1 |
| 25 | + opts.forward_pattern = arg[i] |
| 26 | + elseif a == "-r" or a == "--reverse_pattern" then |
| 27 | + i = i + 1 |
| 28 | + opts.reverse_pattern = arg[i] |
| 29 | + else |
| 30 | + error("Unknown argument: " .. a) |
| 31 | + end |
| 32 | + i = i + 1 |
| 33 | + end |
| 34 | + |
| 35 | + assert(opts.input_fasta, "You must provide --input_fasta (-i)") |
| 36 | + return opts |
| 37 | +end |
| 38 | + |
| 39 | +local function read_fasta_lines(path) |
| 40 | + local file, err = io.open(path, "r") |
| 41 | + assert(file, "Could not open FASTA file: " .. (err or "unknown error")) |
| 42 | + local lines = {} |
| 43 | + for line in file:lines() do |
| 44 | + lines[#lines + 1] = line:gsub("%s+", "") -- strip whitespace |
| 45 | + end |
| 46 | + file:close() |
| 47 | + return lines |
| 48 | +end |
| 49 | + |
| 50 | +local function generate_patterns(fasta_path, label, fwd_prefix, rev_suffix) |
| 51 | + local lines = read_fasta_lines(fasta_path) |
| 52 | + |
| 53 | + -- Extract sequences and headers |
| 54 | + local seqs, headers, entries = {}, {}, {} |
| 55 | + local accumulator = nil |
| 56 | + for _, line in ipairs(lines) do |
| 57 | + if line:sub(1, 1) == ">" then |
| 58 | + if accumulator then |
| 59 | + entries[#entries + 1] = accumulator |
| 60 | + end |
| 61 | + accumulator = { header = line, seq = "" } |
| 62 | + else |
| 63 | + assert(accumulator) |
| 64 | + accumulator.seq = accumulator.seq .. line |
| 65 | + end |
| 66 | + end |
| 67 | + if accumulator then |
| 68 | + entries[#entries + 1] = accumulator |
| 69 | + end |
| 70 | + |
| 71 | + -- Crash if the number of parsed sequences isn't exactly 2 |
| 72 | + assert(#seqs == 2) |
| 73 | + |
| 74 | + -- Parse start coordinates from header lines |
| 75 | + local starts = {} |
| 76 | + for _, header in ipairs(headers) do |
| 77 | + local start = header:match(":(%d+)%-%d+") |
| 78 | + assert(start, "Invalid header format (expected bedtools-style): " .. header) |
| 79 | + starts[#starts + 1] = tonumber(start) |
| 80 | + end |
| 81 | + |
| 82 | + -- Heuristic check for orientation assumption |
| 83 | + if starts[1] > starts[2] then |
| 84 | + io.stderr:write( |
| 85 | + "⚠️ Warning: Please double check that the provided FASTA is formatted like an output from `bedtools getfasta`, e.g.\n\n'>PP599462.1:0-16'" |
| 86 | + ) |
| 87 | + end |
| 88 | + |
| 89 | + -- Build patterns |
| 90 | + local fwd_pattern = fwd_prefix .. seqs[1] |
| 91 | + local rev_pattern = seqs[2] .. rev_suffix |
| 92 | + |
| 93 | + -- Write output |
| 94 | + local out, err = io.open(label .. ".txt", "w") |
| 95 | + assert(out, "Failed to write output: " .. (err or "unknown error")) |
| 96 | + out:write(fwd_pattern .. "\n") |
| 97 | + out:write(rev_pattern .. "\n") |
| 98 | +end |
| 99 | + |
| 100 | +local function main() |
| 101 | + local opts = parse_args() |
| 102 | + |
| 103 | + -- make sure the provided file exists |
| 104 | + local file = io.open(opts.input_fasta, "r") |
| 105 | + assert(file, "Input FASTA file does not exist: " .. opts.input_fasta) |
| 106 | + file:close() |
| 107 | + |
| 108 | + -- generate the patterns and write them to a text file |
| 109 | + generate_patterns(opts.input_fasta, opts.output_prefix, opts.forward_pattern, opts.reverse_pattern) |
| 110 | +end |
| 111 | + |
| 112 | +-- Run main |
| 113 | +if debug.getinfo(1, "S").short_src == arg[0] then |
| 114 | + main() |
| 115 | +end |
0 commit comments