# Class SAMRecordSetBuilder

java.lang.Object
htsjdk.samtools.SAMRecordSetBuilder

All Implemented Interfaces:
  `Iterable<SAMRecord>`

public class SAMRecordSetBuilder extends Object implements Iterable<SAMRecord>
Factory class for creating SAMRecords for testing purposes. Various methods can be called to add new SAM records (or pairs of records) to a list which can then be returned at any point. The records must reference human chromosomes (excluding randoms etc.).

Although this is a class for testing, it is in the src tree because it is included in the sam jarfile.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`DEFAULT_CHROMOSOME_LENGTH`
`static final DuplicateScoringStrategy.ScoringStrategy`
`DEFAULT_DUPLICATE_SCORING_STRATEGY`

## Constructor Summary

Constructors
Constructor
Description
`SAMRecordSetBuilder()`
Constructs a new SAMRecordSetBuilder with all the data needed to keep the records sorted in coordinate order.
`SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder)`
Construct a new SAMRecordSetBuilder.
`SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder, boolean addReadGroup)`
`SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder, boolean addReadGroup, int defaultChromosomeLength)`
`SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder, boolean addReadGroup, int defaultChromosomeLength, DuplicateScoringStrategy.ScoringStrategy duplicateScoringStrategy)`

## Method Summary

Modifier and Type
Method
Description
`SAMRecord`
`addFrag(String name, int contig, int start, boolean negativeStrand)`
Adds a skeletal fragment (non-PE) record to the set using the provided contig start and strand information.
`SAMRecord`
`addFrag(String name, int contig, int start, boolean negativeStrand, boolean recordUnmapped, String cigar, String qualityString, int defaultQuality)`
Adds a fragment record (mapped or unmapped) to the set using the provided contig start and optionally the strand, cigar string, quality string or default quality score.
`SAMRecord`
`addFrag(String name, int contig, int start, boolean negativeStrand, boolean recordUnmapped, String cigar, String qualityString, int defaultQuality, boolean isSecondary)`
Adds a fragment record (mapped or unmapped) to the set using the provided contig start and optionally the strand, cigar string, quality string or default quality score.
`SAMRecord`
`addFrag(String name, int contig, int start, boolean negativeStrand, boolean recordUnmapped, String cigar, String qualityString, int defaultQuality, boolean isSecondary, boolean isSupplementary)`
Adds a fragment record (mapped or unmapped) to the set using the provided contig start and optionally the strand, cigar string, quality string or default quality score.
`void`
`addPair(String name, int contig, int start1, int start2)`
Adds a skeletal pair of records to the set using the provided contig starts.
`List<SAMRecord>`
`addPair(String name, int contig, int start1, int start2, boolean record1Unmapped, boolean record2Unmapped, String cigar1, String cigar2, boolean strand1, boolean strand2, boolean record1NonPrimary, boolean record2NonPrimary, int defaultQuality)`
Adds a pair of records (mapped or unmmapped) to the set using the provided contig starts.
`List<SAMRecord>`
`addPair(String name, int contig, int start1, int start2, boolean record1Unmapped, boolean record2Unmapped, String cigar1, String cigar2, boolean strand1, boolean strand2, int defaultQuality)`
Adds a pair of records (mapped or unmmapped) to the set using the provided contig starts.
`List<SAMRecord>`
`addPair(String name, int contig1, int contig2, int start1, int start2, boolean record1Unmapped, boolean record2Unmapped, String cigar1, String cigar2, boolean strand1, boolean strand2, boolean record1NonPrimary, boolean record2NonPrimary, int defaultQuality)`
Adds a pair of records (mapped or unmmapped) to the set using the provided contig starts.
`void`
`addRecord(SAMRecord record)`
The record should already have the DS and MC tags computed
`void`
`addUnmappedFragment(String name)`
Adds an unmapped fragment read to the builder.
`void`
`addUnmappedPair(String name)`
Adds a pair with both ends unmapped to the builder.
`SAMFileHeader`
`getHeader()`
`Collection<SAMRecord>`
`getRecords()`
Returns the accumulated list of sam records.
`SamReader`
`getSamReader()`
Creates samFileReader from the data in instance of this class
`CloseableIterator<SAMRecord>`
`iterator()`
Returns a CloseableIterator over the collection of SAMRecords.
`static SAMFileHeader`
`makeDefaultHeader(SAMFileHeader.SortOrder sortOrder, int contigLength, boolean addReadGroup)`
creates a simple header
`void`
`setHeader(SAMFileHeader header)`
`void`
`setProgramRecord(SAMProgramRecord programRecord)`
Adds the given program record to the header, and assigns the PG tag to any SAMRecords created after it has been added.
`void`
`setRandomSeed(long seed)`
Set the seed of the random number generator for cases in which repeatable result is desired.
`void`
`setReadGroup(SAMReadGroupRecord readGroup)`
`void`
`setReadLength(int readLength)`
`void`
`setUnmappedHasBasesAndQualities(boolean value)`
`void`
`setUseBamFile(boolean useBamFile)`
Determine whether the class will use a bam (default) or a sam file to hold the records when providing a reader to them.
`void`
`setUseNmFlag(boolean useNmFlag)`
`int`
`size()`
`static void`
`writeRandomReference(SAMFileHeader header, Path fasta)`
Writes a random (but deterministic) reference file given a `SAMFileHeader` output file is expected to have a non-compressed fasta suffix.
`void`
`writeRandomReference(Path fasta)`
Writes a random (but deterministic) reference file given a `SAMFileHeader` output file is expected to have a non-compressed fasta suffix.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface Iterable

