This file is indexed.

/usr/share/perl5/Bio/Assembly/Contig.pm is in libbio-perl-perl 1.6.924-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
#
# BioPerl module for Bio::Assembly::Contig
#   Mostly based on Bio::SimpleAlign by Ewan Birney
#
# Please direct questions and support issues to <bioperl-l@bioperl.org> 
#
# Cared for by Robson Francisco de Souza <rfsouza@citri.iq.usp.br>
#
# Copyright Robson Francisco de Souza
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::Assembly::Contig - Perl module to hold and manipulate
                     sequence assembly contigs.

=head1 SYNOPSIS

    # Module loading
    use Bio::Assembly::IO;

    # Assembly loading methods
    $aio = Bio::Assembly::IO->new(-file=>"test.ace.1",
                                  -format=>'phrap');

    $assembly = $aio->next_assembly;
    foreach $contig ($assembly->all_contigs) {
      # do something
    }

    # OR, if you want to build the contig yourself,

    use Bio::Assembly::Contig;
    $c = Bio::Assembly::Contig->new(-id=>"1");

    $ls  = Bio::LocatableSeq->new(-seq=>"ACCG-T",
                                  -id=>"r1",
                                  -alphabet=>'dna');
    $ls2 = Bio::LocatableSeq->new(-seq=>"ACA-CG-T",
                                  -id=>"r2",
                                  -alphabet=>'dna');

    $ls_coord = Bio::SeqFeature::Generic->new(-start=>3,
                                              -end=>8,
                                              -strand=>1);
    $ls2_coord = Bio::SeqFeature::Generic->new(-start=>1,
                                               -end=>8,
                                               -strand=>1);
    $c->add_seq($ls);
    $c->add_seq($ls2);
    $c->set_seq_coord($ls_coord,$ls);
    $c->set_seq_coord($ls2_coord,$ls2);

    $con = Bio::LocatableSeq->new(-seq=>"ACACCG-T",
                                  -alphabet=>'dna');
    $c->set_consensus_sequence($con);

    $l = $c->change_coord('unaligned r2','ungapped consensus',6);
    print "6 in unaligned r2 => $l in ungapped consensus\n";


=head1 DESCRIPTION

A contig is as a set of sequences, locally aligned to each other, so
that every sequence has overlapping regions with at least one sequence
in the contig, such that a continuous of overlapping sequences is
formed, allowing the deduction of a consensus sequence which may be
longer than any of the sequences from which it was deduced.

In this documentation we refer to the overlapping sequences used to
build the contig as "aligned sequences" and to the sequence deduced
from the overlap of aligned sequences as the "consensus". Methods to
deduce the consensus sequence from aligned sequences were not yet
implemented in this module, but its posssible to add a consensus
sequence deduced by other means, e.g, by the assembly program used to
build the alignment.

All aligned sequences in a Bio::Assembly::Contig must be Bio::Assembly::Locatable
objects and have a unique ID. The unique ID restriction is due to the
nature of the module's internal data structures and is also a request
of some assembly programs. If two sequences with the same ID are added
to a contig, the first sequence added is replaced by the second one.

=head2 Coordinate_systems

There are four base coordinate systems in Bio::Assembly::Contig.  When
you need to access contig elements or data that exists on a certain
range or location, you may be specifying coordinates in relation to
different sequences, which may be either the contig consensus or one
of the aligned sequences that were used to do the assembly.

 =========================================================
          Name           | Referenced sequence
 ---------------------------------------------------------
   "gapped consensus"    | Contig (with gaps)
   "ungapped consensus"  | Contig (without gaps)
   "aligned $seqID"      | sequence $seqID (with gaps)
   "unaligned $seqID"    | sequence $seqID (without gaps)
 =========================================================

"gapped consensus" refers to positions in the aligned consensus
sequence, which is the consensus sequence including the gaps inserted
to align it agains the aligned sequences that were used to assemble
the contig. So, its limits are [ 1, (consensus length + number of gaps
in consensus) ]

"ungapped consensus" is a coordinate system based on the consensus
sequence, but excluding consensus gaps. This is just the coordinate
system that you have when considering the consensus sequence alone,
instead of aligned to other sequences.

"aligned $seqID" refers to locations in the sequence $seqID after
alignment of $seqID against the consensus sequence (reverse
complementing the original sequence, if needed).  Coordinate 1 in
"aligned $seqID" is equivalent to the start location (first base) of
$seqID in the consensus sequence, just like if the aligned sequence
$seqID was a feature of the consensus sequence.

"unaligned $seqID" is equivalent to a location in the isolated
sequence, just like you would have when considering the sequence
alone, out of an alignment.  When changing coordinates from "aligned
$seq2" to "unaligned $seq2", if $seq2 was reverse complemented when
included in the alignment, the output coordinates will be reversed to
fit that fact, i.e. 1 will be changed to length($seq2), 2 will be
length($seq)-1 and so on.

An important note: when you change gap coordinates from a gapped
system ("gapped consensus" or "aligned $seqID") to a system that does
not include gaps ("ungapped consensus" or "unaligned $seqID"), the
position returned will be the first location before all gaps
neighboring the input location.

=head2 Feature_collection

Bio::Assembly::Contig stores much information about a contig in a
Bio::Assembly::SeqFeature::Collection object. Relevant information on the
alignment is accessed by selecting features based on their primary
tags (e.g. all features which have a primary tag of the form
'_aligned_coord:$seqID', where $seqID is an aligned sequence ID, are
coordinates for sequences in the contig alignment) and, by using
methods from Bio::Assembly::SeqFeature::Collection, it's possible to select
features by overlap with other features.

We suggest that you use the primary tags of features as identifiers
for feature classes. By convention, features with primary tags
starting with a '_' are generated by modules that populate the contig
data structure and return the contig object, maybe as part of an
assembly object, e.g.  drivers from the Bio::Assembly::IO set.

Features in the features collection may be associated with particular
aligned sequences. To obtain this, you must attach the sequence to the
feature, using attach() seq from Bio::Assembly::SeqFeatureI, before you add the
feature to the feature collection. We also suggest to add the sequence
id to the primary tag, so that is easy to select feature for a
particular sequence.

There is only one feature class that some methods in
Bio::Assembly::Contig expect to find in the feature collection: features
with primary tags of the form '_aligned_coord:$seqID', where $seqID is
the aligned sequence id (like returned by $seq-E<gt>id()). These features
describe the position (in "gapped consensus" coordinates) of aligned
sequences, and the method set_seq_coord() automatically changes a
feature's primary tag to this form whenever the feature is added to
the collection by this method. Only two methods in Bio::Assembly::Contig
will not work unless there are features from this class:
change_coord() and get_seq_coord().

Other feature classes will be automatically available only when
Bio::Assembly::Contig objects are created by a specific module. Such
feature classes are (or should be) documented in the documentation of
the module which create them, to which the user should refer.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to the
Bioperl mailing lists  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support 

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and 
reponsive experts will be able look at the problem and quickly 
address it. Please include a thorough description of the problem 
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution.  Bug reports can be submitted via the
web:

  https://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR - Robson Francisco de Souza

rfsouza@citri.iq.usp.br

=head1 APPENDIX

The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _

=cut

#'
package Bio::Assembly::Contig;

use strict;

use Bio::DB::SeqFeature::Store; # isa Bio::SeqFeature::CollectionI
use Bio::Seq::PrimaryQual;      # isa Bio::Seq::QualI

use Scalar::Util qw(weaken);

use base qw(Bio::Root::Root Bio::Align::AlignI);

=head1 Object creator

=head2 new

 Title     : new
 Usage     : my $contig = Bio::Assembly::Contig->new();
 Function  : Creates a new contig object
 Returns   : Bio::Assembly::Contig
 Args      : -id         => unique contig ID
             -source     => string for the sequence assembly program used
             -collection => Bio::SeqFeature::CollectionI instance

