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