Skip to content

Commit

Permalink
Partially fixed AndreasHeger#15: --cache option in gat-run.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sarangbg committed May 2, 2023
1 parent f380817 commit 1470f74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
45 changes: 27 additions & 18 deletions gat/Engine.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cython: embedsignature=True
# cython: profile=False

import traceback
import collections
import re
import os
Expand Down Expand Up @@ -3181,6 +3182,7 @@ cdef class Samples(object):
If cache is given, samples will be stored persistently on disk.
'''
E.info(">>>Samples class initialized in Engine.pyx")
self.samples = {}

def add( self, track, sample_id, isochore, segmentlist ):
Expand Down Expand Up @@ -3245,25 +3247,31 @@ cdef class SamplesCached( Samples ):
cdef FILE * findex
cdef dict index
cdef char * filename
cdef char * indexextension

def __init__(self, filename ):
'''create a new SampleCollection.
If cache is given, samples will be stored persistently on disk.
'''
Samples.__init__(self)

self.filename = filename
tmp = self.filename + ".idx"

if not os.path.exists( filename ):
self.fcache = fopen( filename, "wb" )
self.findex = fopen( tmp, "wb" )
self.index = {}
else:
self.fcache = fopen( filename, "rb" )
self.loadIndex()
self.findex = fopen( tmp, "rb" )
try:
E.info(">>>Trying from Engine.pyx: intializing SamplesCached class")
'''create a new SampleCollection.
If cache is given, samples will be stored persistently on disk.
'''
Samples.__init__(self)

self.filename = filename
self.indexextension = ".idx"
tmp = self.filename + self.indexextension

if not os.path.exists( filename ):
self.fcache = fopen( filename, "wb" )
self.findex = fopen( tmp, "wb" )
self.index = {}
else:
self.fcache = fopen( filename, "rb" )
self.loadIndex()
self.findex = fopen( tmp, "rb" )
except Exception:
E.info(">>>Exception from Engine.pyx")
E.info(traceback.format_exc())

def loadIndex( self ):
'''load index from cache.
Expand All @@ -3276,7 +3284,7 @@ cdef class SamplesCached( Samples ):
cdef char keylen
cdef FILE * findex
self.index = {}
tmp = self.filename + ".idx"
tmp = self.filename + self.indexextension
findex = fopen( tmp, "rb" )

ckey = <char*>calloc(sizeof(char) * 256, 1)
Expand Down Expand Up @@ -3339,6 +3347,7 @@ cdef class SamplesCached( Samples ):
return self.toKey(track, sample_id, isochore) in self.index

def __dealloc__(self):
E.info(">>>Saving cache files!")
fclose( self.fcache)
fclose( self.findex)

Expand Down
2 changes: 1 addition & 1 deletion gat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def run(segments,

if cache:
E.info("samples are cached in %s" % cache)
samples = Engine.SamplesCached(filename=cache)
samples = Engine.SamplesCached(filename=cache.encode('utf-8'))
elif sample_files:
if not output_samples_pattern:
raise ValueError(
Expand Down
10 changes: 9 additions & 1 deletion scripts/gat-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@
import gat.Stats as Stats
import gat.SegmentList as SegmentList
import gat.Engine as Engine
import traceback


def fromSegments(options, args):
def _fromSegments(options, args):
'''run analysis from segment files.
This is the most common use case.
Expand Down Expand Up @@ -219,6 +220,13 @@ def fromSegments(options, args):

return annotator_results

def fromSegments(options, args):
try:
E.info(">>>Trying from gat-run.py")
return _fromSegments(options, args)
except Exception:
E.info(">>>Exception from gat-run.py")
E.info(traceback.format_exc())

def main(argv=None):
"""script main.
Expand Down

0 comments on commit 1470f74

Please sign in to comment.