code: def sample_name(in_bam):
      
      with pysam.AlignmentFile(in_bam, "rb", check_sq=False) as in_pysam:
          try:
              if "RG" in in_pysam.header:
                  return in_pysam.header["RG"][0]["SM"]
          except ValueError:
              return None
docs: Get sample name from BAM file.