=cut

#-----------
sub new {
#-----------
    my ($class, @args) = @_;

    my $self = $class->SUPER::new(@args);

    my ($src, $id, $collection) = $self->_rearrange([qw(SOURCE ID COLLECTION)], @args);
    $src && $self->source($src);
    ($id && $self->id($id)) || ($self->{'_id'} = 'NoName'); # Alignment (contig) name
    ($id && $self->id($id)) || ($self->{'_source'} = 'Unknown'); # Program used to build the contig
    # we need to set up internal hashes first!

    # Bio::SimpleAlign derived fields (check which ones are needed for AlignI compatibility)
    $self->{'_elem'} = {}; # contig elements: aligned sequence objects (keyed by ID)
    $self->{'_order'} = {}; # store sequence order
    # $self->{'start_end_lists'} = {}; # References to entries in {'_seq'}. Keyed by seq ids.
    # $self->{'_dis_name'} = {}; # Display names for each sequence
    $self->{'_symbols'} = {}; # List of symbols

    #Contig specific slots
    $self->{'_consensus_sequence'} = undef;
    $self->{'_consensus_quality'} = undef;
    $self->{'_nof_residues'} = 0;
    $self->{'_nof_seqs'} = 0;
    # $self->{'_nof_segments'} = 0; # Let's not make it heavier than needed by now...
    
    # for cases where SF::Collection is shared between Bio::Assembly::Contig 
    if ($collection) {
        $self->throw("Collection must implement Bio::SeqFeature::CollectionI") unless $collection->isa('Bio::SeqFeature::CollectionI');
        $self->{'_sfc'} = $collection;
    } else {
        $self->{'_sfc'} = Bio::DB::SeqFeature::Store->new(
            -adaptor           => 'memory',
            -index_subfeatures => 1,
        );
    }

    # Assembly specifics
    $self->{'_assembly'} = undef; # Bio::Assembly::Scaffold the contig belongs to
    $self->{'_strand'} = 0; # Reverse (-1) or forward (1), if contig is in a scaffold. 0 otherwise
    $self->{'_neighbor_start'} = undef; # Neighbor Bio::Assembly::Contig
    $self->{'_neighbor_end'}   = undef; # Neighbor Bio::Assembly::Contig

    return $self; # success - we hope!
}

=head1 Assembly related methods

These methods exist to enable adding information about possible
relations among contigs, e.g. when you already have a scaffold for
your assembly, describing the ordering of contigs in the final
assembly, but no sequences covering the gaps between neighboring
contigs.

=head2 source

 Title     : source
 Usage     : $contig->source($program);
 Function  : Get/Set program used to build this contig
 Returns   : string
 Argument  : [optional] string

=cut

sub source {
    my $self = shift;
    my $source = shift;

    $self->{'_source'} = $source if (defined $source);
    return $self->{'_source'};
}

=head2 assembly

 Title     : assembly
 Usage     : $contig->assembly($assembly);
 Function  : Get/Set assembly object for this contig
 Returns   : a Bio::Assembly::Scaffold object
 Argument  : a Bio::Assembly::Scaffold object

=cut

sub assembly {
    my $self = shift;
    my $assembly = shift;

    $self->throw("Using non Bio::Assembly::Scaffold object when assign contig to assembly")
    if (defined $assembly && ! $assembly->isa("Bio::Assembly::Scaffold"));
    # We create a circular reference to a Scaffold object. It is made weak
    # to prevent memory leaks.
    $self->{'_assembly'} = $assembly if (defined $assembly); 
    weaken($self->{'_assembly'});

    return $self->{'_assembly'};
}

=head2 strand

 Title     : strand
 Usage     : $contig->strand($num);
 Function  : Get/Set contig orientation in a scaffold/assembly.
             Its equivalent to the strand property of sequence
             objects and sets whether the contig consensus should
             be reversed and complemented before being added to a
             scaffold or assembly.
 Returns   : integer
 Argument  : 1 if orientaion is forward, -1 if reverse and
             0 if none

=cut

sub strand {
    my $self = shift;
    my $ori = shift;

    if (defined $ori) {
        $self->throw("Contig strand must be either 1, -1 or 0")
            unless $ori == 1 || $ori == 0 || $ori == -1;
        $self->{'_strand'} = $ori;
    }

    return $self->{'_strand'};
}

=head2 upstream_neighbor

 Title     : upstream_neighbor
 Usage     : $contig->upstream_neighbor($contig);
 Function  : Get/Set a contig neighbor for the current contig when
             building a scaffold. The upstream neighbor is
             located before $contig first base
 Returns   : nothing
 Argument  : Bio::Assembly::Contig

=cut

sub upstream_neighbor {
    my $self = shift;
    my $ref = shift;

    $self->throw("Trying to assign a non Bio::Assembly::Contig object to upstream contig")
        if (defined $ref && ! $ref->isa("Bio::Assembly::Contig"));

    $self->{'_neighbor_start'} = $ref if (defined $ref);
    return $self->{'_neighbor_start'};
}

=head2 downstream_neighbor

 Title     : downstream_neighbor
 Usage     : $contig->downstream_neighbor($num);
 Function  : Get/Set a contig neighbor for the current contig when
             building a scaffold. The downstream neighbor is
             located after $contig last base
 Returns   : nothing
 Argument  : Bio::Assembly::Contig

=cut

sub downstream_neighbor {
    my $self = shift;
    my $ref = shift;

    $self->throw("Trying to assign a non Bio::Assembly::Contig object to downstream contig")
        if (defined $ref && ! $ref->isa("Bio::Assembly::Contig"));
    $self->{'_neighbor_end'} = $ref if (defined $ref);
    return $self->{'_neighbor_end'};
}

=head1 Contig feature collection methods

=head2 add_features

 Title     : add_features
 Usage     : $contig->add_features($feat,$flag)
 Function  :

             Add an array of features to the contig feature
             collection. The consensus sequence may be attached to the
             added feature, if $flag is set to 1. If $flag is 0 and
             the feature attached to one of the contig aligned
             sequences, the feature is registered as an aligned
             sequence feature. If $flag is 0 and the feature is not
             attched to any sequence in the contig, the feature is
             simply added to the feature collection and no attachment
             or registration is made.

             Note: You must attach aligned sequences to their features
             prior to calling add_features, otherwise you won't be
             able to access the feature through get_seq_feat_by_tag()
             method.

 Returns   : number of features added.
 Argument  :
             $feat : A reference to an array of Bio::SeqFeatureI
             $flag : boolean - true if consensus sequence object
                     should be attached to this feature, false if
                     no consensus attachment should be made.
                     Default: false.

=cut

sub add_features {
    my ($self, $args, $flag) = @_;

    # Adding shortcuts for aligned sequence features
    $flag = 0 unless (defined $flag);
    if ($flag && defined $self->{'_consensus_sequence'}) {
        foreach my $feat (@$args) {
            next if (defined $feat->seq);
            $feat->attach_seq($self->{'_consensus_sequence'});
        }
    } elsif (!$flag) { # Register aligned sequence features
        foreach my $feat (@$args) {
            if (my $seq = $feat->entire_seq()) {
                my $seqID = $seq->id() || $seq->display_id || $seq->primary_id;
                $self->warn("Adding contig feature attached to unknown sequence $seqID!")
                    unless (exists $self->{'_elem'}{$seqID});
                my $tag = $feat->primary_tag;
                $self->{'_elem'}{$seqID}{'_feat'}{$tag} = $feat;
            }
        }
    }

    # Add feature to feature collection
    my $nof_added = $self->get_features_collection->add_features($args);

    return $nof_added;
}

=head2 remove_features

 Title     : remove_features
 Usage     : $contig->remove_features(@feat)
 Function  : Remove an array of contig features
 Returns   : true if successful
 Argument  : An array of Bio::SeqFeature::Generic (Bio::SeqFeatureI)

