Mercurial > repos > fubar > bigwig_outlier_bed
annotate asciihist.py @ 8:032e930ef6a1 draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
author | fubar |
---|---|
date | Wed, 24 Jul 2024 09:19:08 +0000 |
parents | |
children |
rev | line source |
---|---|
8
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
1 from __future__ import print_function |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
2 import numpy as np |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
3 |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
4 |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
5 def asciihist( |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
6 it, |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
7 bins=10, |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
8 minmax=None, |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
9 str_tag="", |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
10 scale_output=30, |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
11 generate_only=False, |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
12 print_function=print, |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
13 ): |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
14 """Create an ASCII histogram from an interable of numbers. |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
15 Author: Boris Gorelik boris@gorelik.net. based on http://econpy.googlecode.com/svn/trunk/pytrix/pytrix.py |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
16 License: MIT |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
17 """ |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
18 ret = [] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
19 itarray = np.asanyarray(it) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
20 if minmax == "auto": |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
21 minmax = np.percentile(it, [5, 95]) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
22 if minmax[0] == minmax[1]: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
23 # for very ugly distributions |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
24 minmax = None |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
25 if minmax is not None: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
26 # discard values that are outside minmax range |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
27 mn = minmax[0] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
28 mx = minmax[1] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
29 itarray = itarray[itarray >= mn] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
30 itarray = itarray[itarray <= mx] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
31 if itarray.size: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
32 total = len(itarray) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
33 counts, cutoffs = np.histogram(itarray, bins=bins) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
34 cutoffs = cutoffs[1:] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
35 if str_tag: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
36 str_tag = "%s " % str_tag |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
37 else: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
38 str_tag = "" |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
39 if scale_output is not None: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
40 scaled_counts = counts.astype(float) / counts.sum() * scale_output |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
41 else: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
42 scaled_counts = counts |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
43 |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
44 if minmax is not None: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
45 ret.append("Trimmed to range (%s - %s)" % (str(minmax[0]), str(minmax[1]))) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
46 for cutoff, original_count, scaled_count in zip(cutoffs, counts, scaled_counts): |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
47 ret.append( |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
48 "{:s}{:>8.2f} |{:<7,d} | {:s}".format( |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
49 str_tag, cutoff, original_count, "*" * int(scaled_count) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
50 ) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
51 ) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
52 ret.append("{:s}{:s} |{:s} | {:s}".format(str_tag, "-" * 8, "-" * 7, "-" * 7)) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
53 ret.append("{:s}{:>8s} |{:<7,d}".format(str_tag, "N=", total)) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
54 else: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
55 ret = [] |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
56 if not generate_only: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
57 for line in ret: |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
58 print_function(line) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
59 ret = "\n".join(ret) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
60 return ret |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
61 |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
62 |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
63 if __name__ == "__main__": |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
64 np.random.seed(11) |
032e930ef6a1
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/bigwig_outlier_bed commit 7eac2f224d3126002edd2c02d1133b23ac1a4881
fubar
parents:
diff
changeset
|
65 asciihist(np.random.randn(10000), minmax="auto", str_tag="Normal") |