-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added date parsing and tested timetree/ancestral modes, additional ar…
…guments still need to be sorted out
- Loading branch information
Showing
3 changed files
with
96 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pandas as pd | ||
from treetime.utils import numeric_date | ||
from base.utils import ambiguous_date_to_date_range | ||
|
||
def parse_metadata(fname): | ||
metadata = pd.read_csv(fname, sep='\t' if fname[-3:]=='tsv' else ',', | ||
skipinitialspace=True) | ||
return metadata | ||
|
||
def meta_to_date_dict(metadata, name_col = None, date_col='date', fmt=None): | ||
possible_name_cols = ['strain', 'name'] if name_col is None else [name_col] | ||
dates = None | ||
for nc in possible_name_cols: | ||
try: | ||
dates = {row.loc[nc]:row.loc[date_col] for ri,row in metadata.iterrows()} | ||
break | ||
except: | ||
pass | ||
|
||
if dates is None: | ||
print("ERROR: Can not find meta data column with strain names") | ||
return None | ||
|
||
if fmt: | ||
from datetime import datetime | ||
numerical_dates = {} | ||
for k,v in dates.items(): | ||
if 'XX' in v: | ||
numerical_dates[k] = [numeric_date(d) for d in ambiguous_date_to_date_range(v, fmt)] | ||
else: | ||
numerical_dates[k] = numeric_date(datetime.strptime(v, fmt)) | ||
else: | ||
numerical_dates = {k:float(v) for k,v in dates.items()} | ||
|
||
return numerical_dates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters