Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
kyclark committed Jan 24, 2021
1 parent 9774b22 commit 70fdf00
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 111 deletions.
10 changes: 6 additions & 4 deletions 19_blastomatic/solution1_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
from typing import NamedTuple, TextIO

from pprint import pprint


class Args(NamedTuple):
""" Command-line arguments """
Expand Down Expand Up @@ -73,14 +75,14 @@ def main():
""" Make a jazz noise here """

args = get_args()

annots_reader = csv.DictReader(args.annotations, delimiter=',')
annots = {}
for row in annots_reader:
if acc := row.get('seq_id'):
annots[acc] = row
break

headers = ['qseqid', 'pident', 'latitude', 'longitude']
headers = ['qseqid', 'pident', 'depth', 'lat_lon']
args.outfile.write(args.delimiter.join(headers) + '\n')
# print(args.delimiter.join(headers), file=args.outfile)

Expand All @@ -104,8 +106,8 @@ def main():
args.delimiter.join([
seq_id,
hit.get('pident'),
info.get('latitude'),
info.get('longitude'),
info.get('depth'),
info.get('lat_lon'),
]) + '\n')

# print(args.delimiter.join([
Expand Down
6 changes: 3 additions & 3 deletions 19_blastomatic/solution2_dict_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def main():

writer = csv.DictWriter(
args.outfile,
fieldnames=['qseqid', 'pident', 'genus', 'species'],
fieldnames=['qseqid', 'pident', 'depth', 'lat_lon'],
delimiter=args.delimiter)
writer.writeheader()

Expand All @@ -101,8 +101,8 @@ def main():
writer.writerow({
'qseqid': seq_id,
'pident': hit.get('pident', 'NA'),
'genus': info.get('genus') or 'NA',
'species': info.get('species') or 'NA',
'depth': info.get('depth') or 'NA',
'lat_lon': info.get('lat_lon') or 'NA',
})

print(f'Exported {num_written:,} to "{args.outfile.name}".')
Expand Down
4 changes: 2 additions & 2 deletions 19_blastomatic/solution3_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def main():
data.append({
'qseqid': hit['qseqid'],
'pident': hit['pident'],
'latitude': info['latitude'] or 'NA',
'longitude': info['longitude'] or 'NA',
'depth': info['depth'] or 'NA',
'lat_lon': info['lat_lon'] or 'NA',
})

df = pd.DataFrame.from_records(data=data)
Expand Down
2 changes: 1 addition & 1 deletion 19_blastomatic/solution4_pandas_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def main():

joined.to_csv(args.outfile,
index=False,
columns=['qseqid', 'pident', 'latitude', 'longitude'],
columns=['qseqid', 'pident', 'depth', 'lat_lon'],
sep=args.delimiter)

print(f'Exported {joined.shape[0]:,} to "{args.outfile.name}".')
Expand Down
9 changes: 9 additions & 0 deletions 19_blastomatic/tests/blastomatic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def test_good_input() -> None:
assert os.path.isfile(outfile)

reader = csv.DictReader(open(outfile), delimiter=',')
assert set(reader.fieldnames
or '') == set(['qseqid', 'pident', 'depth', 'lat_lon'])
records = list(reader)
assert len(records) == 500
assert records[0]['qseqid'] == 'CAM_READ_0234442157'
Expand All @@ -96,6 +98,8 @@ def test_delimiter() -> None:
assert os.path.isfile(outfile)

reader = csv.DictReader(open(outfile), delimiter=delim)
assert set(reader.fieldnames
or '') == set(['qseqid', 'pident', 'depth', 'lat_lon'])
records = list(reader)
assert len(records) == 500
assert records[0]['qseqid'] == 'CAM_READ_0234442157'
Expand Down Expand Up @@ -124,6 +128,8 @@ def test_guess_delimiter() -> None:
assert os.path.isfile(outfile)

reader = csv.DictReader(open(outfile), delimiter=delim)
assert set(reader.fieldnames
or '') == set(['qseqid', 'pident', 'depth', 'lat_lon'])
records = list(reader)
assert len(records) == 252
assert records[-1]['qseqid'] == 'JCVI_READ_1100018174123'
Expand All @@ -148,9 +154,12 @@ def test_pctid() -> None:
assert os.path.isfile(outfile)

reader = csv.DictReader(open(outfile), delimiter='\t')
assert set(reader.fieldnames
or '') == set(['qseqid', 'pident', 'depth', 'lat_lon'])
records = list(reader)
assert len(records) == 101
assert records[-1]['qseqid'] == 'JCVI_READ_1092343670678'
assert all(map(lambda r: float(r['pident']) >= 90, records))
finally:
if os.path.isfile(outfile):
os.remove(outfile)
Expand Down
Loading

0 comments on commit 70fdf00

Please sign in to comment.