Created
March 21, 2018 20:03
-
-
Save indera/0754dcd547d640f2d80c326feaa0d844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Goal: search multiple LOINC codes in a reference file | |
""" | |
import argparse | |
import sys | |
import pandas as pd | |
# from contextlib import redirect_stdout | |
# import io | |
pd.set_option('display.width', 2500) | |
REF_FILE = "/inbox/processing/loinc/loinc_261_core/LoincTableCore.csv" | |
# USECOLS = ["LOINC_NUM", "COMPONENT","PROPERTY","TIME_ASPCT","SYSTEM", | |
#"SCALE_TYP","METHOD_TYP","CLASS","CLASSTYPE","LONG_COMMON_NAME","SHORTNAME"] | |
USECOLS = ["LOINC_NUM", "COMPONENT", "PROPERTY", "TIME_ASPCT", "SYSTEM", | |
"SCALE_TYP", "METHOD_TYP", "LONG_COMMON_NAME"] | |
def run(args): | |
""" Merge two frames """ | |
df_ref = pd.read_csv(args.ref_file, sep=",", usecols=USECOLS) | |
# print("Using reference: {}".format(args.ref_file)) | |
# print("Reference header: \n{}".format(df_ref.head())) | |
df_ref.fillna('', inplace=True) | |
df = pd.read_csv(args.input_file, sep=",", | |
header=None, names=["LOINC_NUM"]) | |
merged = pd.merge(df, df_ref, how="inner") | |
merged.to_csv(args.output_file, sep="\t", index=False, encoding="utf-8") | |
if sys.stdout != args.output_file: | |
print("Result was saved to: {}".format(args.output_file)) | |
# with io.StringIO() as buf, redirect_stdout(buf): | |
# data = buf.getvalue() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="cat data | %prog [args]") | |
parser.add_argument("-r", "--reference", dest="ref_file", | |
default=REF_FILE) | |
parser.add_argument("-i", "--input", dest="input_file", | |
default=sys.stdin) | |
parser.add_argument("-o", "--output", dest="output_file", | |
default=sys.stdout) | |
parsed_args = parser.parse_args() | |
if sys.stdin.isatty(): | |
parser.print_usage() | |
print("for more help use --help") | |
sys.exit(1) | |
run(parsed_args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment