- Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathjoin-metadata-and-clades.py
153 lines (132 loc) · 5.58 KB
/
join-metadata-and-clades.py
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
#!/usr/bin/env python3
"""
Copied from https://github.com/nextstrain/ncov-ingest/blob/master/bin/join-metadata-and-clades
"""
importargparse
importsys
fromdatetimeimportdatetime
importpandasaspd
importnumpyasnp
INSERT_BEFORE_THIS_COLUMN="pango_lineage"
METADATA_JOIN_COLUMN_NAME='strain'
NEXTCLADE_JOIN_COLUMN_NAME='seqName'
VALUE_MISSING_DATA='?'
rate_per_day=0.0007*29903/365
reference_day=datetime(2020,1,1).toordinal()
column_map= {
"clade": "Nextstrain_clade",
"Nextclade_pango": "Nextclade_pango",
"totalMissing": "missing_data",
"totalSubstitutions": "divergence",
"totalNonACGTNs": "nonACGTN",
"privateNucMutations.totalUnlabeledSubstitutions": "rare_mutations",
"privateNucMutations.totalReversionSubstitutions": "reversion_mutations",
"privateNucMutations.totalLabeledSubstitutions": "potential_contaminants",
"qc.overallScore": "QC_overall_score",
"qc.overallStatus": "QC_overall_status",
"qc.missingData.status": "QC_missing_data",
"qc.mixedSites.status": "QC_mixed_sites",
"qc.privateMutations.status": "QC_rare_mutations",
"qc.snpClusters.status": "QC_snp_clusters",
"qc.frameShifts.status": "QC_frame_shifts",
"qc.stopCodons.status": "QC_stop_codons",
"frameShifts": "frame_shifts",
"deletions": "deletions",
"insertions": "insertions",
"substitutions": "substitutions",
"aaSubstitutions": "aaSubstitutions",
"immune_escape": "immune_escape",
"ace2_binding": "ace2_binding",
}
preferred_types= {
"divergence": "int32",
"nonACGTN": "int32",
"missing_data": "int32",
"snp_clusters": "int32",
"rare_mutations": "int32"
}
defreorder_columns(result: pd.DataFrame):
"""
Moves the new clade column after a specified column
"""
columns=list(result.columns)
columns.remove(column_map['clade'])
insert_at=columns.index(INSERT_BEFORE_THIS_COLUMN)
columns.insert(insert_at, column_map['clade'])
returnresult[columns]
defparse_args():
parser=argparse.ArgumentParser(
description="Joins metadata file with Nextclade clade output",
)
parser.add_argument("first_file")
parser.add_argument("second_file")
parser.add_argument("-o", default=sys.stdout)
returnparser.parse_args()
defdatestr_to_ordinal(x):
try:
returndatetime.strptime(x,"%Y-%m-%d").toordinal()
except:
returnnp.nan
defisfloat(value):
try:
float(value)
returnTrue
exceptValueError:
returnFalse
defmain():
args=parse_args()
metadata=pd.read_csv(args.first_file, index_col=METADATA_JOIN_COLUMN_NAME,
sep='\t', low_memory=False)
# Check for existing annotations in the given metadata. Skip join with
# Nextclade QC file, if those annotations already exist and none of the
# columns have empty values. In the case where metadata were combined from
# different sources with and without annotations, the "clock_deviation"
# column will exist but some values will be missing. We handle this case as
# if the annotations do not exist at all and reannotate all columns. We
# cannot look for missing values across all expected columns as evidence of
# incomplete annotations, since a complete annotation by Nextclade will
# include missing values for some columns by design.
expected_columns=list(column_map.values()) + ["clock_deviation"]
existing_annotation_columns=metadata.columns.intersection(expected_columns)
iflen(existing_annotation_columns) ==len(expected_columns):
ifmetadata["clock_deviation"].isnull().sum() ==0:
print(f"Metadata file '{args.first_file}' has already been annotated with Nextclade QC columns. Skipping re-annotation.")
metadata.to_csv(args.o, sep="\t")
return
# Read and rename clade column to be more descriptive
clades=pd.read_csv(args.second_file, index_col=NEXTCLADE_JOIN_COLUMN_NAME,
sep='\t', low_memory=False, na_filter=False) \
.rename(columns=column_map)
clade_columns=clades.columns.intersection(list(column_map.values()))
clades=clades[clade_columns]
# Concatenate on columns
result=pd.merge(
metadata, clades,
left_index=True,
right_index=True,
how='left',
suffixes=["_original", ""],
)
all_clades=result.Nextstrain_clade.unique()
t=result["date"].apply(datestr_to_ordinal)
div_array=np.array([float(x) ifisfloat(x) elsenp.nanforxinresult.divergence])
offset_by_clade= {}
forcladeinall_clades:
ind=result.Nextstrain_clade==clade
ifind.sum()>100:
deviation=div_array[ind] - (t[ind] -reference_day)*rate_per_day
offset_by_clade[clade] =np.mean(deviation[~np.isnan(deviation)])
# extract divergence, time and offset information into vectors or series
offset=result["Nextstrain_clade"].apply(lambdax: offset_by_clade.get(x, 2.0))
# calculate divergence
result["clock_deviation"] =np.array(div_array- ((t-reference_day)*rate_per_day+offset), dtype=int)
result.loc[np.isnan(div_array)|np.isnan(t), "clock_deviation"] =np.nan
forcolinlist(column_map.values()) + ["clock_deviation"]:
ifcolinresult:
result[col] =result[col].fillna(VALUE_MISSING_DATA)
# Move the new column so that it's next to other clade columns
ifINSERT_BEFORE_THIS_COLUMNinresult.columns:
result=reorder_columns(result) #.astype(preferred_types)
result.to_csv(args.o, index_label=METADATA_JOIN_COLUMN_NAME, sep='\t')
if__name__=='__main__':
main()