Skip to content

Commit

Permalink
removing synth.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kyclark committed Feb 9, 2021
1 parent 6b774be commit f28c199
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 267 deletions.
9 changes: 4 additions & 5 deletions 15_seqmagique/seqmagique_rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
""" Mimic seqmagick, print stats on FASTA sequences """

import argparse
import os
import sys
import numpy as np
from typing import List, NamedTuple, TextIO
from rich import box
from rich.console import Console
from rich.progress import track
from rich.table import Table, Column
Expand Down Expand Up @@ -58,11 +55,13 @@ def main() -> None:
Column(header='Num. Seqs', justify='right'),
header_style="bold black")

for file in track([process(fh) for fh in args.file]):
for fh in track(args.file):
file = process(fh)
table.add_row(file.filename, str(file.min_len), str(file.max_len),
str(file.avg_len), str(file.num_seqs))

Console().print(table)
console = Console()
console.print(table)


# --------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions 15_seqmagique/solution1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
""" Mimic seqmagick, print stats on FASTA sequences """
""" Mimic seqmagick """

import argparse
from typing import List, NamedTuple, TextIO
Expand Down Expand Up @@ -28,7 +28,7 @@ def get_args() -> Args:
"""Get command-line arguments"""

parser = argparse.ArgumentParser(
description='Argparse Python script',
description='Mimic seqmagick',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('file',
Expand Down
8 changes: 0 additions & 8 deletions 16_fastx_grep/foo

This file was deleted.

29 changes: 4 additions & 25 deletions 16_fastx_grep/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import sys
from Bio import SeqIO
from typing import List, Match, NamedTuple, TextIO, Optional
from typing import List, NamedTuple, TextIO


class Args(NamedTuple):
Expand All @@ -17,7 +17,6 @@ class Args(NamedTuple):
output_format: str
outfile: TextIO
insensitive: bool
verbose: bool


# --------------------------------------------------
Expand Down Expand Up @@ -65,19 +64,13 @@ def get_args() -> Args:
help='Case-insensitive search',
action='store_true')

parser.add_argument('-v',
'--verbose',
help='Be chatty',
action='store_true')

args = parser.parse_args()

return Args(pattern=args.pattern,
files=args.file,
input_format=args.format,
output_format=args.outfmt,
outfile=args.outfile,
verbose=args.verbose,
insensitive=args.insensitive)


Expand All @@ -86,18 +79,9 @@ def main() -> None:
""" Make a jazz noise here """

args = get_args()
regex = re.compile(args.pattern, re.IGNORECASE if args.insensitive else 0)

def progress(msg: str) -> None:
if args.verbose:
print(msg, file=sys.stderr)

def search(text: str) -> Optional[Match[str]]:
flag = re.IGNORECASE if args.insensitive else 0
return re.search(args.pattern, text, flag)

num_checked, num_took = 0, 0
for i, fh in enumerate(args.files, start=1):
progress(f'{i:3}: {fh.name}')
for fh in args.files:
input_format = args.input_format or guess_format(fh.name)

if not input_format:
Expand All @@ -106,14 +90,9 @@ def search(text: str) -> Optional[Match[str]]:
output_format = args.output_format or input_format

for rec in SeqIO.parse(fh, input_format):
num_checked += 1
if any(map(search, [rec.id, rec.description])):
num_took += 1
if any(map(regex.search, [rec.id, rec.description])):
SeqIO.write(rec, args.outfile, output_format)

outfile = 'STDOUT' if args.outfile == sys.stdout else args.outfile.name
progress(f'Checked {num_checked:,}, wrote {num_took:,} to "{outfile}".')


# --------------------------------------------------
def guess_format(filename: str) -> str:
Expand Down
90 changes: 38 additions & 52 deletions 16_fastx_grep/tests/fastx_grep_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,28 @@ def test_cannot_guess() -> None:
pattern = random_string()
rv, out = getstatusoutput(f'{RUN} {pattern} {BAD_EXT}')
assert rv != 0
assert out == f'Please specify file format for "{BAD_EXT}"'
assert re.search(f'Please specify file format for "{BAD_EXT}"', out)


# --------------------------------------------------
def test_out_file() -> None:
""" out_file """

out_file = random_string()
if os.path.isfile(out_file):
os.remove(out_file)

try:
flag = '-o' if random.choice([0, 1]) else '--outfile'
rv, out = getstatusoutput(f'{RUN} {flag} {out_file} LSU {LSU}')
assert rv == 0
assert os.path.isfile(out_file)
expected = open(LSU + '.upper.out').read().rstrip()
assert open(out_file).read().rstrip() == expected

finally:
if os.path.isfile(out_file):
os.remove(out_file)


# --------------------------------------------------
Expand All @@ -62,11 +83,23 @@ def run(pattern: str,
opts: List[str] = []) -> None:
""" Runs on command-line input """

assert os.path.isfile(expected_file)
expected = open(expected_file).read().rstrip()
cmd = f"{RUN} {' '.join(opts)} {pattern} {input_file}"
rv, out = getstatusoutput(cmd)
assert rv == 0
assert out == expected

out_file = random_string()
if os.path.isfile(out_file):
os.remove(out_file)

try:
cmd = f"{RUN} {' '.join(opts)} {pattern} -o {out_file} {input_file}"
rv, out = getstatusoutput(cmd)

assert os.path.isfile(out_file)
assert rv == 0
assert open(out_file).read().rstrip() == expected
finally:
if os.path.isfile(out_file):
os.remove(out_file)


# --------------------------------------------------
Expand Down Expand Up @@ -105,53 +138,6 @@ def test_lsu_lowercase_insensitive() -> None:
run('lsu', LSU, LSU + '.i.lower.out', ['--insensitive'])


# --------------------------------------------------
def test_outfile() -> None:
""" outfile """

outfile = random_string()
if os.path.isfile(outfile):
os.remove(outfile)

try:
flag = '-o' if random.choice([0, 1]) else '--outfile'
rv, out = getstatusoutput(f'{RUN} {flag} {outfile} LSU {LSU}')
assert rv == 0
assert out == ''
assert os.path.isfile(outfile)
expected = open(LSU + '.upper.out').read().rstrip()
assert open(outfile).read().rstrip() == expected

finally:
if os.path.isfile(outfile):
os.remove(outfile)


# --------------------------------------------------
def test_outfile_verbose() -> None:
""" outfile + verbose """

outfile = random_string()
if os.path.isfile(outfile):
os.remove(outfile)

try:
flag = '-v' if random.choice([0, 1]) else '--verbose'
rv, out = getstatusoutput(f'{RUN} {flag} -o {outfile} LSU {LSU}')
assert rv == 0
assert out.splitlines() == [
' 1: ./tests/inputs/lsu.fq',
f'Checked 4, wrote 2 to "{outfile}".'
]
assert os.path.isfile(outfile)
expected = open(LSU + '.upper.out').read().rstrip()
assert open(outfile).read().rstrip() == expected

finally:
if os.path.isfile(outfile):
os.remove(outfile)


# --------------------------------------------------
def test_outfmt_fastq_to_fasta() -> None:
""" outfmt """
Expand Down
1 change: 1 addition & 0 deletions 17_synth/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
out.fa
synth.py
Loading

0 comments on commit f28c199

Please sign in to comment.