`forEach, spliterator`

## Field Details

### DEFAULT_CHROMOSOME_LENGTH

public static final int DEFAULT_CHROMOSOME_LENGTH

See Also:
  - Constant Field Values

### DEFAULT_DUPLICATE_SCORING_STRATEGY

public static final DuplicateScoringStrategy.ScoringStrategy DEFAULT_DUPLICATE_SCORING_STRATEGY

## Constructor Details

### SAMRecordSetBuilder

public SAMRecordSetBuilder()
Constructs a new SAMRecordSetBuilder with all the data needed to keep the records sorted in coordinate order.

### SAMRecordSetBuilder

public SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder)
Construct a new SAMRecordSetBuilder.

Parameters:
  `sortForMe` - If true, keep the records created in sorted order.
  `sortOrder` - If sortForMe, defines the sort order.

### SAMRecordSetBuilder

public SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder, boolean addReadGroup)

### SAMRecordSetBuilder

public SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder, boolean addReadGroup, int defaultChromosomeLength)

### SAMRecordSetBuilder

public SAMRecordSetBuilder(boolean sortForMe, SAMFileHeader.SortOrder sortOrder, boolean addReadGroup, int defaultChromosomeLength, DuplicateScoringStrategy.ScoringStrategy duplicateScoringStrategy)

## Method Details

### setUseBamFile

public void setUseBamFile(boolean useBamFile)
Determine whether the class will use a bam (default) or a sam file to hold the records when providing a reader to them.

Parameters:
  `useBamFile` - if true will use a BAM file, otherwise it will use a SAM file to hold the records.

### setUnmappedHasBasesAndQualities

public void setUnmappedHasBasesAndQualities(boolean value)

### size

public int size()

### setRandomSeed

public void setRandomSeed(long seed)
Set the seed of the random number generator for cases in which repeatable result is desired.

### setProgramRecord

public void setProgramRecord(SAMProgramRecord programRecord)
Adds the given program record to the header, and assigns the PG tag to any SAMRecords created after it has been added. May be called multiple times in order to assign different PG IDs to different SAMRecords. programRecord may be null to stop assignment of PG tag. It is up to the caller to ensure that program record IDs do not collide.

### setUseNmFlag

public void setUseNmFlag(boolean useNmFlag)

### setReadGroup

public void setReadGroup(SAMReadGroupRecord readGroup)

### getRecords

public Collection<SAMRecord> getRecords()
Returns the accumulated list of sam records.

### setHeader

public void setHeader(SAMFileHeader header)

### addRecord

public void addRecord(SAMRecord record)
The record should already have the DS and MC tags computed

### iterator

public CloseableIterator<SAMRecord> iterator()
Returns a CloseableIterator over the collection of SAMRecords.

Specified by:
  `iterator` in interface `Iterable<SAMRecord>`

### addFrag

public SAMRecord addFrag(String name, int contig, int start, boolean negativeStrand)
Adds a skeletal fragment (non-PE) record to the set using the provided contig start and strand information.

### addFrag

