| 1 | """ |
| 2 | Gather task for BigWig output files |
| 3 | """ |
| 4 | |
| 5 | from collectionsscip-python python python-stdlib 3.11 collections/__init__: import namedtuplescip-python python python-stdlib 3.11 collections/namedtuple()., OrderedDictscip-python python python-stdlib 3.11 collections/OrderedDict#, defaultdictscip-python python python-stdlib 3.11 collections/defaultdict# |
| 6 | import itertoolsscip-python python python-stdlib 3.11 itertools/__init__: |
| 7 | import loggingscip-python python python-stdlib 3.11 logging/__init__: |
| 8 | import mathscip-python python python-stdlib 3.11 math/__init__: |
| 9 | import os.pathscip-python python python-stdlib 3.11 `os.path`/__init__: as op |
| 10 | import sysscip-python python python-stdlib 3.11 sys/__init__: |
| 11 | |
| 12 | from pbcommand.cliscip-python python pbcommand 2.4.4 `pbcommand.cli`/__init__: import (pacbio_args_runner, |
| 13 | get_default_argparser_with_base_opts) |
| 14 | from pbcommand.utilsscip-python python pbcommand 2.4.4 `pbcommand.utils`/__init__: import setup_log |
| 15 | |
| 16 | logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log. = loggingscip-python python python-stdlib 3.11 logging/__init__:.getLoggerscip-python python python-stdlib 3.11 logging/getLogger().(__name__scip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/) |
| 17 | __version__scip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/__version__. = "0.2" |
| 18 | |
| 19 | |
| 20 | def gather_bigwigscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(input_filesscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(input_files), output_filescip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(output_file)): |
| 21 | import pyBigWig |
| 22 | chr_lengths = {} |
| 23 | FileInfo = namedtuplescip-python python python-stdlib 3.11 collections/namedtuple().("FileInfo", ("file_name", "file_id", "seqids")) |
| 24 | files_info = [] |
| 25 | for i, file_name in enumeratescip-python python python-stdlib 3.11 builtins/enumerate#(input_filesscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(input_files)): |
| 26 | logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log..infoscip-python python python-stdlib 3.11 logging/Logger#info().("Reading header info from {f}...".format(f=file_name)) |
| 27 | if op.getsizescip-python python python-stdlib 3.11 genericpath/isfile().(file_name) == 0: |
| 28 | continue |
| 29 | try: |
| 30 | file_id = intscip-python python python-stdlib 3.11 builtins/int#(op.dirnamescip-python python python-stdlib 3.11 genericpath/isfile().(file_name).split("-")[-1]) |
| 31 | except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#: |
| 32 | file_id = i |
| 33 | bw_chunk = pyBigWig.open(file_name) |
| 34 | seqids = [] |
| 35 | for (seqid, length) in bw_chunk.chroms().items(): |
| 36 | chr_lengths.setdefaultscip-python python python-stdlib 3.11 typing/MutableMapping#setdefault().(seqid, 0) |
| 37 | chr_lengths[seqid] = maxscip-python python python-stdlib 3.11 builtins/max().(length, chr_lengths[seqid]) |
| 38 | seqids.appendscip-python python python-stdlib 3.11 builtins/list#append().(seqid) |
| 39 | files_info.appendscip-python python python-stdlib 3.11 builtins/list#append().(FileInfo(file_name, file_id, seqids)) |
| 40 | bw_chunk.close() |
| 41 | if len(files_info) == 0: |
| 42 | with openscip-python python python-stdlib 3.11 builtins/open().(output_filescip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(output_file), "wb") as f: |
| 43 | return output_filescip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(output_file) |
| 44 | bw = pyBigWig.open(output_filescip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(output_file), "w") |
| 45 | files_info.sortscip-python python python-stdlib 3.11 builtins/list#sort().(keyscip-python python python-stdlib 3.11 builtins/list#sort().(key)=lambda f: f.file_id) |
| 46 | regions = OrderedDictscip-python python python-stdlib 3.11 collections/OrderedDict#() |
| 47 | seqid_files = defaultdictscip-python python python-stdlib 3.11 collections/defaultdict#(listscip-python python python-stdlib 3.11 builtins/list#) |
| 48 | for f in files_info: |
| 49 | for seqid in f.seqids: |
| 50 | logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log..debugscip-python python python-stdlib 3.11 logging/Logger#debug().("{f} ({i}): {s} {l}".format(f=f.file_name, |
| 51 | i=f.file_id, |
| 52 | s=seqid, |
| 53 | l=chr_lengths[seqid])) |
| 54 | regions[seqid] = chr_lengths[seqid] |
| 55 | seqid_files[seqid].appendscip-python python python-stdlib 3.11 builtins/list#append().(f) |
| 56 | bw.addHeader([(k, v) for k, v in regions.itemsscip-python python python-stdlib 3.11 collections/OrderedDict#items().()]) |
| 57 | seq_chunk = namedtuplescip-python python python-stdlib 3.11 collections/namedtuple().("SeqChunk", ("starts", "ends", "values")) |
| 58 | for (seqid, length) in regions.itemsscip-python python python-stdlib 3.11 collections/OrderedDict#items().(): |
| 59 | logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log..infoscip-python python python-stdlib 3.11 logging/Logger#info().("Collecting values for {i}...".format(i=seqid)) |
| 60 | chunks = [] |
| 61 | k = 0 |
| 62 | for file_info in seqid_files[seqid]: |
| 63 | logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log..infoscip-python python python-stdlib 3.11 logging/Logger#info().("Reading values from {f}".format(f=file_info.file_name)) |
| 64 | bw_chunk = pyBigWig.open(file_info.file_name) |
| 65 | starts, ends, values = [], [], [] |
| 66 | chr_max = bw_chunk.chroms()[seqid] |
| 67 | for i, val in enumeratescip-python python python-stdlib 3.11 builtins/enumerate#(bw_chunk.values(seqid, 0, chr_max)): |
| 68 | if not mathscip-python python python-stdlib 3.11 math/__init__:.isnanscip-python python python-stdlib 3.11 math/isnan().(val): |
| 69 | starts.appendscip-python python python-stdlib 3.11 builtins/list#append().(i) |
| 70 | ends.appendscip-python python python-stdlib 3.11 builtins/list#append().(i + 1) |
| 71 | values.appendscip-python python python-stdlib 3.11 builtins/list#append().(val) |
| 72 | k += 1 |
| 73 | chunks.appendscip-python python python-stdlib 3.11 builtins/list#append().(seq_chunk(starts, ends, values)) |
| 74 | bw_chunk.close() |
| 75 | chunks.sortscip-python python python-stdlib 3.11 builtins/list#sort().(keyscip-python python python-stdlib 3.11 builtins/list#sort().(key)=lambda c: c.starts[0]) |
| 76 | starts = listscip-python python python-stdlib 3.11 builtins/list#(itertoolsscip-python python python-stdlib 3.11 itertools/__init__:.chainscip-python python python-stdlib 3.11 itertools/chain#(*[x.starts for x in chunks])) |
| 77 | ends = listscip-python python python-stdlib 3.11 builtins/list#(itertoolsscip-python python python-stdlib 3.11 itertools/__init__:.chainscip-python python python-stdlib 3.11 itertools/chain#(*[x.ends for x in chunks])) |
| 78 | values = listscip-python python python-stdlib 3.11 builtins/list#(itertoolsscip-python python python-stdlib 3.11 itertools/__init__:.chainscip-python python python-stdlib 3.11 itertools/chain#(*[x.values for x in chunks])) |
| 79 | seqids = [seqid] * len(starts) |
| 80 | logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log..infoscip-python python python-stdlib 3.11 logging/Logger#info().("Adding {i}:{s}-{e}".format(i=seqid, s=starts[0], e=ends[-1])) |
| 81 | bw.addEntries(seqids, starts, ends=ends, values=values) |
| 82 | bw.close() |
| 83 | return 0 |
| 84 | |
| 85 | |
| 86 | def _get_parserscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/_get_parser().(): |
| 87 | p = get_default_argparser_with_base_opts( |
| 88 | version=__version__scip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/__version__., |
| 89 | description=__doc__scip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/, |
| 90 | default_level="INFO") |
| 91 | p.add_argumentscip-python python python-stdlib 3.11 argparse/_ActionsContainer#add_argument().("merged", help="Name of merged BigWig file") |
| 92 | p.add_argumentscip-python python python-stdlib 3.11 argparse/_ActionsContainer#add_argument().("chunks", nargsscip-python python python-stdlib 3.11 argparse/_ActionsContainer#add_argument().(nargs)="+", help="Chunked BigWig files") |
| 93 | return p |
| 94 | |
| 95 | |
| 96 | def run_argsscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/run_args().(argsscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/run_args().(args)): |
| 97 | return gather_bigwigscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/gather_bigwig().(argsscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/run_args().(args).chunks, argsscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/run_args().(args).merged) |
| 98 | |
| 99 | |
| 100 | def mainscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/main().(argvscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/main().(argv)=sysscip-python python python-stdlib 3.11 sys/__init__:.argvscip-python python python-stdlib 3.11 sys/argv.argv.): |
| 101 | return pacbio_args_runner( |
| 102 | argv=argvscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/main().(argv)[1:], |
| 103 | parser=_get_parserscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/_get_parser().(), |
| 104 | args_runner_func=run_argsscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/run_args()., |
| 105 | alog=logscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/log., |
| 106 | setup_log_func=setup_log) |
| 107 | |
| 108 | |
| 109 | if __name__scip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/ == '__main__': |
| 110 | sysscip-python python python-stdlib 3.11 sys/__init__:.exit(mainscip-python python kineticsTools 0.8.0 `kineticsTools.tasks.gather_bigwig`/main().()) |