File: test_Sequence.cpp

package info (click to toggle)
libxpertmass 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,908 kB
  • sloc: cpp: 50,586; xml: 2,193; python: 417; ansic: 70; makefile: 33
file content (1129 lines) | stat: -rw-r--r-- 36,023 bytes parent folder | download
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
// ./tests/catch2-tests [section] -s


/////////////////////// Qt includes
#include <QDebug>
#include <QString>
#include <QDir>


/////////////////////// Catch2 includes
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>


/////////////////////// Local includes
#include "TestUtils.hpp"
#include <libXpertMass/Sequence.hpp>
#include <libXpertMass/Monomer.hpp>

namespace MsXpS
{

namespace libXpertMassCore
{

TestUtils test_utils_sequence_1_letter("protein-1-letter", 1);
ErrorList error_list_sequence;

SCENARIO("Sequence object can be constructed empty", "[Sequence]")
{
  test_utils_sequence_1_letter.initializeXpertmassLibrary();
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  WHEN("Constructing an empty Sequence")
  {
    qDebug() << "Allocate empty sequence.";

    Sequence sequence;

    THEN("The object is invalid")
    {
      REQUIRE_FALSE(sequence.isValid());
    }

    AND_WHEN("Setting the PolChemDef and then a sequence as 1-letter code text")
    {
      qDebug() << "Will set the pol chem def.";

      sequence.setPolChemDefCstSPtr(pol_chem_def_csp);
      REQUIRE(sequence.getPolChemDef() != nullptr);
      REQUIRE(sequence.getPolChemDef().get() != nullptr);

      std::vector<std::size_t> failing_indices;

      qDebug() << "Will set the sequence.";

      sequence.setSequence(
        test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter,
        failing_indices);

      THEN(
        "The sequence is valid because both PolChemDef and a proper sequence "
        "are defined.")
      {
        REQUIRE_FALSE(failing_indices.size());
        REQUIRE(sequence.isValid());
      }
    }
  }
}

SCENARIO("Sequence objects can be constructed in one go", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A fully qualified Sequence object")
  {
    Sequence sequence(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);

    THEN("The Sequence is valid")
    {
      REQUIRE(sequence.isValid());
    }
  }
}

SCENARIO("Sequence objects can be copy-constructed", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A fully qualified Sequence object")
  {
    Sequence sequence(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);

    THEN("The Sequence is valid")
    {
      REQUIRE(sequence.validate(error_list_sequence));
      REQUIRE(sequence.isValid());
      REQUIRE(sequence.size() == 157);
    }

    WHEN("A new Sequence is copy-constructed")
    {
      REQUIRE(sequence.size() == 157);
      Sequence new_sequence(sequence);
      REQUIRE(new_sequence.size() == 157);

      THEN("The new Sequence is valid and is identical to the initial one")
      {
        REQUIRE(new_sequence.isValid());
        REQUIRE(new_sequence == sequence);
      }
    }
  }
}

SCENARIO("Sequence objects can be assignment-configured", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A fully qualified Sequence instance")
  {
    Sequence sequence(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);

    THEN("The Sequence is valid")
    {
      REQUIRE(sequence.validate(error_list_sequence));
      REQUIRE(sequence.isValid());
    }

    WHEN("Instantiating an empty sequence and assigning to it the previous one")
    {
      Sequence new_sequence;
      new_sequence = sequence;

      REQUIRE((sequence = sequence) == sequence);
      REQUIRE((new_sequence = new_sequence) == new_sequence);

      THEN("The new Sequence is valid and is identical to the initial one")
      {
        REQUIRE(new_sequence.validate(error_list_sequence));
        REQUIRE(new_sequence.isValid());
        REQUIRE(new_sequence == sequence);
      }

      THEN("The sequence can be gotten back ")
      {
        REQUIRE(sequence.getSequence().toStdString() ==
                test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter
                  .toStdString());
        REQUIRE(new_sequence.getSequence().toStdString() ==
                test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter
                  .toStdString());
      }
    }
  }
}

SCENARIO("Sequence objects can be tested for (in)equality", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A fully qualified Sequence object and a copy of it")
  {
    Sequence sequence(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);
    Sequence new_sequence(sequence);

    THEN("The sequences are identical and can be tested for (in)equality")
    {
      REQUIRE(new_sequence == sequence);
      REQUIRE_FALSE(new_sequence != sequence);
    }

    THEN(
      "If the sequence is compared to itself the proper result must be "
      "returned")
    {
      REQUIRE(sequence == sequence);
      REQUIRE_FALSE(sequence != sequence);
    }

    AND_WHEN("A copy of that sequence is made using the assignment operator")
    {
      Sequence new_sequence;
      new_sequence = sequence;

      THEN("Both sequences can be compared")
      {
        REQUIRE(new_sequence == sequence);
        REQUIRE_FALSE(new_sequence != sequence);
      }
    }
  }
}

SCENARIO(
  "Sequence instances can be iterated into code by code (1-letter-code case)",
  "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A newly empty allocated sequence")
  {
    Sequence sequence_1(pol_chem_def_csp, "");

    WHEN("Setting sequence as 1-letter code text")
    {

      std::vector<std::size_t> failing_indices;
      sequence_1.setSequence(
        test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter,
        failing_indices);

      THEN(
        "The sequence cannot be gotten back because makeMonomerList() must be "
        "called first")
      {
        REQUIRE(sequence_1.getSequence().toStdString() ==
                test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter
                  .toStdString());
      }

      THEN("Asking for next code starting at index 0 should work")
      {
        QString sequence = sequence_1.getSequence();
        QString next_code;
        std::size_t last_parsed_code_char_index = 0;
        QString error;
        // All the monomer codes are 1-letter-long.
        std::size_t parsed_code_length = sequence_1.nextCode(
          sequence, next_code, last_parsed_code_char_index, error);

        REQUIRE(next_code.toStdString() == "M");
        // The very first index will be returned because we have a 1-letter
        // code string and the very first code is at index 0..
        REQUIRE(parsed_code_length == 1);
        REQUIRE(last_parsed_code_char_index == 0);
        REQUIRE(!error.size());

        AND_THEN(
          "Asking for next code using and incremented index, should work")
        {
          int parsed_code_length = sequence_1.nextCode(
            sequence, next_code, ++last_parsed_code_char_index, error);

          REQUIRE(next_code.toStdString() == "A");
          REQUIRE(parsed_code_length == 1);
          REQUIRE(last_parsed_code_char_index == 1);
          REQUIRE(!error.size());
        }
      }
    }
  }

