Skip to content

Commit

Permalink
Merge pull request #451 from akmorrow13/python3
Browse files Browse the repository at this point in the history
added compatibility for python3
  • Loading branch information
akmorrow13 authored Oct 18, 2018
2 parents 4b738ba + 624ce5d commit d504a85
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions mango-python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ help:

# This Makefile uses bash features like printf and <()
SHELL=bash
python=python2.7
python=python
pip=pip
tests=bdgenomics
extras=
Expand Down Expand Up @@ -113,7 +113,7 @@ check_build_reqs:


prepare:
$(pip) install pytest==2.8.3
$(pip) install pytest==3.9.1
# ipython version required for python 2.7
$(pip) install jupyter_contrib_nbextensions
$(pip) install -r requirements.txt
Expand Down
11 changes: 6 additions & 5 deletions mango-python/bdgenomics/mango/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def __init__(self):
approximateCounts = lambda counts, sample: int(counts * 1.0/sample)

# restructure each record so record structure is (key: sampleId, value: (coverage, count))
x = map(lambda x: (x[0][0], (x[0][1], approximateCounts(x[1], self.sample))), collectedCounts)
x = list(map(lambda x: (x[0][0], (x[0][1], approximateCounts(x[1], self.sample))), collectedCounts))

# create dictionary where keys are the sampleId
self.collectedCounts = collections.defaultdict(set)
for k, v in x:
Expand Down Expand Up @@ -99,14 +100,14 @@ def plotDistributions(self, normalize = True, cumulative = False, testMode = Fal
f, ax = plt.subplots(figsize=figsize)

# iterate through each sample
for label, data in self.collectedCounts.iteritems():
for label, data in self.collectedCounts.items():

if data == set(): # Do not plot any empty sets of data
continue

sData = sorted(data)
values = map(lambda p: p[0], sData)
counts = map(lambda p: p[1], sData)
values = list(map(lambda p: p[0], sData))
counts = list(map(lambda p: p[1], sData))

if normalize:
# replace distribution counts with normalized values
Expand All @@ -118,7 +119,7 @@ def plotDistributions(self, normalize = True, cumulative = False, testMode = Fal
counts = np.cumsum(counts)

# re-write manipulated data
countDistributions[label]=zip(values, counts)
countDistributions[label]=list(zip(values, counts))

if (not testMode): # For testing: do not run plots if testMode
if (bar_plt):
Expand Down
8 changes: 4 additions & 4 deletions mango-python/bdgenomics/mango/test/alignment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ def test_coverage_distribution(self):
_, cd = coverage.plotDistributions(testMode = True, cumulative = False, normalize=False)

# first sample
items = cd.items()[0][1]
items = list(cd.popitem()[1])
assert(len(items) == 1)
x = items.pop()
assert(x[0] == 1) # all positions have coverage of 1
assert(x[0] == 1) # all positions have coverage of 1
assert(x[1] == 1500)


Expand All @@ -131,9 +132,8 @@ def test_fragment_distribution(self):
_, cd = fragments.plotDistributions(testMode = True, cumulative = False, normalize=False)

# first sample
items = cd.items()[0][1]
items = list(cd.popitem()[1])
assert(len(items) == 1)
print(cd)
x = items[0]
assert(x[0] == 75)
assert(x[1] == 20) # all 20 sequences have same length of 75
Expand All @@ -152,7 +152,7 @@ def test_mapq_distribution(self):
_, md = mapq.plotDistributions(testMode = True, cumulative = False, normalize = False)

# first sample
items = md.items()[0][1]
items = list(md.popitem()[1])
assert(len(items) == 5) # mapq for 24, 28, 35, 40, 60
x = items[0]
assert(x[0] == 24) # 1 read with mapq 24
Expand Down
12 changes: 5 additions & 7 deletions mango-python/bdgenomics/mango/test/coverage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def test_coverage_distribution(self):
assert(len(cd) == 1)

# all items for first sample
items = cd.items()[0][1]
items = list(cd.popitem()[1])

print("Coverage")
print(cd)
assert(items[0][1] == 1500)

def test_example_coverage(self):
Expand All @@ -60,28 +58,28 @@ def test_example_coverage(self):

qc = CoverageDistribution(self.ss, coverage, bin_size = 1)
# sum of all coverage
total = sum(map(lambda x: x[1], qc.collectedCounts.items()[0][1]))

_, cd1 = qc.plotDistributions(testMode = True, cumulative = False, normalize=False)
total = sum(map(lambda x: x[1], list(qc.collectedCounts.items())[0][1]))

# first sample
items = cd1.items()[0][1]
items = list(cd1.popitem()[1])
x = items[0]
assert(x[0] == 1) # 6 locations with read depth 1
assert(x[1] == 6)

_, cd2 = qc.plotDistributions(testMode = True, cumulative = False, normalize=True)

# first sample
items = cd2.items()[0][1]
items = list(cd2.popitem()[1])
x = items[0]
assert(x[0] == 1)
assert(x[1] == 6.0/total) # normalized value

_, cd3 = qc.plotDistributions(testMode = True, cumulative = True, normalize = True)

# first sample
items = cd3.items()[0][1]
items = list(cd3.popitem()[1])
x = items[-1]
assert(x[0] == 89)
assert(x[1] > 0.999) # cumulative and normalized, so last value shound be about 1
14 changes: 6 additions & 8 deletions mango-python/bdgenomics/mango/test/distribution_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def test_normalized_count_distribution(self):
qc = CoverageDistribution(self.ss, coverage)

_, cd = qc.plotDistributions(testMode = True, normalize = True)
items = cd.items()[0][1]
items = list(cd.popitem()[1])
assert(len(items) == 1)
assert(items.pop()[1] == 1.0)

_, cd = qc.plotDistributions(testMode = True, normalize = False)
items = cd.items()[0][1]
items = list(cd.popitem()[1])
assert(len(items) == 1)
assert(items.pop()[1] == 1500)

Expand All @@ -64,15 +64,14 @@ def test_cumulative_count_distribution(self):
_, cd = qc.plotDistributions(testMode = True, cumulative = True, normalize = False)

# first sample
items = cd.items()[0][1]
items = list(cd.popitem()[1])
assert(len(items) == 1)
print(items)
assert(items.pop()[1] == 1500)

_, cd = qc.plotDistributions(testMode = True, cumulative = False, normalize = False)

# first sample
items = cd.items()[0][1]
items = list(cd.popitem()[1])
assert(len(items) == 1)
assert(items.pop()[1] == 1500)

Expand Down Expand Up @@ -103,11 +102,10 @@ def test_sampling(self):
coverage = reads.toCoverage()

cd1 = CoverageDistribution(self.ss, coverage, sample = 0.9)
sum1 = sum(map(lambda x: x[1], cd1.collectedCounts.items()[0][1]))
sum1 = sum(map(lambda x: x[1], cd1.collectedCounts.popitem()[1]))
cd2 = CoverageDistribution(self.ss, coverage, sample = 1.0)
sum2 = sum(map(lambda x: x[1], cd2.collectedCounts.items()[0][1]))
sum2 = sum(map(lambda x: x[1], cd2.collectedCounts.popitem()[1]))

# estimated counts should be around real counts
dev = 100
assert(sum1 > sum2 - dev and sum1 < sum2 + dev)

7 changes: 3 additions & 4 deletions mango-python/bdgenomics/mango/test/notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_example(self):

# this file is converted from ipynb in make test
testFile = self.exampleFile("notebooks/mango-viz.py")
execfile(testFile)
exec(open(testFile).read())

def test_coverage_example(self):
# these variables are read into mango-python.py
Expand All @@ -43,7 +43,7 @@ def test_coverage_example(self):

# this file is converted from mango-python.coverage.ipynb in the Makefile
testCoverageFile = self.exampleFile("notebooks/mango-python-coverage.py")
execfile(testCoverageFile)
exec(open(testCoverageFile).read())

def test_alignment_example(self):
# these variables are read into mango-python.py
Expand All @@ -53,5 +53,4 @@ def test_alignment_example(self):

# this file is converted from mango-python-alignment.ipynb in the Makefile
testAlignmentFile = self.exampleFile("notebooks/mango-python-alignment.py")
execfile(testAlignmentFile)

exec(open(testAlignmentFile).read())

0 comments on commit d504a85

Please sign in to comment.