extract_user_ct.cpp 45.6 KB
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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

#include <fstream>
#include <iostream>
#include <regex>
#include <sys/stat.h>
#include <boost/algorithm/string.hpp>

#include "extract_user_ct.h"
#include "utils.h"


// Check if a vector of string are integers >= 1 and <= l
bool checkBasePairIndex(std::vector < std::string > v, uint l) {
    bool res = true;
    int e;
    for(size_t i = 0, size = v.size(); i != size and res; i++) {
        try {
            e = std::stoi(v[i]);
            if (e < 1 or e > int(l)) {
                res = false;
            }
        } catch (std::string e) {
           throw(e);
        }
    }
    return res;
}

// Check if a vector of string are integers >= 0 and <= l
bool checkIndex(std::vector < std::string > v, uint l) {
    bool res = true;
    int e;
    for(size_t i = 0, size = v.size(); i != size and res; i++) {
        try {
            e = std::stoi(v[i]);
            if (e < 0 or e > l)
                res = false;
        } catch (std::string e) {
            throw (e);
        }
    }
    return res;
}

// Check if a vector of string are integers >= 0 and <= l -2
bool checkLength(std::vector < std::string > v, uint l) {
    bool res = true;
    int e;
    for(size_t i = 0, size = v.size(); i != size and res; i++) {
        try {
            e = std::stoi(v[i]);
            if (e < 0 or e > l-2)
                res = false;
        } catch (std::string e) {
            throw (e);
        }
    }
    return res;
}