public SAMRecord addFrag(String name, int contig, int start, boolean negativeStrand, boolean recordUnmapped, String cigar, String qualityString, int defaultQuality) throws SAMException
Adds a fragment record (mapped or unmapped) to the set using the provided contig start and optionally the strand, cigar string, quality string or default quality score.

Throws:
  `SAMException`

### addFrag

public SAMRecord addFrag(String name, int contig, int start, boolean negativeStrand, boolean recordUnmapped, String cigar, String qualityString, int defaultQuality, boolean isSecondary) throws SAMException
Adds a fragment record (mapped or unmapped) to the set using the provided contig start and optionally the strand, cigar string, quality string or default quality score.

Throws:
  `SAMException`

### addFrag

public SAMRecord addFrag(String name, int contig, int start, boolean negativeStrand, boolean recordUnmapped, String cigar, String qualityString, int defaultQuality, boolean isSecondary, boolean isSupplementary) throws SAMException
Adds a fragment record (mapped or unmapped) to the set using the provided contig start and optionally the strand, cigar string, quality string or default quality score.

Throws:
  `SAMException`

### addUnmappedFragment

public void addUnmappedFragment(String name)
Adds an unmapped fragment read to the builder.

### addPair

public void addPair(String name, int contig, int start1, int start2)
Adds a skeletal pair of records to the set using the provided contig starts. The pair is assumed to be a well formed pair sitting on a single contig.

### addPair

public List<SAMRecord> addPair(String name, int contig, int start1, int start2, boolean record1Unmapped, boolean record2Unmapped, String cigar1, String cigar2, boolean strand1, boolean strand2, int defaultQuality)
Adds a pair of records (mapped or unmmapped) to the set using the provided contig starts. The pair is assumed to be a well formed pair sitting on a single contig.

### addPair

public List<SAMRecord> addPair(String name, int contig1, int contig2, int start1, int start2, boolean record1Unmapped, boolean record2Unmapped, String cigar1, String cigar2, boolean strand1, boolean strand2, boolean record1NonPrimary, boolean record2NonPrimary, int defaultQuality)
Adds a pair of records (mapped or unmmapped) to the set using the provided contig starts. The pair is assumed to be a well formed pair sitting on a single contig.

### addPair

public List<SAMRecord> addPair(String name, int contig, int start1, int start2, boolean record1Unmapped, boolean record2Unmapped, String cigar1, String cigar2, boolean strand1, boolean strand2, boolean record1NonPrimary, boolean record2NonPrimary, int defaultQuality)
Adds a pair of records (mapped or unmmapped) to the set using the provided contig starts. The pair is assumed to be a well formed pair sitting on a single contig.

### addUnmappedPair

public void addUnmappedPair(String name)
Adds a pair with both ends unmapped to the builder.

### getSamReader

public SamReader getSamReader()
Creates samFileReader from the data in instance of this class

Returns:
  SamReader

### getHeader

public SAMFileHeader getHeader()

### setReadLength

public void setReadLength(int readLength)

### makeDefaultHeader

public static SAMFileHeader makeDefaultHeader(SAMFileHeader.SortOrder sortOrder, int contigLength, boolean addReadGroup)
creates a simple header

Parameters:
  `sortOrder` - Use this sort order in the header
  `contigLength` - length of the other contigs
Returns:
  newly formed header

### writeRandomReference

public void writeRandomReference(Path fasta) throws IOException
Writes a random (but deterministic) reference file given a `SAMFileHeader` output file is expected to have a non-compressed fasta suffix. Method will also write .dict and .fai files next to the reference file. files next to the provided. Will use the current `SAMFileHeader` to model the reference on.

Parameters:
  `fasta` - Path to output reference where it will be written. No checks will be performed regarding the writability, or existence of the files prior to writing.
Throws:
  `IOException` - In case of problem during writing the files.

### writeRandomReference

public static void writeRandomReference(SAMFileHeader header, Path fasta) throws IOException
Writes a random (but deterministic) reference file given a `SAMFileHeader` output file is expected to have a non-compressed fasta suffix. Method will also write .dict and .fai files next to the reference file. files next to the provided

Parameters:
  `header` - Header file to base the reference on. Length and names of sequences will be taken from here, as will the order of the contigs in the reference.
  `fasta` - Path to output reference where it will be written. No checks will be performed regarding the writability, or existence of the files prior to writing.
Throws:
  `IOException` - In case of problem during writing the files.