orfm 2.1.1

A pure-Rust port of OrfM - a simple and not slow open reading frame (ORF) caller
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import os

ORFM_C = os.path.expanduser("~/git/OrfM/orfm")
ORFM_RS = "/tmp/orfm-target/release/orfm"

# Number of random sequences and their lengths for benchmarking
NUM_SEQS = 1_000_000
SEQ_LEN = 150
LONG_NUM_SEQS = 100
LONG_SEQ_LEN = 1_000_000
REPLICATES = range(1, 4)

# Input types and their file extensions
INPUT_TYPES = {
    "fasta_unwrapped": ".fasta",
    "fasta_wrapped": ".fasta",
    "fasta_gzipped": ".fasta.gz",
    "fastq": ".fastq",
    "fastq_gzipped": ".fastq.gz",
}
# Long-sequence benchmark types (stored in LONG_DATA_DIR = /tmp)
LONG_INPUT_TYPES = {
    "long_fasta": ".fasta",
    "long_fasta_iupac": ".fasta",
}

# getorf (EMBOSS) sequence format prefix for each input type; None = not supported
GETORF_FORMAT = {
    "fasta_unwrapped": "fasta",
    "fasta_wrapped": "fasta",
    "fasta_gzipped": None,
    "fastq": "fastq",
    "fastq_gzipped": None,
}

DATA_DIR = "benchmark/data"
LONG_DATA_DIR = "/tmp/orfm_long_data"

ruleorder: generate_long_fasta_iupac > generate_long_fasta

rule all:
    input:
        "benchmark/results.tsv",
        "benchmark/results_long.tsv",
        "benchmark/correctness.txt"

rule build_rust:
    output:
        ORFM_RS
    shell:
        "CARGO_HOME=/container_home/.cargo cargo build --release"

rule generate_fasta_unwrapped:
    output:
        f"{DATA_DIR}/random_seqs_fasta_unwrapped_{{rep}}.fasta"
    params:
        num_seqs=NUM_SEQS,
        seq_len=SEQ_LEN
    shell:
        """
        python3 -c "
import random
random.seed({wildcards.rep})
bases = 'ACGT'
with open('{output}', 'w') as f:
    for i in range({params.num_seqs}):
        seq = ''.join(random.choices(bases, k={params.seq_len}))
        f.write(f'>seq_{{i}}\\n{{seq}}\\n')
        "
        """

rule generate_fasta_wrapped:
    output:
        f"{DATA_DIR}/random_seqs_fasta_wrapped_{{rep}}.fasta"
    params:
        num_seqs=NUM_SEQS,
        seq_len=SEQ_LEN,
        wrap_width=60
    shell:
        """
        python3 -c "
import random, textwrap
random.seed({wildcards.rep})
bases = 'ACGT'
with open('{output}', 'w') as f:
    for i in range({params.num_seqs}):
        seq = ''.join(random.choices(bases, k={params.seq_len}))
        f.write(f'>seq_{{i}}\\n')
        for line in textwrap.wrap(seq, {params.wrap_width}):
            f.write(line + '\\n')
        "
        """

rule generate_fasta_gzipped:
    input:
        f"{DATA_DIR}/random_seqs_fasta_unwrapped_{{rep}}.fasta"
    output:
        f"{DATA_DIR}/random_seqs_fasta_gzipped_{{rep}}.fasta.gz"
    shell:
        "gzip -c {input} > {output}"

rule generate_fastq:
    output:
        f"{DATA_DIR}/random_seqs_fastq_{{rep}}.fastq"
    params:
        num_seqs=NUM_SEQS,
        seq_len=SEQ_LEN
    shell:
        """
        python3 -c "
import random
random.seed({wildcards.rep})
bases = 'ACGT'
with open('{output}', 'w') as f:
    for i in range({params.num_seqs}):
        seq = ''.join(random.choices(bases, k={params.seq_len}))
        qual = 'I' * {params.seq_len}
        f.write(f'@seq_{{i}}\\n{{seq}}\\n+\\n{{qual}}\\n')
        "
        """

rule generate_fastq_gzipped:
    input:
        f"{DATA_DIR}/random_seqs_fastq_{{rep}}.fastq"
    output:
        f"{DATA_DIR}/random_seqs_fastq_gzipped_{{rep}}.fastq.gz"
    shell:
        "gzip -c {input} > {output}"