// Add helix to ctHelix
void addHelix(int rna1, int rna2, const std::vector < std::string > &ctElements,
              const std::vector < Rna >& rnaList_, std::vector < Helix * >& ctHelix,
              std::string CTfile, int nLine) {
    int i, j, ibis, jbis, l1;

    try {
        if (rna1 != rna2 and int(ctElements.size()) >= 9) { // Name1 \s Name2 \s IDmotif \s i \s i' \s j' \s j \s length \s ic
            // Check base indexes and confidance index
            if (checkIndex({ctElements[3], ctElements[4]}, rnaList_[rna1].get_n_())
                    and checkIndex({ctElements[5], ctElements[6]}, rnaList_[rna2].get_n_())
                    and checkLength({ctElements[7]}, rnaList_[rna1].get_n_())
                    and checkLength({ctElements[7]}, rnaList_[rna2].get_n_())
                    and checkIC(ctElements[8])) {
                i = std::stoi(ctElements[3]);
                ibis = std::stoi(ctElements[4]);
                jbis = std::stoi(ctElements[5]);
                j = std::stoi(ctElements[6]);
                l1 = std::stoi(ctElements[7]);
                if (i < ibis and jbis < j) {
                    // there is an helix with no information
                    if(i == 0 and j == 0 and ibis == 0 and jbis == 0 and l1 == 0)
                        ctHelix.push_back(new Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                      rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_(), std::stoi(ctElements[8])));
                    else {
                        if (l1 != 0 and i != 0)
                            ibis = i+l1-1;
                        if (l1 != 0 and ibis != 0)
                            i = ibis-l1+1;
                        if (l1 != 0 and jbis != 0)
                            j = jbis+l1-1;
                        if (l1 != 0 and j != 0)
                            jbis = j-l1+1;
                        if (i != 0 and ibis != 0)
                            l1 = ibis-i+1;
                        if (jbis != 0 and j != 0)
                            l1 = j-jbis+1;
                        // there is an helix with all information
                        if(i != 0 and j != 0 and ibis != 0 and jbis != 0 and l1 != 0)
                            ctHelix.push_back(new Helix(int(rna1), int(rna2), i-1, ibis-1, j-1, jbis-1, l1,
                                                          rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_(), std::stoi(ctElements[8])));
                        // there is an helix with length information
                        else if (l1 != 0)
                            ctHelix.push_back(new Helix(int(rna1), int(rna2), -1, -1, -1, -1, l1,
                                                          rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_(), std::stoi(ctElements[8])));
                        else
                            throw (std::string("Missing information in indexes : either all indexes and length, or all indexes,"
                            " or all length sould be provided. Please look at the documentation."));
                    }
                } else {
                    throw (std::string("Rules i < i' and j' < j are not respected."));
                }
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else if (rna1 == rna2 and int(ctElements.size()) >= 8) { // Name1 \space IDmotif \space i \s i' \s j' \s j \s length \s ic
           // Check base indexes and confidance index
           if (checkIndex({ctElements[2], ctElements[3], ctElements[4], ctElements[5]},
                          rnaList_[rna1].get_n_())
                   and checkLength({ctElements[6]}, rnaList_[rna1].get_n_())
                   and checkIC(ctElements[7])) {
               i = std::stoi(ctElements[2]);
               ibis = std::stoi(ctElements[3]);
               jbis = std::stoi(ctElements[4]);
               j = std::stoi(ctElements[5]);
               l1 = std::stoi(ctElements[6]);
               if (i < ibis and ibis < jbis and jbis < j) {
                   // there is an helix with no information
                   if(i == 0 and j == 0 and ibis == 0 and jbis == 0 and l1 == 0)
                       ctHelix.push_back(new Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                     rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_(), std::stoi(ctElements[7])));
                   else {
                       if (l1 != 0 and i != 0)
                           ibis = i+l1-1;
                       if (l1 != 0 and ibis != 0)
                           i = ibis-l1+1;
                       if (l1 != 0 and jbis != 0)
                           j = jbis+l1-1;
                       if (l1 != 0 and j != 0)
                           jbis = j-l1+1;
                       if (i != 0 and ibis != 0)
                           l1 = ibis-i+1;
                       if (jbis != 0 and j != 0)
                           l1 = j-jbis+1;
                       // there is an helix with all information
                       if(i != 0 and j != 0 and ibis != 0 and jbis != 0 and l1 != 0)
                           ctHelix.push_back(new Helix(int(rna1), int(rna1), i-1, ibis-1, j-1, jbis-1, l1,
                                                         rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_(), std::stoi(ctElements[7])));
                       // there is an helix with length information
                       else if (l1 != 0)
                           ctHelix.push_back(new Helix(int(rna1), int(rna1), -1, -1, -1, -1, l1,
                                                         rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_(), std::stoi(ctElements[7])));
                       else {
                           throw (std::string("Missing information in indexes : either all indexes and length, or all indexes,"
                           " or all length sould be provided. Please look at the documentation."));
                       }
                   }
               } else {
                   throw (std::string("Rules i < i' and i' <j' and j' < j are not respected."));
               }
           } else {
               throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
           }
        } else {
           throw (std::string("The format is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in constraint file " + CTfile + ", line " + std::to_string(nLine) + ".\n" + e));
    }
}

// Add pseudoknot to ctPseudo
void addPseudo(int rna1, int rna2, const std::vector < std::string > &ctElements,
              const std::vector < Rna >& rnaList_, std::vector < Pseudoknot * >& ctPseudo,
              std::string CTfile, int nLine) {
    int i1, j1, ibis1, jbis1, i2, j2, ibis2, jbis2, l1, l2;

    try {
        if (rna1 != rna2 and int(ctElements.size()) >= 14) {
            // Name1 \s Name2 \s IDmotif \s i1 \s i1' \s j1' \s j1 \s i2 \s i2' \s j2' \s j2 \s length1 \s length2 \s ic
            // Check base indexes and confidance index
            if (checkIndex({ctElements[3], ctElements[4], ctElements[7], ctElements[8]}, rnaList_[rna1].get_n_())
                    and checkIndex({ctElements[5], ctElements[6], ctElements[9], ctElements[10]}, rnaList_[rna2].get_n_())
                    and checkLength({ctElements[11]}, rnaList_[rna1].get_n_())
                    and checkLength({ctElements[12]}, rnaList_[rna2].get_n_())
                    and checkIC(ctElements[13])) {
                i1 = std::stoi(ctElements[3]);
                ibis1 = std::stoi(ctElements[4]);
                jbis1 = std::stoi(ctElements[5]);
                j1 = std::stoi(ctElements[6]);
                i2 = std::stoi(ctElements[7]);
                ibis2 = std::stoi(ctElements[8]);
                jbis2 = std::stoi(ctElements[9]);
                j2 = std::stoi(ctElements[10]);
                l1 = std::stoi(ctElements[11]);
                l2 = std::stoi(ctElements[12]);
                if (i1 < ibis1 and jbis1 < j1 and i2 < ibis2 and jbis2 < j2) {
                    // there is a pseudoknot with no information
                    if(i1 == 0 and j1 == 0 and ibis1 == 0 and jbis1 == 0 and l1 == 0
                            and i2 == 0 and j2 == 0 and ibis2 == 0 and jbis2 == 0 and l2 == 0)
                        ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                          Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                                rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                          Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                                rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                          std::stoi(ctElements[13])));
                    else {
                        // Complete first helix
                        if (l1 != 0 and i1 != 0)
                            ibis1 = i1+l1-1;
                        if (l1 != 0 and ibis1 != 0)
                            i1 = ibis1-l1+1;
                        if (l1 != 0 and jbis1 != 0)
                            j1 = jbis1+l1-1;
                        if (l1 != 0 and j1 != 0)
                            jbis1 = j1-l1+1;
                        if (i1 != 0 and ibis1 != 0)
                            l1 = ibis1-i1+1;
                        if (jbis1 != 0 and j1 != 0)
                            l1 = j1-jbis1+1;

                        // Complete second helix
                        if (l2 != 0 and i2 != 0)
                            ibis2 = i2+l2-1;
                        if (l2 != 0 and ibis2 != 0)
                            i2 = ibis2-l2+1;
                        if (l2 != 0 and jbis2 != 0)
                            j2 = jbis2+l2-1;
                        if (l2 != 0 and j2 != 0)
                            jbis2 = j2-l2+1;
                        if (i2 != 0 and ibis2 != 0)
                            l2 = ibis2-i2+1;
                        if (jbis2 != 0 and j2 != 0)
                            l2 = j2-jbis2+1;
                        // there are two helices with all information
                        if(i1 != 0 and j1 != 0 and ibis1 != 0 and jbis1 != 0 and l1 != 0
                                and i2 != 0 and j2 != 0 and ibis2 != 0 and jbis2 != 0 and l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                              Helix(int(rna1), int(rna2), i1-1, ibis1-1, j1-1, jbis1-1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              Helix(int(rna1), int(rna2), i2-1, ibis2-1, j2-1, jbis2-1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              std::stoi(ctElements[13])));
                        // the first helix is full
                        else if(i1 != 0 and j1 != 0 and ibis1 != 0 and jbis1 != 0 and l1 != 0
                                and i2 == 0 and j2 == 0 and ibis2 == 0 and jbis2 == 0 and l2 == 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                              Helix(int(rna1), int(rna2), i1-1, ibis1-1, j1-1, jbis1-1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              std::stoi(ctElements[13])));
                        // the second helix is full
                        else if(i1 != 0 and j1 != 0 and ibis1 != 0 and jbis1 != 0 and l1 != 0
                                and i2 != 0 and j2 != 0 and ibis2 != 0 and jbis2 != 0 and l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              Helix(int(rna1), int(rna2), i2-1, ibis2-1, j2-1, jbis2-1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              std::stoi(ctElements[13])));
                        // there are length information
                        else if (l1 != 0 and l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              std::stoi(ctElements[13])));
                        else if (l1 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              std::stoi(ctElements[13])));
                        else if (l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna2),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              Helix(int(rna1), int(rna2), -1, -1, -1, -1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna2].get_seq_()),
                                                              std::stoi(ctElements[13])));
                        else
                            throw (std::string("Missing information in indexes : either all indexes and length, or all indexes,"
                                   " or all length sould be provided. Please look at the documentation."));
                    }
                } else {
                    throw (std::string("Rules i1 < i1' and j1' < j1 and i2 < i2' and j2' < j2 are not respected."));
                }
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else if (rna1 == rna2 and int(ctElements.size()) >= 13) {
            // Name1 \s IDmotif \s i1 \s i1' \s j1' \s j1 \s i2 \s i2' \s j2' \s j2 \s length1 \s length2 \s ic
            // Check base indexes and confidance index
            if (checkIndex({ctElements[2], ctElements[3], ctElements[6], ctElements[7]}, rnaList_[rna1].get_n_())
                    and checkIndex({ctElements[4], ctElements[5], ctElements[8], ctElements[9]}, rnaList_[rna1].get_n_())
                    and checkLength({ctElements[10]}, rnaList_[rna1].get_n_())
                    and checkLength({ctElements[11]}, rnaList_[rna1].get_n_())
                    and checkIC(ctElements[12])) {
                i1 = std::stoi(ctElements[2]);
                ibis1 = std::stoi(ctElements[3]);
                jbis1 = std::stoi(ctElements[4]);
                j1 = std::stoi(ctElements[5]);
                i2 = std::stoi(ctElements[6]);
                ibis2 = std::stoi(ctElements[7]);
                jbis2 = std::stoi(ctElements[8]);
                j2 = std::stoi(ctElements[9]);
                l1 = std::stoi(ctElements[10]);
                l2 = std::stoi(ctElements[11]);
                if (i1 < ibis1 and jbis1 < j1 and i2 < ibis2 and jbis2 < j2) {
                    // there is a pseudoknot with no information
                    if(i1 == 0 and j1 == 0 and ibis1 == 0 and jbis1 == 0 and l1 == 0
                            and i2 == 0 and j2 == 0 and ibis2 == 0 and jbis2 == 0 and l2 == 0)
                        ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                          Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                                rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                          Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                                rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                          std::stoi(ctElements[12])));
                    else {
                        // Complete first helix
                        if (l1 != 0 and i1 != 0)
                            ibis1 = i1+l1-1;
                        if (l1 != 0 and ibis1 != 0)
                            i1 = ibis1-l1+1;
                        if (l1 != 0 and jbis1 != 0)
                            j1 = jbis1+l1-1;
                        if (l1 != 0 and j1 != 0)
                            jbis1 = j1-l1+1;
                        if (i1 != 0 and ibis1 != 0)
                            l1 = ibis1-i1+1;
                        if (jbis1 != 0 and j1 != 0)
                            l1 = j1-jbis1+1;

                        // Complete second helix
                        if (l2 != 0 and i2 != 0)
                            ibis2 = i2+l2-1;
                        if (l2 != 0 and ibis2 != 0)
                            i2 = ibis2-l2+1;
                        if (l2 != 0 and jbis2 != 0)
                            j2 = jbis2+l2-1;
                        if (l2 != 0 and j2 != 0)
                            jbis2 = j2-l2+1;
                        if (i2 != 0 and ibis2 != 0)
                            l2 = ibis2-i2+1;
                        if (jbis2 != 0 and j2 != 0)
                            l2 = j2-jbis2+1;
                        // there are two helices with all information
                        if(i1 != 0 and j1 != 0 and ibis1 != 0 and jbis1 != 0 and l1 != 0
                                and i2 != 0 and j2 != 0 and ibis2 != 0 and jbis2 != 0 and l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                              Helix(int(rna1), int(rna1), i1-1, ibis1-1, j1-1, jbis1-1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              Helix(int(rna1), int(rna1), i2-1, ibis2-1, j2-1, jbis2-1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              std::stoi(ctElements[12])));
                        // the first helix is full
                        else if(i1 != 0 and j1 != 0 and ibis1 != 0 and jbis1 != 0 and l1 != 0
                                and i2 == 0 and j2 == 0 and ibis2 == 0 and jbis2 == 0 and l2 == 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                              Helix(int(rna1), int(rna1), i1-1, ibis1-1, j1-1, jbis1-1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              std::stoi(ctElements[12])));
                        // the second helix is full
                        else if(i1 != 0 and j1 != 0 and ibis1 != 0 and jbis1 != 0 and l1 != 0
                                and i2 != 0 and j2 != 0 and ibis2 != 0 and jbis2 != 0 and l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              Helix(int(rna1), int(rna1), i2-1, ibis2-1, j2-1, jbis2-1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              std::stoi(ctElements[12])));
                        // there are length information
                        else if (l1 != 0 and l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              std::stoi(ctElements[12])));
                        else if (l1 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, l1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              std::stoi(ctElements[12])));
                        else if (l2 != 0)
                            ctPseudo.push_back(new Pseudoknot(int(rna1), int(rna1),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, -1,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              Helix(int(rna1), int(rna1), -1, -1, -1, -1, l2,
                                                                    rnaList_[rna1].get_seq_(), rnaList_[rna1].get_seq_()),
                                                              std::stoi(ctElements[12])));
                        else
                            throw (std::string("Missing information in indexes : either all indexes and length, or all indexes,"
                                   " or all length sould be provided. Please look at the documentation."));
                    }
                } else {
                    throw (std::string("Rules i1 < i1' and i1' < j1' and j1' < j1 and i2 < i2' and i2' < j2' and j2' < j2 are not respected."));
                }
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else {
            throw (std::string("The format is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in the constraint file " + CTfile + ", line " +
                 std::to_string(nLine) + ".\n" + e));
    }
}

