Skip to content

Commit 951f248

Browse files
committed
add more test and fix bugs
1 parent b300325 commit 951f248

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

coolbox/utilities/reader/tab.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ def query(
266266
else:
267267
itr = tabix_query(self.path, gr, split=True)
268268
rows = list(itr)
269-
df = pd.DataFrame(rows)
270269
if self.suffix in [".bed", ".bedgraph", ".bg"]:
271270
columns = FMT2COLUMNS[self.bed_type]
272271
else:
@@ -277,7 +276,7 @@ def query(
277276
columns = self.params.get('columns', None)
278277
if columns is None:
279278
raise ValueError(f"Columns are not specified for file type: {fmt}")
280-
df.columns = columns
279+
df = pd.DataFrame(rows, columns=columns)
281280
return df
282281

283282

tests/test_tab_reader.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,69 @@ def test_oxbow_gtf_query(data_dir, test_interval, test_itv):
149149
assert col in df.columns
150150
assert df.shape[0] > 0
151151

152+
153+
def test_oxbow_bigwig_query(data_dir, test_interval, test_itv):
154+
path = f"{data_dir}/bigwig_{test_itv}.bw"
155+
rdr = TabFileReaderWithOxbow(path)
156+
df = rdr.query(test_interval)
157+
# BigWig should have chrom,start,end,value
158+
for col in FMT2COLUMNS["bigwig"]:
159+
assert col in df.columns
160+
assert df.shape[0] > 0
161+
162+
163+
@pytest.mark.skipif(
164+
platform.system() == "Windows" or not check_tool("samtools")[0],
165+
reason="samtools not available or non-Unix",
166+
)
167+
def test_tabix_bam_query(data_dir, test_interval, test_itv):
168+
path = f"{data_dir}/bam_{test_itv}.bam"
169+
indexed_path = index_tab_file(path)
170+
rdr = TabFileReaderWithTabix(indexed_path)
171+
df = rdr.query(test_interval)
172+
assert list(df.columns) == FMT2COLUMNS["bam"]
173+
assert df.shape[0] > 0
174+
175+
176+
@pytest.mark.skipif(
177+
platform.system() == "Windows" or not check_tool("tabix")[0],
178+
reason="tabix not available or non-Unix",
179+
)
180+
def test_tabix_gtf_query(data_dir, test_interval, test_itv):
181+
path = f"{data_dir}/gtf_{test_itv}.gtf"
182+
indexed_path = index_tab_file(path)
183+
rdr = TabFileReaderWithTabix(indexed_path)
184+
df = rdr.query(test_interval)
185+
assert list(df.columns) == FMT2COLUMNS["gtf"]
186+
assert df.shape[0] > 0
187+
188+
189+
@pytest.mark.skipif(
190+
platform.system() == "Windows"
191+
or not (check_tool("bgzip")[0] and check_tool("pairix")[0]),
192+
reason="bgzip/pairix not available or non-Unix",
193+
)
194+
def test_tabix_bedpe_query(data_dir, test_interval, test_itv):
195+
path = f"{data_dir}/bedpe_{test_itv}.bedpe"
196+
indexed_path = index_tab_file(path)
197+
rdr = TabFileReaderWithTabix(indexed_path)
198+
df_same = rdr.query(test_interval)
199+
assert list(df_same.columns) == FMT2COLUMNS["bedpe"]
200+
assert df_same.shape[0] > 0
201+
df_2d = rdr.query(test_interval, second=test_interval)
202+
assert df_2d.shape[0] > 0
203+
204+
205+
@pytest.mark.skipif(
206+
platform.system() == "Windows" or not check_tool("samtools")[0],
207+
reason="samtools not available or non-Unix",
208+
)
209+
def test_oxbow_bam_query(data_dir, test_interval, test_itv):
210+
path = f"{data_dir}/bam_{test_itv}.bam"
211+
indexed_path = index_tab_file(path)
212+
rdr = TabFileReaderWithOxbow(indexed_path)
213+
df = rdr.query(test_interval)
214+
assert df.shape[0] > 0
215+
# Check a few SAM/BAM fields commonly present
216+
for col in FMT2COLUMNS["bam"]:
217+
assert col in df.columns

0 commit comments

Comments
 (0)