-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiggle_parser.py
32 lines (27 loc) · 1012 Bytes
/
wiggle_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pandas as pd
from io import StringIO
class WiggleParser:
def __init__(self, file_path):
self.file_path = file_path
def parse(self):
chrom_accession = ""
chrom_wig_vals_dict = {}
temp_wig_vals = ""
for line in open(self.file_path).readlines():
if line[0].isnumeric():
temp_wig_vals += line
else:
if "chrom=" in line:
if temp_wig_vals == "":
chrom_accession = line.split('chrom=')[-1].split(' ')[0]
continue
print(f"\tLoading coverage for: {chrom_accession}")
chrom_wig_vals_dict[chrom_accession] = pd.read_csv(StringIO(temp_wig_vals), sep=" ", header=None)
temp_wig_vals = ""
chrom_accession = line.split('chrom=')[-1].split(' ')[0]
if "name=" in line:
wig_name = line.split('name=')[-1].replace('\n', '')
print(f"Parsing file: {wig_name}")
print(f"\tLoading coverage for: {chrom_accession}")
chrom_wig_vals_dict[chrom_accession] = pd.read_csv(StringIO(temp_wig_vals), sep=" ", header=None)
return chrom_wig_vals_dict