  GIVEN("A newly empty allocated sequence")
  {
    Sequence sequence_1(pol_chem_def_csp, "");

    WHEN("Setting sequence as 1-letter code text")
    {
      std::vector<std::size_t> failing_indices;

      sequence_1.setSequence(
        test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter,
        failing_indices);

      THEN("The sequence can be gotten back")
      {
        REQUIRE(sequence_1.getSequence().toStdString() ==
                test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter
                  .toStdString());

        AND_THEN("Asking for next code starting at index 0 should work")
        {
          QString sequence = sequence_1.getSequence();
          QString next_code;
          std::size_t last_parsed_code_char_index = 0;
          QString error;
          // All the monomer codes are 1-letter-long.
          int parsed_code_length = sequence_1.nextCode(
            sequence, next_code, last_parsed_code_char_index, error);

          REQUIRE(next_code.toStdString() == "M");
          // The very first index will be returned because we have a 1-letter
          // code string and the very first code is at index 0..
          REQUIRE(parsed_code_length == 1);
          REQUIRE(last_parsed_code_char_index == 0);
          REQUIRE(!error.size());

          AND_THEN(
            "Asking for next code using an incremented index, should work")
          {
            int parsed_code_length = sequence_1.nextCode(
              sequence, next_code, ++last_parsed_code_char_index, error);

            REQUIRE(next_code.toStdString() == "A");
            REQUIRE(parsed_code_length == 1);
            REQUIRE(last_parsed_code_char_index == 1);
            REQUIRE(!error.size());
          }
        }
      }
    }
  }
}

SCENARIO(
  "Sequence instances can be iterated into code by code (3-letter-code case)",
  "[Sequence]")
{
  TestUtils test_utils_sequence_3_letter("protein-3-letters", 1);

  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_3_letter.msp_polChemDef;

  GIVEN("A newly empty allocated sequence")
  {
    Sequence sequence_1(pol_chem_def_csp, "");

    WHEN("Setting sequence as 3-letter code text")
    {
      std::vector<std::size_t> failing_indices;

      sequence_1.setSequence(
        test_utils_sequence_3_letter.m_telokinAsMonomerText3Letters,
        failing_indices);

      THEN("The sequence can be checked")
      {
        REQUIRE(sequence_1.getSequence().toStdString() ==
                test_utils_sequence_3_letter.m_telokinAsMonomerText3Letters
                  .toStdString());

        AND_THEN("Asking for next code starting at index 0 should work")
        {
          QString sequence = sequence_1.getSequence();
          QString next_code;
          std::size_t last_parsed_code_char_index = 0;
          QString error;
          // All the monomer codes are 3-letter-long.
          int parsed_code_length = sequence_1.nextCode(
            sequence, next_code, last_parsed_code_char_index, error);

          REQUIRE(next_code.toStdString() == "Met");
          // The very first index will be returned because we have a 1-letter
          // code string and the very first code is at index 0..
          REQUIRE(parsed_code_length == 3);
          REQUIRE(last_parsed_code_char_index == 2);
          REQUIRE(!error.size());

          AND_THEN(
            "Asking for next code using and incremented index, should work")
          {
            int parsed_code_length = sequence_1.nextCode(
              sequence, next_code, ++last_parsed_code_char_index, error);

            REQUIRE(next_code.toStdString() == "Ala");
            REQUIRE(parsed_code_length == 3);
            REQUIRE(last_parsed_code_char_index == 5);
            REQUIRE(!error.size());
          }
        }
      }
    }
  }
}

SCENARIO("Sequence_s can be copied and compared", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("Newly allocated sequences with string sequences during construction")
  {
    Sequence sequence_1(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);
    Sequence sequence_2(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinNtermPeptideAsMonomerText1Letter);

    REQUIRE_FALSE(sequence_1 == sequence_2);
    REQUIRE(sequence_1 != sequence_2);

    WHEN("A new Sequence is either copy-constructed or assigned")
    {
      Sequence sequence_3(sequence_1);
      Sequence sequence_4 = sequence_1;

      THEN("The two Sequence objects should be identical (monomer text-wise)")
      {
        REQUIRE(sequence_3 == sequence_1);
        REQUIRE_FALSE(sequence_3 != sequence_1);

        REQUIRE(sequence_4 == sequence_1);
        REQUIRE_FALSE(sequence_4 != sequence_1);
      }

      AND_WHEN("A Sequence is either assigned a different sequence")
      {
        sequence_4 = sequence_2;

        THEN("The two Sequence objects should be identical (monomer text-wise)")
        {
          REQUIRE(sequence_4 == sequence_2);
          REQUIRE_FALSE(sequence_4 != sequence_2);
        }
      }
    }
  }

  GIVEN("Two Sequence instances differing by only one Monomer")
  {
    QString sequence_string_1 = "MAMISGMSGRKAS";
    Sequence sequence_1(pol_chem_def_csp, sequence_string_1);
    QString sequence_string_2 = "MAMISGWSGRKAS";
    Sequence sequence_2(pol_chem_def_csp, sequence_string_2);

    REQUIRE_FALSE(sequence_1 == sequence_2);
    REQUIRE(sequence_1 != sequence_2);

    WHEN("A new Sequence is either copy-constructed or assigned")
    {
      Sequence sequence_3(sequence_1);
      Sequence sequence_4 = sequence_1;

      THEN(
        "The two Sequence objects should be identical (monomer "
        "text-wise)")
      {
        REQUIRE(sequence_3 == sequence_1);
        REQUIRE_FALSE(sequence_3 != sequence_1);

        REQUIRE(sequence_4 == sequence_1);
        REQUIRE_FALSE(sequence_4 != sequence_1);
      }

      AND_WHEN("A Sequence is either assigned a different sequence")
      {
        sequence_4 = sequence_2;

        THEN(
          "The two Sequence objects should be identical (monomer "
          "text-wise)")
        {
          REQUIRE(sequence_4 == sequence_2);
          REQUIRE_FALSE(sequence_4 != sequence_2);
        }
      }
    }
  }
}

SCENARIO("Monomers can be retrieved by pointer from a Sequence", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  Sequence sequence_1(
    pol_chem_def_csp,
    test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);

  GIVEN("A newly allocated sequence with a string sequence during construction")
  {
    WHEN("Retrieving the index of a inexistent Monomer fails")
    {
      MonomerSPtr monomer_sp =
        std::make_shared<Monomer>(pol_chem_def_csp, "NOT_SET", "NOT_SET");

      bool ok = false;
      REQUIRE(sequence_1.monomerIndex(monomer_sp, ok) == 0);
      REQUIRE_FALSE(ok);
    }

    WHEN("Retrieving the Monomer at a given index as MonomerCstSPtr")
    {
      MonomerSPtr monomer_csp = sequence_1.getMonomerCstSPtrAt(4);

      THEN("The identity of the monomer can be checked")
      {
        REQUIRE(monomer_csp->getCode().toStdString() == "S");

        AND_THEN("Retrieving the index of that Monomer using the raw pointer")
        {

          bool ok = false;
          REQUIRE(sequence_1.monomerIndex(monomer_csp.get(), ok) == 4);
          REQUIRE(ok);
        }
      }
    }
  }
}

SCENARIO("A motif can be searched in a Sequence", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A newly allocated sequence with a string sequence during construction")
  {
    Sequence sequence_1(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);
    QString monomer_text = sequence_1.getSequence();
    REQUIRE(sequence_1.size() == static_cast<std::size_t>(monomer_text.size()));

    std::size_t search_index_1 = 0;
    std::size_t search_index_2 = 0;
    std::size_t search_index_3 = 0;
    std::size_t search_index_4 = 0;
    std::size_t search_index_5 = 0;
    std::size_t search_index_6 = 0;
    std::size_t search_index_7 = 0;

    Sequence motif_sequence_1_occurrence(pol_chem_def_csp, "MAMISGM");
    Sequence motif_sequence_6_occurrences(pol_chem_def_csp, "EE");

    WHEN("A single-copy existing motif is searched at a bad index (index: -1)")
    {
      std::size_t bad_index = -1;
      int result_1 =
        sequence_1.findForwardMotif(motif_sequence_1_occurrence, bad_index);

      THEN("The motif cannot be found")
      {
        REQUIRE(result_1 == -1);
      }
    }

    WHEN(
      "A single-copy existing motif is searched at a bad index (index: >= "
      "size())")
    {
      std::size_t bad_index =
        test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter.size();
      int result_1 =
        sequence_1.findForwardMotif(motif_sequence_1_occurrence, bad_index);

      THEN("The motif cannot be found")
      {
        REQUIRE(result_1 == -1);
      }
    }

    WHEN("An empty motif is searched")
    {
      Sequence empty_motif(pol_chem_def_csp, "");
      int result_1 = sequence_1.findForwardMotif(empty_motif, search_index_1);

      THEN("The motif cannot be found")
      {
        REQUIRE(result_1 == 0);
      }
    }

    WHEN(
      "A single-copy existing motif is searched with an index that results "
      "out-of-bounds")
    {
      std::size_t bad_index =
        test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter.size() - 1;
      int result_1 =
        sequence_1.findForwardMotif(motif_sequence_6_occurrences, bad_index);

      THEN("The motif cannot be found")
      {
        REQUIRE(result_1 == 0);
      }
    }

    WHEN("A single-copy existing motif is searched")
    {
      int result_1 = sequence_1.findForwardMotif(motif_sequence_1_occurrence,
                                                 search_index_1);

      search_index_2 = search_index_1 + motif_sequence_1_occurrence.size();
      int result_2   = sequence_1.findForwardMotif(motif_sequence_1_occurrence,
                                                 search_index_2);

      THEN("The motif should be found only once")
      {
        REQUIRE(result_1 == 1);
        REQUIRE(search_index_1 == 0);
        REQUIRE(result_2 == 0);
      }

      AND_WHEN("A a seven-copy existing motif is searched")
      {
        search_index_1 = 0;

        // qDebug()  << "search_index_1: " << search_index_1 << std::endl;
        // qDebug()  << "search_index_2: " << search_index_2 << std::endl;

        int result_1 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_1);

        search_index_2 = search_index_1 + motif_sequence_6_occurrences.size();
        int result_2 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_2);

        search_index_3 = search_index_2 + motif_sequence_6_occurrences.size();
        int result_3 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_3);

        search_index_4 = search_index_3 + motif_sequence_6_occurrences.size();
        int result_4 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_4);

