1 // ***************************************************************************
2 // bamtools_sam.h (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 1 June 2010
7 // ---------------------------------------------------------------------------
8 // Prints a BAM file in the text-based SAM format.
9 // ***************************************************************************
15 #include "bamtools_sam.h"
16 #include "bamtools_options.h"
17 #include "BamReader.h"
20 using namespace BamTools;
24 // ---------------------------------------------
25 // print BamAlignment in SAM format
27 void PrintSAM(const BamAlignment& a) {
30 // <QNAME> <FLAG> <RNAME> <POS> <MAPQ> <CIGAR> <MRNM> <MPOS> <ISIZE> <SEQ> <QUAL> [ <TAG>:<VTYPE>:<VALUE> [...] ]
32 // ******************************* //
33 // ** NOT FULLY IMPLEMENTED YET ** //
34 //******************************** //
36 // Todo : build CIGAR string
38 // there are some quirks, per the spec, regarding when to use '=' or not
40 // ******************************* //
43 // do validity check on RefID / MateRefID ??
47 string cigarString("CIGAR:NOT YET");
50 string tagString("TAG:NOT YET");
52 // print BamAlignment to stdout in SAM format
53 cout << a.Name << '\t'
54 << a.AlignmentFlag << '\t'
55 << references[a.RefID].RefName << '\t'
57 << a.MapQuality << '\t'
58 << cigarString << '\t'
59 << ( a.IsPaired() ? references[a.MateRefID].RefName : "*" ) << '\t'
60 << ( a.IsPaired() ? a.MatePosition : 0 ) << '\t'
61 << ( a.IsPaired() ? a.InsertSize : 0 ) << '\t'
62 << a.QueryBases << '\t'
63 << a.Qualities << '\t'
67 // ---------------------------------------------
68 // SamSettings implementation
70 struct SamTool::SamSettings {
73 bool HasInputBamFilename;
74 bool HasMaximumOutput;
75 bool IsOmittingHeader;
78 string InputBamFilename;
85 : HasInputBamFilename(false)
86 , HasMaximumOutput(false)
87 , IsOmittingHeader(false)
88 , InputBamFilename(Options::StandardIn())
92 // ---------------------------------------------
93 // SamTool implementation
95 SamTool::SamTool(void)
97 , m_settings(new SamSettings)
99 // set program details
100 Options::SetProgramInfo("bamtools sam", "prints BAM file in SAM text format", "-in <filename>");
103 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
104 Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputBamFilename, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
106 OptionGroup* FilterOpts = Options::CreateOptionGroup("Filters");
107 Options::AddOption("-noheader", "omit the SAM header from output", m_settings->IsOmittingHeader, FilterOpts);
108 Options::AddValueOption("-num", "N", "maximum number of alignments to output", "", m_settings->HasMaximumOutput, m_settings->MaximumOutput, FilterOpts);
111 SamTool::~SamTool(void) {
116 int SamTool::Help(void) {
117 Options::DisplayHelp();
121 int SamTool::Run(int argc, char* argv[]) {
123 // parse command line arguments
124 Options::Parse(argc, argv, 1);
126 // open our BAM reader
128 reader.Open(m_settings->InputBamFilename);
130 // if header desired, retrieve and print to stdout
131 if ( !m_settings->IsOmittingHeader ) {
132 string header = reader.GetHeaderText();
133 cout << header << endl;
136 // store reference data
137 references = reader.GetReferenceData();
139 // print all alignments to stdout in SAM format
140 if ( !m_settings->HasMaximumOutput ) {
142 while( reader.GetNextAlignment(ba) ) {
147 // print first N alignments to stdout in SAM format
150 int alignmentsPrinted = 0;
151 while ( reader.GetNextAlignment(ba) && (alignmentsPrinted < m_settings->MaximumOutput) ) {