libStatGen Software  1
Pileup< PILEUP_TYPE, FUNC_CLASS > Class Template Reference

Class to perform a pileup of all reads by position, assuming the reads are coordinate sorted. More...

#include <Pileup.h>

Collaboration diagram for Pileup< PILEUP_TYPE, FUNC_CLASS >:

Public Member Functions

 Pileup (const FUNC_CLASS &fp=FUNC_CLASS())
 Constructor using the default maximum number of bases a read spans.
 
 Pileup (int window, const FUNC_CLASS &fp=FUNC_CLASS())
 Constructor that sets the maximum number of bases a read spans. More...
 
 Pileup (const std::string &refSeqFileName, const FUNC_CLASS &fp=FUNC_CLASS())
 Perform pileup with a reference.
 
 Pileup (int window, const std::string &refSeqFileName, const FUNC_CLASS &fp=FUNC_CLASS())
 Perform pileup with a reference and a specified window size.
 
virtual ~Pileup ()
 Destructor.
 
virtual int processFile (const std::string &fileName, uint16_t excludeFlag=0x0704, uint16_t includeFlag=0)
 Performs a pileup on the specified file. More...
 
virtual void processAlignment (SamRecord &record)
 Add an alignment to the pileup.
 
virtual void processAlignmentRegion (SamRecord &record, int startPos, int endPos, PosList *excludeList=NULL)
 Add only positions that fall within the specified region of the alignment to the pileup and outside of the specified excluded positions. More...
 
void flushPileup ()
 Done processing, flush every position that is currently being stored in the pileup.
 

Protected Member Functions

void addAlignmentPosition (int refPosition, SamRecord &record)
 
virtual void flushPileup (int refID, int refPosition)
 
void flushPileup (int refPosition)
 
int pileupPosition (int refPosition)
 
virtual void resetElement (PILEUP_TYPE &element, int position)
 
virtual void addElement (PILEUP_TYPE &element, SamRecord &record)
 
virtual void analyzeElement (PILEUP_TYPE &element)
 
virtual void analyzeHead ()
 

Protected Attributes

FUNC_CLASS myAnalyzeFuncPtr
 
std::vector< PILEUP_TYPE > myElements
 
int pileupStart
 
int pileupHead
 
int pileupTail
 
int pileupWindow
 
int myCurrentRefID
 
GenomeSequencemyRefPtr
 

Detailed Description

template<class PILEUP_TYPE, class FUNC_CLASS = defaultPileup<PILEUP_TYPE>>
class Pileup< PILEUP_TYPE, FUNC_CLASS >

Class to perform a pileup of all reads by position, assuming the reads are coordinate sorted.

Definition at line 58 of file Pileup.h.

Constructor & Destructor Documentation

◆ Pileup()

template<class PILEUP_TYPE , class FUNC_CLASS >
Pileup< PILEUP_TYPE, FUNC_CLASS >::Pileup ( int  window,
const FUNC_CLASS &  fp = FUNC_CLASS() 
)

Constructor that sets the maximum number of bases a read spans.

This is the "window" the length of the buffer that holds the pileups for each position until the read start has moved past the position.

Definition at line 168 of file Pileup.h.

169  : myAnalyzeFuncPtr(fp),
170  myElements(),
171  pileupStart(0),
172  pileupHead(0),
173  pileupTail(-1),
174  pileupWindow(window),
175  myCurrentRefID(-2),
176  myRefPtr(NULL)
177 {
178  // Not using pointers since this is templated.
179  myElements.resize(window);
180 }

Member Function Documentation

◆ processAlignmentRegion()

template<class PILEUP_TYPE , class FUNC_CLASS >
void Pileup< PILEUP_TYPE, FUNC_CLASS >::processAlignmentRegion ( SamRecord record,
int  startPos,
int  endPos,
PosList excludeList = NULL 
)
virtual

Add only positions that fall within the specified region of the alignment to the pileup and outside of the specified excluded positions.

Parameters
recordalignment to be added to the pileup.
startPos0-based start position of the bases that should be added to the pileup.
endPos0-based end position of the bases that should be added to the pileup (this position is not added). Set to -1 if there is no end position to the region.
excludeListlist of refID/positions to exclude from processing.

Definition at line 316 of file Pileup.h.

320 {
321  int refPosition = record.get0BasedPosition();
322  int refID = record.getReferenceID();
323 
324  // Flush any elements from the pileup that are prior to this record
325  // since the file is sorted, we are done with those positions.
326  flushPileup(refID, refPosition);
327 
328  // Check if the region starts after this reference starts. If so,
329  // we only want to start adding at the region start position.
330  if(startPos > refPosition)
331  {
332  refPosition = startPos;
333  }
334 
335  // Loop through for each reference position covered by the record.
336  // It is up to the PILEUP_TYPE to handle insertions/deletions, etc
337  // that are related with the given reference position.
338  for(; refPosition <= record.get0BasedAlignmentEnd(); ++refPosition)
339  {
340  // Check to see if we have gone past the end of the region, in which
341  // case we can stop processing this record. Check >= since the
342  // end position is not in the region.
343  if((endPos != -1) && (refPosition >= endPos))
344  {
345  break;
346  }
347 
348  // Check to see if this position is in the exclude list.
349  bool addPos = true;
350  if(excludeList != NULL)
351  {
352  // There is an exclude list, so lookup the position.
353  if(excludeList->hasPosition(refID, refPosition))
354  {
355  // This position is in the exclude list, so don't add it.
356  addPos = false;
357  }
358  }
359  if(addPos)
360  {
361  addAlignmentPosition(refPosition, record);
362  }
363  }
364 }

