From 5d2c5df67ed5ff8dfccbaffa56a2ffb63e163a41 Mon Sep 17 00:00:00 2001 From: zprobot <1727697083@qq.com> Date: Thu, 9 Nov 2023 20:07:24 +0800 Subject: [PATCH] json_command --- .../commands/convert_tsv_to_json_command.py | 25 +++++++++++++++++++ python/quantmsio/quantms_io/core/ae.py | 2 +- python/quantmsio/quantms_io/core/de.py | 2 +- python/quantmsio/quantms_io/core/tools.py | 11 +++++--- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 python/quantmsio/quantms_io/commands/convert_tsv_to_json_command.py diff --git a/python/quantmsio/quantms_io/commands/convert_tsv_to_json_command.py b/python/quantmsio/quantms_io/commands/convert_tsv_to_json_command.py new file mode 100644 index 0000000..ac93693 --- /dev/null +++ b/python/quantmsio/quantms_io/commands/convert_tsv_to_json_command.py @@ -0,0 +1,25 @@ +from quantms_io.core.tools import convert_to_json +import click +import os +CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) + + +@click.group(context_settings=CONTEXT_SETTINGS) +def cli(): + """ + This is the main tool that gives access to all commands. + """ + + +@click.command("convert_tsv_to_json", short_help="Register the file to project.json.",) +@click.option("--file", help="AE or DE file", required=True) +@click.pass_context +def convert_tsv_to_json(ctx,file): + if not os.path.exists(file): + raise click.UsageError("The file does not exist.") + + convert_to_json(file) + +cli.add_command(convert_tsv_to_json) +if __name__ == '__main__': + cli() \ No newline at end of file diff --git a/python/quantmsio/quantms_io/core/ae.py b/python/quantmsio/quantms_io/core/ae.py index 9736291..31f28a1 100644 --- a/python/quantmsio/quantms_io/core/ae.py +++ b/python/quantmsio/quantms_io/core/ae.py @@ -29,7 +29,7 @@ class AbsoluteExpressionHander: #INFO= #INFO=\n""" - ABSOLUTE_EXPRESSION_EXTENSION = ".ae.absolute.tsv" + ABSOLUTE_EXPRESSION_EXTENSION = ".absolute.tsv" def __init__(self): self.ibaq_df = None diff --git a/python/quantmsio/quantms_io/core/de.py b/python/quantmsio/quantms_io/core/de.py index a40add0..82e9e0d 100644 --- a/python/quantmsio/quantms_io/core/de.py +++ b/python/quantmsio/quantms_io/core/de.py @@ -53,7 +53,7 @@ class DifferentialExpressionHandler: #INFO= #INFO=\n""" - DIFFERENTIAL_EXPRESSION_EXTENSION = ".de.differential.tsv" + DIFFERENTIAL_EXPRESSION_EXTENSION = ".differential.tsv" def __init__(self): """ diff --git a/python/quantmsio/quantms_io/core/tools.py b/python/quantmsio/quantms_io/core/tools.py index 45f5695..0c7a282 100644 --- a/python/quantmsio/quantms_io/core/tools.py +++ b/python/quantmsio/quantms_io/core/tools.py @@ -11,6 +11,7 @@ """ import os import re +import json import pandas as pd import matplotlib.pyplot as plt @@ -374,11 +375,15 @@ def convert_to_json(file_path): table,content = load_de_or_ae(file_path) output = {} pattern = r'[\\|\|//|/]' - output['id'] = re.split(pattern,file_path)[-1] + file_name = re.split(pattern,file_path)[-1] + output['id'] = file_name output['metadata'] = content records = {} for col in table.columns: records[col] = table.loc[:,col].to_list() output['records'] = records - - return output \ No newline at end of file + b = json.dumps(output) + output_path = ".".join(file_name.split('.')[:-1]) + '.json' + f = open(output_path, 'w') + f.write(b) + f.close() \ No newline at end of file