def __call__(self, **kw): b2wargs = [] control = None if kw.get('control'): control = kw['control'] b2wargs = ["-c", str(control)] bamfile = track(kw['sample'], format='bam') nreads = int(kw.get('normalization') or -1) if nreads < 0: if control is None: nreads = len(set((t[4] for t in bamfile.read()))) else: b2wargs += ["-r"] merge_strands = int(kw.get('merge_strands') or -1) if merge_strands >= 0: suffixes = ["merged"] else: suffixes = ["fwd", "rev"] read_extension = int(kw.get('read_extension') or -1) output = self.temporary_path(fname='density_') with execution(None) as ex: files = bam_to_density(ex, kw['sample'], output, nreads=nreads, merge=merge_strands, read_extension=read_extension, sql=True, args=b2wargs) for n, x in enumerate(files): tout = track(x, format='sql', fields=['start', 'end', 'score'], chrmeta=bamfile.chrmeta, info={'datatype': 'quantitative'}) tout.save() self.new_file(x, 'density_' + suffixes[n]) return 1
def __call__(self, **kw): b2wargs = [] control = None sample = kw.get("sample") assert os.path.exists(str(sample)), "Bam file not found: '%s'." % sample if kw.get('control'): control = kw['control'] b2wargs = ["-c", str(control)] assert os.path.exists(str(control)), "Control file not found: '%s'." % control control = os.path.abspath(control) sample = os.path.abspath(sample) nreads = int(kw.get('normalization') or -1) bamfile = track(sample, format='bam') if nreads < 0: if control is None: nreads = len(set((t[4] for t in bamfile.read()))) else: b2wargs += ["-r"] merge_strands = int(kw.get('merge_strands') or -1) read_extension = int(kw.get('read_extension') or -1) output = self.temporary_path(fname='density_') format = kw.get("format", "sql") with execution(None) as ex: files = bam_to_density(ex, sample, output, nreads=nreads, merge=merge_strands, read_extension=read_extension, sql=True, args=b2wargs) if merge_strands >= 0: suffixes = ["merged"] else: suffixes = ["fwd", "rev"] for n, x in enumerate(files): tsql = track(x, format='sql', fields=['start', 'end', 'score'], chrmeta=bamfile.chrmeta, info={'datatype': 'quantitative'}) tsql.save() if format == "sql": outname = x else: outname = os.path.splitext(x)[0]+"."+format convert(x, outname, mode="overwrite") self.new_file(outname, 'density_'+suffixes[n]) return self.display_time()
def __call__(self, **kw): b2wargs = [] control = None samples = kw.get('BamMulti', {}).get('sample', []) if not isinstance(samples, list): samples = [samples] samples = [os.path.abspath(s) for s in samples if os.path.exists(s)] if kw.get('control'): control = kw['control'] b2wargs = ["-c", str(control)] assert os.path.exists( str(control)), "Control file not found: '%s'." % control control = os.path.abspath(control) try: nreads = int(kw.get('normalization')) except (ValueError, TypeError): nreads = -1 bamfiles = [track(s, format='bam') for s in samples] if nreads < 0: _nreads = [0] * len(samples) if control is not None: b2wargs += ["-r"] else: _nreads = [nreads for s in samples] try: merge_strands = int(kw.get('merge_strands')) except (ValueError, TypeError): merge_strands = -1 try: read_extension = int(kw.get('read_extension')) except (ValueError, TypeError): read_extension = -1 output = [ self.temporary_path(fname=b.name + '_density_') for b in bamfiles ] format = kw.get("format", "sql") with execution(None) as ex: files = [ bam_to_density(ex, s, output[n], nreads=_nreads[n], merge=merge_strands, read_extension=read_extension, sql=True, args=b2wargs) for n, s in enumerate(samples) ] info = {'datatype': 'quantitative', 'read_extension': read_extension} if merge_strands >= 0: suffixes = ["merged"] info['shift'] = merge_strands else: suffixes = ["fwd", "rev"] chrmeta = bamfiles[0].chrmeta for suf in suffixes: all_s_files = [ x for y in files for x in y if x.endswith(suf + ".sql") ] if len(all_s_files) > 1: x = self.temporary_path(fname="Density_average_" + suf + ".sql") tsql = track(x, fields=['start', 'end', 'score'], chrmeta=chrmeta, info={'datatype': 'quantitative'}) insql = [] for f in all_s_files: t = track(f, format='sql', chrmeta=chrmeta) t.save() insql.append(t) for c in tsql.chrmeta: tsql.write(merge_scores([t.read(c) for t in insql]), chrom=c) else: x = all_s_files[0] tsql = track(x, format='sql', fields=['start', 'end', 'score'], chrmeta=chrmeta, info=info) tsql.save() if format in [None, "sql"]: outname = x else: outname = os.path.splitext(x)[0] + "." + format convert(x, outname, mode="overwrite") self.new_file(outname, 'density_' + suf) return self.display_time()
def __call__(self, **kw): b2wargs = [] control = None samples = kw.get('BamMulti',{}).get('sample', []) if not isinstance(samples, list): samples = [samples] samples = [os.path.abspath(s) for s in samples if os.path.exists(s)] if kw.get('control'): control = kw['control'] b2wargs = ["-c", str(control)] assert os.path.exists(str(control)), "Control file not found: '%s'." % control control = os.path.abspath(control) try: nreads = int(kw.get('normalization')) except (ValueError, TypeError): nreads = -1 bamfiles = [track(s, format='bam') for s in samples] if nreads < 0: _nreads = [0]*len(samples) if control is not None: b2wargs += ["-r"] else: _nreads = [nreads for s in samples] try: merge_strands = int(kw.get('merge_strands')) except (ValueError, TypeError): merge_strands = -1 try: read_extension = int(kw.get('read_extension')) except (ValueError, TypeError): read_extension = -1 output = [self.temporary_path(fname=b.name+'_density_') for b in bamfiles] format = kw.get("format", "sql") with execution(None) as ex: files = [bam_to_density( ex, s, output[n], nreads=_nreads[n], merge=merge_strands, read_extension=read_extension, sql=True, args=b2wargs ) for n,s in enumerate(samples)] info = {'datatype': 'quantitative', 'read_extension': read_extension} if merge_strands >= 0: suffixes = ["merged"] info['shift'] = merge_strands else: suffixes = ["fwd", "rev"] chrmeta = bamfiles[0].chrmeta for suf in suffixes: all_s_files = [x for y in files for x in y if x.endswith(suf+".sql")] if len(all_s_files) > 1: x = self.temporary_path(fname="Density_average_"+suf+".sql") tsql = track( x, fields=['start', 'end', 'score'], chrmeta=chrmeta, info={'datatype': 'quantitative'} ) insql = [] for f in all_s_files: t = track(f, format='sql', chrmeta=chrmeta) t.save() insql.append(t) for c in tsql.chrmeta: tsql.write(merge_scores([t.read(c) for t in insql]),chrom=c) else: x = all_s_files[0] tsql = track( x, format='sql', fields=['start', 'end', 'score'], chrmeta=chrmeta, info=info ) tsql.save() if format in [None,"sql"]: outname = x else: outname = os.path.splitext(x)[0]+"."+format convert(x, outname, mode="overwrite") self.new_file(outname, 'density_'+suf) return self.display_time()