◆ processFile()

template<class PILEUP_TYPE , class FUNC_CLASS >
int Pileup< PILEUP_TYPE, FUNC_CLASS >::processFile ( const std::string &  fileName,
uint16_t  excludeFlag = 0x0704,
uint16_t  includeFlag = 0 
)
virtual

Performs a pileup on the specified file.

Parameters
excludeFlagif specified, if any bit set in the exclude flag is set in the record's flag, it will be dropped. Defaulted to exclude:
  • unmapped,
  • not primary alignment
  • failed platform/vendor quality check
  • PCR or optical duplicate
includeFlagif specified, every bit must be set in the record's flag for it to be included - defaulted to 0, no bits are required to be set.
Returns
0 for success and non-zero for failure.

Definition at line 235 of file Pileup.h.

238 {
239  SamFile samIn;
240  SamFileHeader header;
241  SamRecord record;
242 
243  if(myRefPtr != NULL)
244  {
245  samIn.SetReference(myRefPtr);
246  }
247 
248  if(!samIn.OpenForRead(fileName.c_str()))
249  {
250  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
251  return(samIn.GetStatus());
252  }
253 
254  if(!samIn.ReadHeader(header))
255  {
256  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
257  return(samIn.GetStatus());
258  }
259 
260  // The file needs to be sorted by coordinate.
262 
263  // Iterate over all records
264  while (samIn.ReadRecord(header, record))
265  {
266  uint16_t flag = record.getFlag();
267  if(flag & excludeFlag)
268  {
269  // This record has an excluded flag set,
270  // so continue to the next one.
271  continue;
272  }
273  if((flag & includeFlag) != includeFlag)
274  {
275  // This record does not have all required flags set,
276  // so continue to the next one.
277  continue;
278  }
279  processAlignment(record);
280  }
281 
282  flushPileup();
283 
284  int returnValue = 0;
285  if(samIn.GetStatus() != SamStatus::NO_MORE_RECS)
286  {
287  // Failed to read a record.
288  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
289  returnValue = samIn.GetStatus();
290  }
291  return(returnValue);
292 }

The documentation for this class was generated from the following file:
SamFile::COORDINATE
@ COORDINATE
file is sorted by coordinate.
Definition: SamFile.h:49
SamRecord::get0BasedPosition
int32_t get0BasedPosition()
Get the 0-based(BAM) leftmost position of the record.
Definition: SamRecord.cpp:1307
SamRecord::get0BasedAlignmentEnd
int32_t get0BasedAlignmentEnd()
Returns the 0-based inclusive rightmost position of the clipped sequence.
Definition: SamRecord.cpp:1455
SamRecord::getReferenceID
int32_t getReferenceID()
Get the reference sequence id of the record (BAM format rid).
Definition: SamRecord.cpp:1293
SamFile::GetStatusMessage
const char * GetStatusMessage()
Get the Status Message of the last call that sets status.
Definition: SamFile.h:213
StatGenStatus::NO_MORE_RECS
@ NO_MORE_RECS
NO_MORE_RECS: failed to read a record since there are no more to read either in the file or section i...
Definition: StatGenStatus.h:36
SamFile::GetStatus
SamStatus::Status GetStatus()
Get the Status of the last call that sets status.
Definition: SamFile.h:207
SamFile::ReadRecord
bool ReadRecord(SamFileHeader &header, SamRecord &record)
Reads the next record from the file & stores it in the passed in record.
Definition: SamFile.cpp:501
Pileup::flushPileup
void flushPileup()
Done processing, flush every position that is currently being stored in the pileup.
Definition: Pileup.h:368
SamRecord::getFlag
uint16_t getFlag()
Get the flag (FLAG).
Definition: SamRecord.cpp:1372
SamFileHeader
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:34
SamFile::setSortedValidation
void setSortedValidation(SortedType sortType)
Set the flag to validate that the file is sorted as it is read/written.
Definition: SamFile.cpp:669
SamRecord
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition: SamRecord.h:51
SamFile::OpenForRead
bool OpenForRead(const char *filename, SamFileHeader *header=NULL)
Open a sam/bam file for reading with the specified filename, determing the type of file and SAM/BAM b...
Definition: SamFile.cpp:93
SamFile::ReadHeader
bool ReadHeader(SamFileHeader &header)
Reads the header section from the file and stores it in the passed in header.
Definition: SamFile.cpp:437
SamFile
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:35
Pileup::processAlignment
virtual void processAlignment(SamRecord &record)
Add an alignment to the pileup.
Definition: Pileup.h:296
SamFile::SetReference
void SetReference(GenomeSequence *reference)
Sets the reference to the specified genome sequence object.
Definition: SamFile.cpp:380
PosList::hasPosition
bool hasPosition(int refID, int refPosition)
Return whether or not this list contains the specified reference ID and position (negative values wil...
Definition: PosList.cpp:81