rule generate_long_fasta:
    output:
        f"{LONG_DATA_DIR}/random_seqs_long_fasta_{{rep}}.fasta"
    params:
        num_seqs=LONG_NUM_SEQS,
        seq_len=LONG_SEQ_LEN
    shell:
        """
        mkdir -p {LONG_DATA_DIR}
        python3 -c "
import random
random.seed({wildcards.rep})
bases = 'ACGT'
with open('{output}', 'w') as f:
    for i in range({params.num_seqs}):
        seq = ''.join(random.choices(bases, k={params.seq_len}))
        f.write(f'>seq_{{i}}\\n{{seq}}\\n')
        "
        """

rule generate_long_fasta_iupac:
    wildcard_constraints:
        rep=r"\d+"
    output:
        f"{LONG_DATA_DIR}/random_seqs_long_fasta_iupac_{{rep}}.fasta"
    params:
        num_seqs=LONG_NUM_SEQS,
        seq_len=LONG_SEQ_LEN
    shell:
        """
        mkdir -p {LONG_DATA_DIR}
        python3 -c "
import random
random.seed({wildcards.rep})
bases = 'ACGT'
iupac_extra = 'RYSWKMBDHVN'
with open('{output}', 'w') as f:
    for i in range({params.num_seqs}):
        seq = list(''.join(random.choices(bases, k={params.seq_len})))
        for pos in random.sample(range({params.seq_len}), int({params.seq_len} * 0.04)):
            seq[pos] = random.choice(iupac_extra)
        f.write(f'>seq_{{i}}\\n' + ''.join(seq) + '\\n')
        "
        """


def get_long_input_file(wildcards):
    itype = wildcards.itype
    rep = wildcards.rep
    ext = LONG_INPUT_TYPES[itype]
    return f"{LONG_DATA_DIR}/random_seqs_{itype}_{rep}{ext}"


rule benchmark_long_orfm_c:
    input:
        seqs=get_long_input_file,
        bin=ORFM_C
    output:
        time="benchmark/time_long_c_{itype}_{rep}.txt",
        result="/tmp/orfm_long_output/output_long_c_{itype}_{rep}.fasta"
    wildcard_constraints:
        itype="|".join(LONG_INPUT_TYPES.keys()),
        rep=r"\d+"
    run:
        import subprocess, time, resource
        os.makedirs("/tmp/orfm_long_output", exist_ok=True)
        start = time.monotonic()
        with open(output.result, 'w') as out:
            subprocess.run([input.bin, input.seqs], stdout=out, check=True)
        elapsed = time.monotonic() - start
        rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
        with open(output.time, 'w') as f:
            f.write(f"wall_clock_s\t{elapsed:.3f}\nmax_rss_kb\t{rusage.ru_maxrss}\n")


rule benchmark_long_orfm_rs:
    input:
        seqs=get_long_input_file,
        bin=ORFM_RS
    output:
        time="benchmark/time_long_rs_{itype}_{rep}.txt",
        result="/tmp/orfm_long_output/output_long_rs_{itype}_{rep}.fasta"
    wildcard_constraints:
        itype="|".join(LONG_INPUT_TYPES.keys()),
        rep=r"\d+"
    run:
        import subprocess, time, resource
        os.makedirs("/tmp/orfm_long_output", exist_ok=True)
        start = time.monotonic()
        with open(output.result, 'w') as out:
            subprocess.run([input.bin, input.seqs], stdout=out, check=True)
        elapsed = time.monotonic() - start
        rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
        with open(output.time, 'w') as f:
            f.write(f"wall_clock_s\t{elapsed:.3f}\nmax_rss_kb\t{rusage.ru_maxrss}\n")


