1+ #include " MSA.h"
2+
3+ SubMSA::SubMSA () {}
4+ SubMSA::SubMSA (size_t a) : id(a), members({ a }) {}
5+ SubMSA::SubMSA (size_t a, size_t b) : members({ a, b }) {}
6+
7+ void SubMSA::pushMember (size_t other) {
8+ members.insert (members.end (), other);
9+ }
10+ void SubMSA::update (const SubMSA &other) {
11+ members.clear ();
12+ members.assign (other.members .begin (), other.members .end ());
13+ profile_aa = other.profile_aa ;
14+ profile_ss = other.profile_ss ;
15+ mask = other.mask ;
16+ }
17+ void SubMSA::concat (const SubMSA &other) {
18+ members.insert (members.end (), other.members .begin (), other.members .end ());
19+ }
20+ void SubMSA::concat (const std::vector<size_t > &other) {
21+ members.insert (members.end (), other.begin (), other.end ());
22+ }
23+
24+ MSAContainer::MSAContainer () {}
25+ MSAContainer::MSAContainer (size_t n) : dbKeys(n), dbIdToSubMSAVec(n, n), cigars_aa(n), cigars_ss(n) {}
26+
27+ std::vector<SubMSA>::iterator MSAContainer::begin () {
28+ return data.begin ();
29+ }
30+ std::vector<SubMSA>::iterator MSAContainer::end () {
31+ return data.end ();
32+ }
33+ std::vector<SubMSA>::const_iterator MSAContainer::begin () const {
34+ return data.begin ();
35+ }
36+ std::vector<SubMSA>::const_iterator MSAContainer::end () const {
37+ return data.end ();
38+ }
39+
40+ SubMSA& MSAContainer::operator [](size_t index) {
41+ return data[index];
42+ }
43+
44+ SubMSA& MSAContainer::operator [](const std::vector<SubMSA>::iterator& it) {
45+ return *it;
46+ }
47+
48+ SubMSA& MSAContainer::back () {
49+ return data.back ();
50+ }
51+
52+ size_t MSAContainer::size () const {
53+ return data.size ();
54+ }
55+
56+ void MSAContainer::add (size_t index) {
57+ data.emplace_back (index);
58+ dbIdToSubMSAVec[index] = data.size () - 1 ;
59+ }
60+
61+ void MSAContainer::add (const SubMSA &msa) {
62+ data.push_back (msa);
63+ for (size_t i = 0 ; i < msa.members .size (); i++) {
64+ dbIdToSubMSAVec[msa.members [i]] = data.size () - 1 ;
65+ }
66+ }
67+
68+ void MSAContainer::remove (std::vector<size_t > &toRemove) {
69+ std::sort (toRemove.begin (), toRemove.end (), std::greater<int >());
70+ for (size_t index : toRemove) {
71+ data.erase (data.begin () + index);
72+ }
73+ for (size_t i = 0 ; i < data.size (); i++) {
74+ const SubMSA &msa = data[i];
75+ for (size_t member : msa.members ) {
76+ dbIdToSubMSAVec[member] = i;
77+ }
78+ }
79+ }
80+
81+ void MSAContainer::addStructure (size_t id, unsigned int key, size_t length, const char * aa, const char * ss) {
82+ for (size_t j = 0 ; j < length; j++) {
83+ cigars_aa[id].emplace_back (aa[j]);
84+ cigars_ss[id].emplace_back (ss[j]);
85+ }
86+ dbKeys[id] = key;
87+ }
88+
89+
90+ // Merge SubMSA of db id b into SubMSA of db id a
91+ // Returns index of updated SubMSA of db id a
92+ size_t MSAContainer::mergeInto (size_t a, size_t b) {
93+ size_t aIdx = dbIdToSubMSAVec[a];
94+ size_t bIdx = dbIdToSubMSAVec[b];
95+ if (bIdx == cigars_aa.size ()) {
96+ // b isn't a profile
97+ data[aIdx].pushMember (b);
98+ dbIdToSubMSAVec[b] = aIdx;
99+ } else {
100+ data[aIdx].concat (data[bIdx]);
101+ for (size_t i = 0 ; i < data[bIdx].members .size (); i++) {
102+ size_t member = data[bIdx].members [i];
103+ dbIdToSubMSAVec[member] = aIdx;
104+ }
105+ }
106+ return aIdx;
107+ }
108+
109+ // Update the container with newly created SubMSAs, remove stale ones
110+ // 1: new submsa --> add to msa
111+ // 2: one profile, one structure -> add to profile
112+ // 3: both profile -> merge into query
113+ // cases 2 and 3 always identical since profile always made query
114+ void MSAContainer::update (const std::vector<SubMSA> &newMSAs, std::vector<size_t > &toRemove) {
115+ for (const SubMSA &sub : newMSAs) {
116+ add (sub);
117+ }
118+ if (toRemove.size () > 0 ) {
119+ remove (toRemove);
120+ }
121+ }
122+
123+
124+ bool MSAContainer::isProfile (size_t index) {
125+ return (dbIdToSubMSAVec[index] != cigars_aa.size ());
126+ }
0 commit comments