1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#include <pbdata/FASTASequence.hpp>
#include <cstdlib>
FASTASequence::FASTASequence() : DNASequence()
{
title = NULL;
titleLength = 0;
deleteTitleOnExit = false;
// If deleteTitleOnExist is false, whether to delete title
// or not should depend on deleteOnExit; otherwise, delete title
// regardless of deleteOnExit.
}
void FASTASequence::PrintSeq(std::ostream &out, int lineLength, char delim) const
{
out << delim;
if (title) out << title;
out << std::endl;
static_cast<const DNASequence *>(this)->PrintSeq(out, lineLength);
}
int FASTASequence::GetStorageSize() const
{
if (!title) return DNASequence::GetStorageSize();
return strlen(title) + DNASequence::GetStorageSize();
}
std::string FASTASequence::GetName() const
{
std::string name;
int i;
for (i = 0; i < titleLength; i++) {
if (title[i] != ' ' && title[i] != '\t' && title[i] != '\n' && title[i] != '\r') {
name.push_back(title[i]);
} else {
break;
}
}
return name;
}
void FASTASequence::ShallowCopy(const FASTASequence &rhs)
{
CheckBeforeCopyOrReference(rhs, "FASTASequence");
FASTASequence::Free();
static_cast<DNASequence *>(this)->ShallowCopy(rhs);
title = rhs.title;
titleLength = rhs.titleLength;
deleteTitleOnExit = false;
}
std::string FASTASequence::GetTitle() const { return std::string(title); }
// Delete title if this FASTASequence is under control or
// only title is under control.
void FASTASequence::DeleteTitle()
{
if (deleteOnExit or deleteTitleOnExit) {
if (title != NULL) {
delete[] title;
}
} // otherwise, title is controlled by another obj
title = NULL;
titleLength = 0;
deleteTitleOnExit = false;
}
void FASTASequence::CopyTitle(const char *str, int strlen)
{
FASTASequence::DeleteTitle();
// No segfault when str is NULL;
if (str == NULL) {
title = NULL;
titleLength = 0;
} else {
title = new char[strlen + 1];
memcpy(title, str, strlen);
titleLength = strlen;
title[titleLength] = '\0';
}
// In some cases, (e.g., when ReferenceSubstring and CopyTitle
// are called together), this Sequence may only have control over
// title but not seq.
deleteTitleOnExit = true;
}
void FASTASequence::CopyTitle(std::string str)
{
FASTASequence::CopyTitle(str.c_str(), str.size());
}
void FASTASequence::GetFASTATitle(std::string &fastaTitle) const
{
// look for the first space, and return the string until there.
int i;
for (i = 0; i < titleLength; i++) {
if (title[i] == ' ' or title[i] == '\t') {
break;
}
}
fastaTitle.assign(title, i);
}
// Copy rhs.seq[readStart:readEnd] to this Sequence.
void FASTASequence::CopySubsequence(FASTASequence &rhs, int readStart, int readEnd)
{
CheckBeforeCopyOrReference(rhs, "FASTASequence");
// Free before copying anything
FASTASequence::Free();
if (readEnd == -1) {
readEnd = rhs.length;
}
if (readEnd > readStart) {
length = readEnd - readStart;
DNASequence::Copy(rhs, readStart, length);
} else {
seq = NULL;
length = 0;
deleteOnExit = true;
}
FASTASequence::CopyTitle(rhs.title);
}
void FASTASequence::AppendToTitle(std::string str)
{
int newLength = titleLength + str.size() + 1;
if (newLength == 0) {
DeleteTitle();
return;
}
char *tmpTitle = new char[newLength];
memcpy(tmpTitle, title, titleLength);
memcpy(&tmpTitle[titleLength], str.c_str(), str.size());
tmpTitle[newLength - 1] = '\0';
delete[] title;
title = tmpTitle;
titleLength = newLength;
deleteTitleOnExit = true;
}
void FASTASequence::Assign(FASTASequence &rhs) { *this = (FASTASequence &)rhs; }
// Create a reverse complement FASTASequence of *this and assign to rhs.
void FASTASequence::MakeRC(FASTASequence &rhs, DNALength rhsPos, DNALength rhsLength)
{
rhs.Free();
DNASequence::MakeRC((DNASequence &)rhs, rhsPos, rhsLength);
if (title != NULL) {
(static_cast<FASTASequence *>(&rhs))->CopyTitle(title);
}
}
// Reverse complement sequence, don't change the title
FASTASequence &FASTASequence::ReverseComplementSelf(void)
{
FASTASequence rc;
MakeRC(rc);
Copy(rc);
return *this;
}
void FASTASequence::operator=(const FASTASequence &rhs)
{
CheckBeforeCopyOrReference(rhs, "FASTASequence");
// Free before copying anything
FASTASequence::Free();
// Copy seq from rhs
((DNASequence *)this)->Copy((DNASequence &)rhs);
assert(deleteOnExit);
// Copy title from rhs
FASTASequence::CopyTitle(rhs.title, rhs.titleLength);
assert(deleteOnExit);
}
void FASTASequence::Copy(const std::string &rhsTitle, const std::string &rhsSeq)
{
this->Copy(rhsSeq);
this->CopyTitle(rhsTitle);
}
void FASTASequence::Copy(const std::string &rhsSeq)
{
(static_cast<DNASequence *>(this))->Copy(rhsSeq);
}
void FASTASequence::Copy(const FASTASequence &rhs) { *this = (FASTASequence &)rhs; }
#ifdef USE_PBBAM
void FASTASequence::Copy(const PacBio::BAM::BamRecord &record)
{
FASTASequence::Copy(record.Impl().Name(), record.Sequence());
}
#endif
void FASTASequence::Free()
{
// Delete title if title is under control, reset deleteTitleOnExit.
FASTASequence::DeleteTitle();
// Delete seq if under control, reset deleteOnExit.
// Don't call Free() before calling DeleteTitle().
DNASequence::Free();
}
template DNALength ResizeSequence<FASTASequence>(FASTASequence &, DNALength);
|