rule collect_long_results:
    input:
        c_times=expand("benchmark/time_long_c_{itype}_{rep}.txt", itype=LONG_INPUT_TYPES, rep=REPLICATES),
        rs_times=expand("benchmark/time_long_rs_{itype}_{rep}.txt", itype=LONG_INPUT_TYPES, rep=REPLICATES),
    output:
        "benchmark/results_long.tsv"
    run:
        import statistics

        def read_time(prefix, itype, rep):
            vals = {}
            with open(f"benchmark/time_long_{prefix}_{itype}_{rep}.txt") as f:
                for line in f:
                    k, v = line.strip().split("\t")
                    vals[k] = v
            return vals.get("wall_clock_s", "N/A"), vals.get("max_rss_kb", "N/A")

        with open(output[0], 'w') as out:
            out.write("tool\tinput_type\treplicate\twall_clock_s\tmax_rss_kb\n")
            for itype in LONG_INPUT_TYPES:
                for rep in REPLICATES:
                    for tool, prefix in [("orfm_c", "c"), ("orfm_rs", "rs")]:
                        wall, rss = read_time(prefix, itype, rep)
                        out.write(f"{tool}\t{itype}\t{rep}\t{wall}\t{rss}\n")

        def median_or_na(vals):
            nums = [float(v) for v in vals if v != "N/A"]
            return statistics.median(nums) if nums else None

        data = {}
        for itype in LONG_INPUT_TYPES:
            for rep in REPLICATES:
                for tool, prefix in [("C", "c"), ("Rs", "rs")]:
                    wall, rss = read_time(prefix, itype, rep)
                    key = (tool, itype)
                    if key not in data:
                        data[key] = {"wall": []}
                    data[key]["wall"].append(wall)

        col_w = max(max(len(t) for t in LONG_INPUT_TYPES), 5)
        ts_w, rat_w = 8, 6

        def fmt_s(v):      return f"{v:.2f}" if v is not None else "N/A"
        def fmt_rat(c, r): return f"{r/c:.2f}x" if (c and r) else "N/A"

        cols = [("Input", col_w), ("C (s)", ts_w), ("Rs (s)", ts_w), ("Rs/C", rat_w)]

        def hline(l, m, r):
            return l + m.join("" * (w + 2) for _, w in cols) + r

        lines = [hline("", "", "")]
        lines.append("" + "".join(f" {h:^{w}} " for h, w in cols) + "")
        lines.append(hline("", "", ""))
        for i, itype in enumerate(LONG_INPUT_TYPES):
            if i > 0:
                lines.append(hline("", "", ""))
            c_w  = median_or_na(data[("C",  itype)]["wall"])
            r_w  = median_or_na(data[("Rs", itype)]["wall"])
            cells = [
                f" {itype:<{col_w}} ",
                f" {fmt_s(c_w):>{ts_w}} ",
                f" {fmt_s(r_w):>{ts_w}} ",
                f" {fmt_rat(c_w, r_w):>{rat_w}} ",
            ]
            lines.append("" + "".join(cells) + "")
        lines.append(hline("", "", ""))
        print("\n".join(lines))


def get_input_file(wildcards):
    itype = wildcards.itype
    rep = wildcards.rep
    ext = INPUT_TYPES[itype]
    return f"{DATA_DIR}/random_seqs_{itype}_{rep}{ext}"


rule benchmark_orfm_c:
    input:
        seqs=get_input_file,
        bin=ORFM_C
    output:
        time="benchmark/time_c_{itype}_{rep}.txt",
        result="benchmark/output_c_{itype}_{rep}.fasta"
    run:
        import subprocess, time, resource
        start = time.monotonic()
        with open(output.result, 'w') as out:
            subprocess.run([input.bin, input.seqs], stdout=out, check=True)
        elapsed = time.monotonic() - start
        rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
        with open(output.time, 'w') as f:
            f.write(f"wall_clock_s\t{elapsed:.3f}\nmax_rss_kb\t{rusage.ru_maxrss}\n")

rule benchmark_orfm_rs:
    input:
        seqs=get_input_file,
        bin=ORFM_RS
    output:
        time="benchmark/time_rs_{itype}_{rep}.txt",
        result="benchmark/output_rs_{itype}_{rep}.fasta"
    run:
        import subprocess, time, resource
        start = time.monotonic()
        with open(output.result, 'w') as out:
            subprocess.run([input.bin, input.seqs], stdout=out, check=True)
        elapsed = time.monotonic() - start
        rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
        with open(output.time, 'w') as f:
            f.write(f"wall_clock_s\t{elapsed:.3f}\nmax_rss_kb\t{rusage.ru_maxrss}\n")

rule benchmark_getorf:
    input:
        seqs=get_input_file,
    output:
        time="benchmark/time_getorf_{itype}_{rep}.txt",
        result="benchmark/output_getorf_{itype}_{rep}.fasta"
    run:
        import subprocess, time, resource
        fmt = GETORF_FORMAT[wildcards.itype]
        if fmt is None:
            # getorf does not support this format (e.g. gzipped)
            with open(output.time, 'w') as f:
                f.write("wall_clock_s\tN/A\nmax_rss_kb\tN/A\n")
            open(output.result, 'w').close()
        else:
            start = time.monotonic()
            subprocess.run(
                ["getorf",
                 "-sequence", f"{fmt}::{input.seqs}",
                 "-outseq", f"fasta::{output.result}",
                 "-minsize", "96",
                 "-find", "0"],
                check=True, capture_output=True
            )
            elapsed = time.monotonic() - start
            rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
            with open(output.time, 'w') as f:
                f.write(f"wall_clock_s\t{elapsed:.3f}\nmax_rss_kb\t{rusage.ru_maxrss}\n")

