Skip to content

Commit

Permalink
Merge pull request #222 from nvictus/master
Browse files Browse the repository at this point in the history
Fix get_header to work with different peek implementations
  • Loading branch information
nvictus authored Sep 25, 2020
2 parents a565090 + 10b414a commit 583ca35
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: python
python:
# We don't actually use the Travis Python, but this keeps it organized.
- "2.7"
- "3.6"
- "3.7"
- "3.8"
Expand Down
21 changes: 16 additions & 5 deletions cooler/cli/cload.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,28 @@ def get_header(instream, comment_char='#'):
raise ValueError('Please, provide a comment char!')
comment_byte = comment_char.encode()
# get peekable buffer for the instream
inbuffer = instream.buffer
current_peek = inbuffer.peek()
read_f, peek_f = None, None
if hasattr(instream, 'buffer'):
peek_f = instream.buffer.peek
readline_f = instream.buffer.readline
elif hasattr(instream, 'peek'):
peek_f = instream.peek
readline_f = instream.readline
else:
raise ValueError('Cannot find the peek() function of the provided stream!')

current_peek = peek_f(1)
while current_peek.startswith(comment_byte):
# consuming a line from buffer guarantees
# that the remainder of the buffer starts
# with the beginning of the line.
line = inbuffer.readline()
line = readline_f()
if isinstance(line, bytes):
line = line.decode()
# append line to header, since it does start with header
header.append(line.decode().strip())
header.append(line.strip())
# peek into the remainder of the instream
current_peek = inbuffer.peek()
current_peek = peek_f(1)
# apparently, next line does not start with the comment
# return header and the instream, advanced to the beginning of the data
return header, instream
Expand Down
Binary file added tests/data/toy_hash.pairs.gz
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/test_create_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ def test_cload_pairix(bins_path, pairs_path, ref_path):
op.join(testdir, "data", "toy.bins.var.bed"),
op.join(testdir, "data", "toy_hash.pairs"),
op.join(testdir, "data", "toy.symm.upper.var.cool"),
),
(
op.join(testdir, "data", "toy.bins.var.bed"),
op.join(testdir, "data", "toy_hash.pairs.gz"),
op.join(testdir, "data", "toy.symm.upper.var.cool"),
)
],
)
Expand Down

0 comments on commit 583ca35

Please sign in to comment.