Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/walaj/SeqLib
Browse files Browse the repository at this point in the history
  • Loading branch information
walaj committed Apr 25, 2017
2 parents ad8a854 + c645c4a commit 38c98e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions SeqLib/BamRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ class BamRecord {
*/
bool GetZTag(const std::string& tag, std::string& s) const;

/** Get a string of either Z, f or i type. Useful if tag type not known at compile time.
* @param tag Name of the tag. eg "XP"
* @param s The string to be filled in with the tag information
* @return Returns true if the tag is present and is either Z or i, even if empty. Return false if no tag or not Z or i.
*/
bool GetTag(const std::string& tag, std::string& s) const;

/** Get a vector of type int from a Z tag delimited by "^"
* Smart-tags allow one to store vectors of strings, ints or doubles in the alignment tags, and
* do not require an additional data structure on top of bseq1_t.
Expand Down Expand Up @@ -711,6 +718,19 @@ class BamRecord {
return true;
}

/** Get a float (f) tag
* @param tag Name of the tag. eg "AS"
* @param t Value to be filled in with the tag value.
* @return Return true if the tag exists.
*/
inline bool GetFloatTag(const std::string& tag, float& t) const {
uint8_t* p = bam_aux_get(b.get(),tag.c_str());
if (!p)
return false;
t = bam_aux2f(p);
return true;
}

/** Add a string (Z) tag
* @param tag Name of the tag. eg "XP"
* @param val Value for the tag
Expand Down
20 changes: 20 additions & 0 deletions src/BamRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,26 @@ namespace SeqLib {
bam_aux_append(b.get(), tag.data(), 'Z', val.length()+1, (uint8_t*)val.c_str());
}

bool BamRecord::GetTag(const std::string& tag, std::string& s) const {
if (GetZTag(tag, s))
return true;
int32_t t;
if (GetIntTag(tag, t)) {
std::stringstream ss;
ss << t;
s = ss.str();
return true;
}
float f;
if (GetFloatTag(tag, f)) {
std::stringstream ss;
ss << f;
s = ss.str();
return true;
}
return false;
}

bool BamRecord::GetZTag(const std::string& tag, std::string& s) const {
uint8_t* p = bam_aux_get(b.get(),tag.c_str());
if (!p)
Expand Down

0 comments on commit 38c98e3

Please sign in to comment.