        search_index_5 = search_index_4 + motif_sequence_6_occurrences.size();
        int result_5 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_5);

        search_index_6 = search_index_5 + motif_sequence_6_occurrences.size();
        int result_6 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_6);

        search_index_7 = search_index_6 + motif_sequence_6_occurrences.size();
        int result_7 = sequence_1.findForwardMotif(motif_sequence_6_occurrences,
                                                   search_index_7);

        THEN("The motif should be found seven times")
        {
          REQUIRE(result_1 == 1);
          REQUIRE(search_index_1 == 33);

          REQUIRE(result_2 == 1);
          REQUIRE(search_index_2 == 37);

          REQUIRE(result_3 == 1);
          REQUIRE(search_index_3 == 96);

          REQUIRE(result_4 == 1);
          REQUIRE(search_index_4 == 148);

          REQUIRE(result_5 == 1);
          REQUIRE(search_index_5 == 151);

          REQUIRE(result_6 == 1);
          REQUIRE(search_index_6 == 153);

          REQUIRE(result_7 == 1);
          REQUIRE(search_index_7 == 155);
        }
      }
    }
  }
}

SCENARIO("A number of checks might be performed on a Sequence", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A newly allocated sequence with a string sequence during construction")
  {
    Sequence sequence_1(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);
    QString monomer_text = sequence_1.getSequence();
    REQUIRE(monomer_text.size() ==
            test_utils_sequence_1_letter.m_telokinSequenceMonomerCount);
    REQUIRE(static_cast<std::size_t>(monomer_text.size()) == sequence_1.size());

    std::size_t tested_index = 0;


    THEN("It is possible to check if an index is inbound or not")
    {
      REQUIRE(sequence_1.isInBound(tested_index) == true);

      tested_index = monomer_text.size() / 2;
      REQUIRE(sequence_1.isInBound(tested_index) == true);

      tested_index = monomer_text.size() - 1;
      REQUIRE(sequence_1.isInBound(tested_index) == true);

      tested_index = monomer_text.size();
      REQUIRE(sequence_1.isInBound(tested_index) == false);
    }
  }
}