=cut

sub remove_features {
    my ($self, @args) = @_;

    # Removing shortcuts for aligned sequence features
    for my $feat (@args) {
        if (my $seq = $feat->entire_seq()) {
            my $seqID = $seq->id || $seq->display_id || $seq->primary_id;
            my $tag = $feat->primary_tag;
            $tag =~ s/:$seqID$/$1/g;
            delete( $self->{'_elem'}{$seqID}{'_feat'}{$tag} )
                if (exists $self->{'_elem'}{$seqID}{'_feat'}{$tag} &&
                $self->{'_elem'}{$seqID}{'_feat'}{$tag} eq $feat);
        }
    }
   
    # Removing Bio::SeqFeature objects
    return $self->get_features_collection->delete(@args);
}

=head2 get_features_collection

 Title     : get_features_collection
 Usage     : $contig->get_features_collection()
 Function  : Get the collection of all contig features and seqfeatures
 Returns   : Bio::DB::SeqFeature::Store (Bio::SeqFeature::CollectionI)
 Argument  : none

=cut

sub get_features_collection {
    my $self = shift;
    return $self->{'_sfc'};
}

=head2 remove_features_collection

 Title     : remove_features_collection
 Usage     : $contig->remove_features_collection()
 Function  : Remove the collection of all contig features. It is useful
             to save some memory (when contig features are not needed).
 Returns   : none
 Argument  : none

=cut

sub remove_features_collection {
    my $self = shift;
    # Removing shortcuts for aligned sequence features
    for my $seqID (keys %{$self->{'_elem'}}) {
        delete $self->{'_elem'}{$seqID};
    }
    # Removing Bio::SeqFeature::Collection features
    $self->{'_sfc'} = {};
    return;
}

=head1 Coordinate system's related methods

See L<Coordinate_Systems> above.

=head2 change_coord

 Title     : change_coord
 Usage     : $contig->change_coord($in,$out,$query)
 Function  :

             Change coordinate system for $query.  This method
             transforms locations between coordinate systems described
             in section "Coordinate Systems" of this document.

             Note: this method will throw an exception when changing
             coordinates between "ungapped consensus" and other
             systems if consensus sequence was not set. It will also
             throw exceptions when changing coordinates among aligned
             sequence, either with or without gaps, and other systems
             if sequence locations were not set with set_seq_coord().

 Returns   : integer
 Argument  :
             $in    : [string]  input coordinate system
             $out   : [string]  output coordinate system
             $query : [integer] a position in a sequence

=cut

sub change_coord {
    my $self     = shift;
    my $type_in  = shift;
    my $type_out = shift;
    my $query    = shift;

    # Parsing arguments
    # Loading read objects (these calls will throw exceptions whether $read_in or
    # $read_out is not found
    my ($read_in,$read_out) = (undef,undef);
    my $in_ID  = ( split(' ',$type_in)  )[1];
    my $out_ID = ( split(' ',$type_out) )[1];

    if ($in_ID  ne 'consensus') {
        $read_in  = $self->get_seq_coord( $self->get_seq_by_name($in_ID)  );
        $self->throw("Can't change coordinates without sequence location for $in_ID")
            unless (defined $read_in);
    }
    if ($out_ID ne 'consensus') {
        $read_out = $self->get_seq_coord( $self->get_seq_by_name($out_ID) );
        $self->throw("Can't change coordinates without sequence location for $out_ID")
            unless (defined $read_out);
    }

    # Performing transformation between coordinates
    SWITCH1: {

        # Transformations between contig padded and contig unpadded
        (($type_in eq 'gapped consensus') && ($type_out eq 'ungapped consensus')) && do {
            $self->throw("Can't use ungapped consensus coordinates without a consensus sequence")
                unless (defined $self->{'_consensus_sequence'});
            $query = &_padded_unpadded($self->{'_consensus_gaps'}, $query);
            last SWITCH1;
        };
        (($type_in eq 'ungapped consensus') && ($type_out eq 'gapped consensus')) && do {
            $self->throw("Can't use ungapped consensus coordinates without a consensus sequence")
                unless (defined $self->{'_consensus_sequence'});
            $query = &_unpadded_padded($self->{'_consensus_gaps'},$query);
            last SWITCH1;
        };

        # Transformations between contig (padded) and read (padded)
        (($type_in  eq 'gapped consensus') &&
        ($type_out =~ /^aligned /) && defined($read_out)) && do {
            $query = $query - $read_out->start() + 1;
            last SWITCH1;
        };
        (($type_in =~ /^aligned /) && defined($read_in) &&
        ($type_out  eq 'gapped consensus')) && do {
            $query = $query + $read_in->start() - 1;
            last SWITCH1;
        };

        # Transformations between contig (unpadded) and read (padded)
        (($type_in eq 'ungapped consensus') &&
        ($type_out =~ /^aligned /) && defined($read_out)) && do {
            $query = $self->change_coord('ungapped consensus','gapped consensus',$query);
            $query = $self->change_coord('gapped consensus',"aligned $out_ID",$query);
            last SWITCH1;
        };
        (($type_in =~ /^aligned /) && defined($read_in) &&
        ($type_out eq 'ungapped consensus')) && do {
            $query = $self->change_coord("aligned $in_ID",'gapped consensus',$query);
            $query = $self->change_coord('gapped consensus','ungapped consensus',$query);
            last SWITCH1;
        };

        # Transformations between seq $read_in padded and seq $read_out padded
        (defined($read_in)  && ($type_in  =~ /^aligned /)  &&
        defined($read_out) && ($type_out =~ /^aligned /)) && do {
            $query = $self->change_coord("aligned $in_ID",'gapped consensus',$query);
            $query = $self->change_coord('gapped consensus',"aligned $out_ID",$query);
            last SWITCH1;
        };

        # Transformations between seq $read_in padded and seq $read_out unpadded
        (defined($read_in)  && ($type_in  =~ /^aligned /)    &&
        defined($read_out) && ($type_out =~ /^unaligned /)) && do {
            if ($read_in ne $read_out) {
                $query = $self->change_coord("aligned $in_ID",'gapped consensus',$query);
                $query = $self->change_coord('gapped consensus',"aligned $out_ID",$query);
            }
            my $list_out = $self->{'_elem'}{$out_ID}{'_gaps'};
            $query = &_padded_unpadded($list_out,$query);
            # Changing read orientation if read was reverse complemented when aligned
            if ($read_out->strand == -1) {
                my ($length) = $read_out->length();
                $length = $length - &_nof_gaps($list_out,$length);
                $query  = $length - $query + 1;
            }
            last SWITCH1;
        };
        (defined($read_in)  && ($type_in  =~ /^unaligned /) &&
        defined($read_out) && ($type_out =~ /^aligned /))  && do {
            my $list_in = $self->{'_elem'}{$in_ID}{'_gaps'};
            # Changing read orientation if read was reverse complemented when aligned
            if ($read_in->strand == -1) {
                my ($length) = $read_in->length();
                $length = $length - &_nof_gaps($list_in,$length);
                $query  = $length - $query + 1;
            }
            $query = &_unpadded_padded($list_in,$query);
            if ($read_in ne $read_out) {
                $query = $self->change_coord("aligned $in_ID",'gapped consensus',$query);
                $query = $self->change_coord('gapped consensus',"aligned $out_ID",$query);
            }
            last SWITCH1;
        };

        # Transformations between seq $read_in unpadded and seq $read_out unpadded
        (defined($read_in)  && ($type_in  =~ /^unaligned /)    &&
        defined($read_out) && ($type_out =~ /^unaligned /)) && do {
            $query = $self->change_coord("unaligned $in_ID","aligned $out_ID",$query);
            $query = $self->change_coord("aligned $out_ID","unaligned $out_ID",$query);
            last SWITCH1;
        };

        # Transformations between contig (padded) and read (unpadded)
        (($type_in eq 'gapped consensus') &&
        ($type_out =~ /^unaligned /) && defined($read_out)) && do {
            $query = $self->change_coord('gapped consensus',"aligned $out_ID",$query);
            $query = $self->change_coord("aligned $out_ID","unaligned $out_ID",$query);
            last SWITCH1;
        };
        (($type_in =~ /^unaligned /) && defined($read_in) &&
        ($type_out eq 'gapped consensus')) && do {
            $query = $self->change_coord("unaligned $in_ID","aligned $in_ID",$query);
            $query = $self->change_coord("aligned $in_ID",'gapped consensus',$query);
            last SWITCH1;
        };

        # Transformations between contig (unpadded) and read (unpadded)
        (($type_in eq 'ungapped consensus') &&
        ($type_out =~ /^unaligned /) && defined($read_out)) && do {
            $query = $self->change_coord('ungapped consensus','gapped consensus',$query);
            $query = $self->change_coord('gapped consensus',"unaligned $out_ID",$query);
            last SWITCH1;
        };
        (($type_in =~ /^unaligned /) && defined($read_in) &&
        ($type_out eq 'ungapped consensus')) && do {
            $query = $self->change_coord("unaligned $in_ID",'gapped consensus',$query);
            $query = $self->change_coord('gapped consensus','ungapped consensus',$query);
            last SWITCH1;
        };

        $self->throw("Unknow coordinate system. Args: $type_in, $type_out.");
        $query = undef; # If a coordinate systems just requested is unknown
    }

    return $query;
}

