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
|
/**
>HEADER
Copyright (c) 2013 Rob Patro robp@cs.cmu.edu
This file is part of Sailfish.
Sailfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Sailfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Sailfish. If not, see <http://www.gnu.org/licenses/>.
<HEADER
**/
#ifndef TRANSCRIPT_GENE_MAP_HPP
#define TRANSCRIPT_GENE_MAP_HPP
// Allows for the serialization of this class
#include <cereal/archives/binary.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/types/string.hpp>
#include <algorithm>
#include <unordered_map>
#include <vector>
class TranscriptGeneMap {
using Index = size_t;
using Size = size_t;
using NameVector = std::vector<std::string>;
using IndexVector = std::vector<size_t>;
using IndexVectorList = std::vector<std::vector<size_t>>;
private:
NameVector _transcriptNames;
NameVector _geneNames;
IndexVector _transcriptsToGenes;
IndexVectorList _genesToTranscripts;
bool _haveReverseMap;
void _computeReverseMap() {
_genesToTranscripts.resize( _geneNames.size(), {});
Index transcriptID = 0;
size_t maxNumTrans = 0;
Index maxGene;
for ( transcriptID = 0; transcriptID < _transcriptsToGenes.size(); ++transcriptID ) {
_genesToTranscripts[ _transcriptsToGenes[transcriptID] ].push_back( transcriptID );
if ( maxNumTrans < _genesToTranscripts[ _transcriptsToGenes[transcriptID] ].size() ) {
maxNumTrans = _genesToTranscripts[ _transcriptsToGenes[transcriptID] ].size();
maxGene = _transcriptsToGenes[transcriptID];
}
}
std::cerr << "max # of transcripts in a gene was " << maxNumTrans << " in gene " << _geneNames[maxGene] << "\n";
}
friend class cereal::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar(_transcriptNames, _geneNames, _transcriptsToGenes,
_genesToTranscripts, _haveReverseMap);
}
public:
TranscriptGeneMap() :
_transcriptNames(NameVector()), _geneNames(NameVector()),
_transcriptsToGenes(IndexVector()), _haveReverseMap(false) {}
TranscriptGeneMap( const NameVector &transcriptNames,
const NameVector &geneNames,
const IndexVector &transcriptsToGenes ) :
_transcriptNames(transcriptNames), _geneNames(geneNames),
_transcriptsToGenes(transcriptsToGenes), _haveReverseMap(false) {}
TranscriptGeneMap(const TranscriptGeneMap& other) = default;
TranscriptGeneMap& operator=(const TranscriptGeneMap& other) = default;
Index INVALID { std::numeric_limits<Index>::max() };
Index findTranscriptID( const std::string &tname ) {
using std::distance;
using std::lower_bound;
auto it = lower_bound( _transcriptNames.begin(), _transcriptNames.end(), tname );
return ( it == _transcriptNames.end() ) ? INVALID : ( distance(_transcriptNames.begin(), it) );
}
Size numTranscripts() {
return _transcriptNames.size();
}
Size numGenes() {
return _geneNames.size();
}
bool needReverse() {
if ( _haveReverseMap ) {
return false;
} else {
_computeReverseMap();
return true;
}
}
const IndexVector &transcriptsForGene( Index geneID ) {
return _genesToTranscripts[geneID];
}
inline std::string nameFromGeneID( Index geneID ) {
return _geneNames[geneID];
}
inline Index gene( Index transcriptID ) {
return _transcriptsToGenes[transcriptID];
}
inline std::string geneName( Index transcriptID ) {
return _geneNames[_transcriptsToGenes[transcriptID]];
}
inline std::string geneName (const std::string& transcriptName,
bool complain=true) {
auto tid = findTranscriptID(transcriptName);
if (tid != INVALID) {
return geneName(tid);
} else {
std::cerr << "WARNING: couldn't find transcript named ["
<< transcriptName << "]; returning transcript "
<< " as it's own gene\n";
return transcriptName;
}
}
inline std::string transcriptName( Index transcriptID ) {
return _transcriptNames[transcriptID];
}
};
#endif // TRANSCRIPT_GENE_MAP_HPP
|