for ( final SAMReadGroupRecord readGroup : header.getReadGroups() ) {
final String thisReadGroupSample = readGroup.getSample();
if ( thisReadGroupSample == null ) {
throw new UserException(String.format("On-the fly sample renaming was requested for bam file %s, however this " +
"bam file contains a read group (id: %s) with a null sample attribute",
readerID.getSamFilePath(), readGroup.getId()));
}
else if ( firstEncounteredSample == null ) {
firstEncounteredSample = thisReadGroupSample;
}
else if ( ! firstEncounteredSample.equals(thisReadGroupSample) ) {
throw new UserException(String.format("On-the-fly sample renaming was requested for bam file %s, " +
"however this bam file contains reads from more than one sample " +
"(encountered samples %s and %s in the bam header). The GATK requires that " +
"all bams for which on-the-fly sample renaming is requested " +
"contain reads from only a single sample per bam.",
readerID.getSamFilePath(), firstEncounteredSample, thisReadGroupSample));