|
1 | | -import csv |
2 | 1 | import logging |
| 2 | +from smart_open import open |
3 | 3 |
|
4 | 4 | logger = logging.getLogger(__name__) |
5 | 5 |
|
6 | 6 |
|
7 | | -def get_tsv_chromsizes(filename): |
| 7 | +def tileset_info(filename: str) -> dict: |
| 8 | + """Return a standard higlass tileset info object that contains |
| 9 | + chromsizes as an element. |
| 10 | +
|
| 11 | + The chromsizes in the returned object will be a list of [name, size] |
| 12 | + tuples. |
| 13 | +
|
| 14 | + [ |
| 15 | + ['chr1', 1000], |
| 16 | + ['chr2', 2000] |
| 17 | + ] |
| 18 | + """ |
| 19 | + chromsizes = get_tsv_chromsizes(filename) |
| 20 | + |
| 21 | + max_width = sum([int(c[1]) for c in chromsizes]) |
| 22 | + return { |
| 23 | + "max_width": max_width, |
| 24 | + "chromsizes": [[c[0], int(c[1])] for c in chromsizes], |
| 25 | + "min_pos": [0], |
| 26 | + "max_pos": [max_width], |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | +def get_tsv_chromsizes(file): |
8 | 31 | """ |
9 | 32 | Get a list of chromosome sizes from this [presumably] tsv |
10 | | - chromsizes file file. |
| 33 | + chromsizes file. |
11 | 34 |
|
12 | 35 | Parameters: |
13 | 36 | ----------- |
14 | | - filename: string |
15 | | - The filename of the tsv file |
| 37 | + file: string or file-like object |
| 38 | + A file-like object |
16 | 39 |
|
17 | 40 | Returns |
18 | 41 | ------- |
19 | 42 | chromsizes: [(name:string, size:int), ...] |
20 | 43 | An ordered list of chromosome names and sizes |
21 | 44 | """ |
| 45 | + if isinstance(file, str): |
| 46 | + file = open(file, "rb") |
| 47 | + |
22 | 48 | try: |
23 | | - with open(filename, "r") as f: |
24 | | - reader = csv.reader(f, delimiter="\t") |
| 49 | + file.seek(0) |
| 50 | + binary_data = file.read() |
| 51 | + text_data = binary_data.decode("utf-8") |
25 | 52 |
|
26 | | - data = [] |
27 | | - for row in reader: |
28 | | - data.append(row) |
| 53 | + lines = text_data.split("\n") |
| 54 | + data = [line.strip().split("\t") for line in lines if line.strip()] |
29 | 55 | return data |
30 | 56 | except Exception as ex: |
31 | 57 | logger.error(ex) |
32 | 58 |
|
33 | | - err_msg = "WHAT?! Could not load file %s. 😤 (%s)" % (filename, ex) |
| 59 | + err_msg = "WHAT?! Could not load file %s." % (ex) |
34 | 60 |
|
35 | 61 | raise Exception(err_msg) |
0 commit comments