interminebio.iterators

Package interminebio :: Module iterators |
[hide private]
[frames] | no frames]

# Source Code for Module interminebio.iterators

      1 -class BedIterator(object):
      2
      3      BED_PATHS = {}
      4
      5 -    def __init__(self, service, query, ucsc_compatible=True):
      6          if service.root not in BedIterator.BED_PATHS:
      7              BedIterator.BED_PATHS[service.root] = service.resolve_service_path("query.bed")
      8          self.path = BedIterator.BED_PATHS[service.root]
      9          self.service = service
     10          self.query = query
     11          self.ucsc_compatible = ucsc_compatible
     12          self._header = []
     13          self.it = self._get_iter()
     14
     15 -    def header(self):
     16          return "\n".join(self._header)
     17
     18 -    def _get_iter(self):
     19          params = self.query.to_query_params()
     20          if not self.ucsc_compatible:
     21              params["ucscCompatible"] = "no"
     22          try:
     23              path = self.query.bed_path
     24          except:
     25              path = self.path
     26          i = self.service.get_results(path, params, "tsv", self.query.views)
     27          return i
     28
     29 -    def __str__(self):
     30          lines = [line for line in self]
     31          return "\n".join(self._header + lines)
     32
     33 -    def __iter__(self):
     34          return self
     35
     36 -    def next(self):
     37          line = self.it.next()
     38          while line and line.startswith("#") or line.startswith("track"):
     39              self._header.append(line)
     40              line = self.it.next()
     41          if line:
     42              return line
     43          raise StopIteration
     44
     45 -class GFF3Iterator(object):
     46
     47      GFF3_PATHS = {}
     48
     49 -    def __init__(self, service, query):
     50          if service.root not in GFF3Iterator.GFF3_PATHS:
     51              GFF3Iterator.GFF3_PATHS[service.root] = service.resolve_service_path("query.gff3")
     52          self.path = GFF3Iterator.GFF3_PATHS[service.root]
     53          self.service = service
     54          self.query = query
     55          self._header = []
     56          self.it = self._get_iter()
     57
     58 -    def header(self):
     59          return "\n".join(self._header)
     60
     61 -    def _get_iter(self):
     62          params = self.query.to_query_params()
     63          try:
     64              path = self.query.gff3_path
     65          except:
     66              path = self.path
     67          i = self.service.get_results(path, params, "tsv", self.query.views)
     68          return i
     69
     70 -    def __str__(self):
     71          lines = [line for line in self]
     72          return "\n".join(self._header + lines)
     73
     74 -    def __iter__(self):
     75          return self
     76
     77 -    def next(self):
     78          line = self.it.next()
     79          while line and line.startswith("#"):
     80              self._header.append(line)
     81              line = self.it.next()
     82          if line:
     83              return line
     84          raise StopIteration
     85
     86 -class FastaIterator(object):
     87
     88      FASTA_PATHS = {}
     89
     90 -    def __init__(self, service, query):
     91          if service.root not in FastaIterator.FASTA_PATHS:
     92              FastaIterator.FASTA_PATHS[service.root] = service.resolve_service_path("query.fasta")
     93          self.path = FastaIterator.FASTA_PATHS[service.root]
     94          self.service = service
     95          self.query = query
     96          self.it = self._get_iter()
     97          self._holdover = None
     98
     99 -    def _get_iter(self):
    100          params = self.query.to_query_params()
    101          try:
    102              path = self.query.fasta_path
    103          except:
    104              path = self.path
    105          i = self.service.get_results(path, params, "tsv", self.query.views)
    106          return i
    107
    108 -    def __str__(self):
    109          records = [rec for rec in self]
    110          return "\n".join(records)
    111
    112 -    def __iter__(self):
    113          return self
    114
    115 -    def next(self):
    116          lines = []
    117          if self._holdover is not None:
    118              lines.append(self._holdover)
    119              self._holdover = None
    120          else:
    121              try:
    122                  lines.append(self.it.next())
    123              except StopIteration:
    124                  pass
    125
    126          try:
    127              while True:
    128                  line = self.it.next()
    129                  if line.startswith(">"):
    130                      self._holdover = line
    131                      break
    132                  lines.append(line)
    133          except StopIteration:
    134              pass
    135
    136          if len(lines):
    137              return "\n".join(lines)
    138
    139          if self._holdover:
    140              ret = self._holdover
    141              self._holdover = None
    142              return self._holdover
    143
    144          raise StopIteration
    145