changeset 0:116510205617 draft

planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
author nml
date Wed, 07 Mar 2018 11:39:06 -0500
parents
children 661bc10b0612
files combineJSON.py combineJSON.xml test-data/json1.json test-data/json2.json test-data/jsoncombined.json
diffstat 5 files changed, 135 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/combineJSON.py	Wed Mar 07 11:39:06 2018 -0500
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+import argparse
+import json
+import sys
+
+
+def init_parser():
+    parser = argparse.ArgumentParser(
+        prog="combineJSON",
+        formatter_class=argparse.RawDescriptionHelpFormatter,
+        description="Combine JSON data arrays into a single array")
+    parser.add_argument('-i',
+                        nargs='*',
+                        help="Input JSON files to be combined")
+    parser.add_argument('-o',
+                        help='Output file name')
+    return parser
+
+
+parser = init_parser()
+args = parser.parse_args()
+input_files = args.i
+json_file = []
+
+if input_files is None or len(input_files) < 2:
+    print('Not enough input files. '
+          'Please use -i filename.txt filename1.txt '
+          'to combine JSON data files')
+    sys.exit(0)
+
+
+for file_path in input_files:
+    try:
+        # Attempt to open each input file, parse as JSON
+        with open(file_path, 'r') as curr_file:
+            file_data = curr_file.read()
+            parsed_json_file = json.loads(file_data)
+            # Append each valid JSON data array
+            for entry in parsed_json_file:
+                json_file.append(entry)
+    except Exception as e:
+        print("Help! I can't parse this file {}. "
+              "Are you sure this is a valid JSON file?"
+              .format(file_path))
+        raise(e)
+
+if args.o:
+    with open(args.o, 'w') as out_json:
+        json.dump(json_file, out_json)
+else:
+    print(json.dumps(json_file))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/combineJSON.xml	Wed Mar 07 11:39:06 2018 -0500
@@ -0,0 +1,77 @@
+<tool id="combine_json" name="Combine Json" version="0.1">
+  <description>Combine multiple JSON Arrays</description>
+  <command detect_errors="exit_code"><![CDATA[
+    python $__tool_directory__/combineJSON.py
+
+    -i
+
+    #for $f in $json_files.keys
+      $json_files[$f]
+    #end for
+
+    -o 'jsoncombined.json'
+    ]]></command>
+  <inputs>
+    <param name="json_files" type="data_collection" label="Collection of JSON arrays" help="" optional="false" collection_type="list"/>
+  </inputs>
+  <outputs>
+      <data format="json" name="jsoncombined.json" from_work_dir="jsoncombined.json" label="Combined JSON Array"/>
+  </outputs>
+  <tests>
+      <test>
+          <param name="json_files">
+              <collection type="list">
+                  <element name="json1" value="json1.json" />
+                  <element name="json2" value="json2.json" />
+              </collection>
+          </param>
+          <output name="jsoncombined.json" file="jsoncombined.json" />
+      </test>
+  </tests>
+  <help><![CDATA[    
+***************
+  combine_json
+***************
+
+This tool takes two or more JSON arrays and combines them into one JSON array.
+
+Usage
+=====
+1) Input JSON files in a dataset collection
+2) Click Execute
+
+Example
+=======
+
+.. code:: javascript
+
+    JsonArray1
+    [
+      {hello: test}
+    ]
+
+    JsonArray2
+    [
+      {world: testerson}
+    ]
+
+
+`combineJson -JsonArray1.json -JsonArray2.json`
+
+.. code:: javascript
+
+    JsonCombined
+    [
+      {hello: test},
+      {world: testerson}
+    ]
+
+  ]]></help>
+    <citations>
+      <citation type="bibtex">@ARTICLE{a1,
+          title = {JSON Combine}
+          author = {Matthew Gopez}
+          }
+      }</citation>
+    </citations>
+</tool>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/json1.json	Wed Mar 07 11:39:06 2018 -0500
@@ -0,0 +1,3 @@
+[
+	{"hello": "world"}
+]
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/json2.json	Wed Mar 07 11:39:06 2018 -0500
@@ -0,0 +1,3 @@
+[
+	{"wheels": "sick"}
+]
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/jsoncombined.json	Wed Mar 07 11:39:06 2018 -0500
@@ -0,0 +1,1 @@
+[{"hello": "world"}, {"wheels": "sick"}]
\ No newline at end of file