libStatGen Software  1
SamHeaderRecord.h
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 
00018 #ifndef __SAMHEADER_RECORD_H__
00019 #define __SAMHEADER_RECORD_H__
00020 
00021 #include "StringArray.h"
00022 #include "StringHash.h"
00023 #include "SamHeaderTag.h"
00024 
00025 /// This class encapsulates the tag value pairs contained with a SAM Header
00026 /// line with accessors for getting and setting the tags within this header.
00027 class SamHeaderRecord
00028 {
00029 public:
00030     /// Specifies the Type for the sam header record (line).
00031     enum SamHeaderRecordType {
00032         HD, ///< Header
00033         SQ, ///< Sequence Dictionary
00034         RG, ///< Read Group
00035         PG  ///< Program
00036     };
00037 
00038     /// Constructor
00039     SamHeaderRecord();
00040    
00041     /// Destructor
00042     virtual ~SamHeaderRecord();
00043 
00044     /// Return a pointer to a newly created header record of the appropriate type
00045     /// that is a copy of this record. The newly created record will not be
00046     /// deleted by this class and it is the responsibility of the calling method
00047     /// to handle the deletion.
00048     /// Returns NULL on failure to copy.
00049     virtual SamHeaderRecord* createCopy() const = 0;
00050     
00051     /// Set the fields from the passed in line.
00052     /// Return true if successfully set.
00053     bool setFields(const StringArray& tokens);
00054 
00055     /// Check to see if the record is valid.
00056     bool isValid();
00057 
00058     /// Return the value associated with the specified tag.  Returns "" if it
00059     /// is not set.
00060     const char* getTagValue(const char* tag) const;
00061 
00062     /// Set the value of the specified tag to the specified value, deletes
00063     /// the tag when value is NULL.
00064     /// Returns whether or not it was successful, fails if tag is the key tag
00065     /// and the key tag already exists.
00066     bool setTag(const char* tag, const char* value);
00067 
00068     /// Reset this header record to an empty state with no tags.
00069     void reset();
00070 
00071     /// Appends the string representation of this header record
00072     /// to the passed in string.
00073     bool appendString(std::string& header);
00074 
00075     /// Add the key tag with the specified value (not for HD headers).
00076     bool addKey(const char* value);
00077 
00078     /// Get the value associated with the key tag.  Returns "" if it is not set.
00079     const char* getKeyValue() const;
00080 
00081     /// This record is active (true) if there is at least one tag set.
00082     bool isActiveHeaderRecord();
00083 
00084     /// Return the type of this header record (HD, SQ, RG, or PG) as a string.
00085     const char* getTypeString();
00086 
00087     /// Return the type of this header record (HD, SQ, RG, or PG) as an enum.
00088     SamHeaderRecordType getType();
00089 
00090 protected:
00091     void addRequiredTag(const char* requiredTag);
00092 
00093     // Copy this record into the specified new one.
00094     virtual void internalCopy(SamHeaderRecord& newRec) const;
00095 
00096     // The type for this header record.
00097     std::string myTypeString;
00098 
00099     // The type for this header record.
00100     SamHeaderRecordType myType;
00101 
00102     // The TAG name that is the key for this record
00103     // Only applicable if more than one of this type
00104     // of record is allowed.
00105     std::string myKeyTag;
00106 
00107 private:
00108     SamHeaderRecord(const SamHeaderRecord& samHeaderRecord);
00109     SamHeaderRecord& operator=(const SamHeaderRecord& samHeaderRecord);
00110 
00111     // hash from tag name to index into the tag values vector.
00112     StringIntHash myTagHash;
00113     std::vector<SamHeaderTag*> myTags;
00114 
00115     // The tags that are required for this record.
00116     std::vector<String> myRequiredTags;
00117 
00118     int myNumActiveTags;
00119 };
00120 
00121 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends