Repository 'merge_ds_flowtext'
hg clone https://toolshed.g2.bx.psu.edu/repos/immport-devteam/merge_ds_flowtext

Changeset 0:426650130311 (2017-02-27)
Next changeset 1:3c0e4179be7a (2020-06-22)
Commit message:
Uploaded
added:
merge_ds_flowtext/FCStxtMergeDownsample.py
merge_ds_flowtext/FCStxtMergeDownsample.xml
merge_ds_flowtext/test-data/merge1.flowtext
merge_ds_flowtext/test-data/merge2.flowtext
merge_ds_flowtext/test-data/test1/input1.txt
merge_ds_flowtext/test-data/test1/input2.txt
merge_ds_flowtext/test-data/test1/input3.txt
merge_ds_flowtext/test-data/test2/input1.txt
merge_ds_flowtext/test-data/test2/input2.txt
merge_ds_flowtext/test-data/test2/input3.txt
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/FCStxtMergeDownsample.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/FCStxtMergeDownsample.py Mon Feb 27 13:03:02 2017 -0500
[
b'@@ -0,0 +1,225 @@\n+#!/usr/bin/env python\n+\n+######################################################################\n+#                  Copyright (c) 2016 Northrop Grumman.\n+#                          All rights reserved.\n+######################################################################\n+\n+from __future__ import print_function\n+from __future__ import division\n+import sys\n+import os\n+import pandas as pd\n+from argparse import ArgumentParser\n+\n+\n+def is_number(s):\n+    try:\n+        float(s)\n+        return True\n+    except ValueError:\n+        return False\n+\n+\n+def is_integer(s):\n+    try:\n+        int(s)\n+        return True\n+    except ValueError:\n+        return False\n+\n+\n+def compare_headers(files):\n+    headers = {}\n+    for eachfile in files:\n+        with open(eachfile, "r") as ef:\n+            headers[eachfile] = ef.readline().strip().lower().split("\\t")\n+\n+    hdgs_in_common = []\n+    flag = {}\n+\n+    for ref_hdgs in headers[files[0]]:\n+        flag[ref_hdgs] = 1\n+\n+        for ij in range(1, len(files)):\n+            if ref_hdgs in headers[files[ij]]:\n+                flag[ref_hdgs] += 1\n+        if flag[ref_hdgs] == len(files):\n+            hdgs_in_common.append(ref_hdgs)\n+\n+    if not hdgs_in_common:\n+        sys.exit(9)\n+    return(hdgs_in_common)\n+\n+\n+def get_headers_index(list_headings, headings):\n+    idxs = []\n+    lhdgs = [x.lower() for x in headings]\n+    for element in list_headings:\n+        idxs.append(int(lhdgs.index(element)))\n+    return(idxs)\n+\n+\n+def merge_and_DS_txt(in_files, out_file, col_names, factor_ds):\n+    """Concatenates together tab-separated files.\n+    The output will have only the columns in common to all the files provided\n+    as input, as determined by the headers.\n+    All lines after the header line must contain only numbers.\n+    Potential errors are logged to stderr. If the number of errors reaches 10,\n+    the program stops.\n+    If a downsampling factor is given, returns the indicated fraction of\n+    random lines.\n+    """\n+\n+    nb_errors = 0\n+    max_error = 10\n+\n+    # get list of headers in common to all files\n+    list_hdgs = compare_headers(in_files)\n+\n+    with open(out_file, "w") as outf:\n+        ff_order = []\n+        # HEADERS:\n+        with open(in_files[0], "r") as first_file:\n+            headings_ff = first_file.readline().strip()\n+            headings = headings_ff.split("\\t")\n+            # Get index of headers in common:\n+            hdrs_idx = get_headers_index(list_hdgs, headings)\n+\n+            # If column to merge on were provided:\n+            if col_names:\n+                for ix in col_names:\n+                    if ix not in hdrs_idx:\n+                        nb_errors += 1\n+                        sys.stderr.write(" ".join(["WARNING: column", str(ix), "in", in_files[0],\n+                                                   "does not exist in all files or has a different header.\\n"]))\n+                hdrs_idx = col_names\n+\n+            # Print out to output file:\n+            headings_to_write = []\n+            for cti in range(0, len(headings)):\n+                if cti in hdrs_idx:\n+                    headings_to_write.append(headings[cti])\n+                    ff_order.append(headings[cti])\n+            outf.write("\\t".join(headings_to_write) + "\\n")\n+\n+        # DATA\n+        for infile in in_files:\n+            with open(infile, "r") as inf:\n+                headings_inf = inf.readline().strip()\n+                hdgs = headings_inf.split("\\t")\n+                # Get the index of columns to keep:\n+                hdgs_idx = []\n+                for ctc in ff_order:\n+                    hdgs_idx.append(int(hdgs.index(ctc)))\n+                if col_names:\n+                    for iy in col_names:\n+                        if iy not in hdgs_idx:\n+                            nb_errors += 1\n+                            sys.stderr.write(" ".join(["WARNING: column", str(iy), "in", infile,\n+                                                       "does not '..b'\n+                    hdgs_idx = col_names\n+\n+            df = pd.read_table(infile, usecols=hdrs_idx)\n+            wc_file = len(df.index) - 1\n+            df_ds = df.sample(int(wc_file * factor_ds), replace=False)\n+\n+            for cols in df_ds.columns.values:\n+                if df_ds[cols].count() != len(df_ds[cols]):\n+                    sys.stderr.write(infile + "contains non-numeric data\\n")\n+\n+                    with open(infile, "r") as checkfile:\n+                        fl = checkfile.readline()\n+                        count_lines = 1\n+                        for checklines in checkfile:\n+                            to_check = checklines.strip().split("\\t")\n+                            count_lines += 1\n+                            for item in to_check:\n+                                if not is_number(item):\n+                                    sys.stderr.write(" ".join(["WARNING: line", str(count_lines),\n+                                                               "in", infile, "contains non-numeric results\\n"]))\n+                    sys.exit(2)\n+\n+            df_ds = df_ds.ix[:, ff_order]\n+            df_ds.to_csv(outf, sep="\\t", header=False, index=False)\n+\n+    if nb_errors > 0:\n+        exit_code = 3\n+        if nb_errors == max_error:\n+            exit_code = 4\n+            sys.stderr.write("Run aborted - too many errors.")\n+            os.remove(out_file)\n+        sys.exit(exit_code)\n+    return\n+\n+\n+if __name__ == "__main__":\n+    parser = ArgumentParser(\n+             prog="FCStxtmerge",\n+             description="Merge based on headers text-converted FCS files into one text file.")\n+\n+    parser.add_argument(\n+            \'-i\',\n+            dest="input_files",\n+            required=True,\n+            action=\'append\',\n+            help="File location for the text files.")\n+\n+    parser.add_argument(\n+            \'-o\',\n+            dest="output_file",\n+            required=True,\n+            help="Name of the output file.")\n+\n+    parser.add_argument(\n+            \'-c\',\n+            dest="columns",\n+            help="Specify which column to keep in output file")\n+\n+    parser.add_argument(\n+            \'-d\',\n+            dest="downsampling_factor",\n+            help="How much of each file to keep")\n+\n+    args = parser.parse_args()\n+\n+    # Get columns to merge on if any:\n+    default_value_col = ["i.e.:1,2,5", "default", "Default"]\n+    columns = []\n+    if args.columns:\n+        if args.columns not in default_value_col:\n+            tmp_col = args.columns.split(",")\n+            if len(tmp_col) == 1:\n+                if not tmp_col[0].strip():\n+                    columns = []\n+                elif not is_integer(tmp_col[0].strip()):\n+                    sys.exit(7)\n+                else:\n+                    columns.append(int(tmp_col[0].strip()) - 1)\n+            else:\n+                for c in range(0, len(tmp_col)):\n+                    if not is_integer(tmp_col[c].strip()):\n+                        sys.exit(6)\n+                    else:\n+                        columns.append(int(tmp_col[c].strip()) - 1)\n+\n+    # Get down sampling factor if any:\n+    # Note: change \'%\' to \'X\' because somehow that\'s what Galaxy passes?\n+    default_value_ds = ["i.e.:0.1 or 10X", "default", "Default"]\n+    ds_factor = 1\n+    if args.downsampling_factor:\n+        if args.downsampling_factor not in default_value_ds:\n+            args.downsampling_factor = args.downsampling_factor.strip()\n+            downsampling_factor = args.downsampling_factor.rstrip("X")\n+            if is_number(downsampling_factor):\n+                ds_factor = float(downsampling_factor)\n+                if ds_factor > 1:\n+                    ds_factor = float(downsampling_factor) / 100\n+                if ds_factor > 100:\n+                    sys.exit(8)\n+            else:\n+                sys.exit(8)\n+\n+    input_files = [f for f in args.input_files]\n+    merge_and_DS_txt(input_files, args.output_file, columns, ds_factor)\n+    sys.exit(0)\n'
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/FCStxtMergeDownsample.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/FCStxtMergeDownsample.xml Mon Feb 27 13:03:02 2017 -0500
[
@@ -0,0 +1,175 @@
+<tool id="fcstxt_merge_downsample" name="Downsample and merge" version="1.1">
+  <description>txt-converted FCS files into one text file based on headers.</description>
+  <requirements>
+    <requirement type="package" version="1.10.2">numpy</requirement>
+    <requirement type="package" version="0.17.1">pandas</requirement>
+  </requirements>
+  <stdio>
+    <exit_code range="2" level="fatal" description="Non-numeric data. See stderr for more details." />
+    <exit_code range="3" level="warning" description="Selected columns do not exist in all files" />
+    <exit_code range="4" level="fatal" description="Run aborted - too many errors" />
+    <exit_code range="6" level="fatal" description="Please provide integers for columns you want to merge on." />
+    <exit_code range="7" level="fatal" description="Please provide a comma separated list of integers for columns you want to merge on." />
+    <exit_code range="8" level="fatal" description="Please provide a numeric value [0,1] for the downsampling factor." />
+    <exit_code range="9" level="fatal" description="There are no columns in common to all files." />
+  </stdio>
+  <command><![CDATA[
+    python $__tool_directory__/FCStxtMergeDownsample.py -o "${output_file}" -d "${factorDS}"
+ #if $columns
+    -c "${columns}"
+ #end if
+ #for $f in $input#
+    -i "${f}"
+ #end for#
+  ]]>
+  </command>
+  <inputs>
+    <param format="flowtext" name="input" type="data_collection" collection_type="list" label="Text files Collection"/>
+    <param name="factorDS" type="text" label="Downsample by:" value="i.e.:0.1 or 10%" optional="true" help="1 by default (no downsampling)."/>
+    <param name="columns" type="text" label="Merge columns:" value="i.e.:1,2,5" optional="true" help="By default, will merge on the columns in common to all files.">
+    </param>
+  </inputs>
+  <outputs>
+    <data format="flowtext" name="output_file" label="Merge flowtext on ${input.name}"/>
+  </outputs>
+  <tests>
+    <test>
+      <param name="input">
+        <collection type="list">
+          <element name="input1.txt" value="test1/input1.txt"/>
+          <element name="input2.txt" value="test1/input2.txt"/>
+          <element name="input3.txt" value="test1/input3.txt"/>
+        </collection>
+      </param>
+      <param name="factorDS" value=".8"/>
+      <param name="columns" value="i.e.:1,2,5"/>
+      <output name="output_file" file="merge1.flowtext" compare="sim_size"/>
+    </test>
+    <test>
+      <param name="input">
+        <collection type="list">
+          <element name="input1.txt" value="test2/input1.txt"/>
+          <element name="input2.txt" value="test2/input2.txt"/>
+          <element name="input3.txt" value="test2/input3.txt"/>
+        </collection>
+      </param>
+      <param name="factorDS" value="i.e.:0.1 or 10%"/>
+      <param name="columns" value="1,2,3"/>
+      <output name="output_file" file="merge2.flowtext" compare="sim_size"/>
+    </test>
+  </tests>
+  <help><![CDATA[
+   This tool downsamples and merges multiple txt-converted FCS files into one text file.
+
+-----
+
+**Input files**
+
+This tool requires collections of txt, flowtext or tabular files as input.
+
+**Downsampling**
+
+By default, files are not downsampled. If a downsampling factor is provided, each file in the input dataset collection will be downsampled randomly without replacement as follows:
+
+- If n is between 0 and 1, the size of the output will be n times that of the input files.
+- If n is between 1 and 100, the size of the output will be n% that of the input files.
+
+.. class:: warningmark
+
+At this time, up-sampling is not supported. If the number provided is greater than 100, the tool will exit.
+
+**Output file**
+
+The output flowtext file contains is a concatenation of the input files provided all data after the header contains only numbers. By default, only columns existing in all input files (as assessed by the header) are concatenated. The user can specify columns to merge, bypassing the headers check. If a downsampling factor is provided, the corresponding proportion of each input file ONLY will be read in (and checked for errors).
+
+.. class:: warningmark
+
+Potential errors are logged to stderr. If the number of errors reaches 10, the run will be aborted. If a file contains non-numeric data, the run will be aborted.
+
+.. class:: infomark
+
+Tip: Three tools in the Flow File Tools section can help prepare files for merging and/or downsampling:
+
+- Check headers tool provides a list of headers for all files in a collection of text, flowtext or tabular files.
+- Remove, rearrange and/or rename columns tool allows manipulation of the columns of a file or a set of files.
+- Check data tool identifies the lines in a file containing non-numeric data.
+
+-----
+
+**Example**
+
+*File1*::
+
+   Marker1 Marker2 Marker3
+   34      45      12
+   33      65      10
+   87      26      76
+   24      56      32
+   95      83      53
+   74      15      87
+
+*File2*::
+
+   Marker4 Marker5 Marker3
+   19      62      98
+   12      36      58
+   41      42      68
+   76      74      53
+   62      34      45
+   93      21      76
+
+*Output*
+
+.. class:: infomark
+
+If run without specifying the columns::
+
+   Marker3
+   12
+   10
+   76
+   32
+   53
+   87
+   98
+   58
+   68
+   53
+   45
+   76
+
+.. class:: infomark
+
+If run specifying columns 1,2,3::
+
+   Marker1 Marker2 Marker3
+   34      45      12
+   33      65      10
+   87      26      76
+   24      56      32
+   95      83      53
+   74      15      87
+   19      62      98
+   12      36      58
+   41      42      68
+   76      74      53
+   62      34      45
+   93      21      76
+
+.. class:: infomark
+
+If run specifying columns 1,2,3 and with a downsampling factor of 0.5::
+
+   Marker1 Marker2 Marker3
+   34      45      12
+   24      56      32
+   95      83      53
+   19      62      98
+   12      36      58
+   62      34      45
+ ]]>
+  </help>
+  <citations>
+    <citation type="doi">10.1038/srep02327</citation>
+  </citations>
+</tool>
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/merge1.flowtext
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/merge1.flowtext Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,19 @@
+CD4 CCR3 CD8 CCR7
+437 69 0 146
+551 129 169 292
+199 277 320 227
+83 138 335 194
+534 111 83 177
+499 0 0 224
+175 361 225 237
+216 310 270 294
+519 44 51 148
+550 200 0 127
+552 479 0 62
+525 121 0 138
+438 0 626 480
+139 227 293 259
+0 292 641 327
+30 147 483 386
+537 338 568 201
+156 228 734 408
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/merge2.flowtext
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/merge2.flowtext Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,25 @@
+Forward Scatter Side Scatter FITC CD4
+340 115 509
+262 73 437
+894 1023 199
+316 76 50
+449 157 551
+388 97 534
+383 139 499
+394 144 83
+372 126 519
+788 1023 216
+1023 1023 289
+363 76 550
+668 1019 73
+420 211 552
+770 1023 175
+602 578 385
+418 105 561
+352 153 30
+383 190 156
+733 970 139
+451 120 537
+373 104 3
+358 185 0
+289 56 438
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/test1/input1.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/test1/input1.txt Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,10 @@
+CD4 CCR3 CD8 CCR7
+551 129 169 292
+199 277 320 227
+437 69 0 146
+509 268 0 74
+50 0 60 129
+83 138 335 194
+499 0 0 224
+239 284 288 280
+534 111 83 177
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/test1/input2.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/test1/input2.txt Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,10 @@
+CD4 CCR3 CD8 CCR7
+550 200 0 127
+519 44 51 148
+289 401 362 254
+175 361 225 237
+525 121 0 138
+385 286 222 131
+216 310 270 294
+552 479 0 62
+73 193 227 132
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/test1/input3.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/test1/input3.txt Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,10 @@
+CD4 CCR3 CD8 CCR7
+438 0 626 480
+30 147 483 386
+156 228 734 408
+432 121 598 555
+537 338 568 201
+3 110 621 584
+561 0 610 562
+0 292 641 327
+139 227 293 259
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/test2/input1.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/test2/input1.txt Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,10 @@
+Forward Scatter Side Scatter FITC CD4 PE CCR3 PP CD8 APC CCR4
+449 157 551 129 169 292
+894 1023 199 277 320 227
+262 73 437 69 0 146
+340 115 509 268 0 74
+316 76 50 0 60 129
+394 144 83 138 335 194
+383 139 499 0 0 224
+800 1023 239 284 288 280
+388 97 534 111 83 177
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/test2/input2.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/test2/input2.txt Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,10 @@
+Forward Scatter Side Scatter FITC CD4 PE CXCR3 PP CD8 APC CCR5
+363 76 550 200 0 127
+372 126 519 44 51 148
+1023 1023 289 401 362 254
+770 1023 175 361 225 237
+384 111 525 121 0 138
+602 578 385 286 222 131
+788 1023 216 310 270 294
+420 211 552 479 0 62
+668 1019 73 193 227 132
b
diff -r 000000000000 -r 426650130311 merge_ds_flowtext/test-data/test2/input3.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/merge_ds_flowtext/test-data/test2/input3.txt Mon Feb 27 13:03:02 2017 -0500
b
@@ -0,0 +1,10 @@
+Forward Scatter Side Scatter FITC CD4 PE CD25 PP CD3 APC CD45RA
+289 56 438 0 626 480
+352 153 30 147 483 386
+383 190 156 228 734 408
+261 62 432 121 598 555
+451 120 537 338 568 201
+373 104 3 110 621 584
+418 105 561 0 610 562
+358 185 0 292 641 327
+733 970 139 227 293 259