New obi grep working with URI API
This commit is contained in:
@ -1,52 +1,25 @@
|
||||
#cython: language_level=3
|
||||
|
||||
from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport
|
||||
from obitools3.dms.dms import DMS # TODO cimport doesn't work
|
||||
from obitools3.dms.view.view import View, Line_selection # TODO cimport doesn't work
|
||||
|
||||
|
||||
from obitools3.apps.optiongroups import addSequenceInputOption
|
||||
from obitools3.dms import DMS
|
||||
from obitools3.dms.view.view cimport View, Line_selection
|
||||
from obitools3.uri.decode import open_uri
|
||||
from obitools3.apps.optiongroups import addMinimalInputOption, addTaxonomyInputOption, addMinimalOutputOption
|
||||
from obitools3.dms.view import RollbackException
|
||||
from functools import reduce
|
||||
from obitools3.apps.config import logger
|
||||
import time
|
||||
|
||||
__title__="Grep view lines that match the given predicates"
|
||||
|
||||
default_config = { 'inputview' : None,
|
||||
'outputview' : None,
|
||||
'predicates' : []
|
||||
}
|
||||
__title__="Grep view lines that match the given predicates"
|
||||
|
||||
|
||||
def addOptions(parser):
|
||||
|
||||
addSequenceInputOption(parser)
|
||||
|
||||
# TODO put this common group somewhere else but I don't know where
|
||||
group=parser.add_argument_group('DMS and view options')
|
||||
|
||||
group.add_argument('--default-dms','-d',
|
||||
action="store", dest="obi:defaultdms",
|
||||
metavar='<DMS NAME>',
|
||||
default=None,
|
||||
type=str,
|
||||
help="Name of the default DMS for reading and writing data.")
|
||||
|
||||
group.add_argument('--input-view','-i',
|
||||
action="store", dest="obi:inputview",
|
||||
metavar='<INPUT VIEW NAME>',
|
||||
default=None,
|
||||
type=str,
|
||||
help="Name of the input view, either raw if the view is in the default DMS,"
|
||||
" or in the form 'dms:view' if it is in another DMS.")
|
||||
|
||||
group.add_argument('--output-view','-o',
|
||||
action="store", dest="obi:outputview",
|
||||
metavar='<OUTPUT VIEW NAME>',
|
||||
default=None,
|
||||
type=str,
|
||||
help="Name of the output view, either raw if the view is in the default DMS,"
|
||||
" or in the form 'dms:view' if it is in another DMS.")
|
||||
|
||||
|
||||
addMinimalInputOption(parser)
|
||||
addTaxonomyInputOption(parser)
|
||||
addMinimalOutputOption(parser)
|
||||
|
||||
group=parser.add_argument_group('obi grep specific options')
|
||||
|
||||
group.add_argument('--predicate','-p',
|
||||
@ -59,34 +32,63 @@ def addOptions(parser):
|
||||
|
||||
def run(config):
|
||||
|
||||
# Open DMS
|
||||
d = DMS.open(config['obi']['defaultdms'])
|
||||
|
||||
# Open input view 1
|
||||
iview = View.open(d, config['obi']['inputview'])
|
||||
|
||||
DMS.obi_atexit()
|
||||
|
||||
logger("info", "obi grep")
|
||||
|
||||
# Open the input
|
||||
input = open_uri(config['obi']['inputURI'])
|
||||
if input is None:
|
||||
raise Exception("Could not read input view")
|
||||
i_view = input[1]
|
||||
|
||||
# Read the name of the output view
|
||||
uri = config['obi']['outputURI'].split('/')
|
||||
if len(uri)==2:
|
||||
# Check that input and output DMS are the same (predicate, to discuss)
|
||||
if config['obi']['inputURI'].split(b'/')[0] != uri[0]:
|
||||
raise Exception("Input and output DMS must be the same")
|
||||
output_view_name = uri[1]
|
||||
else:
|
||||
output_view_name = uri[0]
|
||||
|
||||
if 'taxoURI' in config['obi'] : # TODO default None problem
|
||||
taxo_uri = open_uri(config['obi']['taxoURI'])
|
||||
if taxo_uri is None:
|
||||
raise Exception("Couldn't open taxonomy")
|
||||
taxo = taxo_uri[1]
|
||||
else :
|
||||
taxo = None
|
||||
|
||||
# Initialize the progress bar
|
||||
pb = ProgressBar(len(iview), config, seconde=5)
|
||||
|
||||
pb = ProgressBar(len(i_view), config, seconde=5)
|
||||
|
||||
# Apply filter
|
||||
selection = Line_selection(iview)
|
||||
for i in range(len(iview)) :
|
||||
selection = Line_selection(i_view)
|
||||
for i in range(len(i_view)):
|
||||
pb(i)
|
||||
line = iview[i]
|
||||
line = i_view[i]
|
||||
|
||||
loc_env = {'sequence': line, 'line': line} # TODO add taxonomy
|
||||
|
||||
loc_env = {'sequence': line, 'line': line, 'taxonomy': taxo}
|
||||
|
||||
good = (reduce(lambda bint x, bint y: x and y,
|
||||
(bool(eval(p, loc_env, line))
|
||||
for p in config['grep']['predicates']), True))
|
||||
|
||||
|
||||
if good :
|
||||
selection.append(i)
|
||||
|
||||
|
||||
# Create output view with the line selection
|
||||
oview = selection.materialize(config['obi']['outputview'], comments="obi grep: "+str(config['grep']['predicates'])+"\n")
|
||||
|
||||
try:
|
||||
o_view = selection.materialize(output_view_name, comments="obi grep: "+str(config['grep']['predicates'])+"\n")
|
||||
except Exception, e:
|
||||
raise RollbackException("obi grep error, rollbacking view: "+str(e), o_view)
|
||||
|
||||
# TODO DISCUSS if output URI to different DMS, copy view?
|
||||
|
||||
print("\n")
|
||||
print(repr(oview))
|
||||
|
||||
d.close()
|
||||
print(repr(o_view))
|
||||
|
||||
input[0].close()
|
||||
#output[0].close()
|
||||
|
||||
|
Reference in New Issue
Block a user