-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grp.py: added tests, improved docstrings
- Loading branch information
lev
committed
Sep 22, 2017
1 parent
f712577
commit 662c443
Showing
5 changed files
with
110 additions
and
54 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
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 |
---|---|---|
@@ -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") |
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,5 @@ | ||
#a | ||
r | ||
d | ||
e | ||
#f |
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
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() |