=head2 get_seq_coord

 Title     : get_seq_coord
 Usage     : $contig->get_seq_coord($seq);
 Function  : Get "gapped consensus" location for aligned sequence
 Returns   : Bio::SeqFeature::Generic for coordinates or undef.
             A warning is printed if sequence coordinates were not set.
 Argument  : Bio::LocatableSeq object

=cut

sub get_seq_coord {
    my ($self,$seq) = @_;

    if( !ref $seq || ! $seq->isa('Bio::LocatableSeq') ) {
        $self->throw("$seq is not a Bio::LocatableSeq");
    }
    my $seqID = $seq->id() || $seq->display_id || $seq->primary_id;

    unless (exists( $self->{'_elem'}{$seqID} )) {
        $self->warn("No such sequence ($seqID) in contig ".$self->id);
        return;
    }
    unless (exists( $self->{'_elem'}{$seqID}{'_feat'}{"_aligned_coord:$seqID"} )) {
        # $self->warn("Chad. Location not set for sequence ($seqID) in contig ".$self->id);
        return;
    }

    return $self->{'_elem'}{$seqID}{'_feat'}{"_aligned_coord:$seqID"};
}

=head2 set_seq_coord

 Title     : set_seq_coord
 Usage     : $contig->set_seq_coord($feat,$seq);
 Function  :

             Set "gapped consensus" location for an aligned
             sequence. If the sequence was previously added using
             add_seq, its coordinates are changed/set.  Otherwise,
             add_seq is called and the sequence is added to the
             contig.

 Returns   : Bio::SeqFeature::Generic for old coordinates or undef.
 Argument  :
             $feat  : a Bio::SeqFeature::Generic object
                      representing a location for the
                      aligned sequence, in "gapped
                      consensus" coordinates.

             Note: the original feature primary tag will
                   be lost.

             $seq   : a Bio::LocatableSeq object

=cut

sub set_seq_coord {
    my ($self,$feat,$seq) = @_;

    if( !ref $seq || ! $seq->isa('Bio::LocatableSeq') ) {
        $self->throw("Unable to process non locatable sequences [".ref($seq)."]");
    }

    # Complaining about inadequate feature object
    $self->throw("Coordinates must be a Bio::SeqFeature::Generic object!")
        unless ( $feat->isa("Bio::SeqFeature::Generic") );
    $self->throw("Sequence coordinates must have an end!")
        unless (defined $feat->end);
    $self->throw("Sequence coordinates must have a start!")
        unless (defined $feat->start);

    my $seqID = $seq->id() || $seq->display_id || $seq->primary_id;
    if ( exists( $self->{'_elem'}{$seqID} ) &&
         exists( $self->{'_elem'}{$seqID}{'_seq'} ) &&
         defined( $self->{'_elem'}{$seqID}{'_seq'} ) &&
         ($seq ne $self->{'_elem'}{$seqID}{'_seq'}) ) {
        $self->warn("Replacing sequence $seqID\n");
        $self->remove_seq($self->{'_elem'}{$seqID}{'_seq'});
        $self->remove_features($feat);
    }

    # Add new sequence and Bio::Generic::SeqFeature
    $self->add_seq($seq);

    $feat->add_tag_value('contig',$self->id) unless ( $feat->has_tag('contig') );
    $feat->primary_tag("_aligned_coord");
    $feat->source_tag($seqID);
    $feat->attach_seq($seq);

    $self->{'_elem'}{$seqID}{'_feat'}{"_aligned_coord:$seqID"} = $feat;
    $self->add_features([ $feat ]);
}

=head1 Bio::Assembly::Contig consensus methods

=head2 set_consensus_sequence

 Title     : set_consensus_sequence
 Usage     : $contig->set_consensus_sequence($seq)
 Function  : Set the consensus sequence object for this contig
 Returns   : consensus length
 Argument  : Bio::LocatableSeq

=cut

sub set_consensus_sequence {
    my $self = shift;
    my $seq  = shift;

    $self->throw("Consensus sequence must be a Bio::LocatableSeq!")
        unless ($seq->isa("Bio::LocatableSeq"));

    $self->{'_consensus_gaps'} = []; # Consensus Gap registry
    $self->_register_gaps( $seq->seq, $self->{'_consensus_gaps'} );
    $self->{'_consensus_sequence'} = $seq;

    $seq->start(1);
    $seq->end($seq->_ungapped_len);

    my $con_len = $seq->length;

    return $con_len;
}

=head2 set_consensus_quality

 Title     : set_consensus_quality
 Usage     : $contig->set_consensus_quality($qual)
 Function  : Set the quality object for consensus sequence
 Returns   : nothing
 Argument  : Bio::Seq::QualI object

=cut

sub set_consensus_quality {
    my ($self, $qual) = @_;

    $self->throw("Consensus quality must be a Bio::Seq::QualI object!")
        unless ( $qual->isa("Bio::Seq::QualI") );

    $self->throw("Consensus quality can't be added before you set the consensus sequence!")
        unless (defined $self->{'_consensus_sequence'});

    $self->{'_consensus_quality'} = $qual;
}

=head2 get_consensus_length

 Title     : get_consensus_length
 Usage     : $contig->get_consensus_length()
 Function  : Get consensus sequence length
 Returns   : integer
 Argument  : none

=cut

sub get_consensus_length {
    my $self = shift;

    return $self->{'_consensus_sequence'}->length();
}

=head2 get_consensus_sequence

 Title     : get_consensus_sequence
 Usage     : $contig->get_consensus_sequence()
 Function  : Get a reference to the consensus sequence object
             for this contig
 Returns   : Bio::SeqI object
 Argument  : none

=cut

sub get_consensus_sequence {
    my ($self, @args) = @_;

    return $self->{'_consensus_sequence'};
}

=head2 get_consensus_quality

 Title     : get_consensus_quality
 Usage     : $contig->get_consensus_quality()
 Function  : Get a reference to the consensus quality object
             for this contig.
 Returns   : A Bio::Seq::QualI object
 Argument  : none

=cut

sub get_consensus_quality {
    my ($self, @args) = @_;

    return $self->{'_consensus_quality'};
}

=head1 Bio::Assembly::Contig aligned sequences methods

=head2 set_seq_qual

 Title     : set_seq_qual
 Usage     : $contig->set_seq_qual($seq,$qual);
 Function  : Adds quality to an aligned sequence.
 Returns   : nothing
 Argument  : a Bio::LocatableSeq object and
             a Bio::Seq::QualI object

