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
712import os
813import 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 " )
0 commit comments