| 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 | // this file contains functions to prettyprint the contents of |
|---|
| 30 | // the alignment matrix for debugging purposes |
|---|
| 31 | |
|---|
| 32 | #include <iostream> |
|---|
| 33 | #include <fstream> |
|---|
| 34 | #include "mesh.h" |
|---|
| 35 | |
|---|
| 36 | /* writes dot format commands to out that represent the given sequence |
|---|
| 37 | * care is taken to display the nodes either vertically or horizontally |
|---|
| 38 | */ |
|---|
| 39 | template<typename SEQUENCE> |
|---|
| 40 | void |
|---|
| 41 | draw_axis(SEQUENCE &s, |
|---|
| 42 | typename SEQUENCE::iterator &begin, |
|---|
| 43 | typename SEQUENCE::iterator &end, |
|---|
| 44 | unsigned int from, unsigned int to, std::ostream& out, |
|---|
| 45 | bool horizontal) { |
|---|
| 46 | const char *nname = (horizontal?"h":"v"); |
|---|
| 47 | |
|---|
| 48 | typename SEQUENCE::iterator it = s.begin(); |
|---|
| 49 | begin = s.begin(); |
|---|
| 50 | end = s.end(); |
|---|
| 51 | |
|---|
| 52 | // find parts to show; |
|---|
| 53 | while (it != end && it->getPosition() < from) { |
|---|
| 54 | ++it; |
|---|
| 55 | } |
|---|
| 56 | begin=it; |
|---|
| 57 | while (it != end && it->getPosition() < to) { |
|---|
| 58 | ++it; |
|---|
| 59 | } |
|---|
| 60 | end=it; |
|---|
| 61 | |
|---|
| 62 | // print node labels |
|---|
| 63 | for (it = begin; it != end && it->getPosition() < to; ++it) { |
|---|
| 64 | int mnode_id = get_node_id(s,it); |
|---|
| 65 | out << nname << mnode_id << " [label=\"" |
|---|
| 66 | << *it << "\",style=solid]; " << std::endl; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | // cluster master nodes, link with edges |
|---|
| 70 | out << "{ edge [style=invis]; " << std::endl; |
|---|
| 71 | if (horizontal) { |
|---|
| 72 | out << " rank=min;" << std:: endl; |
|---|
| 73 | } |
|---|
| 74 | out << "origin -> "; |
|---|
| 75 | for (it = begin; it != end && it->getPosition() < to-1; ++it) { |
|---|
| 76 | out << nname << get_node_id(s,it) << " -> "; |
|---|
| 77 | } |
|---|
| 78 | out << nname << get_node_id(s,it) << std::endl |
|---|
| 79 | << "}" << std::endl; |
|---|
| 80 | |
|---|
| 81 | // show master edges |
|---|
| 82 | out << "{ edge [style=solid, constraint=true]; " |
|---|
| 83 | << std::endl; |
|---|
| 84 | for (it = begin; it != end; ++it) { |
|---|
| 85 | int mnode_id = get_node_id(s,it); |
|---|
| 86 | |
|---|
| 87 | typename SEQUENCE::pn_iterator |
|---|
| 88 | pit = it.next_begin(), |
|---|
| 89 | pit_end = it.next_end(); |
|---|
| 90 | |
|---|
| 91 | for (; pit != pit_end; ++pit) { |
|---|
| 92 | if (pit->getPosition() < to) { |
|---|
| 93 | out << nname << mnode_id << |
|---|
| 94 | " -> " << nname << get_node_id(s,pit) << std::endl; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | out << "}" << std::endl; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | template<typename MESH> |
|---|
| 102 | void |
|---|
| 103 | mesh_to_svg(MESH& mesh, unsigned int from, unsigned int to, std::ostream& out) { |
|---|
| 104 | out << "digraph {" << std::endl |
|---|
| 105 | // << "graph [rankdir=LR]; " << std::endl |
|---|
| 106 | << "node [style=invis]; " << std::endl |
|---|
| 107 | << "origin [style=invis]; " << std::endl |
|---|
| 108 | ; |
|---|
| 109 | |
|---|
| 110 | using midx_type = typename MESH::master_idx_type; |
|---|
| 111 | using sidx_type = typename MESH::slave_idx_type; |
|---|
| 112 | |
|---|
| 113 | typename MESH::master_type::iterator mit, mit_end, mit_begin; |
|---|
| 114 | draw_axis(mesh._master, mit_begin, mit_end, from, to, out, false); |
|---|
| 115 | |
|---|
| 116 | typename MESH::slave_type::iterator sit, sit_end, sit_begin; |
|---|
| 117 | draw_axis(mesh._slave, sit_begin, sit_end, from, to, out, true); |
|---|
| 118 | |
|---|
| 119 | // print node labels |
|---|
| 120 | for (mit = mit_begin; mit != mit_end; ++mit) { |
|---|
| 121 | midx_type midx = get_node_id(mesh._master, mit); |
|---|
| 122 | for (sit = sit_begin; sit != sit_end; ++sit) { |
|---|
| 123 | sidx_type sidx = get_node_id(mesh._slave, sit); |
|---|
| 124 | out << "f_" << midx << "_" << sidx << " [label=<<TABLE BORDER=\"0\"" |
|---|
| 125 | << R"( CELLBORDER="1" CELLSPACING="0">)" |
|---|
| 126 | << "<TR><TD>" << -mesh(midx,sidx).value |
|---|
| 127 | << " (" << mesh(midx,sidx).value - |
|---|
| 128 | mesh(mesh(midx,sidx).value_midx, |
|---|
| 129 | mesh(midx,sidx).value_sidx).value << ")" |
|---|
| 130 | << "</TD></TR><TR><TD>" << -mesh(midx,sidx).gapm_val |
|---|
| 131 | << "/" << -mesh(midx,sidx).gaps_val |
|---|
| 132 | //<< "|" << mesh(midx,sidx).gaps_max |
|---|
| 133 | << "</TD></TR><TR><TD>" << *mit |
|---|
| 134 | << "/" << *sit |
|---|
| 135 | << "</TD></TR></TABLE>>];" << std:: endl; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | // link horizontally |
|---|
| 140 | for (mit = mit_begin; mit != mit_end; ++mit) { |
|---|
| 141 | midx_type midx = get_node_id(mesh._master, mit); |
|---|
| 142 | out << "{ rank=same; edge [style=invis]; v" << midx; |
|---|
| 143 | for (sit = sit_begin; sit != sit_end; ++sit) { |
|---|
| 144 | sidx_type sidx = get_node_id(mesh._slave, sit); |
|---|
| 145 | out << " -> " << "f_" << midx << "_" << sidx; |
|---|
| 146 | } |
|---|
| 147 | out << "}" << std::endl; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | // link vertically |
|---|
| 151 | for (sit = sit_begin; sit != sit_end; ++sit) { |
|---|
| 152 | sidx_type sidx = get_node_id(mesh._slave, sit); |
|---|
| 153 | out << "{ edge [style=invis]; h" << sidx; |
|---|
| 154 | for (mit = mit_begin; mit != mit_end; ++mit) { |
|---|
| 155 | midx_type midx = get_node_id(mesh._master, mit); |
|---|
| 156 | |
|---|
| 157 | out << " -> " << "f_" << midx << "_" << sidx; |
|---|
| 158 | } |
|---|
| 159 | out << "}" << std::endl; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | out << "edge [style=solid,constraint=true]; " << endl; |
|---|
| 163 | |
|---|
| 164 | for (sit = sit_begin; sit != sit_end; ++sit) { |
|---|
| 165 | sidx_type sidx = get_node_id(mesh._slave, sit); |
|---|
| 166 | for (mit = mit_begin; mit != mit_end; ++mit) { |
|---|
| 167 | midx_type midx = get_node_id(mesh._master, mit); |
|---|
| 168 | midx_type tgt_midx = mesh(midx,sidx).value_midx; |
|---|
| 169 | sidx_type tgt_sidx = mesh(midx,sidx).value_sidx; |
|---|
| 170 | if (mesh._master.getById(tgt_midx).getPosition() >= from && |
|---|
| 171 | mesh._slave.getById(tgt_sidx).getPosition() >= from) { |
|---|
| 172 | out << "f_" << midx << "_" << sidx |
|---|
| 173 | << " -> " |
|---|
| 174 | << "f_" << mesh(midx,sidx).value_midx |
|---|
| 175 | << "_" << mesh(midx,sidx).value_sidx |
|---|
| 176 | << ";" << endl; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | out << "}" << std::endl; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | template<typename MESH> |
|---|
| 185 | void |
|---|
| 186 | mesh_to_svg(MESH& mesh, unsigned int from, unsigned int to, const char* outfile) { |
|---|
| 187 | std::ofstream out(outfile); |
|---|
| 188 | if (out.bad()) { |
|---|
| 189 | std::cerr << "unable to open file " << outfile << std::endl; |
|---|
| 190 | return; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | mesh_to_svg(mesh, from, to, out); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | struct default_weight { |
|---|
| 200 | int operator[](int /*unused*/) { return 1; } |
|---|
| 201 | }; |
|---|
| 202 | |
|---|
| 203 | template<typename L, typename R> |
|---|
| 204 | std::pair<float,float> |
|---|
| 205 | seq_compare(L& left, R& right) { |
|---|
| 206 | default_weight w; |
|---|
| 207 | return seq_compare(left,right,w); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | template<typename L, typename R, typename W> |
|---|
| 212 | std::pair<float,float> |
|---|
| 213 | seq_compare(L& left, R& right, W& weight) { |
|---|
| 214 | typename L::iterator l_it = left.begin(), l_end = left.end(); |
|---|
| 215 | typename R::iterator r_it = right.begin(), r_end = right.end(); |
|---|
| 216 | |
|---|
| 217 | float mismatches = 0; |
|---|
| 218 | float matches = 0; |
|---|
| 219 | |
|---|
| 220 | if (left.size() != right.size()) { |
|---|
| 221 | std::cout << "seq_compare: length differs by " |
|---|
| 222 | << left.size()-right.size() << std::endl; |
|---|
| 223 | } |
|---|
| 224 | if (left.getBases() != right.getBases()) { |
|---|
| 225 | std::cout << "seq_compare: bases differ" << std::endl; |
|---|
| 226 | for(;l_it != l_end && r_it != r_end; ++l_it, ++r_it) { |
|---|
| 227 | if(l_it->getBase() != r_it->getBase()) { |
|---|
| 228 | std::cout << *l_it << " " << *r_it << std::endl; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | while(l_it != l_end && r_it != r_end) { |
|---|
| 234 | int lpos = l_it->getPosition(); |
|---|
| 235 | int rpos = r_it->getPosition(); |
|---|
| 236 | if (lpos < rpos) { |
|---|
| 237 | mismatches += weight[rpos]; |
|---|
| 238 | ++l_it; |
|---|
| 239 | } else if ( rpos < lpos ) { |
|---|
| 240 | mismatches += weight[rpos]; |
|---|
| 241 | ++r_it; |
|---|
| 242 | } else { // rpos <=> lops |
|---|
| 243 | if (l_it->getBase() != r_it->getBase()) { |
|---|
| 244 | mismatches += weight[rpos]; |
|---|
| 245 | } else { |
|---|
| 246 | matches += weight[rpos]; |
|---|
| 247 | } |
|---|
| 248 | ++r_it; |
|---|
| 249 | ++l_it; |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | return std::make_pair(mismatches,matches); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | /* |
|---|
| 258 | |
|---|
| 259 | digraph G { |
|---|
| 260 | graph [rankdir=TD] |
|---|
| 261 | edge [style=invis]; |
|---|
| 262 | o [style=invis]; |
|---|
| 263 | subgraph cluserHead{ |
|---|
| 264 | rank=min; |
|---|
| 265 | o x1 x2 x3 x4 x5 |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | subgraph cluserLeft{ |
|---|
| 269 | o -> y1 -> y2 -> y3 -> y4 -> y5 |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | f11 [label=<<TABLE BORDER="0"><TR><TD COLSPAN="2">6</TD></TR><TR><TD>3</TD><TD>2</TD></TR></TABLE>>]; |
|---|
| 273 | |
|---|
| 274 | { rank=same; y1 -> f11 -> f21 -> f31 -> f41 -> f51 } |
|---|
| 275 | { rank=same; y2 -> f12 -> f22 -> f32 -> f42 -> f52 } |
|---|
| 276 | { rank=same; y3 -> f13 -> f23 -> f33 -> f43 -> f53 } |
|---|
| 277 | { rank=same; y4 -> f14 -> f24 -> f34 -> f44 -> f54 } |
|---|
| 278 | { rank=same; y5 -> f15 -> f25 -> f35 -> f45 -> f55 } |
|---|
| 279 | |
|---|
| 280 | { x1 -> f11 -> f12 -> f13 -> f14 -> f15 } |
|---|
| 281 | { x2 -> f21 -> f22 -> f23 -> f24 -> f25 } |
|---|
| 282 | { x3 -> f31 -> f32 -> f33 -> f34 -> f35 } |
|---|
| 283 | { x4 -> f41 -> f42 -> f43 -> f44 -> f45 } |
|---|
| 284 | { x5 -> f51 -> f52 -> f53 -> f54 -> f55 } |
|---|
| 285 | |
|---|
| 286 | edge [style=solid]; |
|---|
| 287 | { edge [constraint=false]; |
|---|
| 288 | f22 -> f11; |
|---|
| 289 | f45 -> f22; |
|---|
| 290 | } |
|---|
| 291 | */ |
|---|