1 '''
2 Provides a function to read and parse a BPM file into a nice data structure.
3 '''
4 import csv
5 import os
6 import sys
7
9 '''
10 A BPM file reader function. Assumes 'f' is in the CSV file format that is
11 the output from the 'mkbpms' program. (i.e., one module per line with the
12 module name in the first column followed by its genes in subsequent
13 columns.)
14
15 The returned data structure is a list of tuples. Each tuple represents a
16 BPM where the first element is a list of genes in the first module and
17 the second element is a list of genes in the second module.
18
19 i.e., [([Gene], [Gene])]
20 '''
21 if not os.access(f, os.R_OK):
22 print >> sys.stderr, 'Cannot read %s' % f
23 sys.exit(1)
24
25 bpms = []
26
27 reader = csv.reader(open(f), delimiter='\t')
28 for mod1 in reader:
29 mod2 = reader.next()
30 bpms.append((mod1[1:], mod2[1:]))
31
32 return bpms
33