# HG changeset patch # User proteore # Date 1552310082 14400 # Node ID b4641c0f8a82414f2263e728f22f57086d5a9ef8 # Parent 33ca9ba2495a22db9686e6aeb6b7b50e5844ce5d planemo upload commit 77279e994f5751c6cd9aa165aa0604db3d241271-dirty diff -r 33ca9ba2495a -r b4641c0f8a82 filter_kw_val.py --- a/filter_kw_val.py Tue Mar 05 07:37:10 2019 -0500 +++ b/filter_kw_val.py Mon Mar 11 09:14:42 2019 -0400 @@ -1,4 +1,4 @@ -import argparse, re, csv +import argparse, re, csv, sys def options(): """ @@ -42,6 +42,12 @@ else: raise argparse.ArgumentTypeError('Boolean value expected.') +def proper_ncol (ncol,file): + if ncol not in range(len(file[0])): + print("Column "+str(ncol+1)+" not found in input file") + #traceback.print_exc(file=sys.stdout) + sys.exit(1) + #Check if a variable is a float or an integer def is_number(number_format, n): float_format = re.compile(r"^[-]?[0-9][0-9]*.?[0-9]+$") @@ -76,7 +82,7 @@ key_files = args.kw_file for kf in key_files: header = str_to_bool(kf[1]) - ncol = column_from_txt(kf[2]) + ncol = column_from_txt(kf[2],csv_file) keywords = read_keywords_file(kf[0],header,ncol) results_dict=filter_keyword(csv_file, header, results_dict, keywords, kf[3], kf[4]) @@ -85,7 +91,7 @@ v[0] = v[0].replace(",",".") v[2] = operator_dict[v[2]] if is_number("float", v[0]): - csv_file = comma_number_to_float(csv_file,column_from_txt(v[1]),header) + csv_file = comma_number_to_float(csv_file,column_from_txt(v[1],csv_file),header) results_dict = filter_value(csv_file, header, results_dict, v[0], v[1], v[2]) else: raise ValueError("Please enter a number in filter by value") @@ -93,7 +99,7 @@ if args.values_range: for vr in args.values_range: vr[:2] = [value.replace(",",".") for value in vr[:2]] - csv_file = comma_number_to_float(csv_file,column_from_txt(vr[2]),header) + csv_file = comma_number_to_float(csv_file,column_from_txt(vr[2],csv_file),header) if (is_number("float", vr[0]) or is_number("int", vr[0])) and (is_number("float",vr[1]) or is_number("int",vr[1])): results_dict = filter_values_range(csv_file, header, results_dict, vr[0], vr[1], vr[2], vr[3]) @@ -124,7 +130,7 @@ #sort of results by column if args.sort_col : sort_col=args.sort_col.split(",")[0] - sort_col=column_from_txt(sort_col) + sort_col=column_from_txt(sort_col,csv_file) reverse=str_to_bool(args.sort_col.split(",")[1]) remaining_lines= sort_by_column(remaining_lines,sort_col,reverse,header) filtered_lines = sort_by_column(filtered_lines,sort_col,reverse,header) @@ -239,7 +245,7 @@ #seek for keywords in rows of csvfile, return a dictionary of boolean (true if keyword found, false otherwise) def filter_keyword(csv_file, header, results_dict, keywords, ncol, match): match=str_to_bool(match) - ncol=column_from_txt(ncol) + ncol=column_from_txt(ncol,csv_file) if type(keywords) != list : keywords = keywords.upper().split() # Split list of filter keyword for id_line,line in enumerate(csv_file): @@ -262,7 +268,7 @@ def filter_value(csv_file, header, results_dict, filter_value, ncol, opt): filter_value = float(filter_value) - ncol=column_from_txt(ncol) + ncol=column_from_txt(ncol,csv_file) nb_string=0 for id_line,line in enumerate(csv_file): @@ -297,7 +303,7 @@ inclusive=str_to_bool(inclusive) bottom_value = float(bottom_value) top_value=float(top_value) - ncol=column_from_txt(ncol) + ncol=column_from_txt(ncol,csv_file) nb_string=0 for id_line, line in enumerate(csv_file): @@ -332,13 +338,16 @@ return results_dict -def column_from_txt(ncol): +def column_from_txt(ncol,file): if is_number("int", ncol.replace("c", "")): ncol = int(ncol.replace("c", "")) - 1 else: raise ValueError("Please specify the column where " "you would like to apply the filter " "with valid format") + + proper_ncol (ncol,file) + return ncol #return True if value is in the determined values, false otherwise diff -r 33ca9ba2495a -r b4641c0f8a82 filter_kw_val.xml --- a/filter_kw_val.xml Tue Mar 05 07:37:10 2019 -0500 +++ b/filter_kw_val.xml Mon Mar 11 09:14:42 2019 -0400 @@ -1,4 +1,4 @@ - +