| 1 | /* |
|---|
| 2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
|---|
| 3 | |
|---|
| 4 | This file is part of SINA. |
|---|
| 5 | SINA is free software: you can redistribute it and/or modify it under |
|---|
| 6 | the terms of the GNU General Public License as published by the Free |
|---|
| 7 | Software Foundation, either version 3 of the License, or (at your |
|---|
| 8 | option) any later version. |
|---|
| 9 | |
|---|
| 10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 13 | for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | Additional permission under GNU GPL version 3 section 7 |
|---|
| 19 | |
|---|
| 20 | If you modify SINA, or any covered work, by linking or combining it |
|---|
| 21 | with components of ARB (or a modified version of that software), |
|---|
| 22 | containing parts covered by the terms of the |
|---|
| 23 | ARB-public-library-license, the licensors of SINA grant you additional |
|---|
| 24 | permission to convey the resulting work. Corresponding Source for a |
|---|
| 25 | non-source form of such a combination shall include the source code |
|---|
| 26 | for the parts of ARB used as well as that of the covered work. |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "log.h" |
|---|
| 30 | static const char* module_name = "mseq"; |
|---|
| 31 | static auto logger = sina::Log::create_logger(module_name); |
|---|
| 32 | |
|---|
| 33 | #include "mseq.h" |
|---|
| 34 | #include "timer.h" |
|---|
| 35 | #include "log.h" |
|---|
| 36 | #include <algorithm> |
|---|
| 37 | #include <limits> |
|---|
| 38 | #include <cmath> |
|---|
| 39 | |
|---|
| 40 | using std::vector; |
|---|
| 41 | using std::min; |
|---|
| 42 | |
|---|
| 43 | using namespace sina; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /* turns a number of sequences into a graph */ |
|---|
| 47 | mseq::mseq(std::vector<const cseq*>::iterator seqs_begin, |
|---|
| 48 | std::vector<const cseq*>::iterator seqs_end, |
|---|
| 49 | float weight) |
|---|
| 50 | : num_seqs(seqs_end-seqs_begin), bases_width(0) |
|---|
| 51 | { |
|---|
| 52 | if (seqs_begin != seqs_end) { |
|---|
| 53 | bases_width = (*seqs_begin)->getWidth(); |
|---|
| 54 | } |
|---|
| 55 | // Sanity check input |
|---|
| 56 | for (auto it = seqs_begin; it != seqs_end; ++it) { |
|---|
| 57 | if (bases_width != (*it)->getWidth()) { |
|---|
| 58 | logger->critical("Sequence {} ({}/{}): length = {} expected {}", |
|---|
| 59 | (*it)->getName(), it-seqs_begin, seqs_end - seqs_begin, |
|---|
| 60 | (*it)->getWidth(), bases_width); |
|---|
| 61 | throw std::runtime_error( |
|---|
| 62 | "Aligned sequences to be stored in mseq of differ in length!" |
|---|
| 63 | ); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | vector<cseq::const_iterator> citv; |
|---|
| 68 | citv.reserve(num_seqs); |
|---|
| 69 | vector<cseq::const_iterator> citv_end; |
|---|
| 70 | citv_end.reserve(num_seqs); |
|---|
| 71 | for (auto seq_it = seqs_begin; seq_it != seqs_end; ++seq_it) { |
|---|
| 72 | citv.push_back((*seq_it)->begin()); |
|---|
| 73 | citv_end.push_back((*seq_it)->end()); |
|---|
| 74 | } |
|---|
| 75 | aligned_base::idx_type min_next=0; |
|---|
| 76 | vector<iterator>::size_type nodes_size = std::numeric_limits<value_type::base_type>::max(); |
|---|
| 77 | vector<iterator> nodes(nodes_size); |
|---|
| 78 | |
|---|
| 79 | vector<iterator> last(num_seqs); |
|---|
| 80 | // iterate over alignment columns left to right |
|---|
| 81 | for (unsigned int i=0; i < bases_width; i++) { |
|---|
| 82 | if (min_next > i) { |
|---|
| 83 | continue; |
|---|
| 84 | } |
|---|
| 85 | min_next = std::numeric_limits<int>::max(); |
|---|
| 86 | nodes.assign(nodes_size, iterator()); |
|---|
| 87 | |
|---|
| 88 | // check all sequences for that column |
|---|
| 89 | for (unsigned int j = 0; j < num_seqs; j++) { |
|---|
| 90 | if (citv[j] != citv_end[j] && citv[j]->getPosition() == i) { |
|---|
| 91 | iterator newnode; |
|---|
| 92 | unsigned char base = citv[j]->getBase(); |
|---|
| 93 | if (nodes[base].isNull()) { |
|---|
| 94 | nodes[base] = newnode = insert(*citv[j]); |
|---|
| 95 | } else { |
|---|
| 96 | newnode = nodes[base]; |
|---|
| 97 | newnode->weight += 1.f; |
|---|
| 98 | } |
|---|
| 99 | if (!last[j].isNull()) { |
|---|
| 100 | link(last[j],newnode); |
|---|
| 101 | } |
|---|
| 102 | last[j]=newnode; |
|---|
| 103 | |
|---|
| 104 | ++citv[j]; |
|---|
| 105 | } |
|---|
| 106 | if (citv[j] != citv_end[j]) { |
|---|
| 107 | min_next=min(min_next,citv[j]->getPosition()); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | for (auto & node : nodes) { |
|---|
| 112 | if (!node.isNull()) { |
|---|
| 113 | node->weight = 1.0/(weight+1) + weight * (node->weight/num_seqs); |
|---|
| 114 | //std::min(20.0, -log(1 - (*it)->weight/num_seqs)); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /* |
|---|
| 121 | Local Variables: |
|---|
| 122 | mode:c++ |
|---|
| 123 | c-file-style:"stroustrup" |
|---|
| 124 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
|---|
| 125 | indent-tabs-mode:nil |
|---|
| 126 | fill-column:99 |
|---|
| 127 | End: |
|---|
| 128 | */ |
|---|
| 129 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
|---|
| 130 | |
|---|