Get columns of data from inFile. The order of the rows is respected :param inFile: column file separated by delim :param header: if True the first line will be considered a header line :returns: a tuple of 2 dicts (cols, indexToName). cols dict has keys that are headings i
(inFile, delim="\t", header=True)
| 1 | def getColumns(inFile, delim="\t", header=True): |
| 2 | """ |
| 3 | Get columns of data from inFile. The order of the rows is respected |
| 4 | |
| 5 | :param inFile: column file separated by delim |
| 6 | :param header: if True the first line will be considered a header line |
| 7 | :returns: a tuple of 2 dicts (cols, indexToName). cols dict has keys that |
| 8 | are headings in the inFile, and values are a list of all the entries in that |
| 9 | column. indexToName dict maps column index to names that are used as keys in |
| 10 | the cols dict. The names are the same as the headings used in inFile. If |
| 11 | header is False, then column indices (starting from 0) are used for the |
| 12 | heading names (i.e. the keys in the cols dict) |
| 13 | """ |
| 14 | cols = {} |
| 15 | indexToName = {} |
| 16 | for lineNum, line in enumerate(inFile): |
| 17 | if lineNum == 0: |
| 18 | headings = line.split(delim) |
| 19 | i = 0 |
| 20 | for heading in headings: |
| 21 | heading = heading.strip() |
| 22 | if header: |
| 23 | cols[heading] = [] |
| 24 | indexToName[i] = heading |
| 25 | else: |
| 26 | # in this case the heading is actually just a cell |
| 27 | cols[i] = [heading] |
| 28 | indexToName[i] = i |
| 29 | i += 1 |
| 30 | else: |
| 31 | cells = line.split(delim) |
| 32 | i = 0 |
| 33 | for cell in cells: |
| 34 | cell = cell.strip() |
| 35 | cols[indexToName[i]] += [cell] |
| 36 | i += 1 |
| 37 | |
| 38 | return cols, indexToName |