SCENARIO("Monomers can be added or removed from a Sequence", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A newly allocated sequence with a string sequence during construction")
  {
    Sequence sequence_1(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);
    QString monomer_text = sequence_1.getSequence();
    REQUIRE(
      monomer_text.toStdString() ==
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter.toStdString());
    REQUIRE(monomer_text.size() ==
            test_utils_sequence_1_letter.m_telokinSequenceMonomerCount);
    REQUIRE(static_cast<std::size_t>(monomer_text.size()) == sequence_1.size());

    WHEN("A Monomer object is inserted at the beginining of the sequence")
    {
      Monomer monomer(pol_chem_def_csp, "Tryptophan", "W");

      sequence_1.insertMonomerAt(monomer, 0);

      THEN("That monomer has to be found there")
      {
        REQUIRE(sequence_1.getMonomerCstSPtrAt(0)->getCode().toStdString() ==
                "W");

        QString w_monomer_text = monomer_text;
        w_monomer_text.prepend("W");

        REQUIRE(sequence_1.getSequence().toStdString() ==
                w_monomer_text.toStdString());
      }

      AND_WHEN("When searching for a motif comprising that monomer")
      {
        Sequence motif_sequence_1_occurrence(pol_chem_def_csp, "WMAMISGMSGR");
        std::size_t search_index_1 = 0;

        int result_1 = sequence_1.findForwardMotif(motif_sequence_1_occurrence,
                                                   search_index_1);

        THEN("The search is successful")
        {
          REQUIRE(result_1 == true);
          REQUIRE(search_index_1 == 0);
        }
      }
    }

    WHEN("A monomer is inserted at the end of the sequence")
    {
      Monomer monomer(pol_chem_def_csp, "Tryptophan", "W");

      // qDebug() << "The size of the sequence:" << sequence_1.size()
      //          << "The count of monomers:"
      //          << test_utils_sequence_1_letter.m_telokinSequenceMonomerCount;

      sequence_1.insertMonomerAt(
        monomer, test_utils_sequence_1_letter.m_telokinSequenceMonomerCount);

      THEN("That monomer has to be found there")
      {
        REQUIRE(sequence_1
                  .getMonomerCstSPtrAt(
                    test_utils_sequence_1_letter.m_telokinSequenceMonomerCount)
                  ->getCode()
                  .toStdString() == "W");
      }

      AND_WHEN("When searching for a motif comprising that monomer")
      {
        Sequence motif_sequence_1_occurrence(pol_chem_def_csp, "EEEEEEW");
        std::size_t search_index_1 = 0;

        int result_1 = sequence_1.findForwardMotif(motif_sequence_1_occurrence,
                                                   search_index_1);

        THEN("The search is successful")
        {
          REQUIRE(result_1 == true);
          REQUIRE(search_index_1 == 151);
        }
      }
    }

    WHEN("A monomer is inserted in the middle of the sequence")
    {
      Monomer monomer(pol_chem_def_csp, "Tryptophan", "W");

      int floor = std::floor(sequence_1.size() / 2);
      // qDebug() << "The floor:" << floor;

      sequence_1.insertMonomerAt(monomer, floor);

      THEN("That monomer has to be found there")
      {
        REQUIRE(
          sequence_1.getMonomerCstSPtrAt(floor)->getCode().toStdString() ==
          "W");
      }

      AND_WHEN("When searching for a motif comprising that monomer")
      {
        Sequence motif_sequence_1_occurrence(pol_chem_def_csp, "DPEVMWWYKDDQ");
        std::size_t search_index_1 = 0;

        int result_1 = sequence_1.findForwardMotif(motif_sequence_1_occurrence,
                                                   search_index_1);

        THEN("The search is successful")
        {
          REQUIRE(result_1 == true);
          REQUIRE(search_index_1 == 72);
        }

        AND_WHEN("That monomer is removed back")
        {
          sequence_1.removeMonomerAt(floor);

          THEN("The sequence with the inserted monomer cannot be found anymore")
          {
            search_index_1 = 0;

            result_1 = sequence_1.findForwardMotif(motif_sequence_1_occurrence,
                                                   search_index_1);

            REQUIRE(result_1 == 0);
            REQUIRE(search_index_1 == 0);

            AND_THEN("The original sequence can be found again")
            {
              std::vector<std::size_t> failing_indices;

              motif_sequence_1_occurrence.setSequence("DPEVMWYKDDQ",
                                                      failing_indices);
              search_index_1 = 0;

              result_1 = sequence_1.findForwardMotif(
                motif_sequence_1_occurrence, search_index_1);

              REQUIRE(result_1 == 1);
              REQUIRE(search_index_1 == 72);
            }
          }

          AND_WHEN("An empty monomer sequence text is appended to the Sequence")
          {
            Sequence sequence_2 = sequence_1;

            std::vector<std::size_t> failing_indices;

            sequence_1.appendSequence("", failing_indices);

            THEN("The sequence should remain unchanged")
            {
              REQUIRE(sequence_1.size() == sequence_2.size());
              REQUIRE(sequence_1 == sequence_2);
            }

            AND_WHEN("A monomer sequence text is appended to the Sequence")
            {
              THEN("The original sequence can be found again")
              {
                // qDebug() << "The sequence:" << sequence_1.getSequence(70,
                // 85);
                std::vector<std::size_t> failing_indices;

                motif_sequence_1_occurrence.setSequence("DPEVMWYKDDQ",
                                                        failing_indices);

                search_index_1 = 0;

                result_1 = sequence_1.findForwardMotif(
                  motif_sequence_1_occurrence, search_index_1);

                // qDebug() << "The index at which the motif was found:"
                //          << search_index_1;

                REQUIRE(result_1 == 1);
                REQUIRE(search_index_1 == 72);
              }

              REQUIRE(sequence_1.getSequence().toStdString() ==
                      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter
                        .toStdString());

              std::vector<std::size_t> failing_indices;

              sequence_1.appendSequence("THISTEQST", failing_indices);

              THEN("Searching for it should return the right index")
              {
                Sequence motif_sequence_1_occurrence(pol_chem_def_csp,
                                                     "EEEEEETHISTEQST");
                std::size_t search_index_1 = 0;

                int result_1 = sequence_1.findForwardMotif(
                  motif_sequence_1_occurrence, search_index_1);

                REQUIRE(result_1 == true);
                REQUIRE(search_index_1 == 151);
              }
            }
          }
        }
      }
    }
  }
}

