<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def BEDIterator(filehandle, sortedby=None, verbose=False, scoreType=int, dropAfter=None):
 """ Get an iterator for a BED file :param filehandle: this can be either a string, or a stream-like object. In the former case, it is treated as a filename. The format of the file/stream must be BED. :param sortedby: if None, order is not checked. if == ITERATOR_SORTED_START, elements in file must be sorted by chrom and start index (an exception is raised if they are not) if == ITERATOR_SORTED_END, element must be sorted by chrom and end index. :param verbose: if True, output additional progress messages to stderr :param scoreType: The data type for scores (the fifth column) in the BED file. :param dropAfter: an int indicating that any fields after and including this field should be ignored as they don't conform to the BED format. By default, None, meaning we use all fields. Index from zero. :return: iterator where subsequent calls to next() yield the next BED element in the stream as a GenomicInterval object. """

  chromsSeen = set()
  prev = None
  if type(filehandle).__name__ == "str":
    filehandle = open(filehandle)

  if verbose:
    try:
      pind = ProgressIndicator(totalToDo=os.path.getsize(filehandle.name),
                               messagePrefix="completed",
                               messageSuffix="of processing " +
                                             filehandle.name)
    except (AttributeError, OSError) as e:
      sys.stderr.write("BEDIterator -- warning: " +
                       "unable to show progress for stream")
      verbose = False

  for line in filehandle:
    if verbose:
      pind.done = filehandle.tell()
      pind.showProgress()

    if line.strip() == "":
      continue
    try:
      e = parseBEDString(line, scoreType, dropAfter=dropAfter)
    except GenomicIntervalError as e:
      raise BEDError(str(e) + " on line " + line)

    # sorting by name?
    if ((sortedby == ITERATOR_SORTED_NAME and prev is not None) and
       (prev.name > e.name)):
      raise BEDError("bed file " + filehandle.name +
                     " not sorted by element name" +
                     " found " + e.name + " after " +
                     prev.name)

    # first item
    if prev is None:
      chromsSeen.add(e.chrom)

    # 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 BEDError("bed file " + filehandle.name +
                       " not sorted by start index - saw item " +
                       str(prev) + " before " + str(e))
      if sortedby == ITERATOR_SORTED_END and prev.end > e.end:
        raise BEDError("bed file " + filehandle.name +
                       " not sorted by end 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 or
          sortedby == ITERATOR_SORTED_END or
          sortedby == ITERATOR_SORTED_CHROM) and\
         (e.chrom in chromsSeen or prev.chrom > e.chrom):
        try:
          e_fn = filehandle.name
        except AttributeError:
          e_fn = "UNNAMED STREAM"
        raise BEDError("BED file " + e_fn + " not sorted by chrom")
      chromsSeen.add(e.chrom)

    # all good..
    yield e
    prev = e