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          i = self.service.get_results(self.path, params, "tsv", self.query.views)
     23          return i
     24
     25 -    def __str__(self):
     26          lines = [line for line in self]
     27          return "\n".join(self._header + lines)
     28
     29 -    def __iter__(self):
     30          return self
     31
     32 -    def next(self):
     33          line = self.it.next()
     34          while line and line.startswith("#") or line.startswith("track"):
     35              self._header.append(line)
     36              line = self.it.next()
     37          if line:
     38              return line
     39          raise StopIteration
     40
     41 -class GFF3Iterator(object):
     42
     43      GFF3_PATHS = {}
     44
     45 -    def __init__(self, service, query):
     46          if service.root not in GFF3Iterator.GFF3_PATHS:
     47              GFF3Iterator.GFF3_PATHS[service.root] = service.resolve_service_path("query.gff3")
     48          self.path = GFF3Iterator.GFF3_PATHS[service.root]
     49          self.service = service
     50          self.query = query
     51          self._header = []
     52          self.it = self._get_iter()
     53
     54 -    def header(self):
     55          return "\n".join(self._header)
     56
     57 -    def _get_iter(self):
     58          params = self.query.to_query_params()
     59          i = self.service.get_results(self.path, params, "tsv", self.query.views)
     60          return i
     61
     62 -    def __str__(self):
     63          lines = [line for line in self]
     64          return "\n".join(self._header + lines)
     65
     66 -    def __iter__(self):
     67          return self
     68
     69 -    def next(self):
     70          line = self.it.next()
     71          while line and line.startswith("#"):
     72              self._header.append(line)
     73              line = self.it.next()
     74          if line:
     75              return line
     76          raise StopIteration
     77
     78 -class FastaIterator(object):
     79
     80      FASTA_PATHS = {}
     81
     82 -    def __init__(self, service, query):
     83          if service.root not in FastaIterator.FASTA_PATHS:
     84              FastaIterator.FASTA_PATHS[service.root] = service.resolve_service_path("query.fasta")
     85          self.path = FastaIterator.FASTA_PATHS[service.root]
     86          self.service = service
     87          self.query = query
     88          self.it = self._get_iter()
     89          self._holdover = None
     90
     91 -    def _get_iter(self):
     92          params = self.query.to_query_params()
     93          i = self.service.get_results(self.path, params, "tsv", self.query.views)
     94          return i
     95
     96 -    def __str__(self):
     97          records = [rec for rec in self]
     98          return "\n".join(records)
     99
    100 -    def __iter__(self):
    101          return self
    102
    103 -    def next(self):
    104          lines = []
    105          if self._holdover is not None:
    106              lines.append(self._holdover)
    107              self._holdover = None
    108          else:
    109              try:
    110                  lines.append(self.it.next())
    111              except StopIteration:
    112                  pass
    113
    114          try:
    115              while True:
    116                  line = self.it.next()
    117                  if line.startswith(">"):
    118                      self._holdover = line
    119                      break
    120                  lines.append(line)
    121          except StopIteration:
    122              pass
    123
    124          if len(lines):
    125              return "\n".join(lines)
    126
    127          if self._holdover:
    128              ret = self._holdover
    129              self._holdover = None
    130              return self._holdover
    131
    132          raise StopIteration
    133