libStatGen Software  1
Main.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "SamFile.h"
18 #include "Pileup.h"
19 #include "PileupElementBaseQual.h"
20 
21 
22 void newAnalyze(PileupElementBaseQual& element)
23 {
24  std::cout << "newAnalyze: ";
25  element.analyze();
26 }
27 
28 
30 {
31 public:
32  AnalyzeClass()
33  {
34  myCounter = 33;
35  }
36  bool operator() (PileupElementBaseQual& element)
37  {
38  std::cout << "Class Analyze: Counter = " << myCounter << ": ";
39  element.analyze();
40  ++myCounter;
41  return(true);
42  }
43  int myCounter;
44 private:
45 };
46 
47 
48 int main(int argc, char ** argv)
49 {
50  const char* fileName = "../../test/testFiles/sortedBam.bam";
51  const char* indexName = "../../test/testFiles/sortedBam.bam.bai";
52 
53  printf("\nPileup<PileupElementBaseQual> on entire file: %s\n", fileName);
54  Pileup<PileupElementBaseQual> pileup(1024);
55  pileup.processFile(fileName);
56 
57  printf("\nPileup<PileupElement> on entire file: %s\n", fileName);
58  Pileup<PileupElement> pileup1(1024);
59  pileup1.processFile(fileName);
60 
61  printf("\nPileup<PileupElementBaseQual> on a section of file: %s\n", fileName);
62  // Read a sorted & indexed BAM file.
63  Pileup<PileupElementBaseQual> pileup2(100);
64 
65  SamFile samIn;
66  SamFileHeader header;
67  SamRecord record;
68 
69  if(!samIn.OpenForRead(fileName))
70  {
71  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
72  return(samIn.GetStatus());
73  }
74 
75  // Open the bam index file for reading.
76  if(!samIn.ReadBamIndex(indexName))
77  {
78  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
79  return(samIn.GetStatus());
80  }
81 
82  if(!samIn.ReadHeader(header))
83  {
84  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
85  return(samIn.GetStatus());
86  }
87 
88  const char* refName = "1";
89  int start = 1000;
90  int end = 1500;
91  if(!samIn.SetReadSection(refName, start, end))
92  {
93  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
94  return(samIn.GetStatus());
95  }
96 
97  // Iterate over all records
98  while (samIn.ReadRecord(header, record))
99  {
100  pileup2.processAlignment(record);
101  }
102 
103  pileup2.flushPileup();
104 
105  int returnValue = 0;
106  if(samIn.GetStatus() != SamStatus::NO_MORE_RECS)
107  {
108  // Failed to read a record.
109  fprintf(stderr, "%s\n", samIn.GetStatusMessage());
110  returnValue = samIn.GetStatus();
111  }
112 
113  printf("\nPileup<PileupElementBaseQual> on entire file, newAnalyze: %s\n", fileName);
114 
115  void (*fnPtr)(PileupElementBaseQual&) = newAnalyze;
116 
117  Pileup<PileupElementBaseQual, void (*)(PileupElementBaseQual&)> pileup3(1024, fnPtr);
118  pileup3.processFile(fileName);
119 
120  printf("\nPileup<PileupElementBaseQual> on entire file, newAnalyze: %s\n", fileName);
121 
122  AnalyzeClass myAnalyzeClass;
123  myAnalyzeClass.myCounter = 2;
124  Pileup<PileupElementBaseQual, AnalyzeClass> pileup4(1024, myAnalyzeClass);
125  pileup4.processFile(fileName);
126 
127  return(0);
128 }
This class inherits from the base class and stores base and qualities.
virtual void analyze()
Perform the analysis associated with this class.
Class to perform a pileup of all reads by position, assuming the reads are coordinate sorted.
Definition: Pileup.h:59
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:35
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:36
bool ReadHeader(SamFileHeader &header)
Reads the header section from the file and stores it in the passed in header.
Definition: SamFile.cpp:450
const char * GetStatusMessage()
Get the Status Message of the last call that sets status.
Definition: SamFile.h:218
bool SetReadSection(int32_t refID)
Sets which reference id (index into the BAM list of reference information) of the BAM file should be ...
Definition: SamFile.cpp:696
bool ReadRecord(SamFileHeader &header, SamRecord &record)
Reads the next record from the file & stores it in the passed in record.
Definition: SamFile.cpp:514
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
SamStatus::Status GetStatus()
Get the Status of the last call that sets status.
Definition: SamFile.h:212
bool ReadBamIndex(const char *filename)
Read the specified bam index file.
Definition: SamFile.cpp:300
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition: SamRecord.h:52
@ 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