// Add internal loop to ctIntLoop
void addIntLoop(int rna1, const std::vector < std::string > &ctElements,
              const std::vector < Rna >& rnaList_, std::vector < Interloop * >& ctIntLoop,
              std::string CTfile, int nLine) {
    int i, j, ibis, jbis, l1, l2;
    try {
        if (int(ctElements.size()) >= 9) { // Name1 \space IDmotif \space i \s i' \s j' \s j \s length1 \s length2 \s ic
            // Check base indexes and confidance index
            if (checkIndex({ctElements[2], ctElements[3], ctElements[4], ctElements[5]},
                           rnaList_[rna1].get_n_())
                    and checkLength({ctElements[6], ctElements[7]}, rnaList_[rna1].get_n_())
                    and checkIC(ctElements[8])) {
                i = std::stoi(ctElements[2]);
                ibis = std::stoi(ctElements[3]);
                jbis = std::stoi(ctElements[4]);
                j = std::stoi(ctElements[5]);
                l1 = std::stoi(ctElements[6]);
                l2 = std::stoi(ctElements[7]);
                if (i < ibis and ibis < jbis and jbis < j) {
                    // there is an internal loop with no information
                    if(i == 0 and j == 0 and ibis == 0 and jbis == 0 and l1 == 0 and l2 == 0)
                        ctIntLoop.push_back(new Interloop (int(rna1), rnaList_[rna1].get_seq_(), -1, -1, -1, -1, -1, -1));
                    else {
                        if (l1 != 0 and i != 0)
                            ibis = i+l1+1;
                        if (l1 != 0 and ibis != 0)
                            i = ibis-l1-1;
                        if (l2 != 0 and jbis != 0)
                            j = jbis+l2+1;
                        if (l2 != 0 and j != 0)
                            jbis = j-l2-1;
                        if (i != 0 and ibis != 0)
                            l1 = ibis-i-1;
                        if (jbis != 0 and j != 0)
                            l2 = j-jbis-1;
                        // there is an internal loop with all information
                        if(i != 0 and j != 0 and ibis != 0 and jbis != 0 and l1 != 0 and l2 != 0)
                            ctIntLoop.push_back(new Interloop (int(rna1), rnaList_[rna1].get_seq_(),
                                                               i-1, ibis-1, j-1, jbis-1, l1, l2, std::stoi(ctElements[8])));
                        // there is an internal loop with length information
                        else if (l1 != 0 and l2 != 0)
                            ctIntLoop.push_back(new Interloop (int(rna1), rnaList_[rna1].get_seq_(),
                                                               -1, -1, -1, -1, l1, l2, std::stoi(ctElements[8])));
                        else
                            throw (std::string("Missing information in indexes : either all indexes and length, or all indexes,"
                            " or all length sould be provided. Please look at the documentation."));
                    }
                } else {
                    throw (std::string("Rules i < i' and i' < j' and j' < j are not respected."));
                }
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else {
            throw (std::string("The format is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in the constraint file " + CTfile + ", line " +
                 std::to_string(nLine) + ".\n" + e));
    }
}

// Add hairpin loop to ctHairLoop
void addHairLoop(int rna1, const std::vector < std::string > &ctElements,
                 const std::vector < Rna >& rnaList_, std::vector < Hairpinloop * >& ctHairLoop,
                 std::string CTfile, int nLine) {
    int i, j, l1;
    try {
        if (int(ctElements.size()) >= 5) { // Name1 \space IDmotif \space i \s j \s length \s ic
            // Check base indexes and confidance index
            if (checkIndex({ctElements[2], ctElements[3]}, rnaList_[rna1].get_n_())
                    and checkLength({ctElements[4]}, rnaList_[rna1].get_n_())
                    and checkIC(ctElements[5])) {
                i = std::stoi(ctElements[2]);
                j = std::stoi(ctElements[3]);
                l1 = std::stoi(ctElements[4]);
                if (i <= j) {
                    if(i == 0 and j == 0 and l1 == 0)
                        ctHairLoop.push_back(new Hairpinloop(int(rna1), rnaList_[rna1].get_seq_(), -1, -1, -1, std::stoi(ctElements[5])));
                    else if (i != 0 and j != 0 and l1 != 0)
                        ctHairLoop.push_back(new Hairpinloop(int(rna1), rnaList_[rna1].get_seq_(), i-1, j-1, l1, std::stoi(ctElements[5])));
                    else if (i == 0 and j == 0 and l1 != 0)
                        ctHairLoop.push_back(new Hairpinloop(int(rna1), rnaList_[rna1].get_seq_(), -1, -1, l1, std::stoi(ctElements[5])));
                    else if (i == 0 and l1 != 0)
                        ctHairLoop.push_back(new Hairpinloop(int(rna1), rnaList_[rna1].get_seq_(), j-l1-1, j-1, l1, std::stoi(ctElements[5])));
                    else if (j == 0 and l1 != 0)
                        ctHairLoop.push_back(new Hairpinloop(int(rna1), rnaList_[rna1].get_seq_(), i-1, i+l1, l1, std::stoi(ctElements[5])));
                    else if (l1 == 0)
                        ctHairLoop.push_back(new Hairpinloop(int(rna1), rnaList_[rna1].get_seq_(), i-1, j-1, j-i-1, std::stoi(ctElements[5])));
                    else
                        throw (std::string("Missing information in indexes : either all indexes and length, or all indexes,"
                        " or all length sould be provided. Please look at the documentation."));
                } else {
                    throw (std::string("Rule i < j is not respected."));
                }
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else {
            throw (std::string("The format is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in the constraint file " + CTfile + ", line " +
                 std::to_string(nLine) + ".\n" + e));
    }
}

// Add multijunction loop to ctMultiLoop
void addMultiLoop(int rna1, const std::vector < std::string > &ctElements,
                 const std::vector < Rna >& rnaList_, std::vector < Multiloop * >& ctMultiLoop,
                 std::string CTfile, int nLine) {
    std::vector < int > indexes;
    int ic;
    std::vector < std::pair < int, int > > ctMulti;
    bool cond = true;
    try {
        if (ctElements.size()%2 == 1) {
            for (size_t i = 2, size = ctElements.size(); i != size and cond; i++) {
                if (i != size-1) {
                    if (checkBasePairIndex({ctElements[i]}, rnaList_[rna1].get_n_())) {
                        indexes.push_back(std::stoi(ctElements[i]));
                    } else {
                        cond = false;
                    }
                } else if (checkIC(ctElements[i])) {
                    ic = std::stoi(ctElements[i]);
                } else {
                    cond = false;
                }
            }
            if (!cond) {
                throw (std::string("The format is not correct."));
            }
            // Recover base pair
            for(int i = 0, size = int(indexes.size())-1; i != size and cond; i++) {
                if (indexes[i] < indexes[i+1]) {
                    ctMulti.push_back(std::make_pair(indexes[i], indexes[i+1]));
                } else {
                    cond = false;
                }
            }
            // Add Multiloop
            if (ctMulti.empty())
                ctMultiLoop.push_back(new Multiloop(rna1, rnaList_[rna1].get_seq_(), std::vector < std::pair < int, int > > (), ic));
            else {
                ctMultiLoop.push_back(new Multiloop(rna1, rnaList_[rna1].get_seq_(), ctMulti, ic));
            }
        } else {
            throw (std::string("The format is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in the constraint file " + CTfile + ", line " +
                 std::to_string(nLine) + ".\n" + e));
    }
}

// Add base pair to ctApp and ctAppTwo
void addBp(int rna1, int rna2, const std::vector < std::string > &ctElements,
                 const std::vector < Rna >& rnaList_,
                 std::vector< std::tuple < std::pair < uint, uint >, std::pair < uint, uint >, uint > >& ctAppTwo,
                 std::vector< std::tuple < uint, uint, uint > >& ctApp,
                 std::string CTfile, int nLine) {
    try {
        if (rna1 != rna2 and int(ctElements.size()) >= 6) { // Name1 \space Name2 \space IDmotif \space i \s j \s ic
            // Check base indexes and confidance index
            if (checkBasePairIndex({ctElements[3]}, rnaList_[rna1].get_n_())
                    and checkIndex({ctElements[4]}, rnaList_[rna2].get_n_())
                    and checkIC(ctElements[5])) {
                if(std::stoi(ctElements[4]) == 0)
                    ctApp.push_back(std::make_tuple(rna1, std::stoi(ctElements[3])-1, std::stoi(ctElements[5])));
                else
                    ctAppTwo.push_back(std::make_tuple(std::make_pair(rna1, std::stoi(ctElements[3])-1),
                                                    std::make_pair(rna2, std::stoi(ctElements[4])-1),
                                                    std::stoi(ctElements[5])));
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else if ( rna1 == rna2 and int(ctElements.size()) >= 5) { // Name1 \space IDmotif \space i \s j \s ic
            // Check base indexes and confidance index
            if (checkBasePairIndex({ctElements[2]}, rnaList_[rna1].get_n_())
                    and checkIndex({ctElements[3]}, rnaList_[rna1].get_n_())
                    and checkIC(ctElements[4])) {
                if(std::stoi(ctElements[3]) == 0)
                    ctApp.push_back(std::make_tuple(rna1, std::stoi(ctElements[2])-1, std::stoi(ctElements[4])));
                else
                    ctAppTwo.push_back(std::make_tuple(std::make_pair(rna1, std::stoi(ctElements[2])-1),
                                                    std::make_pair(rna1, std::stoi(ctElements[3])-1),
                                                    std::stoi(ctElements[4])));
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else {
            throw (std::string("The format is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in the constraint file " + CTfile + ", line " +
                 std::to_string(nLine) + ".\n" + e));
    }
}

// Add alone pair to ctSingle
void addSingle(int rna1, const std::vector < std::string > &ctElements,
                 const std::vector < Rna >& rnaList_, std::vector < std::tuple < uint, uint, uint > >& ctSingle,
                 std::string CTfile, int nLine) {
    try {
        if (int(ctElements.size()) >= 5) { // Name1 \space IDmotif \space i \s ic
            // Check base indexes and confidance index
            if (checkBasePairIndex({ctElements[2]}, rnaList_[rna1].get_n_())
                    and checkIC(ctElements[3])) {
                ctSingle.push_back(std::make_tuple(rna1, std::stoi(ctElements[2])-1, std::stoi(ctElements[3])));
            } else {
                throw (std::string("Index of a base, the length or the confidence index is not correct. Should be between 0 and length of the RNA."));
            }
        } else {
            throw (std::string("The format of is not correct."));
        }
    } catch (std::string e) {
        throw (std::string("Error in the constraint file " + CTfile + ", line " +
                 std::to_string(nLine) + ".\n" + e));
    }
}

void extract_ct(const std::string CTfile, const std::vector < Rna >& rnaList_,
                std::vector< std::tuple < std::pair < uint, uint >, std::pair < uint, uint >, uint > >& ctAppTwo,
                std::vector< std::tuple < uint, uint, uint > >& ctApp,
                std::vector< std::tuple < uint, uint, uint > >& ctSingle,
                std::vector < Interloop * >& ctIntLoop,
                std::vector < Hairpinloop * >& ctHairLoop,
                std::vector < Helix * >& ctHelix,
                std::vector < Multiloop * >& ctMultiLoop,
                std::vector < Pseudoknot * >& ctPseudo)
{

    struct stat buf;
    if( (stat(CTfile.c_str(), &buf) == 0)) {
        std::string name1 = "", name2 = "", idMotif = "", line = "";
        std::vector< std::string > ctElements;
        std::vector< std::string > motifs = {"B", "A", "H", "I", "M", "S", "P"};
        std::vector < int > cts;
        size_t k, size;
        int nLine = 1;

        int rna1, rna2; // the RNA index

        std::ifstream ifs(CTfile);
        while (std::getline(ifs, line)) {

            if (line.compare("\n") != 0 and line[0] != '#' and line != "") {
                //std::cout << "nLine : " << nLine << std::endl;
                ctElements.clear();
                boost::split( ctElements, line, boost::is_any_of(" \t"), boost::token_compress_on );

                /*std::cout << "line " << nLine << ": ";
            for(size_t i = 0, size = ctElements.size(); i != size; i++)
                std::cout << ctElements[i] << " ";
            std::cout << std::endl;*/

                if(int(ctElements.size()) >= 2) { // Name1 \space (Name2) \space IDmotif \space (depending on IDmotif optional)
                    // Check RNA indexes
                    name1 = "";
                    name2 = "";
                    name1 = ctElements[0];
                    name2 = ctElements[1];
                    rna1 = -1;
                    rna2 = -1;
                    for (k = 0, size = rnaList_.size(); k != size; k++)
                        if (name1.compare(rnaList_[k].get_name_()) == 0)
                            rna1 = k;
                    for (k = 0, size = rnaList_.size(); k != size; k++)
                        if (name2.compare(rnaList_[k].get_name_()) == 0)
                            rna2 = k;
                    if (rna2 == -1) { // Check idMotif if not rna2
                        idMotif = "";
                        for (k = 0, size = motifs.size(); k != size; k++)
                            if (ctElements[1].compare(motifs[k]) == 0)
                                idMotif = ctElements[1];
                    }
                    if (rna1 != -1 and rna2 != -1) { // Interaction motif: RNA indexes are ok
                        if(int(ctElements.size()) >= 3) { // Name1 \space (Name2) \space IDmotif \space (depending on IDmotif optional)
                            // Check idMotif
                            idMotif = "";
                            for (k = 0, size = motifs.size(); k != size; k++)
                                if (ctElements[2].compare(motifs[k]) == 0)
                                    idMotif = ctElements[2];


                            try {
                                if (idMotif.compare("B") == 0) {
                                    addBp(rna1, rna2, ctElements, rnaList_,
                                            ctAppTwo, ctApp, CTfile, nLine);

                                } else if (idMotif.compare("S") == 0) {
                                    addHelix(rna1, rna2, ctElements,
                                                      rnaList_, ctHelix,
                                                      CTfile, nLine);
                                } else if (idMotif.compare("P") == 0) {
                                    addPseudo(rna1, rna2, ctElements, rnaList_,
                                              ctPseudo, CTfile, nLine);
                                } else {
                                    throw (std::string("The motif id " + idMotif + ", line " + std::to_string(nLine) +
                                            " of the constraint file is not correct."));
                                }
                            } catch (std::string e) {
                                throw (e);
                            }
                        } else {
                            throw (std::string("The format of the constraint file " + CTfile + ", line " + std::to_string(nLine) +
                                    " is not correct. Please look at the example inputs."));
                        }

                    } else if (rna1 != -1 and idMotif != "") {  // Structure motif: RNA index and idMotif is ok

                        try {
                            if (idMotif.compare("B") == 0) {
                                addBp(rna1, rna1, ctElements, rnaList_,
                                        ctAppTwo, ctApp, CTfile, nLine);
                            } else if (idMotif.compare("A") == 0) {
                                addSingle(rna1, ctElements, rnaList_,
                                        ctSingle, CTfile, nLine);
                            } else if (idMotif.compare("H") == 0) {
                                addHairLoop(rna1, ctElements, rnaList_,
                                        ctHairLoop, CTfile, nLine);
                            } else if (idMotif.compare("I") == 0) {
                                addIntLoop(rna1, ctElements, rnaList_,
                                        ctIntLoop, CTfile, nLine);
                            } else if (idMotif.compare("M") == 0) {
                                addMultiLoop(rna1, ctElements, rnaList_,
                                             ctMultiLoop, CTfile, nLine);
                            } else if (idMotif.compare("S") == 0) {
                                addHelix(rna1, rna1, ctElements,
                                          rnaList_, ctHelix,
                                          CTfile, nLine);
                            } else if (idMotif.compare("P") == 0) {
                                addPseudo(rna1, rna1, ctElements, rnaList_,
                                          ctPseudo, CTfile, nLine);
                            } else {
                                throw (std::string("The motif id " + idMotif + ", line " + std::to_string(nLine) +
                                        " of the cosntraint file is not correct."));
                            }
                        } catch (std::string e) {
                            throw (e);
                        }
                    } else if (rna1 == -1 and rna2 == -1) {
                        throw (std::string("The RNAs " + name1 + " and " + name2 + " do not exist in the fasta file. Found in constraint file line " +
                                   std::to_string(nLine) + "."));
                    } else if (rna1 == -1) {
                        throw (std::string("The RNA " + name1 + " does not exist in the fasta file. Found in constraint file line " +
                                   std::to_string(nLine) + "."));
                    } else if (rna2 == -1 or idMotif == "") {
                        throw (std::string("The RNA or motif id " + name2 + " does not exist. Found in constraint file line " +
                                   std::to_string(nLine) + "."));
                    }
                } else {
                   throw (std::string("The format of the constraint file " + CTfile + ", line " +
                            std::to_string(nLine) + " is not correct. Please look at the example inputs."));
                }
            }
            nLine++;
        }
    }
    else
    {
        throw (std::string("The file " + CTfile + " doesn't exist."));
    }

#ifdef _DEBUG
    std::cout << "fin extract_ct" << std::endl;
#endif
}