Skip to content

Commit

Permalink
update split call (for python2) added gzip file comp
Browse files Browse the repository at this point in the history
  • Loading branch information
aewebb80 committed Jul 6, 2020
1 parent 829646b commit f3603d0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
69 changes: 64 additions & 5 deletions jhtests/jhtests_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import logging
import shutil
import zipfile
import gzip
import tempfile
import hashlib

## Import scripts to test

Expand All @@ -20,10 +23,66 @@
def file_comp(test_output, expected_output):
return filecmp.cmp(test_output, expected_output)

## Used to compare two gz files, uses file_comp and a temp dir
def gz_file_comp (test_output, expected_output, tmp_dir):

# Create the tmp paths
tmp_test_path = os.path.join(tmp_dir, 'Test')
tmp_expected_path = os.path.join(tmp_dir, 'Expected')

# Create test tmp directories, if needed
if not os.path.exists(tmp_test_path):
os.makedirs(tmp_test_path)

# Create test expected directories, if needed
if not os.path.exists(tmp_expected_path):
os.makedirs(tmp_expected_path)

# Assign the tmp output files
tmp_test_output = os.path.join(tmp_test_path, os.path.basename(test_output))
tmp_expected_output = os.path.join(tmp_expected_path, os.path.basename(expected_output))

# Open the gzip file
with gzip.open(test_output, 'rb') as test_file:

# Open the gunzip file
with open(tmp_test_output, 'wb') as tmp_test_file:

# Copy the file
shutil.copyfileobj(test_file, tmp_test_file)

# Open the gzip file
with gzip.open(expected_output, 'rb') as expected_file:

# Open the gunzip file
with open(tmp_expected_output, 'wb') as tmp_expected_file:

# Copy the file
shutil.copyfileobj(expected_file, tmp_expected_file)

# Check if the files have the same content
file_compare_results = file_comp(tmp_test_output, tmp_expected_output)

# Remove the tmp dirs
shutil.rmtree(tmp_test_path)
shutil.rmtree(tmp_expected_path)

# Return the results
return file_compare_results

class jh_function_tests (unittest.TestCase):

@classmethod
def setUpClass (cls):
# Create a temporary directory
cls.test_dir = tempfile.mkdtemp()

@classmethod
def tearDownClass (cls):

# Remove the test directory after the tests
shutil.rmtree(cls.test_dir)

def test_vcf_bed_to_seq1 (self):
vcf_bed_to_seq.run(['--vcf','pan_example.vcf.gz',
'--fasta-reference',"pan_example_ref.fa",
Expand Down Expand Up @@ -110,9 +169,9 @@ def test_vcf_to_treemix1(self):
'--out','ppp_test_temp.out',
'--bed-file',"pan_example_regions.bed",
'--kblock','1000'])

# Confirm that the output is what is expected
self.assertTrue(file_comp('ppp_test_temp.out.gz',
'results/vcf_treemixtest1.gz'))
self.assertTrue(gz_file_comp('ppp_test_temp.out.gz', 'results/vcf_treemixtest1.gz', self.test_dir))

# Remove the ouput
self.addCleanup(os.remove, 'ppp_test_temp.out.gz')
Expand All @@ -122,9 +181,9 @@ def test_vcf_to_treemix2(self):
'--model-file',"panmodels.model",
'--modelname',"4Pop",
'--out','ppp_test_temp.out'])

# Confirm that the output is what is expected
self.assertTrue(file_comp('ppp_test_temp.out.gz',
'results/vcf_treemixtest2.gz'))
self.assertTrue(gz_file_comp('ppp_test_temp.out.gz', 'results/vcf_treemixtest2.gz', self.test_dir))

# Remove the ouput
self.addCleanup(os.remove, 'ppp_test_temp.out.gz')
Expand All @@ -137,7 +196,7 @@ def test_vcf_to_fastsimcoal1(self):
'--modelname','3Pop',
'--dim','1','2','m',
'--basename','ppp_test_temp.out'])
# must extract archives to check that files match
# must extract archives to check that files match
pz = zipfile.ZipFile('ppp_test_temp.out.zip',mode='r')
vz = zipfile.ZipFile('results/vcf_fsc1.zip',mode='r')
pznl = pz.namelist()
Expand Down
2 changes: 1 addition & 1 deletion pgpipe/vcf_bed_to_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def get_model_sequences_from_region(vcf=None,popmodel=None,
assert isinstance(region,str)
regionstr = region
chrname = regionstr[:regionstr.find(':')]
[start,end] = map(int,regionstr[regionstr.find(':')+1:].split(sep='-'))
[start,end] = map(int,regionstr[regionstr.find(':')+1:].split('-'))
region = Region(start-1,end,chrname)
else: # make regionstr
regionstr = '%s:%d-%d'%(region.chrom,region.start+1,region.end)# must add 1 to start because Region's use half open 0-based
Expand Down

0 comments on commit f3603d0

Please sign in to comment.