libStatGen Software  1
Main.cpp
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include "SamFile.h"
00018 #include "Pileup.h"
00019 #include  "PileupElementBaseQual.h"
00020 
00021 
00022 void newAnalyze(PileupElementBaseQual& element)
00023 {
00024     std::cout << "newAnalyze: ";
00025     element.analyze();
00026 }
00027 
00028 
00029 class AnalyzeClass
00030 {
00031 public:
00032     AnalyzeClass()
00033     {
00034         myCounter = 33;
00035     }
00036     bool operator() (PileupElementBaseQual& element)
00037     {
00038         std::cout << "Class Analyze: Counter = " << myCounter << ": ";
00039         element.analyze();
00040         ++myCounter;
00041         return(true);
00042     }
00043     int myCounter;
00044 private:
00045 };
00046 
00047 
00048 int main(int argc, char ** argv)
00049 {
00050     const char* fileName = "../../test/testFiles/sortedBam.bam";
00051     const char* indexName = "../../test/testFiles/sortedBam.bam.bai";
00052 
00053     printf("\nPileup<PileupElementBaseQual> on entire file: %s\n", fileName);
00054     Pileup<PileupElementBaseQual> pileup(1024);
00055     pileup.processFile(fileName);
00056 
00057     printf("\nPileup<PileupElement> on entire file: %s\n", fileName);
00058     Pileup<PileupElement> pileup1(1024);
00059     pileup1.processFile(fileName);
00060 
00061     printf("\nPileup<PileupElementBaseQual> on a section of file: %s\n", fileName);
00062     // Read a sorted & indexed BAM file.
00063     Pileup<PileupElementBaseQual> pileup2(100);
00064     
00065     SamFile samIn;
00066     SamFileHeader header;
00067     SamRecord record;
00068     
00069     if(!samIn.OpenForRead(fileName))
00070     {
00071         fprintf(stderr, "%s\n", samIn.GetStatusMessage());
00072         return(samIn.GetStatus());
00073     }
00074     
00075     // Open the bam index file for reading.
00076     if(!samIn.ReadBamIndex(indexName))
00077     {
00078         fprintf(stderr, "%s\n", samIn.GetStatusMessage());
00079         return(samIn.GetStatus());
00080     }
00081 
00082     if(!samIn.ReadHeader(header))
00083     {
00084         fprintf(stderr, "%s\n", samIn.GetStatusMessage());
00085         return(samIn.GetStatus());
00086     }
00087 
00088     const char* refName = "1";
00089     int start = 1000;
00090     int end = 1500;
00091     if(!samIn.SetReadSection(refName, start, end))
00092     {
00093         fprintf(stderr, "%s\n", samIn.GetStatusMessage());
00094         return(samIn.GetStatus());
00095     }
00096 
00097     // Iterate over all records
00098     while (samIn.ReadRecord(header, record))
00099     {
00100         pileup2.processAlignment(record);
00101     }
00102 
00103     pileup2.flushPileup();
00104 
00105     int returnValue = 0;
00106     if(samIn.GetStatus() != SamStatus::NO_MORE_RECS)
00107     {
00108         // Failed to read a record.
00109         fprintf(stderr, "%s\n", samIn.GetStatusMessage());
00110         returnValue = samIn.GetStatus();
00111     }
00112 
00113     printf("\nPileup<PileupElementBaseQual> on entire file, newAnalyze: %s\n", fileName);
00114 
00115     void (*fnPtr)(PileupElementBaseQual&) = newAnalyze;
00116 
00117     Pileup<PileupElementBaseQual, void (*)(PileupElementBaseQual&)> pileup3(1024, fnPtr);
00118     pileup3.processFile(fileName);
00119 
00120     printf("\nPileup<PileupElementBaseQual> on entire file, newAnalyze: %s\n", fileName);
00121     
00122     AnalyzeClass myAnalyzeClass;
00123     myAnalyzeClass.myCounter = 2;
00124     Pileup<PileupElementBaseQual, AnalyzeClass> pileup4(1024, myAnalyzeClass);
00125     pileup4.processFile(fileName);
00126 
00127     return(0);
00128 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends