Skip to content

Commit

Permalink
grp.py: added tests, improved docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
lev committed Sep 22, 2017
1 parent f712577 commit 662c443
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 54 deletions.
32 changes: 27 additions & 5 deletions cmapPy/set_io/gmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@


def read(file_path):
"""
Read a gmt file at the path specified by file_path.
""" Read a gmt file at the path specified by file_path.
Args:
file_path (string): path to gmt file
Returns:
gmt (GMT object): list of dicts, where each dict corresponds to one
line of the GMT file
"""
# Read in file
actual_file_path = os.path.expanduser(file_path)
Expand Down Expand Up @@ -67,6 +74,15 @@ def read(file_path):


def verify_gmt_integrity(gmt):
""" Make sure that set ids are unique.
Args:
gmt (GMT object): list of dicts
Returns:
None
"""

# Verify that set ids are unique
set_ids = [d[SET_IDENTIFIER_FIELD] for d in gmt]
Expand All @@ -75,10 +91,16 @@ def verify_gmt_integrity(gmt):


def write(gmt, out_path):
"""
Write a GMT to a text file.
"""
""" Write a GMT to a text file.
Args:
gmt (GMT object): list of dicts
out_path (string): output path
Returns:
None
"""
with open(out_path, 'w') as f:
for _, each_dict in enumerate(gmt):
f.write(each_dict[SET_IDENTIFIER_FIELD] + '\t')
Expand Down
84 changes: 37 additions & 47 deletions cmapPy/set_io/grp.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,49 @@
"""
module with class definition and methods for reading and writing .grp files
Created on Jun 20, 2012
@author: David Wadden
grp.py
IO methods for handling GRP files.
A GRP file is stored as a list. Lines beginning with # are ignored.
AUTHOR: David Wadden, Broad Institute, 2012
MODIFIED: Lev Litichevskiy, 2017
"""

import os
import re


class GRP:
"""
class to read .grp files and return a list
"""
def __init__(self, src):
# if it"s a file string, check that it exists and read the file
if type(src) is str:
assert os.path.exists(src), "{0} is not a valid file path. Use a list to input plate names directly".format(src)
self.read(src)
# if it's a list, just read it in
elif type(src) is list:
self.grp = src

def read(self, in_path):
"""
read a .grp file
"""
with open(in_path, "r") as f:
lines = f.readlines()
# need the second conditional to ignore comment lines
self.grp = [line.strip() for line in lines if line and not re.match("^#", line)]

def write(self, out):
"""
write a .grp file
"""
with open(out, "w") as f:
for x in self.grp:
f.write(str(x) + "\n")


def write_grp(in_list, out):
"""
standalone methods to write .grp files
"""
with open(out, "w") as f:
for x in in_list:
f.write(str(x) + "\n")
def read(in_path):
""" Read a grp file at the path specified by in_path.
Args:
in_path (string): path to GRP file
Returns:
grp (list)
def read_grp(in_path):
"""
standalone method to read .grp files
"""
assert os.path.exists(in_path), "The following file can't be found. in_path: {}".format(in_path)
assert os.path.exists(in_path), "The following GRP file can't be found. in_path: {}".format(in_path)

with open(in_path, "r") as f:
lines = f.readlines()
# again, second conditional ignores comment lines
return [line.strip() for line in lines if line and not re.match("^#", line)]
# need the second conditional to ignore comment lines
grp = [line.strip() for line in lines if line and not re.match("^#", line)]

return grp


def write(grp, out_path):
""" Write a GRP to a text file.
Args:
grp (list): GRP object to write to new-line delimited text file
out_path (string): output path
Returns:
None
"""
with open(out_path, "w") as f:
for x in grp:
f.write(str(x) + "\n")
5 changes: 5 additions & 0 deletions cmapPy/set_io/tests/functional_tests/test.grp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#a
r
d
e
#f
4 changes: 2 additions & 2 deletions cmapPy/set_io/tests/test_gmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import os
import unittest
from cmapPy.pandasGEXpress import setup_GCToo_logger as setup_logger
import cmapPy.pandasGEXpress.gmt as gmt
import cmapPy.set_io.gmt as gmt

logger = logging.getLogger(setup_logger.LOGGER_NAME)
FUNCTIONAL_TESTS_DIR = "functional_tests"


class GMT(unittest.TestCase):
class TestGMT(unittest.TestCase):

@classmethod
def setUpClass(cls):
Expand Down
39 changes: 39 additions & 0 deletions cmapPy/set_io/tests/test_grp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
import os
import unittest
from cmapPy.pandasGEXpress import setup_GCToo_logger as setup_logger
import cmapPy.set_io.grp as grp

logger = logging.getLogger(setup_logger.LOGGER_NAME)
FUNCTIONAL_TESTS_DIR = "functional_tests"


class TestGRP(unittest.TestCase):

def test_read(self):

in_grp = grp.read(os.path.join(FUNCTIONAL_TESTS_DIR, "test.grp"))
self.assertEqual(in_grp, ["r", "d", "e"])

with self.assertRaises(AssertionError) as e:
grp.read("testt.grp")
self.assertIn("The following GRP file", str(e.exception))

def test_write(self):

example_grp = ["x", "z", "w"]

out_path = os.path.join(FUNCTIONAL_TESTS_DIR, "test_write.grp")
grp.write(example_grp, out_path)
self.assertTrue(os.path.exists(out_path))

read_back_in = grp.read(out_path)
self.assertEqual(example_grp, read_back_in)

# Cleanup
os.remove(out_path)

if __name__ == "__main__":
setup_logger.setup(verbose=True)

unittest.main()

0 comments on commit 662c443

Please sign in to comment.