nodes: 0, module; 1, function_definition; 2, function_name:load_maf; 3, parameters; 4, block; 5, identifier:path; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, expression_statement; 12, comment:# pylint: disable=no-member; 13, comment:# pylint gets confused by read_csv inside load_maf_dataframe; 14, expression_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, expression_statement; 19, for_statement; 20, return_statement; 21, identifier:optional_cols; 22, list; 23, identifier:sort_key; 24, identifier:variant_ascending_position_sort_key; 25, identifier:distinct; 26, True; 27, identifier:raise_on_error; 28, True; 29, identifier:encoding; 30, None; 31, comment:"""
      Load reference name and Variant objects from MAF filename.
  
      Parameters
      ----------
  
      path : str
          Path to MAF (*.maf).
  
      optional_cols : list, optional
          A list of MAF columns to include as metadata if they are present in the MAF.
          Does not result in an error if those columns are not present.
  
      sort_key : fn
          Function which maps each element to a sorting criterion.
          Set to None to not to sort the variants.
  
      distinct : bool
          Don't keep repeated variants
  
      raise_on_error : bool
          Raise an exception upon encountering an error or just log a warning.
  
      encoding : str, optional
          Encoding to use for UTF when reading MAF file.
      """; 32, assignment; 33, boolean_operator; 34, block; 35, assignment; 36, assignment; 37, assignment; 38, pattern_list; 39, call; 40, block; 41, call; 42, identifier:maf_df; 43, call; 44, comparison_operator:len(maf_df) == 0; 45, identifier:raise_on_error; 46, raise_statement; 47, identifier:ensembl_objects; 48, dictionary; 49, identifier:variants; 50, list; 51, identifier:metadata; 52, dictionary; 53, identifier:_; 54, identifier:x; 55, attribute; 56, argument_list; 57, expression_statement; 58, if_statement; 59, expression_statement; 60, expression_statement; 61, comment:# it's possible in a MAF file to have multiple Ensembl releases; 62, comment:# mixed in a single MAF file (the genome assembly is; 63, comment:# specified by the NCBI_Build column); 64, expression_statement; 65, if_statement; 66, comment:# have to try both Tumor_Seq_Allele1 and Tumor_Seq_Allele2; 67, comment:# to figure out which is different from the reference allele; 68, if_statement; 69, expression_statement; 70, comment:# keep metadata about the variant and its TCGA annotation; 71, expression_statement; 72, for_statement; 73, expression_statement; 74, identifier:VariantCollection; 75, argument_list; 76, identifier:load_maf_dataframe; 77, argument_list; 78, call; 79, integer:0; 80, call; 81, identifier:maf_df; 82, identifier:iterrows; 83, assignment; 84, call; 85, block; 86, assignment; 87, assignment; 88, assignment; 89, comparison_operator:ncbi_build in ensembl_objects; 90, block; 91, else_clause; 92, comparison_operator:x.Tumor_Seq_Allele1 != ref; 93, block; 94, else_clause; 95, assignment; 96, assignment; 97, identifier:optional_col; 98, identifier:optional_cols; 99, block; 100, call; 101, keyword_argument; 102, keyword_argument; 103, keyword_argument; 104, keyword_argument; 105, identifier:path; 106, keyword_argument; 107, keyword_argument; 108, identifier:len; 109, argument_list; 110, identifier:ValueError; 111, argument_list; 112, identifier:contig; 113, attribute; 114, identifier:isnull; 115, argument_list; 116, expression_statement; 117, if_statement; 118, identifier:start_pos; 119, attribute; 120, identifier:ref; 121, attribute; 122, identifier:ncbi_build; 123, attribute; 124, identifier:ncbi_build; 125, identifier:ensembl_objects; 126, expression_statement; 127, block; 128, attribute; 129, identifier:ref; 130, expression_statement; 131, block; 132, identifier:variant; 133, call; 134, subscript; 135, dictionary; 136, if_statement; 137, attribute; 138, argument_list; 139, identifier:variants; 140, identifier:variants; 141, identifier:source_to_metadata_dict; 142, dictionary; 143, identifier:sort_key; 144, identifier:sort_key; 145, identifier:distinct; 146, identifier:distinct; 147, identifier:raise_on_error; 148, identifier:raise_on_error; 149, identifier:encoding; 150, identifier:encoding; 151, identifier:maf_df; 152, binary_operator:"Empty MAF file %s" % path; 153, identifier:x; 154, identifier:Chromosome; 155, identifier:contig; 156, assignment; 157, identifier:raise_on_error; 158, block; 159, else_clause; 160, identifier:x; 161, identifier:Start_Position; 162, identifier:x; 163, identifier:Reference_Allele; 164, identifier:x; 165, identifier:NCBI_Build; 166, assignment; 167, if_statement; 168, expression_statement; 169, expression_statement; 170, identifier:x; 171, identifier:Tumor_Seq_Allele1; 172, assignment; 173, if_statement; 174, expression_statement; 175, identifier:Variant; 176, argument_list; 177, identifier:metadata; 178, identifier:variant; 179, pair; 180, pair; 181, pair; 182, pair; 183, pair; 184, pair; 185, pair; 186, pair; 187, pair; 188, comparison_operator:optional_col in x; 189, block; 190, identifier:variants; 191, identifier:append; 192, identifier:variant; 193, pair; 194, string:"Empty MAF file %s"; 195, identifier:path; 196, identifier:error_message; 197, binary_operator:"Invalid contig name: %s" % (contig,); 198, raise_statement; 199, block; 200, identifier:ensembl; 201, subscript; 202, call; 203, block; 204, else_clause; 205, assignment; 206, assignment; 207, identifier:alt; 208, attribute; 209, comparison_operator:x.Tumor_Seq_Allele2 == ref; 210, block; 211, assignment; 212, identifier:contig; 213, identifier:start_pos; 214, call; 215, call; 216, keyword_argument; 217, string; 218, attribute; 219, string; 220, attribute; 221, string; 222, attribute; 223, string; 224, attribute; 225, string; 226, attribute; 227, string; 228, attribute; 229, string; 230, attribute; 231, string; 232, attribute; 233, string; 234, attribute; 235, identifier:optional_col; 236, identifier:x; 237, expression_statement; 238, identifier:path; 239, identifier:metadata; 240, string:"Invalid contig name: %s"; 241, tuple; 242, call; 243, expression_statement; 244, continue_statement; 245, identifier:ensembl_objects; 246, identifier:ncbi_build; 247, identifier:isinstance; 248, argument_list; 249, expression_statement; 250, block; 251, identifier:ensembl; 252, call; 253, subscript; 254, identifier:ensembl; 255, identifier:x; 256, identifier:Tumor_Seq_Allele1; 257, attribute; 258, identifier:ref; 259, expression_statement; 260, if_statement; 261, identifier:alt; 262, attribute; 263, identifier:str; 264, argument_list; 265, identifier:str; 266, argument_list; 267, identifier:ensembl; 268, identifier:ensembl; 269, string_content:Hugo_Symbol; 270, identifier:x; 271, identifier:Hugo_Symbol; 272, string_content:Center; 273, identifier:x; 274, identifier:Center; 275, string_content:Strand; 276, identifier:x; 277, identifier:Strand; 278, string_content:Variant_Classification; 279, identifier:x; 280, identifier:Variant_Classification; 281, string_content:Variant_Type; 282, identifier:x; 283, identifier:Variant_Type; 284, string_content:dbSNP_RS; 285, identifier:x; 286, identifier:dbSNP_RS; 287, string_content:dbSNP_Val_Status; 288, identifier:x; 289, identifier:dbSNP_Val_Status; 290, string_content:Tumor_Sample_Barcode; 291, identifier:x; 292, identifier:Tumor_Sample_Barcode; 293, string_content:Matched_Norm_Sample_Barcode; 294, identifier:x; 295, identifier:Matched_Norm_Sample_Barcode; 296, assignment; 297, identifier:contig; 298, identifier:ValueError; 299, argument_list; 300, call; 301, identifier:ncbi_build; 302, identifier:int; 303, assignment; 304, expression_statement; 305, identifier:infer_genome; 306, argument_list; 307, identifier:ensembl_objects; 308, identifier:ncbi_build; 309, identifier:x; 310, identifier:Tumor_Seq_Allele2; 311, assignment; 312, identifier:raise_on_error; 313, block; 314, else_clause; 315, identifier:x; 316, identifier:Tumor_Seq_Allele2; 317, identifier:ref; 318, identifier:alt; 319, subscript; 320, subscript; 321, identifier:error_message; 322, attribute; 323, argument_list; 324, identifier:reference_name; 325, binary_operator:"B%d" % ncbi_build; 326, assignment; 327, identifier:reference_name; 328, identifier:error_message; 329, parenthesized_expression; 330, raise_statement; 331, block; 332, subscript; 333, identifier:optional_col; 334, identifier:x; 335, identifier:optional_col; 336, identifier:logging; 337, identifier:warn; 338, identifier:error_message; 339, string:"B%d"; 340, identifier:ncbi_build; 341, identifier:reference_name; 342, call; 343, binary_operator:"Both tumor alleles agree with reference %s: %s" % (
                          ref, x,); 344, call; 345, expression_statement; 346, continue_statement; 347, identifier:metadata; 348, identifier:variant; 349, identifier:str; 350, argument_list; 351, string:"Both tumor alleles agree with reference %s: %s"; 352, tuple; 353, identifier:ValueError; 354, argument_list; 355, call; 356, identifier:ncbi_build; 357, identifier:ref; 358, identifier:x; 359, identifier:error_message; 360, attribute; 361, argument_list; 362, identifier:logging; 363, identifier:warn; 364, identifier:error_message
code: def load_maf(
          path,
          optional_cols=[],
          sort_key=variant_ascending_position_sort_key,
          distinct=True,
          raise_on_error=True,
          encoding=None):
      """
      Load reference name and Variant objects from MAF filename.
  
      Parameters
      ----------
  
      path : str
          Path to MAF (*.maf).
  
      optional_cols : list, optional
          A list of MAF columns to include as metadata if they are present in the MAF.
          Does not result in an error if those columns are not present.
  
      sort_key : fn
          Function which maps each element to a sorting criterion.
          Set to None to not to sort the variants.
  
      distinct : bool
          Don't keep repeated variants
  
      raise_on_error : bool
          Raise an exception upon encountering an error or just log a warning.
  
      encoding : str, optional
          Encoding to use for UTF when reading MAF file.
      """
      # pylint: disable=no-member
      # pylint gets confused by read_csv inside load_maf_dataframe
      maf_df = load_maf_dataframe(path, raise_on_error=raise_on_error, encoding=encoding)
  
      if len(maf_df) == 0 and raise_on_error:
          raise ValueError("Empty MAF file %s" % path)
  
      ensembl_objects = {}
      variants = []
      metadata = {}
      for _, x in maf_df.iterrows():
          contig = x.Chromosome
          if isnull(contig):
              error_message = "Invalid contig name: %s" % (contig,)
              if raise_on_error:
                  raise ValueError(error_message)
              else:
                  logging.warn(error_message)
                  continue
  
          start_pos = x.Start_Position
          ref = x.Reference_Allele
  
          # it's possible in a MAF file to have multiple Ensembl releases
          # mixed in a single MAF file (the genome assembly is
          # specified by the NCBI_Build column)
          ncbi_build = x.NCBI_Build
          if ncbi_build in ensembl_objects:
              ensembl = ensembl_objects[ncbi_build]
          else:
              if isinstance(ncbi_build, int):
                  reference_name = "B%d" % ncbi_build
              else:
                  reference_name = str(ncbi_build)
              ensembl = infer_genome(reference_name)
              ensembl_objects[ncbi_build] = ensembl
  
          # have to try both Tumor_Seq_Allele1 and Tumor_Seq_Allele2
          # to figure out which is different from the reference allele
          if x.Tumor_Seq_Allele1 != ref:
              alt = x.Tumor_Seq_Allele1
          else:
              if x.Tumor_Seq_Allele2 == ref:
                  error_message = (
                      "Both tumor alleles agree with reference %s: %s" % (
                          ref, x,))
                  if raise_on_error:
                      raise ValueError(error_message)
                  else:
                      logging.warn(error_message)
                      continue
              alt = x.Tumor_Seq_Allele2
  
          variant = Variant(
              contig,
              start_pos,
              str(ref),
              str(alt),
              ensembl=ensembl)
  
          # keep metadata about the variant and its TCGA annotation
          metadata[variant] = {
              'Hugo_Symbol': x.Hugo_Symbol,
              'Center': x.Center,
              'Strand': x.Strand,
              'Variant_Classification': x.Variant_Classification,
              'Variant_Type': x.Variant_Type,
              'dbSNP_RS': x.dbSNP_RS,
              'dbSNP_Val_Status': x.dbSNP_Val_Status,
              'Tumor_Sample_Barcode': x.Tumor_Sample_Barcode,
              'Matched_Norm_Sample_Barcode': x.Matched_Norm_Sample_Barcode,
          }
          for optional_col in optional_cols:
              if optional_col in x:
                  metadata[variant][optional_col] = x[optional_col]
  
          variants.append(variant)
  
      return VariantCollection(
          variants=variants,
          source_to_metadata_dict={path: metadata},
          sort_key=sort_key,
          distinct=distinct)