code: def _target_chroms_and_header(bam_file, data):
      
      special_remaps = {"chrM": "MT", "MT": "chrM"}
      target_chroms = dict([(x.name, i) for i, x in enumerate(ref.file_contigs(dd.get_ref_file(data)))
                            if chromhacks.is_autosomal_or_sex(x.name)])
      out_chroms = []
      with pysam.Samfile(bam_file, "rb") as bamfile:
          for bami, bam_contig in enumerate([c["SN"] for c in bamfile.header["SQ"]]):
              if bam_contig in target_chroms:
                  target_chrom = bam_contig
              elif bam_contig in special_remaps and special_remaps[bam_contig] in target_chroms:
                  target_chrom = special_remaps[bam_contig]
              elif bam_contig.startswith("chr") and bam_contig.replace("chr", "") in target_chroms:
                  target_chrom = bam_contig.replace("chr", "")
              elif "chr%s" % bam_contig in target_chroms:
                  target_chrom = "chr%s" % bam_contig
              else:
                  target_chrom = None
              # target_chrom == bam_contig ensures we don't try chr1 -> 1 style remapping
              if target_chrom and target_chrom == bam_contig:
                  # Order not required if dealing with SAM file header fixing
                  #assert bami == target_chroms[target_chrom], \
                  #    ("remove_extracontigs: Non-matching order of standard contig: %s %s (%s vs %s)" %
                  #     (bam_file, target_chrom, bami, target_chroms[target_chrom]))
                  out_chroms.append(target_chrom)
      assert out_chroms, ("remove_extracontigs: Did not find any chromosomes in reference file: %s %s" %
                          (bam_file, target_chroms))
      return out_chroms
docs: Get a list of chromosomes to target and new updated ref_file header.
  
      Could potentially handle remapping from chr1 -> 1 but currently disabled due
      to speed issues.