changeset 0:a80d960a253b draft

planemo upload commit 9e980862c8b763cf3dd0209be49f0d2d75a90614-dirty
author scottx611x
date Tue, 22 Nov 2016 16:39:51 -0500
parents
children 9c4e9f323b6d
files recursive_agg_onefile.py recursive_agg_onefile.xml tool_dependencies.xml
diffstat 3 files changed, 232 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/recursive_agg_onefile.py	Tue Nov 22 16:39:51 2016 -0500
@@ -0,0 +1,104 @@
+from __future__ import division, print_function
+import sys
+
+from multiprocessing import Pool
+import argparse
+import warnings
+
+import numpy as np
+import cooler
+from cooler.io import CoolerAggregator
+import cooler.ice
+import h5py
+
+
+FACTOR = 2
+TILESIZE = 256
+N_CPU = 8
+
+
+def main(infile, outfile, chunksize):
+    c = cooler.Cooler(infile)
+    binsize = c.info['bin-size']
+    chromtable = c.chroms()[:]
+    chromsizes = chromtable.set_index('name')['length']
+    chroms = chromtable['name'].values
+    lengths = chromtable['length'].values
+    total_length = np.sum(chromsizes.values)
+    n_tiles = total_length / binsize / TILESIZE
+    n_zooms = int(np.ceil(np.log2(n_tiles)))
+
+    print(
+        "Copying base matrix to level {0} and producing {0} zoom levels starting from 0...".format(
+            n_zooms),
+        file=sys.stdout
+    )
+
+    # transfer base matrix
+    with h5py.File(outfile, 'w') as dest, \
+            h5py.File(infile, 'r') as src:
+        zoomLevel = str(n_zooms)
+        src.copy('/', dest, zoomLevel)
+        print(zoomLevel, file=sys.stdout)
+
+    # produce aggregations
+    with h5py.File(outfile, 'r+') as f:
+        grp = f[str(n_zooms)]
+        c = cooler.Cooler(grp)
+        binsize = cooler.info(grp)['bin-size']
+
+        for i in range(n_zooms - 1, -1, -1):
+            zoomLevel = str(i)
+
+            # aggregate
+            new_binsize = binsize * FACTOR
+            new_bins = cooler.util.binnify(chromsizes, new_binsize)
+
+            reader = CoolerAggregator(c, new_bins, chunksize)
+
+            grp = f.create_group(zoomLevel)
+            f.attrs[zoomLevel] = new_binsize
+            cooler.io.create(grp, chroms, lengths, new_bins, reader)
+
+            # balance
+            # with Pool(N_CPU) as pool:
+            too_close = 20000  # for HindIII
+            ignore_diags = max(int(np.ceil(too_close / new_binsize)), 3)
+
+            bias = cooler.ice.iterative_correction(
+                f, zoomLevel,
+                chunksize=chunksize,
+                min_nnz=10,
+                mad_max=3,
+                ignore_diags=ignore_diags,
+                map=map)
+            h5opts = dict(compression='gzip', compression_opts=6)
+            grp['bins'].create_dataset('weight', data=bias, **h5opts)
+
+            print(zoomLevel, file=sys.stdout)
+
+            c = cooler.Cooler(grp)
+            binsize = new_binsize
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(
+        description="Recursively aggregate a single resolution cooler file into a multi-resolution file.")
+    parser.add_argument(
+        "cooler_file",
+        help="Cooler file",
+        metavar="COOLER_PATH")
+    parser.add_argument(
+        "--out", "-o",
+        help="Output text file")
+    args = vars(parser.parse_args())
+
+    infile = args['cooler_file']
+    if args['out'] is None:
+        outfile = infile.replace('.cool', '.multires.cool')
+    else:
+        outfile = args['out']
+
+    chunksize = int(1e6)
+    with warnings.catch_warnings():
+        warnings.simplefilter("ignore")
+        main(infile, outfile, chunksize)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/recursive_agg_onefile.xml	Tue Nov 22 16:39:51 2016 -0500
@@ -0,0 +1,16 @@
+
+<tool id="generate_multires_cooler" name="Generate Multi-res Cooler File" version="1.1.0">
+  <description>Recursively aggregate a single resolution cooler file into a multi-resolution file.</description>
+  <command interpreter="python">$__tool_directory__/recursive_agg_onefile.py $input --out $output</command>
+  <inputs>
+    <param format="h5" name="input" type="data" label="Source cooler file"/>
+  </inputs>
+  <outputs>
+	  <data format="multires.cool" name="output" />
+  </outputs>
+
+<help>
+	Recursively aggregate a single resolution cooler file into a multi-resolution file.  
+</help>
+
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_dependencies.xml	Tue Nov 22 16:39:51 2016 -0500
@@ -0,0 +1,112 @@
+<?xml version="1.0"?>
+<tool_dependency>
+    <package name="setuptools" version="19.0">
+        <repository changeset_revision="c0a6821af233" name="package_setuptools_19_0" owner="iuc" prior_installation_required="True" toolshed="https://testtoolshed.g2.bx.psu.edu" />
+    </package>
+    <package name="hdf5" version="1.8.12">
+        <repository changeset_revision="b6d5355e3754" name="package_hdf5_1_8_12" owner="iuc" prior_installation_required="True" toolshed="https://testtoolshed.g2.bx.psu.edu" />
+    </package>
+    <package name="cooler" version="0.5.3">
+        <install name="pandas_install" version="1.11.0">
+            <actions>
+                <action md5="42b9dba111a8f916352f49b512c029aa" type="download_by_url">https://pypi.python.org/packages/11/09/e66eb844daba8680ddff26335d5b4fead77f60f957678243549a8dd4830d/pandas-0.18.1.tar.gz</action>
+                <action type="set_environment_for_install">
+                    <repository changeset_revision="c0a6821af233" name="package_setuptools_19_0" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="setuptools" version="19.0" />
+                    </repository>
+                </action>
+                <action type="make_directory">$INSTALL_DIR/lib/python</action>
+                <action type="shell_command">
+                    export PYTHONPATH=$PYTHONPATH:$INSTALL_DIR/lib/python &amp;&amp;
+                    python setup.py install --install-lib $INSTALL_DIR/lib/python --install-scripts $INSTALL_DIR/bin
+                </action>
+                <action type="set_environment">
+                    <environment_variable action="prepend_to" name="PYTHONPATH">$INSTALL_DIR/lib/python</environment_variable>
+                    <environment_variable action="prepend_to" name="PATH">$INSTALL_DIR/bin</environment_variable>
+                </action>
+            </actions>
+        </install>
+        <install name="h5py_install">
+            <actions>
+                <action md5="ec476211bd1de3f5ac150544189b0bf4" type="download_by_url">https://pypi.python.org/packages/22/82/64dada5382a60471f85f16eb7d01cc1a9620aea855cd665609adf6fdbb0d/h5py-2.6.0.tar.gz</action>
+                <action type="set_environment_for_install">
+                    <repository changeset_revision="c0a6821af233" name="package_setuptools_19_0" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="setuptools" version="19.0" />
+                    </repository>
+                    <repository changeset_revision="b6d5355e3754" name="package_hdf5_1_8_12" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="hdf5" version="1.8.12" />
+                    </repository>
+                </action>
+                <action type="make_directory">$INSTALL_DIR/lib/python</action>
+                <action type="shell_command">
+                    export PYTHONPATH=$PYTHONPATH:$INSTALL_DIR/lib/python &amp;&amp;
+                    python setup.py install --install-lib $INSTALL_DIR/lib/python --install-scripts $INSTALL_DIR/bin
+                </action>
+                <action type="set_environment">
+                    <environment_variable action="prepend_to" name="PYTHONPATH">$INSTALL_DIR/lib/python</environment_variable>
+                    <environment_variable action="prepend_to" name="PATH">$INSTALL_DIR/bin</environment_variable>
+                </action>
+            </actions>
+        </install>
+        <install name="scipy_install">
+            <actions>
+                <action md5="5fb5fb7ccb113ab3a039702b6c2f3327" type="download_by_url">https://pypi.python.org/packages/22/41/b1538a75309ae4913cdbbdc8d1cc54cae6d37981d2759532c1aa37a41121/scipy-0.18.1.tar.gz</action>
+                <action type="set_environment_for_install">
+                    <repository changeset_revision="c0a6821af233" name="package_setuptools_19_0" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="setuptools" version="19.0" />
+                    </repository>
+                </action>
+                <action type="make_directory">$INSTALL_DIR/lib/python</action>
+                <action type="shell_command">
+                    export PYTHONPATH=$PYTHONPATH:$INSTALL_DIR/lib/python &amp;&amp;
+                    python setup.py install --install-lib $INSTALL_DIR/lib/python --install-scripts $INSTALL_DIR/bin
+                </action>
+                <action type="set_environment">
+                    <environment_variable action="prepend_to" name="PYTHONPATH">$INSTALL_DIR/lib/python</environment_variable>
+                    <environment_variable action="prepend_to" name="PATH">$INSTALL_DIR/bin</environment_variable>
+                </action>
+            </actions>
+        </install>
+        <install name="numpy_install" version="1.11.0">
+            <actions>
+                <action md5="bc56fb9fc2895aa4961802ffbdb31d0b" type="download_by_url">https://pypi.python.org/packages/1a/5c/57c6920bf4a1b1c11645b625e5483d778cedb3823ba21a017112730f0a12/numpy-1.11.0.tar.gz</action>
+                <action type="set_environment_for_install">
+                    <repository changeset_revision="c0a6821af233" name="package_setuptools_19_0" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="setuptools" version="19.0" />
+                    </repository>
+                </action>
+                <action type="make_directory">$INSTALL_DIR/lib/python</action>
+                <action type="shell_command">
+                    export PYTHONPATH=$PYTHONPATH:$INSTALL_DIR/lib/python &amp;&amp;
+                    python setup.py install --install-lib $INSTALL_DIR/lib/python --install-scripts $INSTALL_DIR/bin
+                </action>
+                <action type="set_environment">
+                    <environment_variable action="prepend_to" name="PYTHONPATH">$INSTALL_DIR/lib/python</environment_variable>
+                    <environment_variable action="prepend_to" name="PATH">$INSTALL_DIR/bin</environment_variable>
+                </action>
+            </actions>
+        </install>
+        <install name="cooler_install" version="0.5.3">
+            <actions>
+                <action md5="f95024c8c201df01fbaa2de10d81edd3" type="download_by_url">https://pypi.python.org/packages/64/6e/8ed3ba753e269e0cb393ce8898292e4540a763f155bf6cf855b454dbbd2a/cooler-0.5.3.tar.gz</action>
+                <action type="set_environment_for_install">
+                    <repository changeset_revision="c0a6821af233" name="package_setuptools_19_0" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="setuptools" version="19.0" />
+                    </repository>
+                    <repository changeset_revision="b6d5355e3754" name="package_hdf5_1_8_12" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu">
+                        <package name="hdf5" version="1.8.12" />
+                    </repository>
+                </action>
+                <action type="make_directory">$INSTALL_DIR/lib/python</action>
+                <action type="shell_command">
+                    export PYTHONPATH=$PYTHONPATH:$INSTALL_DIR/lib/python &amp;&amp;
+                    python setup.py install --install-lib $INSTALL_DIR/lib/python --install-scripts $INSTALL_DIR/bin
+                </action>
+                <action type="set_environment">
+                    <environment_variable action="prepend_to" name="PYTHONPATH">$INSTALL_DIR/lib/python</environment_variable>
+                    <environment_variable action="prepend_to" name="PATH">$INSTALL_DIR/bin</environment_variable>
+                </action>
+            </actions>
+        </install>
+    </package>
+</tool_dependency>