Last active
February 25, 2021 21:17
-
-
Save alexpreynolds/7813fab82849b35d5667e3bcc6019f7d to your computer and use it in GitHub Desktop.
Pyranges equivalent via subprocess + BEDOPS bedmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import subprocess | |
import json | |
a = sys.argv[1] | |
b = sys.argv[2] | |
class SetEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, set): | |
return list(obj) | |
return json.JSONEncoder.default(self, obj) | |
m = {} | |
p = subprocess.Popen(['bedmap', '--echo', '--echo-map-id-uniq', '--mean', a, b], stdout=subprocess.PIPE) | |
for l in iter(p.stdout.readline, b''): | |
e = l.decode().rstrip().split('\t') | |
(a_k, a_v) = e[3].split('-') | |
if a_k not in m: | |
m[a_k] = {} | |
if a_v not in m[a_k]: | |
m[a_k][a_v] = {} | |
(a_s, b_kvps, ab_mean) = e[4].split('|') | |
b_kvs = [x.split('-') for x in b_kvps.split(';')] | |
for b_kv in b_kvs: | |
if len(b_kv[0]) > 0: | |
if b_kv[0] not in m[a_k][a_v]: | |
m[a_k][a_v][b_kv[0]] = { 'average_signal' : ab_mean , 'elements' : set() } | |
m[a_k][a_v][b_kv[0]]['elements'].add(b_kv[1]) | |
sys.stdout.write(json.dumps(m, cls=SetEncoder, sort_keys=True, indent=2)) |
Author
alexpreynolds
commented
Feb 25, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment