Skip to content

Commit 662c443

Browse files
author
lev
committed
grp.py: added tests, improved docstrings
1 parent f712577 commit 662c443

File tree

5 files changed

+110
-54
lines changed

5 files changed

+110
-54
lines changed

cmapPy/set_io/gmt.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@
2222

2323

2424
def read(file_path):
25-
"""
26-
Read a gmt file at the path specified by file_path.
25+
""" Read a gmt file at the path specified by file_path.
26+
27+
Args:
28+
file_path (string): path to gmt file
29+
30+
Returns:
31+
gmt (GMT object): list of dicts, where each dict corresponds to one
32+
line of the GMT file
33+
2734
"""
2835
# Read in file
2936
actual_file_path = os.path.expanduser(file_path)
@@ -67,6 +74,15 @@ def read(file_path):
6774

6875

6976
def verify_gmt_integrity(gmt):
77+
""" Make sure that set ids are unique.
78+
79+
Args:
80+
gmt (GMT object): list of dicts
81+
82+
Returns:
83+
None
84+
85+
"""
7086

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

7692

7793
def write(gmt, out_path):
78-
"""
79-
Write a GMT to a text file.
80-
"""
94+
""" Write a GMT to a text file.
8195
96+
Args:
97+
gmt (GMT object): list of dicts
98+
out_path (string): output path
99+
100+
Returns:
101+
None
102+
103+
"""
82104
with open(out_path, 'w') as f:
83105
for _, each_dict in enumerate(gmt):
84106
f.write(each_dict[SET_IDENTIFIER_FIELD] + '\t')

cmapPy/set_io/grp.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,49 @@
11
"""
2-
module with class definition and methods for reading and writing .grp files
3-
Created on Jun 20, 2012
4-
@author: David Wadden
2+
grp.py
3+
4+
IO methods for handling GRP files.
5+
6+
A GRP file is stored as a list. Lines beginning with # are ignored.
7+
8+
AUTHOR: David Wadden, Broad Institute, 2012
9+
MODIFIED: Lev Litichevskiy, 2017
510
"""
611

712
import os
813
import re
914

1015

11-
class GRP:
12-
"""
13-
class to read .grp files and return a list
14-
"""
15-
def __init__(self, src):
16-
# if it"s a file string, check that it exists and read the file
17-
if type(src) is str:
18-
assert os.path.exists(src), "{0} is not a valid file path. Use a list to input plate names directly".format(src)
19-
self.read(src)
20-
# if it's a list, just read it in
21-
elif type(src) is list:
22-
self.grp = src
23-
24-
def read(self, in_path):
25-
"""
26-
read a .grp file
27-
"""
28-
with open(in_path, "r") as f:
29-
lines = f.readlines()
30-
# need the second conditional to ignore comment lines
31-
self.grp = [line.strip() for line in lines if line and not re.match("^#", line)]
32-
33-
def write(self, out):
34-
"""
35-
write a .grp file
36-
"""
37-
with open(out, "w") as f:
38-
for x in self.grp:
39-
f.write(str(x) + "\n")
40-
41-
42-
def write_grp(in_list, out):
43-
"""
44-
standalone methods to write .grp files
45-
"""
46-
with open(out, "w") as f:
47-
for x in in_list:
48-
f.write(str(x) + "\n")
16+
def read(in_path):
17+
""" Read a grp file at the path specified by in_path.
4918
19+
Args:
20+
in_path (string): path to GRP file
21+
22+
Returns:
23+
grp (list)
5024
51-
def read_grp(in_path):
52-
"""
53-
standalone method to read .grp files
5425
"""
55-
assert os.path.exists(in_path), "The following file can't be found. in_path: {}".format(in_path)
26+
assert os.path.exists(in_path), "The following GRP file can't be found. in_path: {}".format(in_path)
27+
5628
with open(in_path, "r") as f:
5729
lines = f.readlines()
58-
# again, second conditional ignores comment lines
59-
return [line.strip() for line in lines if line and not re.match("^#", line)]
30+
# need the second conditional to ignore comment lines
31+
grp = [line.strip() for line in lines if line and not re.match("^#", line)]
32+
33+
return grp
34+
35+
36+
def write(grp, out_path):
37+
""" Write a GRP to a text file.
38+
39+
Args:
40+
grp (list): GRP object to write to new-line delimited text file
41+
out_path (string): output path
42+
43+
Returns:
44+
None
45+
46+
"""
47+
with open(out_path, "w") as f:
48+
for x in grp:
49+
f.write(str(x) + "\n")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#a
2+
r
3+
d
4+
e
5+
#f

cmapPy/set_io/tests/test_gmt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import os
33
import unittest
44
from cmapPy.pandasGEXpress import setup_GCToo_logger as setup_logger
5-
import cmapPy.pandasGEXpress.gmt as gmt
5+
import cmapPy.set_io.gmt as gmt
66

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

1010

11-
class GMT(unittest.TestCase):
11+
class TestGMT(unittest.TestCase):
1212

1313
@classmethod
1414
def setUpClass(cls):

cmapPy/set_io/tests/test_grp.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import logging
2+
import os
3+
import unittest
4+
from cmapPy.pandasGEXpress import setup_GCToo_logger as setup_logger
5+
import cmapPy.set_io.grp as grp
6+
7+
logger = logging.getLogger(setup_logger.LOGGER_NAME)
8+
FUNCTIONAL_TESTS_DIR = "functional_tests"
9+
10+
11+
class TestGRP(unittest.TestCase):
12+
13+
def test_read(self):
14+
15+
in_grp = grp.read(os.path.join(FUNCTIONAL_TESTS_DIR, "test.grp"))
16+
self.assertEqual(in_grp, ["r", "d", "e"])
17+
18+
with self.assertRaises(AssertionError) as e:
19+
grp.read("testt.grp")
20+
self.assertIn("The following GRP file", str(e.exception))
21+
22+
def test_write(self):
23+
24+
example_grp = ["x", "z", "w"]
25+
26+
out_path = os.path.join(FUNCTIONAL_TESTS_DIR, "test_write.grp")
27+
grp.write(example_grp, out_path)
28+
self.assertTrue(os.path.exists(out_path))
29+
30+
read_back_in = grp.read(out_path)
31+
self.assertEqual(example_grp, read_back_in)
32+
33+
# Cleanup
34+
os.remove(out_path)
35+
36+
if __name__ == "__main__":
37+
setup_logger.setup(verbose=True)
38+
39+
unittest.main()

0 commit comments

Comments
 (0)