def fixedWigIterator(fd, verbose=False, sortedby=None, scoreType=int): """ @summary: """ fh = openFD(fd) if verbose: try: pind = ProgressIndicator(totalToDo=os.path.getsize(fh.name), messagePrefix="completed", messageSuffix="of processing " + fh.name) except AttributeError: sys.stderr.write("WigIterator -- warning: " + "unable to show progress for stream") verbose = False chromsSeen = set() prev = None # NUMBERS = set(['1','2','3','4','5','6','7','8','9','0','.']) currentChrom, at, step = None, None, None for line in fh: line = line.strip() if line == "": continue if line[0] == 't' or line[0] == 'f': parts = line.split() if parts[0] == "track": continue elif parts[0] == "fixedStep": currentChrom = parts[1].split("=")[1] at = int(parts[2].split("=")[1]) step = int(parts[3].split("=")[1]) else: val = float(line) e = GenomicInterval(currentChrom, at, at + step, None, val, scoreType=scoreType) # on same chrom as the prev item, make sure order is right if prev is not None and sortedby is not None and e.chrom == prev.chrom: if sortedby == ITERATOR_SORTED_START and prev.start > e.start: raise WigIteratorError("Wig file " + fd.name + " not sorted by start index - saw item " + str(prev) + " before " + str(e)) # starting a new chrom.. make sure we haven't already seen it if prev is not None and prev.chrom != e.chrom: if (sortedby == ITERATOR_SORTED_START) and\ (e.chrom in chromsSeen or prev.chrom > e.chrom): raise WigIteratorError("Wig file " + fd.name + " not sorted by chrom") chromsSeen.add(e.chrom) # all good.. yield e prev = e at += step if verbose : pind.done = fh.tell() pind.showProgress()
def regularWigIterator(fd, verbose=False, sortedby=None, scoreType=int): """ @param sortedBy: if not None, should be one of ITERATOR_SORTED_BY_START indicating an order that the input stream must be sorted in @raise WigIteratorError: if sortedBy is set and stream is not sorted """ if verbose: try: totalLines = linesInFile(fd) pind = ProgressIndicator(totalToDo=totalLines, messagePrefix="completed", messageSuffix="of processing " + getFDName(fd)) except AttributeError: sys.stderr.write("WigIterator -- warning: " + "unable to show progress for stream") verbose = False chromsSeen = set() prev = None fh = openFD(fd) for line in fh: if verbose: pind.done += 1 pind.showProgress() line = line.strip() if line == "": continue e = parseWigString(line, scoreType=scoreType) # on same chrom as the prev item, make sure order is right if prev is not None and sortedby is not None and e.chrom == prev.chrom: if sortedby == ITERATOR_SORTED_START and prev.start > e.start: raise WigIteratorError( "bed file " + fd.name + " not sorted by start index - saw item " + str(prev) + " before " + str(e)) # starting a new chrom.. make sure we haven't already seen it if prev is not None and prev.chrom != e.chrom: if (sortedby == ITERATOR_SORTED_START) and\ (e.chrom in chromsSeen or prev.chrom > e.chrom): raise WigIteratorError("BED file " + fd.name + " not sorted by chrom") chromsSeen.add(e.chrom) # all good.. yield e prev = e
def regularWigIterator(fd, verbose=False, sortedby=None, scoreType=int): """ @param sortedBy: if not None, should be one of ITERATOR_SORTED_BY_START indicating an order that the input stream must be sorted in @raise WigIteratorError: if sortedBy is set and stream is not sorted """ if verbose: try: totalLines = linesInFile(fd) pind = ProgressIndicator(totalToDo=totalLines, messagePrefix="completed", messageSuffix="of processing " + getFDName(fd)) except AttributeError: sys.stderr.write("WigIterator -- warning: " + "unable to show progress for stream") verbose = False chromsSeen = set() prev = None fh = openFD(fd) for line in fh: if verbose: pind.done += 1 pind.showProgress() line = line.strip() if line == "": continue e = parseWigString(line, scoreType=scoreType) # on same chrom as the prev item, make sure order is right if prev is not None and sortedby is not None and e.chrom == prev.chrom: if sortedby == ITERATOR_SORTED_START and prev.start > e.start: raise WigIteratorError("bed file " + fd.name + " not sorted by start index - saw item " + str(prev) + " before " + str(e)) # starting a new chrom.. make sure we haven't already seen it if prev is not None and prev.chrom != e.chrom: if (sortedby == ITERATOR_SORTED_START) and\ (e.chrom in chromsSeen or prev.chrom > e.chrom): raise WigIteratorError("BED file " + fd.name + " not sorted by chrom") chromsSeen.add(e.chrom) # all good.. yield e prev = e
def wigIterator(fd, verbose=False, sortedby=None, scoreType=int): # peak at the first line to see if it's a regular wig, or # fixed-step wig fh = openFD(fd) at = fh.tell() line = None while(line is None): l = fh.readline() if l.strip() != "": line = l fh.seek(at) if line.split()[0] == "fixedStep": return fixedWigIterator(fd, verbose, sortedby) else: return regularWigIterator(fd, verbose, sortedby, scoreType=scoreType)
def wigIterator(fd, verbose=False, sortedby=None, scoreType=int): # peak at the first line to see if it's a regular wig, or # fixed-step wig fh = openFD(fd) at = fh.tell() line = None while (line is None): l = fh.readline() if l.strip() != "": line = l fh.seek(at) if line.split()[0] == "fixedStep": return fixedWigIterator(fd, verbose, sortedby) else: return regularWigIterator(fd, verbose, sortedby, scoreType=scoreType)
def fixedWigIterator(fd, verbose=False, sortedby=None, scoreType=int): """ @summary: """ fh = openFD(fd) if verbose: try: pind = ProgressIndicator(totalToDo=os.path.getsize(fh.name), messagePrefix="completed", messageSuffix="of processing " + fh.name) except AttributeError: sys.stderr.write("WigIterator -- warning: " + "unable to show progress for stream") verbose = False chromsSeen = set() prev = None # NUMBERS = set(['1','2','3','4','5','6','7','8','9','0','.']) currentChrom, at, step = None, None, None for line in fh: line = line.strip() if line == "": continue if line[0] == 't' or line[0] == 'f': parts = line.split() if parts[0] == "track": continue elif parts[0] == "fixedStep": currentChrom = parts[1].split("=")[1] at = int(parts[2].split("=")[1]) step = int(parts[3].split("=")[1]) else: val = float(line) e = GenomicInterval(currentChrom, at, at + step, None, val, scoreType=scoreType) # on same chrom as the prev item, make sure order is right if prev is not None and sortedby is not None and e.chrom == prev.chrom: if sortedby == ITERATOR_SORTED_START and prev.start > e.start: raise WigIteratorError( "Wig file " + fd.name + " not sorted by start index - saw item " + str(prev) + " before " + str(e)) # starting a new chrom.. make sure we haven't already seen it if prev is not None and prev.chrom != e.chrom: if (sortedby == ITERATOR_SORTED_START) and\ (e.chrom in chromsSeen or prev.chrom > e.chrom): raise WigIteratorError("Wig file " + fd.name + " not sorted by chrom") chromsSeen.add(e.chrom) # all good.. yield e prev = e at += step if verbose: pind.done = fh.tell() pind.showProgress()