nodes: 0, module; 1, function_definition; 2, function_name:load_vcf; 3, parameters; 4, block; 5, identifier:path; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, default_parameter; 13, default_parameter; 14, default_parameter; 15, expression_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, comment:# Loading a local file.; 20, comment:# The file will be opened twice: first to parse the header with pyvcf, then; 21, comment:# by pandas to read the data.; 22, comment:# PyVCF reads the metadata immediately and stops at the first line with; 23, comment:# data. We can close the file after that.; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, expression_statement; 28, if_statement; 29, return_statement; 30, identifier:genome; 31, None; 32, identifier:reference_vcf_key; 33, string:"reference"; 34, identifier:only_passing; 35, True; 36, identifier:allow_extended_nucleotides; 37, False; 38, identifier:include_info; 39, True; 40, identifier:chunk_size; 41, binary_operator:10 ** 5; 42, identifier:max_variants; 43, None; 44, identifier:sort_key; 45, identifier:variant_ascending_position_sort_key; 46, identifier:distinct; 47, True; 48, comment:"""
      Load reference name and Variant objects from the given VCF filename.
  
      Currently only local files are supported by this function (no http). If you
      call this on an HTTP URL, it will fall back to `load_vcf`.
  
      Parameters
      ----------
  
      path : str
          Path to VCF (*.vcf) or compressed VCF (*.vcf.gz).
  
      genome : {pyensembl.Genome, reference name, Ensembl version int}, optional
          Optionally pass in a PyEnsembl Genome object, name of reference, or
          PyEnsembl release version to specify the reference associated with a
          VCF (otherwise infer reference from VCF using reference_vcf_key)
  
      reference_vcf_key : str, optional
          Name of metadata field which contains path to reference FASTA
          file (default = 'reference')
  
      only_passing : boolean, optional
          If true, any entries whose FILTER field is not one of "." or "PASS" is
          dropped.
  
      allow_extended_nucleotides : boolean, default False
          Allow characters other that A,C,T,G in the ref and alt strings.
  
      include_info : boolean, default True
          Whether to parse the INFO and per-sample columns. If you don't need
          these, set to False for faster parsing.
  
      chunk_size: int, optional
          Number of records to load in memory at once.
  
      max_variants : int, optional
          If specified, return only the first max_variants variants.
  
      sort_key : fn
          Function which maps each element to a sorting criterion.
          Set to None to not to sort the variants.
  
      distinct : boolean, default True
          Don't keep repeated variants
      """; 49, call; 50, assignment; 51, boolean_operator; 52, comment:# pandas.read_table nominally supports HTTP, but it tends to crash on; 53, comment:# large files and does not support gzip. Switching to the python-based; 54, comment:# implementation of read_table (with engine="python") helps with some; 55, comment:# issues but introduces a new set of problems (e.g. the dtype parameter; 56, comment:# is not accepted). For these reasons, we're currently not attempting; 57, comment:# to load VCFs over HTTP with pandas directly, and instead download it; 58, comment:# to a temporary file and open that.; 59, block; 60, assignment; 61, call; 62, assignment; 63, assignment; 64, identifier:include_info; 65, block; 66, else_clause; 67, call; 68, integer:10; 69, integer:5; 70, identifier:require_string; 71, argument_list; 72, identifier:parsed_path; 73, call; 74, attribute; 75, comparison_operator:parsed_path.scheme.lower() != "file"; 76, expression_statement; 77, try_statement; 78, identifier:handle; 79, call; 80, attribute; 81, argument_list; 82, identifier:genome; 83, call; 84, identifier:df_iterator; 85, call; 86, function_definition; 87, block; 88, identifier:dataframes_to_variant_collection; 89, argument_list; 90, identifier:path; 91, string:"Path or URL to VCF"; 92, identifier:parse_url_or_path; 93, argument_list; 94, identifier:parsed_path; 95, identifier:scheme; 96, call; 97, string:"file"; 98, assignment; 99, comment:# The downloaded file has no file extension, which confuses pyvcf; 100, comment:# for gziped files in Python 3. We rename it to have the correct; 101, comment:# file extension.; 102, block; 103, finally_clause; 104, identifier:PyVCFReaderFromPathOrURL; 105, argument_list; 106, identifier:handle; 107, identifier:close; 108, identifier:infer_genome_from_vcf; 109, argument_list; 110, identifier:read_vcf_into_dataframe; 111, argument_list; 112, function_name:sample_info_parser; 113, parameters; 114, block; 115, expression_statement; 116, identifier:df_iterator; 117, keyword_argument; 118, keyword_argument; 119, keyword_argument; 120, keyword_argument; 121, keyword_argument; 122, keyword_argument; 123, keyword_argument; 124, keyword_argument; 125, identifier:path; 126, attribute; 127, argument_list; 128, tuple_pattern; 129, call; 130, expression_statement; 131, expression_statement; 132, expression_statement; 133, return_statement; 134, block; 135, identifier:path; 136, identifier:genome; 137, attribute; 138, identifier:reference_vcf_key; 139, identifier:path; 140, keyword_argument; 141, keyword_argument; 142, keyword_argument; 143, identifier:unparsed_sample_info_strings; 144, identifier:format_string; 145, expression_statement; 146, return_statement; 147, assignment; 148, identifier:source_path; 149, identifier:path; 150, identifier:info_parser; 151, conditional_expression:handle.vcf_reader._parse_info if include_info else None; 152, identifier:only_passing; 153, identifier:only_passing; 154, identifier:max_variants; 155, identifier:max_variants; 156, identifier:sample_names; 157, conditional_expression:handle.vcf_reader.samples if include_info else None; 158, identifier:sample_info_parser; 159, identifier:sample_info_parser; 160, identifier:variant_kwargs; 161, dictionary; 162, identifier:variant_collection_kwargs; 163, dictionary; 164, attribute; 165, identifier:lower; 166, identifier:filename; 167, identifier:headers; 168, attribute; 169, argument_list; 170, assignment; 171, call; 172, assignment; 173, call; 174, expression_statement; 175, expression_statement; 176, identifier:handle; 177, identifier:vcf_reader; 178, identifier:include_info; 179, identifier:include_info; 180, identifier:sample_names; 181, conditional_expression:handle.vcf_reader.samples if include_info else None; 182, identifier:chunk_size; 183, identifier:chunk_size; 184, comment:"""
              Given a format string like "GT:AD:ADP:DP:FS"
              and a list of sample info strings where each entry is like
              "0/1:3,22:T=3,G=22:25:33", return a dict that maps:
              sample name -> field name -> value. Uses pyvcf to parse the fields.
              """; 185, call; 186, identifier:sample_info_parser; 187, None; 188, attribute; 189, identifier:include_info; 190, None; 191, attribute; 192, identifier:include_info; 193, None; 194, pair; 195, pair; 196, pair; 197, pair; 198, identifier:parsed_path; 199, identifier:scheme; 200, attribute; 201, identifier:urlretrieve; 202, identifier:path; 203, identifier:new_filename; 204, binary_operator:"%s.%s" % (
                  filename, parsed_path.path.split(".")[-1]); 205, attribute; 206, argument_list; 207, identifier:filename; 208, identifier:new_filename; 209, identifier:load_vcf; 210, argument_list; 211, call; 212, call; 213, attribute; 214, identifier:include_info; 215, None; 216, identifier:pyvcf_calls_to_sample_info_list; 217, argument_list; 218, attribute; 219, identifier:_parse_info; 220, attribute; 221, identifier:samples; 222, string; 223, identifier:genome; 224, string; 225, identifier:allow_extended_nucleotides; 226, string; 227, identifier:sort_key; 228, string; 229, identifier:distinct; 230, identifier:urllib; 231, identifier:request; 232, string:"%s.%s"; 233, tuple; 234, identifier:os; 235, identifier:rename; 236, identifier:filename; 237, identifier:new_filename; 238, identifier:filename; 239, keyword_argument; 240, keyword_argument; 241, keyword_argument; 242, keyword_argument; 243, keyword_argument; 244, keyword_argument; 245, keyword_argument; 246, keyword_argument; 247, keyword_argument; 248, attribute; 249, argument_list; 250, attribute; 251, argument_list; 252, attribute; 253, identifier:samples; 254, call; 255, identifier:handle; 256, identifier:vcf_reader; 257, identifier:handle; 258, identifier:vcf_reader; 259, string_content:ensembl; 260, string_content:allow_extended_nucleotides; 261, string_content:sort_key; 262, string_content:distinct; 263, identifier:filename; 264, subscript; 265, identifier:genome; 266, identifier:genome; 267, identifier:reference_vcf_key; 268, identifier:reference_vcf_key; 269, identifier:only_passing; 270, identifier:only_passing; 271, identifier:allow_extended_nucleotides; 272, identifier:allow_extended_nucleotides; 273, identifier:include_info; 274, identifier:include_info; 275, identifier:chunk_size; 276, identifier:chunk_size; 277, identifier:max_variants; 278, identifier:max_variants; 279, identifier:sort_key; 280, identifier:sort_key; 281, identifier:distinct; 282, identifier:distinct; 283, identifier:logger; 284, identifier:info; 285, string:"Removing temporary file: %s"; 286, identifier:filename; 287, identifier:os; 288, identifier:unlink; 289, identifier:filename; 290, identifier:handle; 291, identifier:vcf_reader; 292, attribute; 293, argument_list; 294, call; 295, unary_operator; 296, attribute; 297, identifier:_parse_samples; 298, identifier:unparsed_sample_info_strings; 299, identifier:format_string; 300, None; 301, attribute; 302, argument_list; 303, integer:1; 304, identifier:handle; 305, identifier:vcf_reader; 306, attribute; 307, identifier:split; 308, string:"."; 309, identifier:parsed_path; 310, identifier:path
code: def load_vcf(
          path,
          genome=None,
          reference_vcf_key="reference",
          only_passing=True,
          allow_extended_nucleotides=False,
          include_info=True,
          chunk_size=10 ** 5,
          max_variants=None,
          sort_key=variant_ascending_position_sort_key,
          distinct=True):
      """
      Load reference name and Variant objects from the given VCF filename.
  
      Currently only local files are supported by this function (no http). If you
      call this on an HTTP URL, it will fall back to `load_vcf`.
  
      Parameters
      ----------
  
      path : str
          Path to VCF (*.vcf) or compressed VCF (*.vcf.gz).
  
      genome : {pyensembl.Genome, reference name, Ensembl version int}, optional
          Optionally pass in a PyEnsembl Genome object, name of reference, or
          PyEnsembl release version to specify the reference associated with a
          VCF (otherwise infer reference from VCF using reference_vcf_key)
  
      reference_vcf_key : str, optional
          Name of metadata field which contains path to reference FASTA
          file (default = 'reference')
  
      only_passing : boolean, optional
          If true, any entries whose FILTER field is not one of "." or "PASS" is
          dropped.
  
      allow_extended_nucleotides : boolean, default False
          Allow characters other that A,C,T,G in the ref and alt strings.
  
      include_info : boolean, default True
          Whether to parse the INFO and per-sample columns. If you don't need
          these, set to False for faster parsing.
  
      chunk_size: int, optional
          Number of records to load in memory at once.
  
      max_variants : int, optional
          If specified, return only the first max_variants variants.
  
      sort_key : fn
          Function which maps each element to a sorting criterion.
          Set to None to not to sort the variants.
  
      distinct : boolean, default True
          Don't keep repeated variants
      """
  
      require_string(path, "Path or URL to VCF")
      parsed_path = parse_url_or_path(path)
  
      if parsed_path.scheme and parsed_path.scheme.lower() != "file":
          # pandas.read_table nominally supports HTTP, but it tends to crash on
          # large files and does not support gzip. Switching to the python-based
          # implementation of read_table (with engine="python") helps with some
          # issues but introduces a new set of problems (e.g. the dtype parameter
          # is not accepted). For these reasons, we're currently not attempting
          # to load VCFs over HTTP with pandas directly, and instead download it
          # to a temporary file and open that.
  
          (filename, headers) = urllib.request.urlretrieve(path)
          try:
              # The downloaded file has no file extension, which confuses pyvcf
              # for gziped files in Python 3. We rename it to have the correct
              # file extension.
              new_filename = "%s.%s" % (
                  filename, parsed_path.path.split(".")[-1])
              os.rename(filename, new_filename)
              filename = new_filename
              return load_vcf(
                  filename,
                  genome=genome,
                  reference_vcf_key=reference_vcf_key,
                  only_passing=only_passing,
                  allow_extended_nucleotides=allow_extended_nucleotides,
                  include_info=include_info,
                  chunk_size=chunk_size,
                  max_variants=max_variants,
                  sort_key=sort_key,
                  distinct=distinct)
          finally:
              logger.info("Removing temporary file: %s", filename)
              os.unlink(filename)
  
      # Loading a local file.
      # The file will be opened twice: first to parse the header with pyvcf, then
      # by pandas to read the data.
  
      # PyVCF reads the metadata immediately and stops at the first line with
      # data. We can close the file after that.
      handle = PyVCFReaderFromPathOrURL(path)
      handle.close()
      genome = infer_genome_from_vcf(
          genome,
          handle.vcf_reader,
          reference_vcf_key)
  
      df_iterator = read_vcf_into_dataframe(
          path,
          include_info=include_info,
          sample_names=handle.vcf_reader.samples if include_info else None,
          chunk_size=chunk_size)
  
      if include_info:
          def sample_info_parser(unparsed_sample_info_strings, format_string):
              """
              Given a format string like "GT:AD:ADP:DP:FS"
              and a list of sample info strings where each entry is like
              "0/1:3,22:T=3,G=22:25:33", return a dict that maps:
              sample name -> field name -> value. Uses pyvcf to parse the fields.
              """
              return pyvcf_calls_to_sample_info_list(
                  handle.vcf_reader._parse_samples(
                      unparsed_sample_info_strings, format_string, None))
      else:
          sample_info_parser = None
  
      return dataframes_to_variant_collection(
          df_iterator,
          source_path=path,
          info_parser=handle.vcf_reader._parse_info if include_info else None,
          only_passing=only_passing,
          max_variants=max_variants,
          sample_names=handle.vcf_reader.samples if include_info else None,
          sample_info_parser=sample_info_parser,
          variant_kwargs={
              'ensembl': genome,
              'allow_extended_nucleotides': allow_extended_nucleotides},
          variant_collection_kwargs={
              'sort_key': sort_key,
              'distinct': distinct})