code: def _check_bam_contigs(in_bam, ref_file, config):
      
      # GATK allows chromosome M to be in multiple locations, skip checking it
      allowed_outoforder = ["chrM", "MT"]
      ref_contigs = [c.name for c in ref.file_contigs(ref_file, config)]
      with pysam.Samfile(in_bam, "rb") as bamfile:
          bam_contigs = [c["SN"] for c in bamfile.header["SQ"]]
      extra_bcs = [x for x in bam_contigs if x not in ref_contigs]
      extra_rcs = [x for x in ref_contigs if x not in bam_contigs]
      problems = []
      warnings = []
      for bc, rc in zip_longest([x for x in bam_contigs if (x not in extra_bcs and
                                                                       x not in allowed_outoforder)],
                                           [x for x in ref_contigs if (x not in extra_rcs and
                                                                       x not in allowed_outoforder)]):
          if bc != rc:
              if bc and rc:
                  problems.append("Reference mismatch. BAM: %s Reference: %s" % (bc, rc))
              elif bc:
                  warnings.append("Extra BAM chromosomes: %s" % bc)
              elif rc:
                  warnings.append("Extra reference chromosomes: %s" % rc)
      for bc in extra_bcs:
          warnings.append("Extra BAM chromosomes: %s" % bc)
      for rc in extra_rcs:
          warnings.append("Extra reference chromosomes: %s" % rc)
      if problems:
          raise ValueError("Unexpected order, name or contig mismatches between input BAM and reference file:\n%s\n"
                           "Setting `bam_clean: remove_extracontigs` in the configuration can often fix this issue."
                           % "\n".join(problems))
      if warnings:
          print("*** Potential problems in input BAM compared to reference:\n%s\n" %
                "\n".join(warnings))
docs: Ensure a pre-aligned BAM file matches the expected reference genome.