Skip to content

Commit e430bc3

Browse files
Added readme and import/export json
1 parent 851025c commit e430bc3

15 files changed

Lines changed: 678 additions & 22 deletions

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
js/*
2-
*
2+
33
**/.DS_Store
44

55
!.gitignore
6-
!python
6+
!python
7+
python/sample_composites/composite_average.out
8+
/python/sample_composites
5.83 KB
Binary file not shown.
7.02 KB
Binary file not shown.
4.12 KB
Binary file not shown.
43.9 KB
Binary file not shown.
962 Bytes
Binary file not shown.
14.2 KB
Binary file not shown.

python/composite.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ def __init__(self, xmin=None, xmax=None, sense=[], anti=[], id=""):
1515

1616
# Object to store composite data with options for plotting, similar to a settings row
1717
class Composite:
18-
def __init__(self, scale=1, color=None, secondary_color=None, i=None, opacity=None, smoothing=None, bp_shift=None, hide_sense=False, hide_anti=False, baseline=0, name=None):
18+
def __init__(self, scale=1, color=None, secondary_color=None, i=None, opacity=None, smoothing=None, bp_shift=None, hide_sense=False, hide_anti=False, baseline=0, name=None, sense=None, anti=None, xmin=None, xmax=None):
1919
# Sets default values
2020
self.scale = scale if scale is not None else 1
2121
self.color = color if color is not None else "#0000FF"
2222
self.secondary_color = secondary_color if secondary_color is not None else color
2323
self.baseline = baseline if baseline is not None else 0
24-
self.xmin = 0
25-
self.xmax = 0
26-
self.sense = []
27-
self.anti = []
24+
self.xmin = xmin if xmin is not None else 0
25+
self.xmax = xmax if xmax is not None else 0
26+
self.sense = sense if sense is not None else []
27+
self.anti = anti if anti is not None else []
28+
# Don't assign defaults to opacity, smoothing, and bp_shift so plot can apply plot defaults
2829
self.opacity = opacity
2930
self.smoothing = smoothing
3031
self.bp_shift = bp_shift
@@ -75,12 +76,12 @@ def load_composite_dict(self,composite_dict: dict):
7576
self.anti = prefix + self.anti + suffix
7677
# Update sense and anti arrays
7778
j = composite.xmin - self.xmin
78-
print(composite.sense)
7979
while j <= composite.xmax - composite.xmin:
8080
idx = composite.xmin - self.xmin + j
8181
self.sense[idx] += composite.sense[j]
8282
self.anti[idx] += composite.anti[j]
8383
j += 1
8484
self.individual_files[composite.id] = composite
85+
8586
def __str__(self):
8687
return str(self.individual_files)

python/out.svg

Lines changed: 123 additions & 0 deletions
Loading

