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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
#ifndef GENE_MODEL_HPP
#define GENE_MODEL_HPP
#include <vector>
#include <string>
#include <unordered_map>
#include <seqan/gff_io.h>
enum Strandedness {FORWARD, REVERSE, UNKNOWN};
enum BioType {PROTEIN, PSEUDO, OTHER};
Strandedness operator~(const Strandedness &s) {
switch (s) {
case FORWARD: return REVERSE; break;
case REVERSE: return FORWARD; break;
case UNKNOWN: return REVERSE; break;
}
}
std::string typeToString(BioType t) {
switch(t) {
case BioType::PROTEIN:
return "PROTEIN"; break;
case BioType::PSEUDO:
return "PSEUDO"; break;
default:
return "OTHER"; break;
}
}
char strandToChar(Strandedness s) {
switch(s) {
case Strandedness::FORWARD:
return '+'; break;
case Strandedness::REVERSE:
return '-'; break;
default:
return '?'; break;
}
}
BioType stringToType(const std::string &s) {
if (s == "PROTEIN") {
return BioType::PROTEIN;
} else if (s == "PSEUDO") {
return BioType::PSEUDO;
} else {
return BioType::OTHER;
}
}
Strandedness charToStrand(char c) {
switch (c) {
case '+':
return Strandedness::FORWARD; break;
case '-':
return Strandedness::REVERSE; break;
default:
return Strandedness::UNKNOWN; break;
}
}
struct ExonModel {
std::string chr;
int start,stop;
Strandedness strand;
};
struct TranscriptModel {
std::string id; // like ENST
std::string chr;
int start,stop;
std::vector<ExonModel> exons;
Strandedness strand;
BioType type;
};
struct GeneModel {
std::string id; // like ENSG
std::string name; // like ALB
std::unordered_map<std::string,TranscriptModel> transcripts;
std::string chr;
int start,stop; // start 0-based, 1 bp past end for stop
Strandedness strand;
BioType type;
};
struct Transcriptome {
std::unordered_map<std::string,GeneModel> genes;
std::unordered_map<std::string,std::string> trxToGeneId;
std::unordered_map<std::string, seqan::CharString> seqs;
// maps transcript tr and 0-based position trpos
// to chr:chrpos in genome mapping to gene gene_id
bool translateTrPosition(const std::string &tr, const int _trpos, std::string &chr, int& chrpos, std::string &gene_id) {
auto gid_it = trxToGeneId.find(tr);
if (gid_it == trxToGeneId.end()) {
return false;
}
gene_id = gid_it->second;
auto g_it = genes.find(gene_id);
if (g_it == genes.end()) {
return false;
}
auto t_it = g_it->second.transcripts.find(tr);
if (t_it == g_it->second.transcripts.end()) {
return false;
}
auto &trxmodel = t_it->second;
chr = trxmodel.chr;
if (trxmodel.strand == Strandedness::UNKNOWN) {
return false;
}
bool fw = (trxmodel.strand == Strandedness::FORWARD);
int trpos = _trpos;
chrpos = trxmodel.start;
for (auto& exon : trxmodel.exons) {
int len = (exon.strand == Strandedness::FORWARD) ? (exon.stop - exon.start) : (exon.start - exon.stop);
assert(len > 0);
if (trpos < len) {
// maps to this exon
chrpos = exon.start;
if (exon.strand == Strandedness::FORWARD) {
chrpos += trpos;
} else {
chrpos -= trpos;
}
break;
} else {
trpos -= len;
}
}
if (trpos > 0) {
// goes beyond last exon, map to end
chrpos = trxmodel.stop + trpos - 1;
}
return true;
}
};
void writeTranscriptome(Transcriptome &transcriptome, std::ostream &out) {
for(const auto &gene : transcriptome.genes) {
const auto &gene_id = gene.first;
const auto &model = gene.second;
out << "GENE" << "\t" << model.id << "\t"
<< model.name << "\t"
<< model.chr << "\t"
<< typeToString(model.type) << "\t"
<< strandToChar(model.strand) << "\t"
<< model.start << "\t"
<< model.stop << "\t";
bool firsttr = true;
for (auto &trlist : model.transcripts) {
if (!firsttr) {
out << ";";
} else {
firsttr = false;
}
out << trlist.first;
}
out << "\n";
}
for(const auto &gene : transcriptome.genes) {
const auto &gene_id = gene.first;
const auto &model = gene.second;
for (auto &trlist : model.transcripts) {
const auto &tr_id = trlist.first;
const auto &tr = trlist.second;
out << "TRANSCRIPT" << "\t"
<< tr.id << "\t"
<< gene_id << "\t"
<< tr.chr << "\t"
<< typeToString(tr.type) << "\t"
<< strandToChar(tr.strand) << "\t"
<< tr.start << "\t"
<< tr.stop << "\t";
bool firstex = true;
for(const auto &exon : tr.exons) {
if (!firstex) {
out << ";";
} else {
firstex = false;
}
out << exon.start << "," << exon.stop;
}
out << "\n";
}
}
}
void loadTranscriptome(Transcriptome& transcriptome, std::istream &in, const ProgramOptions& options) {
std::string line;
std::string segment;
std::string type;
std::string gtype, ttype;
std::string gene_id, tr_id;
while (in.good()) {
in >> type;
if (type == "GENE") {
GeneModel model;
char strand = '?';
in >> gene_id>> model.name >> model.chr >> gtype >> strand >> model.start >> model.stop;
model.id = gene_id;
model.strand = charToStrand(strand);
if (options.ignoreProtein) {
model.type = BioType::PROTEIN;
} else {
model.type = stringToType(gtype);
}
in >> line; // rest of transcripts, ignore for now
transcriptome.genes.insert({gene_id, std::move(model)});
} else if (type == "TRANSCRIPT") {
TranscriptModel model;
char strand = '?';
in >> tr_id >> gene_id >> model.chr >> ttype >> strand >> model.start >> model.stop;
model.id = tr_id;
model.strand = charToStrand(strand);
if (options.ignoreProtein) {
model.type = BioType::PROTEIN;
} else {
model.type = stringToType(ttype);
}
in >> line;
std::stringstream strline(line);
while (std::getline(strline, segment, ';')) {
ExonModel ex;
ex.chr = model.chr;
ex.strand = model.strand;
int p = segment.find(',');
ex.start = std::stoi(segment.substr(0,p));
ex.stop = std::stoi(segment.substr(p+1));
model.exons.push_back(std::move(ex));
}
auto it = transcriptome.genes.find(gene_id);
if (it != transcriptome.genes.end()) {
GeneModel &gene_model = it->second;
gene_model.transcripts.insert({tr_id, std::move(model)});
transcriptome.trxToGeneId.insert({tr_id, gene_id});
} else {
std::cerr << "unknown gene id " << gene_id << std::endl;
}
} else {
std::getline(in, line); // read until end of line
}
}
}
void parseFasta(Transcriptome &transcriptome, const std::string &fasta_fn) {
seqan::SeqFileIn seqFileIn(seqan::toCString(fasta_fn));
seqan::CharString id;
seqan::CharString seq;
while(!atEnd(seqFileIn)) {
seqan::readRecord(id,seq,seqFileIn);
std::string name = std::string(seqan::toCString(id));
size_t sp = name.find(' ');
size_t pipe = name.find('|');
seqan::toUpper(seq);
transcriptome.seqs.insert({name.substr(0,std::min(sp,pipe)), std::move(seq)});
}
}
void parseGTF(Transcriptome &transcriptome, const std::string >f_fn, const ProgramOptions& options) {
seqan::GffFileIn gtf(gtf_fn.c_str());
seqan::GffRecord record;
int n = 0;
while(!seqan::atEnd(gtf)) {
n++;
seqan::readRecord(record,gtf);
if (record.type == "gene") {
GeneModel model;
std::string gene_version;
for (int i = 0; i < length(record.tagNames); i++) {
if (record.tagNames[i] == "gene_id") {
model.id = seqan::toCString(record.tagValues[i]);
}
if (record.tagNames[i] == "gene_name") {
model.name = seqan::toCString(record.tagValues[i]);
}
if (record.tagNames[i] == "gene_biotype" || record.tagNames[i] == "gene_type") {
std::string val = std::string(seqan::toCString(record.tagValues[i]));
//auto &val = record.tagValues[i];
if (val == "protein_coding") {
model.type = BioType::PROTEIN;
} else if (val.find("pseudogene") != std::string::npos) {
model.type = BioType::PSEUDO;
} else {
model.type = BioType::OTHER;
}
}
if (record.tagNames[i] == "gene_version") {
gene_version = std::string(seqan::toCString(record.tagValues[i]));
}
}
if (!gene_version.empty() && model.id.find('.') == std::string::npos) {
model.id += "." + gene_version;
}
if (model.name.empty()) {
model.name = model.id;
}
if (options.ignoreProtein) {
model.type = BioType::PROTEIN;
}
model.chr = seqan::toCString(record.ref);
model.start = record.beginPos;
model.stop = record.endPos;
if (record.strand == '+') {
model.strand = Strandedness::FORWARD;
} else if (record.strand == '-') {
model.strand = Strandedness::REVERSE;
} else {
model.strand = Strandedness::UNKNOWN;
}
transcriptome.genes.insert({model.id,std::move(model)});
} else if (record.type == "transcript") {
TranscriptModel model;
bool bioTypeSet = false;
model.type = BioType::OTHER;
std::string gene_id, gene_version, txp_version;
for (int i = 0; i < length(record.tagNames); i++) {
if (record.tagNames[i] == "gene_id") {
gene_id = seqan::toCString(record.tagValues[i]);
} else if (record.tagNames[i] == "transcript_id") {
model.id = seqan::toCString(record.tagValues[i]);
}
if (record.tagNames[i] == "transcript_biotype" || record.tagNames[i] == "transcript_type") {
std::string val = std::string(seqan::toCString(record.tagValues[i]));
//auto &val = record.tagValues[i];
if (val == "protein_coding") {
model.type = BioType::PROTEIN;
} else if (val.find("pseudogene") != std::string::npos) {
model.type = BioType::PSEUDO;
}
bioTypeSet = true;
}
if (record.tagNames[i] == "gene_version") {
gene_version = std::string(seqan::toCString(record.tagValues[i]));
}
if (record.tagNames[i] == "transcript_version") {
txp_version = std::string(seqan::toCString(record.tagValues[i]));
}
}
if (!gene_version.empty() && gene_id.find('.') == std::string::npos) {
gene_id += "." + gene_version;
}
if (!txp_version.empty() && model.id.find('.') == std::string::npos) {
model.id += "." + txp_version;
}
if (!bioTypeSet) {
std::string source = seqan::toCString(record.source);
// we need this for Ensembl versions 76 and below,
// transcripts don't have transcript_[bio]type set but store this info in the source name ?!?
if (source == "protein_coding") {
model.type = BioType::PROTEIN;
} else if (source.find("pseudogene") != std::string::npos) {
model.type = BioType::PSEUDO;
}
}
if (options.ignoreProtein) {
model.type = BioType::PROTEIN;
}
model.chr = seqan::toCString(record.ref);
model.start = record.beginPos;
model.stop = record.endPos;
if (record.strand == '+') {
model.strand = Strandedness::FORWARD;
} else if (record.strand == '-') {
model.strand = Strandedness::REVERSE;
} else {
model.strand = Strandedness::UNKNOWN;
}
assert(!gene_id.empty());
auto it = transcriptome.genes.find(gene_id);
assert(transcriptome.trxToGeneId.find(model.id) == transcriptome.trxToGeneId.end());
transcriptome.trxToGeneId.insert({model.id,gene_id});
assert(it != transcriptome.genes.end());
it->second.transcripts.insert({model.id,model});
} else if (record.type == "exon") {
ExonModel model;
std::string trx_id;
std::string gene_id;
std::string txp_version, gene_version;
for (int i = 0; i < length(record.tagNames); i++) {
if (record.tagNames[i] == "gene_id") {
gene_id = seqan::toCString(record.tagValues[i]);
} else if (record.tagNames[i] == "transcript_id") {
trx_id = seqan::toCString(record.tagValues[i]);
}
if (record.tagNames[i] == "gene_version") {
gene_version = std::string(seqan::toCString(record.tagValues[i]));
}
if (record.tagNames[i] == "transcript_version") {
txp_version = std::string(seqan::toCString(record.tagValues[i]));
}
}
if (!gene_version.empty() && gene_id.find('.') == std::string::npos) {
gene_id += "." + gene_version;
}
if (!txp_version.empty() && trx_id.find('.') == std::string::npos) {
trx_id += "." + txp_version;
}
model.chr = seqan::toCString(record.ref);
model.start = record.beginPos;
model.stop = record.endPos;
if (record.strand == '+') {
model.strand = Strandedness::FORWARD;
} else if (record.strand == '-') {
model.strand = Strandedness::REVERSE;
} else {
model.strand = Strandedness::UNKNOWN;
}
auto g_it = transcriptome.genes.find(gene_id);
assert(g_it != transcriptome.genes.end());
auto t_it = g_it->second.transcripts.find(trx_id);
assert(t_it != g_it->second.transcripts.end());
t_it->second.exons.push_back(std::move(model));
}
}
std::cerr << "GTF file contains " << transcriptome.genes.size() << " genes and " << transcriptome.trxToGeneId.size() << " transcripts" << std::endl;
/* for (auto it : transcriptome.genes) {
const auto &gene = it.second;
//std::cout << "Gene = " << gene.id << ", name = " << gene.name << ", pos = " << gene.chr << ":" << (gene.start+1) << "-" << gene.stop << std::endl;
}*/
}
#endif // GENE_MODEL_HPP
|