nodes: 0, module; 1, function_definition; 2, function_name:BEDIterator; 3, parameters; 4, block; 5, identifier:filehandle; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, if_statement; 15, for_statement; 16, identifier:sortedby; 17, None; 18, identifier:verbose; 19, False; 20, identifier:scoreType; 21, identifier:int; 22, identifier:dropAfter; 23, None; 24, comment:"""
    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.
    """; 25, assignment; 26, assignment; 27, comparison_operator:type(filehandle).__name__ == "str"; 28, block; 29, identifier:verbose; 30, block; 31, identifier:line; 32, identifier:filehandle; 33, block; 34, identifier:chromsSeen; 35, call; 36, identifier:prev; 37, None; 38, attribute; 39, string:"str"; 40, expression_statement; 41, try_statement; 42, if_statement; 43, if_statement; 44, try_statement; 45, comment:# sorting by name?; 46, if_statement; 47, comment:# first item; 48, if_statement; 49, comment:# on same chrom as the prev item, make sure order is right; 50, if_statement; 51, comment:# starting a new chrom.. make sure we haven't already seen it; 52, if_statement; 53, comment:# all good..; 54, expression_statement; 55, expression_statement; 56, identifier:set; 57, argument_list; 58, call; 59, identifier:__name__; 60, assignment; 61, block; 62, except_clause; 63, identifier:verbose; 64, block; 65, comparison_operator:line.strip() == ""; 66, block; 67, block; 68, except_clause; 69, parenthesized_expression; 70, block; 71, comparison_operator:prev is None; 72, block; 73, boolean_operator; 74, block; 75, boolean_operator; 76, block; 77, yield; 78, assignment; 79, identifier:type; 80, argument_list; 81, identifier:filehandle; 82, call; 83, expression_statement; 84, as_pattern; 85, block; 86, expression_statement; 87, expression_statement; 88, call; 89, string:""; 90, continue_statement; 91, expression_statement; 92, as_pattern; 93, block; 94, boolean_operator; 95, raise_statement; 96, identifier:prev; 97, None; 98, expression_statement; 99, boolean_operator; 100, comparison_operator:e.chrom == prev.chrom; 101, if_statement; 102, if_statement; 103, comparison_operator:prev is not None; 104, comparison_operator:prev.chrom != e.chrom; 105, if_statement; 106, expression_statement; 107, identifier:e; 108, identifier:prev; 109, identifier:e; 110, identifier:filehandle; 111, identifier:open; 112, argument_list; 113, assignment; 114, tuple; 115, as_pattern_target; 116, expression_statement; 117, expression_statement; 118, assignment; 119, call; 120, attribute; 121, argument_list; 122, assignment; 123, identifier:GenomicIntervalError; 124, as_pattern_target; 125, raise_statement; 126, parenthesized_expression; 127, parenthesized_expression; 128, call; 129, call; 130, comparison_operator:prev is not None; 131, comparison_operator:sortedby is not None; 132, attribute; 133, attribute; 134, boolean_operator; 135, block; 136, boolean_operator; 137, block; 138, identifier:prev; 139, None; 140, attribute; 141, attribute; 142, boolean_operator; 143, block; 144, call; 145, identifier:filehandle; 146, identifier:pind; 147, call; 148, identifier:AttributeError; 149, identifier:OSError; 150, identifier:e; 151, call; 152, assignment; 153, attribute; 154, call; 155, attribute; 156, argument_list; 157, identifier:line; 158, identifier:strip; 159, identifier:e; 160, call; 161, identifier:e; 162, call; 163, boolean_operator; 164, comparison_operator:prev.name > e.name; 165, identifier:BEDError; 166, argument_list; 167, attribute; 168, argument_list; 169, identifier:prev; 170, None; 171, identifier:sortedby; 172, None; 173, identifier:e; 174, identifier:chrom; 175, identifier:prev; 176, identifier:chrom; 177, comparison_operator:sortedby == ITERATOR_SORTED_START; 178, comparison_operator:prev.start > e.start; 179, raise_statement; 180, comparison_operator:sortedby == ITERATOR_SORTED_END; 181, comparison_operator:prev.end > e.end; 182, raise_statement; 183, identifier:prev; 184, identifier:chrom; 185, identifier:e; 186, identifier:chrom; 187, parenthesized_expression; 188, line_continuation:\; 189, parenthesized_expression; 190, try_statement; 191, raise_statement; 192, attribute; 193, argument_list; 194, identifier:ProgressIndicator; 195, argument_list; 196, attribute; 197, argument_list; 198, identifier:verbose; 199, False; 200, identifier:pind; 201, identifier:done; 202, attribute; 203, argument_list; 204, identifier:pind; 205, identifier:showProgress; 206, identifier:parseBEDString; 207, argument_list; 208, identifier:BEDError; 209, argument_list; 210, comparison_operator:sortedby == ITERATOR_SORTED_NAME; 211, comparison_operator:prev is not None; 212, attribute; 213, attribute; 214, binary_operator:"bed file " + filehandle.name +
                       " not sorted by element name" +
                       " found " + e.name + " after " +
                       prev.name; 215, identifier:chromsSeen; 216, identifier:add; 217, attribute; 218, identifier:sortedby; 219, identifier:ITERATOR_SORTED_START; 220, attribute; 221, attribute; 222, call; 223, identifier:sortedby; 224, identifier:ITERATOR_SORTED_END; 225, attribute; 226, attribute; 227, call; 228, boolean_operator; 229, boolean_operator; 230, block; 231, except_clause; 232, call; 233, identifier:chromsSeen; 234, identifier:add; 235, attribute; 236, keyword_argument; 237, keyword_argument; 238, keyword_argument; 239, attribute; 240, identifier:write; 241, binary_operator:"BEDIterator -- warning: " +
                         "unable to show progress for stream"; 242, identifier:filehandle; 243, identifier:tell; 244, identifier:line; 245, identifier:scoreType; 246, keyword_argument; 247, binary_operator:str(e) + " on line " + line; 248, identifier:sortedby; 249, identifier:ITERATOR_SORTED_NAME; 250, identifier:prev; 251, None; 252, identifier:prev; 253, identifier:name; 254, identifier:e; 255, identifier:name; 256, binary_operator:"bed file " + filehandle.name +
                       " not sorted by element name" +
                       " found " + e.name + " after "; 257, attribute; 258, identifier:e; 259, identifier:chrom; 260, identifier:prev; 261, identifier:start; 262, identifier:e; 263, identifier:start; 264, identifier:BEDError; 265, argument_list; 266, identifier:prev; 267, identifier:end; 268, identifier:e; 269, identifier:end; 270, identifier:BEDError; 271, argument_list; 272, boolean_operator; 273, comparison_operator:sortedby == ITERATOR_SORTED_CHROM; 274, comparison_operator:e.chrom in chromsSeen; 275, comparison_operator:prev.chrom > e.chrom; 276, expression_statement; 277, identifier:AttributeError; 278, block; 279, identifier:BEDError; 280, argument_list; 281, identifier:e; 282, identifier:chrom; 283, identifier:totalToDo; 284, call; 285, identifier:messagePrefix; 286, string:"completed"; 287, identifier:messageSuffix; 288, binary_operator:"of processing " +
                                               filehandle.name; 289, identifier:sys; 290, identifier:stderr; 291, string:"BEDIterator -- warning: "; 292, string:"unable to show progress for stream"; 293, identifier:dropAfter; 294, identifier:dropAfter; 295, binary_operator:str(e) + " on line "; 296, identifier:line; 297, binary_operator:"bed file " + filehandle.name +
                       " not sorted by element name" +
                       " found " + e.name; 298, string:" after "; 299, identifier:prev; 300, identifier:name; 301, binary_operator:"bed file " + filehandle.name +
                         " not sorted by start index - saw item " +
                         str(prev) + " before " + str(e); 302, binary_operator:"bed file " + filehandle.name +
                         " not sorted by end index - saw item " +
                         str(prev) + " before " + str(e); 303, comparison_operator:sortedby == ITERATOR_SORTED_START; 304, comparison_operator:sortedby == ITERATOR_SORTED_END; 305, identifier:sortedby; 306, identifier:ITERATOR_SORTED_CHROM; 307, attribute; 308, identifier:chromsSeen; 309, attribute; 310, attribute; 311, assignment; 312, expression_statement; 313, binary_operator:"BED file " + e_fn + " not sorted by chrom"; 314, attribute; 315, argument_list; 316, string:"of processing "; 317, attribute; 318, call; 319, string:" on line "; 320, binary_operator:"bed file " + filehandle.name +
                       " not sorted by element name" +
                       " found "; 321, attribute; 322, binary_operator:"bed file " + filehandle.name +
                         " not sorted by start index - saw item " +
                         str(prev) + " before "; 323, call; 324, binary_operator:"bed file " + filehandle.name +
                         " not sorted by end index - saw item " +
                         str(prev) + " before "; 325, call; 326, identifier:sortedby; 327, identifier:ITERATOR_SORTED_START; 328, identifier:sortedby; 329, identifier:ITERATOR_SORTED_END; 330, identifier:e; 331, identifier:chrom; 332, identifier:prev; 333, identifier:chrom; 334, identifier:e; 335, identifier:chrom; 336, identifier:e_fn; 337, attribute; 338, assignment; 339, binary_operator:"BED file " + e_fn; 340, string:" not sorted by chrom"; 341, attribute; 342, identifier:getsize; 343, attribute; 344, identifier:filehandle; 345, identifier:name; 346, identifier:str; 347, argument_list; 348, binary_operator:"bed file " + filehandle.name +
                       " not sorted by element name"; 349, string:" found "; 350, identifier:e; 351, identifier:name; 352, binary_operator:"bed file " + filehandle.name +
                         " not sorted by start index - saw item " +
                         str(prev); 353, string:" before "; 354, identifier:str; 355, argument_list; 356, binary_operator:"bed file " + filehandle.name +
                         " not sorted by end index - saw item " +
                         str(prev); 357, string:" before "; 358, identifier:str; 359, argument_list; 360, identifier:filehandle; 361, identifier:name; 362, identifier:e_fn; 363, string:"UNNAMED STREAM"; 364, string:"BED file "; 365, identifier:e_fn; 366, identifier:os; 367, identifier:path; 368, identifier:filehandle; 369, identifier:name; 370, identifier:e; 371, binary_operator:"bed file " + filehandle.name; 372, string:" not sorted by element name"; 373, binary_operator:"bed file " + filehandle.name +
                         " not sorted by start index - saw item "; 374, call; 375, identifier:e; 376, binary_operator:"bed file " + filehandle.name +
                         " not sorted by end index - saw item "; 377, call; 378, identifier:e; 379, string:"bed file "; 380, attribute; 381, binary_operator:"bed file " + filehandle.name; 382, string:" not sorted by start index - saw item "; 383, identifier:str; 384, argument_list; 385, binary_operator:"bed file " + filehandle.name; 386, string:" not sorted by end index - saw item "; 387, identifier:str; 388, argument_list; 389, identifier:filehandle; 390, identifier:name; 391, string:"bed file "; 392, attribute; 393, identifier:prev; 394, string:"bed file "; 395, attribute; 396, identifier:prev; 397, identifier:filehandle; 398, identifier:name; 399, identifier:filehandle; 400, identifier:name
code: 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