SCENARIO(
  "Ranges of Monomer instances can be extracted in various ways from a "
  "Sequence",
  "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  GIVEN("A newly allocated sequence with a string sequence during construction")
  {
    Sequence sequence_1(
      pol_chem_def_csp,
      test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);
    QString monomer_text = sequence_1.getSequence();
    REQUIRE(monomer_text.size() ==
            test_utils_sequence_1_letter.m_telokinSequenceMonomerCount);
    REQUIRE(static_cast<std::size_t>(monomer_text.size()) == sequence_1.size());

    IndexRange index_range(0, 12);
    IndexRangeCollection index_range_collection;
    index_range_collection.appendIndexRange(index_range);

    WHEN("Trying to get an index-based Monomer text")
    {
      QString text = sequence_1.getSequence(0, 12, /*with_modif*/ false);

      THEN("The returned string must the right size and sequence")
      {
        REQUIRE(text.size() == 13);
        REQUIRE(text.toStdString() == "MAMISGMSGRKAS");
      }
    }

    WHEN("Trying to get a sequence ranged-based Monomer text")
    {
      QString text = sequence_1.getSequence(index_range_collection,
                                            /*with_modif*/ false,
                                            /*delimited_regions*/ false);

      THEN("The returned string must the right size and sequence")
      {
        REQUIRE(text.size() == 13);
        REQUIRE(text.toStdString() == "MAMISGMSGRKAS");
      }
    }

    WHEN(
      "Modifying one monomer in the Sequence (using std::const_pointer_cast)")
    {
      std::unique_ptr<Modif> phospho_up =
        std::make_unique<Modif>(pol_chem_def_csp, "Phosphorylation", "H1O3P1");
      phospho_up->setTargets("S");
      phospho_up->setMaxCount(1);

      MonomerSPtr serine_7_csp = sequence_1.getMonomerCstSPtrAt(7);
      // qDebug() << "The monomer at index 7:" << serine_7_csp->toString();
      REQUIRE(serine_7_csp->getCode().toStdString() == "S");
      REQUIRE(serine_7_csp->isModifTarget(*phospho_up.get()) == true);

      //  Need to cast away constness of the MonomerCstSPtr.
      MonomerSPtr serine_7_sp = std::const_pointer_cast<Monomer>(serine_7_csp);
      REQUIRE(serine_7_sp->modify(*phospho_up.get(),
                                  /* override modif count */ false,
                                  error_list_sequence) != nullptr);

      AND_WHEN(
        "Asking for the monomer index-based range without specifying that "
        "modifs are requested")
      {
        QString text = sequence_1.getSequence(0, 12, /*with_modif*/ false);

        THEN("The returned string must match the peptidic Monomer text")
        {
          REQUIRE(text.toStdString() == "MAMISGMSGRKAS");
        }
      }

      AND_WHEN(
        "Asking for the monomer coordinates-based range without specifying "
        "that modifs are requested")
      {
        QString text = sequence_1.getSequence(index_range_collection,
                                              /*with_modif*/ false,
                                              /*delimited_regions*/ false);

        THEN("The returned string must match the peptidic Monomer text")
        {
          REQUIRE(text.toStdString() == "MAMISGMSGRKAS");
        }
      }

      AND_WHEN(
        "Asking for the monomer index-based range with specifying that modifs "
        "are requested")
      {
        QString text = sequence_1.getSequence(0, 12, /*with_modif*/ true);

        THEN("The returned string must match the peptidic Monomer text")
        {
          REQUIRE(text.toStdString() == "MAMISGMS<Phosphorylation>GRKAS");
        }
      }

      AND_WHEN(
        "Asking for the monomer coordinates-based range with specifying that "
        "modifs "
        "are "
        "requested")
      {
        QString text = sequence_1.getSequence(index_range_collection,
                                              /*with_modif*/ true,
                                              /*delimited_regions*/ false);

        THEN("The returned string must match the peptidic Monomer text")
        {
          REQUIRE(text.toStdString() == "MAMISGMS<Phosphorylation>GRKAS");
        }
      }
      AND_WHEN(
        "Asking for the monomer coordinates-based range with specifying that "
        "modifs "
        "are "
        "requested and with delimited regions")
      {
        QString text = sequence_1.getSequence(index_range_collection,
                                              /*with_modif*/ true,
                                              /*delimited_regions*/ true);

        THEN("The returned string must match the peptidic Monomer text")
        {
          REQUIRE(text.toStdString() ==
                  "Region [1-13]: MAMISGMS<Phosphorylation>GRKAS\n");
        }
      }
    }
  }
}