rule check_correctness:
    input:
        c=expand("benchmark/output_c_{itype}_{rep}.fasta", itype=INPUT_TYPES, rep=REPLICATES),
        rs=expand("benchmark/output_rs_{itype}_{rep}.fasta", itype=INPUT_TYPES, rep=REPLICATES)
    output:
        "benchmark/correctness.txt"
    run:
        import subprocess
        all_ok = True
        with open(output[0], 'w') as f:
            for itype in INPUT_TYPES:
                for rep in REPLICATES:
                    c_file = f"benchmark/output_c_{itype}_{rep}.fasta"
                    rs_file = f"benchmark/output_rs_{itype}_{rep}.fasta"
                    result = subprocess.run(["diff", c_file, rs_file], capture_output=True, text=True)
                    if result.returncode == 0:
                        f.write(f"{itype} replicate {rep}: PASS (outputs identical)\n")
                    else:
                        f.write(f"{itype} replicate {rep}: FAIL\n")
                        f.write(result.stdout[:500] + "\n")
                        all_ok = False
            if all_ok:
                f.write("\nAll replicates produce identical output.\n")

rule collect_results:
    input:
        c_times=expand("benchmark/time_c_{itype}_{rep}.txt", itype=INPUT_TYPES, rep=REPLICATES),
        rs_times=expand("benchmark/time_rs_{itype}_{rep}.txt", itype=INPUT_TYPES, rep=REPLICATES),
        go_times=expand("benchmark/time_getorf_{itype}_{rep}.txt", itype=INPUT_TYPES, rep=REPLICATES),
    output:
        "benchmark/results.tsv"
    run:
        import statistics

        def read_time(prefix, itype, rep):
            vals = {}
            with open(f"benchmark/time_{prefix}_{itype}_{rep}.txt") as f:
                for line in f:
                    k, v = line.strip().split("\t")
                    vals[k] = v
            return vals.get("wall_clock_s", "N/A"), vals.get("max_rss_kb", "N/A")

        with open(output[0], 'w') as out:
            out.write("tool\tinput_type\treplicate\twall_clock_s\tmax_rss_kb\n")
            for itype in INPUT_TYPES:
                for rep in REPLICATES:
                    for tool, prefix in [("orfm_c", "c"), ("orfm_rs", "rs"), ("getorf", "getorf")]:
                        wall, rss = read_time(prefix, itype, rep)
                        out.write(f"{tool}\t{itype}\t{rep}\t{wall}\t{rss}\n")

        def median_or_na(vals):
            nums = [float(v) for v in vals if v != "N/A"]
            return statistics.median(nums) if nums else None

        data = {}
        for itype in INPUT_TYPES:
            for rep in REPLICATES:
                for tool, prefix in [("C", "c"), ("Rs", "rs"), ("getorf", "getorf")]:
                    wall, rss = read_time(prefix, itype, rep)
                    key = (tool, itype)
                    if key not in data:
                        data[key] = {"wall": [], "rss": []}
                    data[key]["wall"].append(wall)
                    data[key]["rss"].append(rss)

        def fmt_s(v):   return f"{v:.2f}" if v is not None else "N/A"
        def fmt_mb(v):  return f"{v/1024:.0f}" if v is not None else "N/A"
        def fmt_rat(c, r): return f"{r/c:.2f}x" if (c and r) else "N/A"

        def print_table(type_list, include_getorf):
            col_w = max(max(len(t) for t in type_list), 5)
            ts_w, rat_w = 6, 6
            cols = [("Input", col_w), ("C (s)", ts_w), ("Rs (s)", ts_w)]
            if include_getorf:
                cols.append(("go (s)", ts_w))
            cols.append(("Rs/C", rat_w))

            def hline(l, m, r):
                return l + m.join("" * (w + 2) for _, w in cols) + r

            lines = [hline("", "", "")]
            lines.append("" + "".join(f" {h:^{w}} " for h, w in cols) + "")
            lines.append(hline("", "", ""))
            for i, itype in enumerate(type_list):
                if i > 0:
                    lines.append(hline("", "", ""))
                c_w  = median_or_na(data[("C",  itype)]["wall"])
                r_w  = median_or_na(data[("Rs", itype)]["wall"])
                g_w  = median_or_na(data[("getorf", itype)]["wall"]) if include_getorf else None
                cells = [
                    f" {itype:<{col_w}} ",
                    f" {fmt_s(c_w):>{ts_w}} ",
                    f" {fmt_s(r_w):>{ts_w}} ",
                ]
                if include_getorf:
                    cells.append(f" {fmt_s(g_w):>{ts_w}} ")
                cells.append(f" {fmt_rat(c_w, r_w):>{rat_w}} ")
                lines.append("" + "".join(cells) + "")
            lines.append(hline("", "", ""))
            print("\n".join(lines))

        print_table(list(INPUT_TYPES.keys()), include_getorf=True)