See L<Bio::LocatableSeq> for more information.

=cut

sub set_seq_qual {
    my ($self,$seq,$qual) = @_;

    if( !ref $seq || ! $seq->isa('Bio::LocatableSeq') ) {
        $self->throw("Unable to process non locatable sequences [".ref($seq)."]");
    }
    my $seqID = $seq->id() || $seq->display_id || $seq->primary_id;

    $self->throw("Consensus quality must be a Bio::Seq::QualI object!")
        unless ( $qual->isa("Bio::Seq::QualI") );
    $self->throw("Use add_seq first: aligned sequence qualities can't be added before you load the sequence!")
        unless (exists $self->{'_elem'}{$seqID}{'_seq'});
    $self->throw("Use set_seq_coord first: aligned sequence qualities can't be added before you add coordinates for the sequence!") unless (defined( $self->get_seq_coord($seq) ));

    # Adding gaps to quality object
    my $sequence = $self->{'_elem'}{$seqID}{'_seq'}->seq();
    my $tmp = $qual->qual();
    @{$tmp} = reverse(@{$tmp}) if ($self->get_seq_coord($seq)->strand() == -1);
    my @quality  = ();
    my $previous = 0;
    my $next     = 0;
    my $i = 0; my $j = 0;
    while ($i <= $#{$tmp}) {
        # IF base is a gap, quality is the average for neighbouring sites
        if ($j > $i && substr($sequence,$j,1) eq '-') {
            $previous = $tmp->[$i-1] unless ($i == 0);
            if ($i < $#{$tmp}) {
                $next = $tmp->[$i+1];
            } else {
                $next = 0;
            }
            push(@quality,int( ($previous+$next)/2 ));
        } else {
            push(@quality,$tmp->[$i]);
            $i++;
        }
        $j++;
    }

    $self->{'_elem'}{$seqID}{'_qual'} = Bio::Seq::PrimaryQual->new(
        -qual=>join(" ",@quality), -id=>$seqID );
}

=head2 get_seq_ids

 Title     : get_seq_ids
 Usage     : $contig->get_seq_ids( -start => $start,
                                   -end   => $end,
                                   -type  => "gapped A0QR67B08.b" );
 Function  : Get list of sequence IDs overlapping interval [$start, $end]
             The default interval is [1,$contig->length]
             Default coordinate system is "gapped contig"
 Returns   : An array
 Argument  : A hash with optional elements:
             -start : consensus subsequence start
             -end   : consensus subsequence end
             -type  : the coordinate system type for $start and $end arguments
                      Coordinate system available are:
                      "gapped consensus"   : consensus coordinates with gaps
                      "ungapped consensus" : consensus coordinates without gaps
                      "aligned $ReadID"    : read $ReadID coordinates with gaps
                      "unaligned $ReadID"  : read $ReadID coordinates without gaps


=cut

sub get_seq_ids {
    my ($self, @args) = @_;

    my ($type, $start, $end) = $self->_rearrange([qw(TYPE START END)], @args);

    my @list;
    if (defined($start) && defined($end)) {
        if (defined($type) && ($type ne 'gapped consensus')) {
            $start = $self->change_coord($type,'gapped consensus',$start);
            $end   = $self->change_coord($type,'gapped consensus',$end);
        }
        @list = $self->get_features_collection->features(
           -type         => '_aligned_coord', # primary tag
           -start        => $start,
           -end          => $end,
           #-contain     => 0,
           #-strandmatch => 'ignore',
        );
        @list = map { $_->entire_seq->id } @list;
    } else {
        # Entire aligned sequences list
        @list = map { $self->{'_order'}{$_} } sort { $a cmp $b } keys %{ $self->{'_order'} };
    }

    return @list;
}

=head2 get_seq_feat_by_tag

 Title     : get_seq_feat_by_tag
 Usage     : $seq = $contig->get_seq_feat_by_tag($seq,"_aligned_coord:$seqID")
 Function  : Get a sequence feature based on its primary_tag.
 Returns   : a Bio::SeqFeature object
 Argument  : a Bio::LocatableSeq and a string (feature primary tag)

=cut

sub get_seq_feat_by_tag {
    my ($self,$seq,$tag) = @_;

    if( !ref $seq || ! $seq->isa('Bio::LocatableSeq') ) {
        $self->throw("Unable to process non locatable sequences [".ref($seq)."]");
    }
    my $seqID = $seq->id || $seq->display_id || $seq->primary_id;

    return $self->{'_elem'}{$seqID}{'_feat'}{$tag};
}

=head2 get_seq_by_name

 Title     : get_seq_by_name
 Usage     : $seq = $contig->get_seq_by_name('Seq1')
 Function  : Gets a sequence based on its id.
 Returns   : a Bio::LocatableSeq object
             undef if name is not found
 Argument  : string

=cut

sub get_seq_by_name {
    my $self = shift;
    my ($seqID) = @_;

    unless (exists $self->{'_elem'}{$seqID}{'_seq'}) {
        $self->throw("Could not find sequence $seqID in contig ".$self->id);
    return;
    }

    return $self->{'_elem'}{$seqID}{'_seq'};
}

=head2 get_qual_by_name

 Title     : get_qual_by_name
 Usage     : $seq = $contig->get_qual_by_name('Seq1')
 Function  :

             Gets Bio::Seq::QualI object for a sequence
             through its id ( as given by $qual->id() ).

 Returns   : a Bio::Seq::QualI object.
             undef if name is not found
 Argument  : string

=cut

sub get_qual_by_name {
    my $self = shift;
    my ($seqID) = @_;

    unless (exists $self->{'_elem'}{$seqID}{'_qual'}) {
        $self->warn("Could not find quality for $seqID in contig!");
        return;
    }

    return $self->{'_elem'}{$seqID}{'_qual'};
}

=head1 Bio::Align::AlignI compatible methods

=head2 Modifier methods

These methods modify the MSE by adding, removing or shuffling complete
sequences.

=head2 add_seq

 Title     : add_seq
 Usage     : $contig->add_seq($newseq);
 Function  :

             Adds a sequence to the contig. *Does*
             *not* align it - just adds it to the
             hashes.

 Returns   : nothing
 Argument  : a Bio::LocatableSeq object

See L<Bio::LocatableSeq> for more information.

=cut

