def getRawCoverageLargeMode(self, chrom, start, end, largeMode=10): """Retrieve an array with the genome coverage.""" out = np.zeros(end-start) infile = BigWigFile(self.path) wigs = infile.fetch(chrom, start, end) for wig in wigs: out[wig[0]-start:wig[1]-start] = wig[2] infile.close() return out[::largeMode]
def bigwig_fetcher(bigwig, ichr, istart, iend): bw=BigWigFile(bigwig) scores = [] with BigWigFile(bigwig) as bw: for i in bw.fetch(chrom=ichr,start=istart,stop=iend): scores.append(i.score) return scores bw.wWigIO.close()
def bigwig_loader(bigwig, chromInfo): print >> sys.stderr, "Loading the BigWig file in RAM memory ...", bw=BigWigFile(bigwig) for row in csv.reader(open(chromInfo), delimiter = '\t'): chr = row[0] chr_size = int(row[1]) for i in bw.fetch(chrom=chr,start=0,stop=chr_size): phylop_bigwig[chr].append(i.score) bw.wWigIO.close() print >> sys.stderr, "OK",
class BigWigDatasource(Datasource): """ A datasource derived from a BigWig file. For variants spanning a genomic range (i.e. non SNVs), the median of values from the BigWig are returned. """ def __init__(self, src_file, title='', version=None): # only necessary to import ngslib if instance of BigWigDatasource is created # This should not run on OS X machines from ngslib import BigWigFile super(BigWigDatasource, self).__init__(src_file, title=title, version=version) self.output_headers = [title + '_score'] self.bigwig_fh = BigWigFile(src_file) self.has_chr = True if self.bigwig_fh.chroms[0].startswith('chr') else False def annotate_mutation(self, mutation): if self.has_chr and not mutation.chr.startswith('chr'): chrn = 'chr' + mutation.chr else: chrn = mutation.chr variant_start, variant_end = int(mutation.start) - 1, int(mutation.end) #start - 1 because bigwig format is zero-based coords scores = [r[2] for r in self.bigwig_fh.fetch(chrom=chrn, start=variant_start, stop=variant_end)] if not scores: final_score = None elif len(scores) == 1: final_score = scores[0] else: final_score = np.median(scores) mutation.createAnnotation(self.output_headers[0], final_score, annotationSource=self.title) return mutation def close(self): self.bigwig_fh.close()
def bigwig_mean(bigwig, chr, start, end): bw=BigWigFile(bigwig) score_sum = 0 mean_score = 0 with BigWigFile(bigwig) as bw: for i in bw.fetch(chrom=chr,start=start,stop=end): score_sum += i.score if (end-start) != 0: mean_score = score_sum/(end-start) else: mean_score = 0 return mean_score bw.wWigIO.close()