python/parseComposite.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import csv
2+
import xml.dom.minidom as dom
3+
import argparse
4+
import math
5+
import composite
6+
import os
7+
8+
# Returns a simple composite from a single file
9+
def parse_simple(file):
10+
fileArr = open(file, "r").read().split("\n")
11+
xmin = None
12+
xmax = None
13+
sense = []
14+
anti = []
15+
xmin_curr = 0
16+
xmax_curr = 0
17+
offset = 0
18+
for line in fileArr:
19+
# Skip empty
20+
if len(line.strip()) == 0 :
21+
continue
22+
# Separate fields
23+
fields = line.split("\t")
24+
if not fields[0].strip() or fields[0] == "NAME":
25+
xmin_curr = int(float(fields[1]))
26+
xmax_curr = int(float(fields[-1]))
27+
# If the x domain starts at 0 shift it to the left
28+
if xmin_curr == 0:
29+
xmin_curr -= math.floor(xmax_curr / 2)
30+
xmax_curr -= math.floor(xmax_curr / 2)
31+
# If the x domain is not defined yet, define it
32+
if xmin == None or xmax == None:
33+
xmin = xmin_curr
34+
xmax = xmax_curr
35+
# Redefine min and max if necessary
36+
xmax = max(xmax_curr, xmax)
37+
xmin = min(xmin_curr, xmin)
38+
sense = [0] * (xmax - xmin + 1)
39+
anti = [0] * (xmax - xmin + 1)
40+
# Add the values to sense and anti arrays
41+
if "sense" in fields[0].lower():
42+
i = 1
43+
while i < len(fields):
44+
sense[offset + i - 1] += float(fields[i])
45+
i += 1
46+
elif "anti" in fields[0].lower():
47+
i = 1
48+
while i < len(fields):
49+
anti[offset + i - 1] += float(fields[i])
50+
i += 1
51+
# If the first field is not empty or "NAME" and does not contain "sense" or "anti" parse as combined or midpoint data
52+
elif not (fields[0] == "" or fields[0] == "NAME"):
53+
i = 1
54+
while i < len(fields):
55+
sense[offset + i - 1] += float(fields[i]) / 2
56+
anti[offset + i - 1] += float(fields[i]) / 2
57+
return composite.SimpleComposite(xmin, xmax, sense, anti, os.path.basename(file).split('_')[0])
58+
59+
# Returns list of prefixes from multi-composite file, mimics the plotter method
60+
def get_prefixes_from_multiple_composites(file):
61+
lines = open(file, "r").read().split("\n")
62+
names_list = []
63+
i = 0
64+
while i < len(lines):
65+
line = lines[i]
66+
# Skip empty
67+
if line.strip() == "":
68+
i += 1
69+
continue
70+
# Get the first field
71+
col0 = line.split("\t")[0]
72+
if col0 == "" or col0[0] == "NAME":
73+
# Get the names of the composites for lines immediately following the xdomain
74+
i += 1
75+
names_list.append(lines[i].split("\t")[0])
76+
i += 1
77+
# Take the first name and split it by "_"
78+
split_name = names_list[0].split("_")
79+
idx = None
80+
# Iterate over each possible prefix-suffix split
81+
for i in range(1, len(split_name) - 1):
82+
prefix = "_".join(split_name[:i])
83+
suffix = "_".join(split_name[i:])
84+
n_prefix = sum(1 for n in names_list if n.startswith(prefix))
85+
n_suffix = sum(1 for n in names_list if n.endswith(suffix))
86+
if n_prefix * n_suffix == len(names_list):
87+
if n_suffix == len(names_list):
88+
idx = i if idx is None else idx
89+
break
90+
idx = i
91+
suffix = "_".join(split_name[idx:])
92+
# Get the prefixes by removing the suffix from the names
93+
return [n[:-len(suffix)] for n in names_list if n.endswith(suffix)]
94+
95+
# Returns dictionary with composite from multi-composite file, mimics the plotter method
96+
def parse_multiple_composite(file, prefix):
97+
lines = open(file, "r").read().split("\n")
98+
composites = {}
99+
xmin = None
100+
xmax = None
101+
sense = []
102+
anti = []
103+
i = 0
104+
id = 0
105+
save_comp = False
106+
while i < len(lines):
107+
line = lines[i]
108+
# Skip empty
109+
if line.strip() == "":
110+
i += 1
111+
continue
112+
# Get the first field
113+
fields = line.split("\t")
114+
col0 = fields[0]
115+
if not col0.strip() or col0 == "NAME":
116+
# If the x domain is defined, save the composite
117+
if save_comp:
118+
composites[id] = composite.SimpleComposite(xmin, xmax, sense, anti, id)
119+
save_comp = False
120+
# Get the nex x domain
121+
fields = [field for field in fields if field.strip()]
122+
xmin = int(float(fields[0]))
123+
xmax = int(float(fields[-1]))
124+
# If the x domain starts at 0 shift it to the left
125+
if xmin == 0:
126+
xmin -= math.floor(xmax / 2)
127+
xmin -= math.floor(xmax / 2)
128+
elif col0.startswith(prefix):
129+
id = col0[len(prefix):].split("_")[0]
130+
save_comp = True
131+
# Add the values to sense and anti arrays
132+
fields = [field for field in fields if field.strip()]
133+
if "sense" in fields[0].lower():
134+
sense = [float(val) for val in fields[1:]]
135+
elif "anti" in fields[0].lower():
136+
anti = [float(val) for val in fields[1:]]
137+
else:
138+
sense = [float(val) / 2 for val in fields[1:]]
139+
anti = [float(val) / 2 for val in fields[1:]]
140+
i += 1
141+
# Save the last composite
142+
if save_comp:
143+
composites[id] = composite.SimpleComposite(xmin, xmax, sense, anti, id)
144+
return composites

0 commit comments

Comments
 (0)