SCENARIO("A sequence can compute a checksum", "[Sequence]")
{
  PolChemDefCstSPtr pol_chem_def_csp =
    test_utils_sequence_1_letter.msp_polChemDef;

  Sequence sequence_1(
    pol_chem_def_csp,
    test_utils_sequence_1_letter.m_telokinAsMonomerText1Letter);

  quint16 check_sum_no_modifs = 0;
  quint16 check_sum_modifs    = 0;

  GIVEN(
    "A newly allocated non-modified sequence with a string sequence during "
    "construction")
  {
    WHEN("Computing the checksum")
    {
      qDebug() << "The sequence size is:"<< sequence_1.getSequence().size();

      quint16 check_sum =
        sequence_1.checksum(0, sequence_1.getSequence().size() - 1);

      THEN("The returned check sum is not zero")
      {
        REQUIRE(check_sum != 0);
      }
    }

    WHEN("Computing the checksum without modifs")
    {
      check_sum_no_modifs = sequence_1.checksum(
        0, sequence_1.getSequence().size() - 1, /*with_modifs*/ false);

      AND_WHEN("Computing the checksum with the modifs")
      {
        check_sum_modifs = sequence_1.checksum(
          0, sequence_1.getSequence().size() - 1, /*with_modifs*/ true);

        THEN(
          "Both check sums should be identical because the sequence is not "
          "modified")
        {
          REQUIRE(check_sum_no_modifs == check_sum_modifs);
        }

        AND_WHEN("The sequence is phosphorylated on a serine")
        {
          std::unique_ptr<Modif> phospho_up = std::make_unique<Modif>(
            pol_chem_def_csp, "Phosphorylation", "H1O3P1");
          phospho_up->setTargets("S");
          phospho_up->setMaxCount(1);

          MonomerSPtr serine_7_csp = sequence_1.getMonomerCstSPtrAt(7);
          REQUIRE(serine_7_csp->getCode().toStdString() == "S");
          REQUIRE(serine_7_csp->isModifTarget(*phospho_up.get()) == true);

          //  Need to cast away constness of the MonomerCstSPtr.
          MonomerSPtr serine_7_sp =
            std::const_pointer_cast<Monomer>(serine_7_csp);
          REQUIRE(serine_7_sp->modify(*phospho_up.get(),
                                      /* override modif count */ false,
                                      error_list_sequence) != nullptr);

          THEN(
            "The new checksum with modifs must be different than the one "
            "calculated before")
          {
            quint16 new_check_sum = sequence_1.checksum(
              0, sequence_1.getSequence().size() - 1, /*with_modifs*/ true);
            REQUIRE(new_check_sum != 0);
            REQUIRE(new_check_sum != check_sum_modifs);
          }
        }
      }
    }
  }
}

} // namespace libXpertMassCore
} // namespace MsXpS