filter supports exceptions and column naming
@@ -20,14 +20,19 @@ def add_arguments(parser):
# '$label == "/r/DefinedAs" && $node2=="/c/en/number_zero"'
parser.add_argument('-dt', "--datatype", action="store", type=str, dest="datatype", help="Datatype of the input file, e.g., tsv or csv.", default="tsv")
parser.add_argument('-p', '--pattern', action="store", type=str, dest="pattern", help="Pattern to filter on, for instance, \" ; P154 ; \" ")
+ parser.add_argument('--subj', action="store", type=str, dest='subj_col', help="Subject column, default is node1", default="node1")
+ parser.add_argument('--pred', action="store", type=str, dest='pred_col', help="Predicate column, default is label", default="label")
+ parser.add_argument('--obj', action="store", type=str, dest='obj_col', help="Object column, default is node2", default="node2")
parser.add_argument("input", nargs="?", action="store")
-def run(datatype, pattern, input):
+
+def run(datatype, pattern, input, subj_col, pred_col, obj_col):
# import modules locally
import socket
import sh # type: ignore
+ from kgtk.exceptions import KGTKException
- props=['node1', 'label', 'node2']
+ props=[subj_col, pred_col, obj_col]
def prepare_filter(property, prop_pattern):
prop_pattern=prop_pattern.strip()
@@ -38,20 +43,18 @@ def run(datatype, pattern, input):
prop_filters.append(value_filter)
return '(%s)' % ' || '.join(prop_filters)
- print('helo', pattern)
+
+ try:
n1_filter, lbl_filter,n2_filter=pattern.split(';')
- print('helo')
filters=[]
for i, col_filter in enumerate([n1_filter, lbl_filter, n2_filter]):
if col_filter.strip():
ready=prepare_filter(props[i], col_filter)
filters.append(ready)
- print(ready)
filter_str='\'%s\'' % ' && '.join(filters)
- print(filter_str)
if filter_str:
if input:
sh.mlr('--%s' % datatype, 'filter', filter_str, input,
@@ -59,4 +62,5 @@ def run(datatype, pattern, input):
elif not sys.stdin.isatty():
sh.mlr('--%s' % datatype, 'filter', filter_str,
_in=sys.stdin, _out=sys.stdout, _err=sys.stderr)
-
+ except:
+ raise KGTKException
