Skip to content

Commit

Permalink
Cache AGP file and object line counts
Browse files Browse the repository at this point in the history
  • Loading branch information
twrightsman committed Feb 13, 2024
1 parent df751bf commit 4b43afa
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ragtag_utilities/AGPFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def __init__(self, in_file, mode="r"):
self._comment_lines = []
self._objects = []

self._n_lines = 0

# Store info enabling us to keep track of the state of the AGP file
self._current_obj = None
self._seen_objs = set()
Expand Down Expand Up @@ -106,6 +108,7 @@ def _read_file(self):
if line.startswith("#"):
if not in_body:
self._comment_lines.append(line)
self._n_lines += 1
else:
raise AGPError(self.fname, line_number, "illegal comment in AGP body")
continue
Expand Down Expand Up @@ -141,13 +144,15 @@ def _add_line(self, agp_line):
# Add the new object to our master list
agp_obj = AGPObject(self.fname, agp_line)
self._objects.append(agp_obj)
self._n_lines += agp_obj.num_lines

# Initialize all the info for this new object
self._current_obj = agp_obj.obj
self._seen_objs.add(agp_obj.obj)

else:
self._objects[-1].add_line(agp_line)
self._n_lines += 1

@property
def agp_version(self):
Expand All @@ -160,7 +165,7 @@ def fname(self):
@property
def num_lines(self):
""" Calculate the number of lines in the current state of the AGP file. """
return sum([len(self._comment_lines)] + [obj.num_lines for obj in self._objects])
return self._n_lines

@property
def num_objs(self):
Expand All @@ -169,13 +174,15 @@ def num_objs(self):
def add_pragma(self):
pragma = "## agp-version {}".format(self.agp_version)
if self._comment_lines:
self._n_lines -= len(self._comment_lines)
new_comment_lines = [pragma]
for i in self._comment_lines:
if i != pragma:
new_comment_lines.append(i)
self._comment_lines = new_comment_lines
else:
self._comment_lines.append(pragma)
self._n_lines += len(self._comment_lines)

def iterate_objs(self):
""" Iterate over the objects of the AGP file. """
Expand All @@ -197,6 +204,7 @@ def add_comment(self, c):

if c not in self._comment_lines:
self._comment_lines.append(c)
self._n_lines += 1

def add_seq_line(self, obj, obj_beg, obj_end, pid, comp_type, comp, comp_beg, comp_end, orientation):
"""
Expand Down Expand Up @@ -244,6 +252,8 @@ def pop_agp_line(self):
else:
self._objects[-1].pop_line()

self._n_lines -= 1

def write(self):
""" Write the agp contents to a file. """
with open(self.fname, "w") as f:
Expand All @@ -269,6 +279,7 @@ def __init__(self, agp_fname, in_agp_line):
self.fname = agp_fname
self._obj = in_agp_line.obj
self._agp_lines = []
self._n_agp_lines = 0

# Store info enabling us to keep track of the state of the object
self.previous_pid = 0
Expand Down Expand Up @@ -300,7 +311,7 @@ def obj_len(self):

@property
def num_lines(self):
return len(self._agp_lines)
return self._n_agp_lines

def add_line(self, agp_line):
# Perform validity checks if this is a new object
Expand All @@ -319,6 +330,7 @@ def add_line(self, agp_line):
self.previous_pid = agp_line.pid
self.obj_intervals.append((agp_line.obj_beg - 1, agp_line.obj_end))
self._agp_lines.append(agp_line)
self._n_agp_lines += 1

def iterate_lines(self):
for i in self._agp_lines:
Expand Down

0 comments on commit 4b43afa

Please sign in to comment.