-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_filters.py
284 lines (248 loc) · 9.63 KB
/
test_filters.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Test GENIE filters
"""
import datetime
import os
import sys
from unittest.mock import patch
import pandas as pd
from genie.process_functions import seqDateFilter
from genie import database_to_staging
from genie.database_to_staging import (
seq_assay_id_filter,
redact_phi,
no_genepanel_filter,
_to_redact_interval,
_redact_year,
_to_redact_difference,
)
from genie.consortium_to_public import commonVariantFilter
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(SCRIPT_DIR, "../../genie"))
# syn = synapseclient.login()
def test_seqassayfilter():
# SEQ ASSAY ID filter, only seq assay ids with more than
sagetest = ["SAGE-TEST-1"] * 51
sagetest.extend(["SAGE-TEST-2"] * 10)
clinicalDf = pd.DataFrame(sagetest)
clinicalDf.rename(columns={0: "SEQ_ASSAY_ID"}, inplace=True)
clinicalDf["SAMPLE_ID"] = range(0, len(sagetest))
# Make sure that only samples of seq assay ids that have 50 or
# more samples are returned
samples = seq_assay_id_filter(clinicalDf).reset_index()
# Must make sure that seq assays have the same index to do comparisons
del samples["index"]
samples = samples["SAMPLE_ID"]
assert all(samples == pd.Series(range(51, 61)))
def test_seqdatefilter():
# Check that seq date filter works correctly
SEQ_DATE = "Jan-2018"
processingDate = datetime.datetime.strptime(SEQ_DATE, "%b-%Y")
clinicalDf = pd.DataFrame(
[
"SAGE-TEST-1",
"SAGE-TEST-2",
"SAGE-TEST-3",
"SAGE-TEST-4",
"SAGE-TEST-5",
"SAGE-TEST-6",
"SAGE-TEST-7",
]
)
clinicalDf.rename(columns={0: "SAMPLE_ID"}, inplace=True)
expected = [
"SAGE-TEST-1",
"SAGE-TEST-2",
"SAGE-TEST-3",
"SAGE-TEST-4",
"SAGE-TEST-5",
"SAGE-TEST-6",
]
clinicalDf["SEQ_DATE"] = [
"Jan-2018",
"Apr-2018",
"Oct-2017",
"Jul-2017",
"Apr-2017",
"Jan-2017",
"Oct-2016",
]
samples = seqDateFilter(clinicalDf, processingDate, 366)
assert all(samples == expected)
# Half year filter
samples = seqDateFilter(clinicalDf, processingDate, 184)
expected = ["SAGE-TEST-1", "SAGE-TEST-2", "SAGE-TEST-3", "SAGE-TEST-4"]
assert all(samples == expected)
# Test leap year
SEQ_DATE = "Jan-2017"
processingDate = datetime.datetime.strptime(SEQ_DATE, "%b-%Y")
clinicalDf = pd.DataFrame(
[
"SAGE-TEST-1",
"SAGE-TEST-2",
"SAGE-TEST-3",
"SAGE-TEST-4",
"SAGE-TEST-5",
"SAGE-TEST-6",
"SAGE-TEST-7",
]
)
clinicalDf.rename(columns={0: "SAMPLE_ID"}, inplace=True)
clinicalDf["SEQ_DATE"] = [
"Jan-2017",
"Apr-2017",
"Oct-2016",
"Jul-2016",
"Apr-2016",
"Jan-2016",
"Oct-2015",
]
expected = [
"SAGE-TEST-1",
"SAGE-TEST-2",
"SAGE-TEST-3",
"SAGE-TEST-4",
"SAGE-TEST-5",
"SAGE-TEST-6",
]
samples = seqDateFilter(clinicalDf, processingDate, 366)
assert all(samples == expected)
# Check half year release date during leap year
SEQ_DATE = "Jul-2016"
processingDate = datetime.datetime.strptime(SEQ_DATE, "%b-%Y")
samples = seqDateFilter(clinicalDf, processingDate, 184)
assert all(samples == expected)
def test__to_redact_interval():
"""Tests the correct boolean vectors are returned for phi and pediatric
redaction"""
values = pd.Series([32850, 32485, 6570, 6569, "<foo", ">testing"])
to_redact, to_redact_peds = _to_redact_interval(values)
expected_redact = [True, False, False, False, False, True]
expected_redact_peds = [False, False, False, True, True, False]
assert to_redact.to_list() == expected_redact
assert to_redact_peds.to_list() == expected_redact_peds
def test__redact_year():
"""Tests redaction of birth year based on < and >"""
values = pd.Series([1923, 2003, "<foo", ">testing"])
redacted = _redact_year(values)
expected_redact = [1923, 2003, "withheld", "cannotReleaseHIPAA"]
assert redacted.to_list() == expected_redact
def test___to_redact_difference():
"""Tests if a difference between two year columns is >89, redact"""
year1 = pd.Series([1923, 2000, float("nan")])
year2 = pd.Series([1926, 2100, 2000])
redacted = _to_redact_difference(year1, year2)
expected_redact = [False, True, False]
assert redacted.to_list() == expected_redact
def test_redact_phi():
"""Redacts PHI interval and years"""
return_bools = ([True, False, False], [False, False, True])
clinicaldf = pd.DataFrame(["SAGE-TEST-1", "SAGE-TEST-2", "SAGE-TEST-3"])
clinicaldf.rename(columns={0: "SAMPLE_ID"}, inplace=True)
clinicaldf["AGE_AT_SEQ_REPORT"] = [32850, 32485, 6570]
clinicaldf["BIRTH_YEAR"] = [1900, "<1900", 1902]
# These are the years that are returned by the _redact_year
# Use these against YEAR_CONTACT and YEAR_DEATH to calculate
# expected_birth
return_year = pd.Series([2000, 1903, 1904])
clinicaldf["YEAR_CONTACT"] = [2100, 1904, float("nan")]
clinicaldf["YEAR_DEATH"] = [2001, float("nan"), 2000]
expected_age = pd.Series([">32485", 32485, "<6570"])
expected_birth = pd.Series(["cannotReleaseHIPAA", 1903, "cannotReleaseHIPAA"])
with patch.object(
database_to_staging, "_to_redact_interval", return_value=return_bools
), patch.object(database_to_staging, "_redact_year", return_value=return_year):
redacted_clin = redact_phi(
clinicaldf, interval_cols_to_redact=["AGE_AT_SEQ_REPORT"]
)
assert all(redacted_clin["AGE_AT_SEQ_REPORT"] == expected_age)
assert all(redacted_clin["BIRTH_YEAR"] == expected_birth)
# def test_MAFinBED():
# syn = mock.create_autospec(synapseclient.Synapse)
# # MAF in BED filter (Make sure that the only Hugo symbol that
# passes through this filter is the ERRFI1 gene)
# databaseSynIdMapping = syn.tableQuery('select * from syn11600968')
# databaseSynIdMappingDf = databaseSynIdMapping.asDataFrame()
# CENTER_MAPPING = syn.tableQuery(
# 'SELECT * FROM syn11601248 where stagingSynId is not null')
# CENTER_MAPPING_DF = CENTER_MAPPING.asDataFrame()
# mafPath = os.path.join(SCRIPT_DIR,"testingmaf.txt")
# temp = runMAFinBED(
# syn, mafPath, CENTER_MAPPING_DF, databaseSynIdMappingDf, test=True)
# maf = pd.read_csv(mafPath,sep="\t")
# assert all(maf['Hugo_Symbol'][maf['inBED'] == True].unique() == "ERRFI1")
# os.unlink(mafPath)
# def test_MutationInCis():
# def createMockTable(dataframe):
# table = mock.create_autospec(synapseclient.table.CsvFileTable)
# table.asDataFrame.return_value= dataframe
# return(table)
# def table_query_results(*args):
# return(table_query_results_map[args])
# databaseMapping = pd.DataFrame(dict(Database=['mutationsInCis'],
# Id=['syn7765462']))
# centerMapping = pd.DataFrame(dict(center=['SAGE'],
# inputSynId=['syn11601335'],
# stagingSynId=['syn11601337']))
# mutCisDf = pd.DataFrame(dict(Flag=['mutationsInCis'],
# Center=['syn7765462'],
# Tumor_Sample_Barcode=["GENIE-SAGE-ID1-1"],
# Variant_Classification=["Nonsense_Mutation"],
# Hugo_Symbol=["AKT1"],
# HGVSp_Short=["p.1234"],
# Chromosome=["1"],
# Start_Position=[324234],
# Reference_Allele=["AGC"],
# Tumor_Seq_Allele2=["GCCCT"],
# t_alt_count_num=[3],
# t_depth=[234]))
# #This is the gene positions that all bed
# dataframe will be processed against
# table_query_results_map = {
# ('select * from syn11600968',):
# createMockTable(databaseMapping),
# ('SELECT * FROM syn11601248 where staginsgSynId is not null',):
# createMockTable(centerMapping),
# ("select * from syn7765462 where Center = 'SAGE'",):
# createMockTable(mutCisDf)
# }
# syn = mock.create_autospec(synapseclient.Synapse)
# syn.tableQuery.side_effect=table_query_results
# # Mutation in cis filter check (There is one TOSS sample,
# it should be GENIE-TEST-0-1)
# remove_samples = mutation_in_cis_filter(
# syn, True, None, None, "syn11601206", None)
# assert all(remove_samples == "GENIE-TEST-0-1")
def test_commonvariantfilter():
"""
Test common variant filter, make sure none
of the common_variant rows are kept
"""
mutationDf = pd.DataFrame(
[
"common_variant",
"test1",
"asdfasd:common_variant",
"fo;common_variant",
"test2",
"test3",
]
)
mutationDf.rename(columns={0: "FILTER"}, inplace=True)
maf = commonVariantFilter(mutationDf)
expected = ["test1", "test2", "test3"]
assert all(maf["FILTER"] == expected)
def test_no_genepanel_filter():
"""
Tests the filter to remove samples
with no bed files
"""
sagetest = ["SAGE-TEST-1"] * 51
beddf = pd.DataFrame(sagetest)
beddf.rename(columns={0: "SEQ_ASSAY_ID"}, inplace=True)
sagetest.extend(["SAGE-TEST-2"] * 10)
clinicaldf = pd.DataFrame(sagetest)
clinicaldf.rename(columns={0: "SEQ_ASSAY_ID"}, inplace=True)
clinicaldf["SAMPLE_ID"] = sagetest
remove_samples = no_genepanel_filter(clinicaldf, beddf)
assert all(remove_samples == ["SAGE-TEST-2"] * 10)