sub add_seq {
    my $self = shift;
    my $seq = shift;

    if( !ref $seq || ! $seq->isa('Bio::LocatableSeq') ) {
        $self->throw("Unable to process non locatable sequences [".ref($seq)."]");
    }

    my $seqID = $seq->id() || $seq->display_id || $seq->primary_id;
    $self->{'_elem'}{$seqID} = {} unless (exists $self->{'_elem'}{$seqID});

    if (exists( $self->{'_elem'}{$seqID}{'_seq'} ) &&
    ($seq eq $self->{'_elem'}{$seqID}{'_seq'}) ) {
        $self->warn("Adding sequence $seqID, which has already been added");
    }

    # Our locatable sequences are always considered to be complete sequences
    $seq->start(1);
    $seq->end($seq->_ungapped_len);
    
    my $alphabet = $seq->alphabet;
    
    $alphabet = lc($alphabet) if defined $alphabet;
    
    $self->warn("Adding non-nucleotidic sequence ".$seqID)
        if (!$alphabet || ($alphabet ne 'dna' && $alphabet ne 'rna'));

    # build the symbol list for this sequence,
    # will prune out the gap and missing/match chars
    # when actually asked for the symbol list in the
    # symbol_chars
    if (defined $seq->seq) {
        map { $self->{'_symbols'}->{$_} = 1; } split(//,$seq->seq);
    } else {
        $self->{'_symbols'} = {};
    }

    my $seq_no = ++$self->{'_nof_seqs'};

    if (ref( $self->{'_elem'}{$seqID}{'_seq'} )) {
        $self->warn("Replacing one sequence [$seqID]\n");
    } else {
        #print STDERR "Assigning $seqID to $order\n";
        $self->{'_order'}->{$seq_no} = $seqID;
        # $self->{'_start_end_lists'}->{$id} = []
        # unless(exists $self->{'_start_end_lists'}->{$id});
        # push @{$self->{'_start_end_lists'}->{$id}}, $seq;
    }

    $self->{'_elem'}{$seqID}{'_seq'}  = $seq;
    $self->{'_elem'}{$seqID}{'_feat'} = {};
    $self->{'_elem'}{$seqID}{'_gaps'} = [];
    my $dbref = $self->{'_elem'}{$seqID}{'_gaps'};
    my $nofgaps = $self->_register_gaps($seq->seq,$dbref);

    # Updating residue count
    $self->{'_nof_residues'} += $seq->length - $nofgaps;

    return 1;
}

=head2 remove_seq

 Title     : remove_seq
 Usage     : $contig->remove_seq($seq);
 Function  : Removes a single sequence from a contig
 Returns   : 1 on success, 0 otherwise
 Argument  : a Bio::LocatableSeq object

=cut

sub remove_seq {
    my ($self,$seq) = @_;

    if( !ref $seq || ! $seq->isa('Bio::LocatableSeq') ) {
        $self->throw("Unable to process non locatable sequences [".ref($seq)."]");
    }

    my $seqID = $seq->id() || $seq->display_id || $seq->primary_id;
    unless (exists $self->{'_elem'}{$seqID} ) {
        $self->warn("No sequence named $seqID  [$seq]");
        return 0;
    }

    # Updating residue count
    $self->{'_nof_residues'} -= $seq->length() +
    &_nof_gaps( $self->{'_elem'}{$seqID}{'_gaps'}, $seq->length );
    
    # Update number of sequences
    $self->{'_nof_seqs'}--; 
    
    # Update order of sequences (order starts at 1)
    my $max_order = $self->{'_nof_seqs'} + 1;
    my $target_order = $max_order + 1;
    for (my $order = 1 ; $order <= $max_order ; $order++) {
      if ($self->{'_order'}->{$order} eq $seqID) {
        # Found the wanted sequence order
        $target_order = $order;
      }
      if ($order > $target_order) {
        # Decrement this sequence order by one order
        $self->{'_order'}->{$order-1} = $self->{'_order'}->{$order};
      }
      if ($order == $max_order) {
        # Remove last order
        delete $self->{'_order'}->{$order};
      }
    }

    # Remove all references to features of this sequence
    my @feats = ();
    for my $tag (keys %{ $self->{'_elem'}{$seqID}{'_feat'} }) {
        push(@feats, $self->{'_elem'}{$seqID}{'_feat'}{$tag});
    }
    $self->{'_sfc'}->remove_features(\@feats);
    delete $self->{'_elem'}{$seqID};

    return 1;
}

=head2 purge

 Title   : purge
 Usage   : $contig->purge(0.7);
 Function:

           Removes sequences above whatever %id.

           This function will grind on large alignments. Beware!
           (perhaps not ideally implemented)

 Example :
 Returns : An array of the removed sequences
 Argument:


=cut

sub purge {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 sort_alphabetically

 Title     : sort_alphabetically
 Usage     : $contig->sort_alphabetically
 Function  :

             Changes the order of the alignemnt to alphabetical on name
             followed by numerical by number.

 Returns   :
 Argument  :

=cut

sub sort_alphabetically {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 Sequence selection methods

Methods returning one or more sequences objects.

=head2 each_seq

 Title     : each_seq
 Usage     : foreach $seq ( $contig->each_seq() )
 Function  : Gets an array of Seq objects from the alignment
 Returns   : an array
 Argument  :

=cut

sub each_seq {
    my ($self) = @_;

    my (@arr,$seqID);

    foreach $seqID ( map { $self->{'_order'}{$_} } sort { $a <=> $b } keys %{$self->{'_order'}} ) {
        push(@arr,$self->{'_elem'}{$seqID}{'_seq'});
    }

    return @arr;
}

=head2 each_alphabetically

 Title     : each_alphabetically
 Usage     : foreach $seq ( $contig->each_alphabetically() )
 Function  :

             Returns an array of sequence object sorted alphabetically
             by name and then by start point.
             Does not change the order of the alignment

 Returns   :
 Argument  :

=cut

sub each_alphabetically {
    my($self) = @_;
    $self->throw_not_implemented();
}

=head2 each_seq_with_id

 Title     : each_seq_with_id
 Usage     : foreach $seq ( $contig->each_seq_with_id() )
 Function  :

             Gets an array of Seq objects from the
             alignment, the contents being those sequences
             with the given name (there may be more than one)

 Returns   : an array
 Argument  : a seq name

=cut

sub each_seq_with_id {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 get_seq_by_pos

 Title     : get_seq_by_pos
 Usage     : $seq = $contig->get_seq_by_pos(3)
 Function  :

             Gets a sequence based on its position in the alignment.
             Numbering starts from 1.  Sequence positions larger than
             num_sequences() will thow an error.

 Returns   : a Bio::LocatableSeq object
 Argument  : positive integer for the sequence osition

=cut

sub get_seq_by_pos {
    my $self = shift;
    my ($pos) = @_;

    $self->throw("Sequence position has to be a positive integer, not [$pos]")
        unless $pos =~ /^\d+$/ and $pos > 0;
    $self->throw("No sequence at position [$pos]")
        unless $pos <= $self->num_sequences ;

    my $seqID = $self->{'_order'}->{--$pos};
    return $self->{'_elem'}{$seqID}{'_seq'};
}

=head2 Create new alignments

The result of these methods are horizontal or vertical subsets of the
current MSE.

=head2 select

 Title     : select
 Usage     : $contig2 = $contig->select(1, 3) # three first sequences
 Function  :

             Creates a new alignment from a continuous subset of
             sequences.  Numbering starts from 1.  Sequence positions
             larger than num_sequences() will thow an error.

 Returns   : a Bio::Assembly::Contig object
 Argument  : positive integer for the first sequence
             positive integer for the last sequence to include (optional)

=cut

sub select {
    my ($self) = @_;
    $self->throw_not_implemented();
}


=head2 select_noncont

 Title     : select_noncont
 Usage     : $contig2 = $contig->select_noncont(1, 3) # first and 3rd sequences
 Function  :

             Creates a new alignment from a subset of
             sequences.  Numbering starts from 1.  Sequence positions
             larger than num_sequences() will throw an error.

 Returns   : a Bio::Assembly::Contig object
 Args      : array of integers for the sequences

=cut

sub select_noncont {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 slice

 Title     : slice
 Usage     : $contig2 = $contig->slice(20, 30)
 Function  :

             Creates a slice from the alignment inclusive of start and
             end columns.  Sequences with no residues in the slice are
             excluded from the new alignment and a warning is printed.
             Slice beyond the length of the sequence does not do
             padding.

 Returns   : a Bio::Assembly::Contig object
 Argument  : positive integer for start column
             positive integer for end column

=cut

sub slice {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 Change sequences within the MSE

These methods affect characters in all sequences without changeing the
alignment.


=head2 map_chars

 Title     : map_chars
 Usage     : $contig->map_chars('\.','-')
 Function  :

             Does a s/$arg1/$arg2/ on the sequences. Useful for gap
             characters

             Notice that the from (arg1) is interpretted as a regex,
             so be careful about quoting meta characters (eg
             $contig->map_chars('.','-') wont do what you want)

 Returns   :
 Argument  : 'from' rexexp
             'to' string

=cut

sub map_chars {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 uppercase

 Title     : uppercase()
 Usage     : $contig->uppercase()
 Function  : Sets all the sequences to uppercase
 Returns   :
 Argument  :

=cut

sub uppercase {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 match_line

 Title    : match_line()
 Usage    : $contig->match_line()
 Function : Generates a match line - much like consensus string
            except that a line indicating the '*' for a match.
 Argument : (optional) Match line characters ('*' by default)
            (optional) Strong match char (':' by default)
            (optional) Weak match char ('.' by default)

=cut

sub match_line {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 match

 Title     : match()
 Usage     : $contig->match()
 Function  :

             Goes through all columns and changes residues that are
             identical to residue in first sequence to match '.'
             character. Sets match_char.

             USE WITH CARE: Most MSE formats do not support match
             characters in sequences, so this is mostly for output
             only. NEXUS format (Bio::AlignIO::nexus) can handle
             it.

 Returns   : 1
 Argument  : a match character, optional, defaults to '.'

=cut

sub match {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 unmatch

 Title     : unmatch()
 Usage     : $contig->unmatch()
 Function  :

             Undoes the effect of method match. Unsets match_char.

 Returns   : 1
 Argument  : a match character, optional, defaults to '.'

=cut

sub unmatch {
    my ($self) = @_;
    $self->throw_not_implemented();
}


=head2 MSE attibutes

Methods for setting and reading the MSE attributes.

Note that the methods defining character semantics depend on the user
to set them sensibly.  They are needed only by certain input/output
methods. Unset them by setting to an empty string ('').

=head2 id

 Title     : id
 Usage     : $contig->id("Ig")
 Function  : Gets/sets the id field of the alignment
 Returns   : An id string
 Argument  : An id string (optional)

=cut

sub id {
    my ($self, $contig_name) = @_;

    if (defined( $contig_name )) {
        $self->{'_id'} = $contig_name;
    }

    return $self->{'_id'};
}

=head2 missing_char

 Title     : missing_char
 Usage     : $contig->missing_char("?")
 Function  : Gets/sets the missing_char attribute of the alignment
             It is generally recommended to set it to 'n' or 'N'
             for nucleotides and to 'X' for protein.
 Returns   : An missing_char string,
 Argument  : An missing_char string (optional)

=cut

sub missing_char {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 match_char

 Title     : match_char
 Usage     : $contig->match_char('.')
 Function  : Gets/sets the match_char attribute of the alignment
 Returns   : An match_char string,
 Argument  : An match_char string (optional)

=cut

sub match_char {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 gap_char

 Title     : gap_char
 Usage     : $contig->gap_char('-')
 Function  : Gets/sets the gap_char attribute of the alignment
 Returns   : An gap_char string, defaults to '-'
 Argument  : An gap_char string (optional)

=cut

sub gap_char {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 symbol_chars

 Title   : symbol_chars
 Usage   : my @symbolchars = $contig->symbol_chars;
 Function: Returns all the seen symbols (other than gaps)
 Returns : array of characters that are the seen symbols
 Argument: boolean to include the gap/missing/match characters

=cut

sub symbol_chars{
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 Alignment descriptors

These read only methods describe the MSE in various ways.


=head2 consensus_string

 Title     : consensus_string
 Usage     : $str = $contig->consensus_string($threshold_percent)
 Function  : Makes a strict consensus
 Returns   :
 Argument  : Optional threshold ranging from 0 to 100.
             The consensus residue has to appear at least threshold %
             of the sequences at a given location, otherwise a '?'
             character will be placed at that location.
             (Default value = 0%)

=cut

sub consensus_string {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 consensus_iupac

 Title     : consensus_iupac
 Usage     : $str = $contig->consensus_iupac()
 Function  :

             Makes a consensus using IUPAC ambiguity codes from DNA
             and RNA. The output is in upper case except when gaps in
             a column force output to be in lower case.

             Note that if your alignment sequences contain a lot of
             IUPAC ambiquity codes you often have to manually set
             alphabet.  Bio::PrimarySeq::_guess_type thinks they
             indicate a protein sequence.

 Returns   : consensus string
 Argument  : none
 Throws    : on protein sequences


=cut

sub consensus_iupac {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 is_flush

 Title     : is_flush
 Usage     : if( $contig->is_flush() )
           :
           :
 Function  : Tells you whether the alignment
           : is flush, ie all of the same length
           :
           :
 Returns   : 1 or 0
 Argument  :

=cut

sub is_flush {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 length

 Title     : length()
 Usage     : $len = $contig->length()
 Function  : Returns the maximum length of the alignment.
             To be sure the alignment is a block, use is_flush
 Returns   :
 Argument  :

=cut

sub length {
    my ($self) = @_;

    $self->throw_not_implemented();
}

=head2 maxname_length

 Title     : maxname_length
 Usage     : $contig->maxname_length()
 Function  :

             Gets the maximum length of the displayname in the
             alignment. Used in writing out various MSE formats.

 Returns   : integer
 Argument  :

=cut

sub maxname_length {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 num_residues

 Title     : num_residues
 Usage     : $no = $contig->num_residues
 Function  : number of residues in total in the alignment
 Returns   : integer
 Argument  :
 Note      : replaces no_residues

=cut

sub num_residues {
    my ($self) = @_;

    return $self->{'_nof_residues'};
}

=head2 num_sequences

 Title     : num_sequences
 Usage     : $depth = $contig->num_sequences
 Function  : number of sequence in the sequence alignment
 Returns   : integer
 Argument  : None
 Note      : replaces no_sequences

=cut

sub num_sequences {
    my ($self) = @_;

    return scalar( keys %{ $self->{'_elem'} } );
}

=head2 percentage_identity

 Title   : percentage_identity
 Usage   : $id = $contig->percentage_identity
 Function: The function calculates the percentage identity of the alignment
 Returns : The percentage identity of the alignment (as defined by the
                             implementation)
 Argument: None

=cut

sub percentage_identity{
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 overall_percentage_identity

 Title   : percentage_identity
 Usage   : $id = $contig->percentage_identity
 Function: The function calculates the percentage identity of
           the conserved columns
 Returns : The percentage identity of the conserved columns
 Args    : None

=cut

sub overall_percentage_identity{
    my ($self) = @_;
    $self->throw_not_implemented();
}


=head2 average_percentage_identity

 Title   : average_percentage_identity
 Usage   : $id = $contig->average_percentage_identity
 Function: The function uses a fast method to calculate the average
           percentage identity of the alignment
 Returns : The average percentage identity of the alignment
 Args    : None

=cut

sub average_percentage_identity {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 Alignment positions

Methods to map a sequence position into an alignment column and back.
column_from_residue_number() does the former. The latter is really a
property of the sequence object and can done using
L<Bio::LocatableSeq::location_from_column>:

    # select somehow a sequence from the alignment, e.g.
    my $seq = $contig->get_seq_by_pos(1);
    #$loc is undef or Bio::LocationI object
    my $loc = $seq->location_from_column(5);


=head2 column_from_residue_number

 Title   : column_from_residue_number
 Usage   : $col = $contig->column_from_residue_number( $seqname, $resnumber)
 Function:

           This function gives the position in the alignment
           (i.e. column number) of the given residue number in the
           sequence with the given name. For example, for the
           alignment

           Seq1/91-97 AC..DEF.GH
           Seq2/24-30 ACGG.RTY..
           Seq3/43-51 AC.DDEFGHI

           column_from_residue_number( "Seq1", 94 ) returns 5.
           column_from_residue_number( "Seq2", 25 ) returns 2.
           column_from_residue_number( "Seq3", 50 ) returns 9.

           An exception is thrown if the residue number would lie
           outside the length of the aligment
           (e.g. column_from_residue_number( "Seq2", 22 )

      Note: If the the parent sequence is represented by more than
      one alignment sequence and the residue number is present in
      them, this method finds only the first one.

 Returns : A column number for the position in the alignment of the
           given residue in the given sequence (1 = first column)
 Args    : A sequence id/name (not a name/start-end)
           A residue number in the whole sequence (not just that
           segment of it in the alignment)

=cut

sub column_from_residue_number {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 Sequence names

Methods to manipulate the display name. The default name based on the
sequence id and subsequence positions can be overridden in various
ways.

=head2 displayname

 Title     : displayname
 Usage     : $contig->displayname("Ig", "IgA")
 Function  : Gets/sets the display name of a sequence in the alignment
           :
 Returns   : A display name string
 Argument  : name of the sequence
             displayname of the sequence (optional)

=cut

sub displayname { # Do nothing
}

=head2 set_displayname_count

 Title     : set_displayname_count
 Usage     : $contig->set_displayname_count
 Function  :

             Sets the names to be name_# where # is the number of
             times this name has been used.

 Returns   : None
 Argument  : None

=cut

sub set_displayname_count {
    my ($self) = @_;
    $self->throw_not_implemented();
}

=head2 set_displayname_flat

 Title     : set_displayname_flat
 Usage     : $contig->set_displayname_flat()
 Function  : Makes all the sequences be displayed as just their name,
             not name/start-end
 Returns   : 1
 Argument  : None

=cut

sub set_displayname_flat { # Do nothing!
}

=head2 set_displayname_normal

 Title     : set_displayname_normal
 Usage     : $contig->set_displayname_normal()
 Function  : Makes all the sequences be displayed as name/start-end
 Returns   : None
 Argument  : None

=cut

sub set_displayname_normal { # Do nothing!
}

=head1 Internal Methods

=head2 _binary_search

 Title     : _binary_search
 Usage     : _binary_search($list,$query)
 Function  :

             Find a number in a sorted list of numbers.  Return values
             may be on or two integers. One positive integer or zero
             (>=0) is the index of the element that stores the queried
             value.  Two positive integers (or zero and another
             number) are the indexes of elements among which the
             queried value should be placed. Negative single values
             mean:

             -1: $query is smaller than smallest element in list
             -2: $query is greater than greatest element in list

 Returns   : array of integers
 Argument  :
             $list  : array reference
             $query : integer

=cut

sub _binary_search {
    my $list   = shift;
    my $query  = shift;
    #
    # If there is only one element in list
    if (!$#{$list} && ($query == $list->[0])) { return (0) }
    # If there are others...
    my $start = 0;
    my $end   = $#{$list};
    (&_compare($query,$list->[$start]) == 0) && do { return ($start) };
    (&_compare($query,$list->[$end])   == 0) && do { return ($end) };
    (&_compare($query,$list->[$start])  < 0) && do { return (-1) };
    (&_compare($query,$list->[$end])    > 0) && do { return (-2) };
    my $middle = 0;
    while ($end - $start > 1) {
        $middle = int(($end+$middle)/2);
        (&_compare($query,$list->[$middle]) == 0) && return ($middle);
        (&_compare($query,$list->[$middle]) <  0) && do { $end   = $middle ; $middle = 0; next };
        $start = $middle; # If &_compare() > 0, move region beggining
    }
    return ($start,$end);
}

=head2 _compare

    Title   : _compare
    Usage   : _compare($arg1,$arg2)
    Function: Perform numeric or string comparisons
    Returns : integer (0, 1 or -1)
    Args    : values to be compared

=cut

sub _compare {
    my $arg1 = shift;
    my $arg2 = shift;
    #
    if (($arg1 =~ /^\d+$/) && ($arg2 =~ /^\d+$/)) { return $arg1 <=> $arg2 }
    else { return $arg1 cmp $arg2 }
}

=head2 _nof_gaps

    Title   : _nof_gaps
    Usage   : _nof_gaps($array_ref, $query)
    Function: number of gaps found before position $query
    Returns : integer
    Args    :
              $array_ref : gap registry reference
              $query     : [integer] a position in a sequence

=cut

#' emacs...
sub _nof_gaps {
    my $list  = shift;
    my $query = shift;
    # If there are no gaps in this contig
    return 0 unless (defined($list) && scalar(@{$list}));
    # Locate query index in gap list (if any)
    my @index = &_binary_search($list,$query);
    # If after all alignments, correct using total number of align
    if ($index[0] == -2) { $query = scalar(@{$list}) }
    # If before any alignment, return 0
    elsif ($index[0] == -1) { $query = 0 }
    elsif ($index[0] >= 0) {
    # If query is between alignments, translate coordinates
    if ($#index > 0) { $query = $index[0] + 1 }
    # If query sits upon an alignment, do another correction
    elsif ($#index == 0) { $query = $index[0] }
    }
    #
    return $query;
}

=head2 _padded_unpadded

    Title   : _padded_unpadded
    Usage   : _padded_unpadded($array_ref, $query)
    Function:

              Returns a coordinate corresponding to
              position $query after gaps were
              removed from a sequence.

    Returns : integer
    Args    :
              $array_ref : reference to this gap registry
              $query     : [integer] coordionate to change

=cut

sub _padded_unpadded {
    my $list  = shift;
    my $query = shift;

    my $align = &_nof_gaps($list,$query);
    $query-- if (defined($list->[$align]) && ($list->[$align] == $query));
    $query = $query - $align;
    #
    return $query;
}

=head2 _unpadded_padded

    Title   : _unpadded_padded
    Usage   : _unpadded_padded($array_ref, $query)
    Function:

              Returns the value corresponding to
              ungapped position $query when gaps are
              counted as valid sites in a sequence

    Returns :
    Args    : $array_ref = a reference to this sequence's gap registry
              $query = [integer] location to change

=cut

#'
sub _unpadded_padded {
    my $list  = shift;
    my $query = shift;

    my $align  = &_nof_gaps($list,$query);
    $query = $query + $align;
    my $new_align = &_nof_gaps($list,$query);
    while ($new_align - $align > 0) {
        $query = $query + $new_align - $align;
        $align  = $new_align;
        $new_align = &_nof_gaps($list,$query);
    }
    # If current position is also a align, look for the first upstream base
    while (defined($list->[$align]) && ($list->[$align] == $query)) {
        $query++; $align++;
    }
    #
    return $query;
}

=head2 _register_gaps

    Title   : _register_gaps
    Usage   : $self->_register_gaps($seq, $array_ref)
    Function: stores gap locations for a sequence
    Returns : number of gaps found
    Args    :
              $seq       : sequence string
              $array_ref : a reference to an array,
                           where gap locations will
                           be stored

=cut

sub _register_gaps {
    my $self     = shift;
    my $sequence = shift;
    my $dbref    = shift;

    $self->throw("Not an aligned sequence string to register gaps")
        if (ref($sequence));

    $self->throw("Not an array reference for gap registry")
        unless (ref($dbref) eq 'ARRAY');

    # Registering alignments
    @{$dbref} = (); # Cleaning registry
    if (defined $sequence) {
        my $i = -1;
        while(1) {
            $i = index($sequence,"-",$i+1);
            last if ($i == -1);
            push(@{$dbref},$i+1);
        }
    } else {
        # $self->warn("Found undefined sequence while registering gaps");
        return 0;
    }

    return scalar(@{$dbref});
}

=head1 Deprecated methods

=cut

=head2 no_residues

 Title     : no_residues
 Usage     : $no = $ali->no_residues
 Function  : number of residues in total in the alignment
 Returns   : integer
 Argument  :
 Note      : deprecated in favor of num_residues() 

=cut

sub no_residues {
    my $self = shift;
    $self->deprecated(-warn_version  => 1.0069,
                      -throw_version => 1.0075);
    $self->num_residues(@_);
}

=head2 no_sequences

 Title     : no_sequences
 Usage     : $depth = $ali->no_sequences
 Function  : number of sequence in the sequence alignment
 Returns   : integer
 Argument  :
 Note      : deprecated in favor of num_sequences()

=cut

sub no_sequences {
    my $self = shift;
    $self->deprecated(-warn_version => 1.0069,
                      -throw_version => 1.0075);
    $self->num_sequences(@_);
}

1;