Permalink
Browse files

Merge pull request #251 from nextstrain/multiple_titer_inputs

allow multiple titer files as input for titer model fitting
  • Loading branch information...
rneher committed Jan 5, 2019
2 parents 001de94 + 5d2a121 commit 75dba85d469715c90fa4254fdc5437119c3d5ec3
Showing with 32 additions and 23 deletions.
  1. +30 −21 augur/titer_model.py
  2. +2 −2 augur/titers.py
@@ -16,7 +16,7 @@ class TiterCollection(object):
Container for raw titer values and methods for analyzing these values.
"""
@staticmethod
def load_from_file(filename, excluded_sources=None):
def load_from_file(filenames, excluded_sources=None):
"""Load titers from a tab-delimited file.
Parameters
@@ -48,34 +48,41 @@ def load_from_file(filename, excluded_sources=None):
5
>>> measurements.get(("A/Acores/11/2013", ("A/Alabama/5/2010", "F27/10")))
>>>
>>> output = TiterCollection.load_from_file("tests/data/titer_model/missing.tsv")
Traceback (most recent call last):
File "<ipython-input-2-0ea96a90d45d>", line 1, in <module>
open("tests/data/titer_model/missing.tsv", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'tests/data/titer_model/missing.tsv'
"""
if excluded_sources is None:
excluded_sources = []

measurements = defaultdict(list)
strains = set()
sources = set()
titer_files = [filenames] if type(filenames)==str else filenames

with myopen(filename, 'r') as infile:
for line in infile:
entries = line.strip().split('\t')
try:
val = float(entries[4])
except:
continue
test, ref_virus, serum, src_id = (entries[0], entries[1],entries[2],
entries[3])

ref = (ref_virus, serum)
if src_id not in excluded_sources:
for fname in titer_files:
with myopen(fname, 'r') as infile:
for line in infile:
entries = line.strip().split('\t')
try:
measurements[(test, (ref_virus, serum))].append(val)
strains.update([test, ref_virus])
sources.add(src_id)
val = float(entries[4])
except:
print(line.strip())

print("Read titers from %s, found:" % filename, file=sys.stderr)
continue
test, ref_virus, serum, src_id = (entries[0], entries[1],entries[2],
entries[3])

ref = (ref_virus, serum)
if src_id not in excluded_sources:
try:
measurements[(test, (ref_virus, serum))].append(val)
strains.update([test, ref_virus])
sources.add(src_id)
except:
print(line.strip())

print("Read titers from %s, found:" % ' '.join(titer_files), file=sys.stderr)
print(" --- %i strains" % len(strains), file=sys.stderr)
print(" --- %i data sources" % len(sources), file=sys.stderr)
print(" --- %i total measurements" % sum([len(x) for x in measurements.values()]), file=sys.stderr)
@@ -167,7 +174,8 @@ def __init__(self, titers, **kwargs):
Description
"""
# Assign titers and prepare list of strains.
if isinstance(titers, str) and os.path.isfile(titers):
if (isinstance(titers, str) and os.path.isfile(titers))\
or isinstance(titers, list):
self.read_titers(titers)
else:
self.titers = titers
@@ -331,7 +339,8 @@ def __init__(self, serum_Kc=0, **kwargs):
def assign_titers(self, titers, strains):
# Load titer measurements from a file or from a given dictionary of
# measurements.
if isinstance(titers, str) and os.path.isfile(titers):
if (isinstance(titers, str) and os.path.isfile(titers))\
or isinstance(titers, list):
titer_measurements, strains, sera = TiterCollection.load_from_file(titers)
else:
titer_measurements = titers
@@ -18,15 +18,15 @@ def register_arguments(parser):
add_default_command(parser)

tree_model = subparsers.add_parser('tree', help='tree model')
tree_model.add_argument('--titers', type=str, required=True, help="file with titer measurements")
tree_model.add_argument('--titers', nargs='+', type=str, required=True, help="file with titer measurements")
tree_model.add_argument('--tree', '-t', type=str, required=True, help="tree to perform fit titer model to")
tree_model.add_argument('--output', '-o', type=str, required=True, help='JSON file to save titer model')
tree_model.set_defaults(
__command__ = infer_tree_model
)

sub_model = subparsers.add_parser('sub', help='substitution model')
sub_model.add_argument('--titers', type=str, required=True, help="file with titer measurements")
sub_model.add_argument('--titers', nargs='+', type=str, required=True, help="file with titer measurements")
sub_model.add_argument('--alignment', nargs='+', type=str, required=True, help="sequence to be used in the substitution model, supplied as fasta files")
sub_model.add_argument('--gene-names', nargs='+', type=str, required=True, help="names of the sequences in the alignment, same order assumed")
sub_model.add_argument('--tree', '-t', type=str, help="optional tree to annotate fit titer model to")

0 comments on commit 75dba85

Please sign in to comment.