-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #635 from broadinstitute/is-new-add-utils
added various file-handling utils, and tests for them
- Loading branch information
Showing
5 changed files
with
167 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
__author__ = "[email protected]" | ||
|
||
import unittest | ||
import os | ||
import os, os.path | ||
import tempfile | ||
import shutil | ||
import Bio.SeqIO, Bio.SeqRecord, Bio.Seq | ||
import util | ||
import util.file | ||
import tools | ||
|
@@ -67,4 +68,16 @@ def test_filterByCigarString(self): | |
# '^((?:[0-9]+[ID]){1}(?:[0-9]+[MNIDSHPX=])+)|((?:[0-9]+[MNIDSHPX=])+(?:[0-9]+[ID]){1})$' | ||
samtools.filterByCigarString(in_sam, out_bam) | ||
|
||
assert samtools.count(out_bam)==39, "Output read count does not match the expected count." | ||
assert samtools.count(out_bam)==39, "Output read count does not match the expected count." | ||
|
||
def test_bam2fa(self): | ||
samtools = tools.samtools.SamtoolsTool() | ||
sam = os.path.join(util.file.get_test_input_path(self), 'simple.sam') | ||
|
||
with samtools.bam2fa_tmp(sam) as (fa1, fa2): | ||
for fa in (fa1, fa2): | ||
assert len(list(Bio.SeqIO.parse(fa, 'fasta')))==1 | ||
|
||
assert not os.path.isfile(fa1) and not os.path.isfile(fa2) | ||
|
||
|
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,50 @@ | ||
# Unit tests for util.file.py | ||
|
||
__author__ = "[email protected]" | ||
|
||
import os, os.path | ||
import pytest | ||
import util.file | ||
|
||
def testTempFiles(): | ||
'''Test creation of tempfiles using context managers, as well as dump_file/slurp_file routines''' | ||
tmp_fns = [] | ||
sfx='tmp-file-test' | ||
with util.file.tempfname(sfx) as my_tmp_fn: | ||
tmp_dir_listing = os.listdir( os.path.dirname( my_tmp_fn ) ) | ||
assert os.path.basename( my_tmp_fn ) in tmp_dir_listing | ||
|
||
with util.file.tempfnames((sfx+'-A',sfx+'-B')) as my_tmp_fns: | ||
|
||
assert len(my_tmp_fns)==2 | ||
for fn in my_tmp_fns: | ||
assert os.path.dirname(fn) == os.path.dirname(my_tmp_fn) | ||
assert os.path.basename(fn) not in tmp_dir_listing | ||
|
||
for fn in my_tmp_fns: | ||
|
||
assert sfx in fn | ||
assert os.path.isfile(fn) | ||
assert os.path.getsize(fn)==0 | ||
assert os.access(fn, os.R_OK | os.W_OK) | ||
|
||
fileValue='my\ntest\ndata\n' + fn + '\n' | ||
util.file.dump_file( fname=fn, value=fileValue ) | ||
assert os.path.isfile(fn) | ||
assert os.path.getsize(fn) == len(fileValue) | ||
assert util.file.slurp_file(fn) == fileValue | ||
|
||
util.file.make_empty(fn) | ||
assert os.path.getsize(fn)==0 | ||
|
||
tmp_fns.append(fn) | ||
|
||
assert os.path.isfile(my_tmp_fn) and not os.path.isfile(my_tmp_fns[0]) and not os.path.isfile(my_tmp_fns[1]) | ||
|
||
largeString = 'A' * (2*1024*1024) | ||
util.file.dump_file(fname=my_tmp_fn, value=largeString) | ||
with pytest.raises(RuntimeError): | ||
util.file.slurp_file(my_tmp_fn, maxSizeMb=1) | ||
|
||
assert not os.path.isfile(my_tmp_fn) | ||
|
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
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