Circular sequence comparison: algorithms and applications

Background Sequence comparison is a fundamental step in many important tasks in bioinformatics; from phylogenetic reconstruction to the reconstruction of genomes. Traditional algorithms for measuring approximation in sequence comparison are based on the notions of distance or similarity, and are generally computed through sequence alignment techniques. As circular molecular structure is a common phenomenon in nature, a caveat of the adaptation of alignment techniques for circular sequence comparison is that they are computationally expensive, requiring from super-quadratic to cubic time in the length of the sequences. Results In this paper, we introduce a new distance measure based on q-grams, and show how it can be applied effectively and computed efficiently for circular sequence comparison. Experimental results, using real DNA, RNA, and protein sequences as well as synthetic data, demonstrate orders-of-magnitude superiority of our approach in terms of efficiency, while maintaining an accuracy very competitive to the state of the art.


Biological motivation
Circular molecular structures are present, in abundance, in all domains of life: bacteria, archaea, and eukaryotes; and in viruses. They can be composed of either amino or nucleic acids. The following is an overview of such occurrences, and exhaustive reviews can be found in [1] (proteins) and [2] (DNA).
Double-stranded, circular chromosomes and plasmids are found in most bacteria and archaea. Whole-genome comparison is a very useful tool in classifying bacterial strains, as well as inferring phylogenetic associations between them. This is due to the dense structure of bacterial chromosomes, caused by the absence of introns, and the organisation of genes into operons. The extended benefit of aligning plasmids is the ability to identify important genes, such as antibiotic resistance genes, thereby enabling their study and exploitation by genetic engineering techniques [3].
The most familiar examples of such structures in eukaryotes are mitochondrial (MtDNA) and plastid DNA. MtDNA is, in most cases, inherited solely from the mother, and so is generally conserved. Human MtDNA is double-stranded, with a length of 16,569 base pairs (bp), consisting of just 37 genes encoding 13 proteins and 24 RNA molecules [4]. The absence of recombination in these sequences allows them to be used as simple indicators of phylogenetic evolution, and their high mutation rate is a powerful discriminative feature [5,6]. There also exist smaller structures, called extrachromosomal circular DNA, which are similar to plasmids in bacterial cells. They are described as one of the characteristics of genomic plasticity in eukaryotes [7] and may derive from MtDNA [8].
It is common knowledge that many viral genomes are circular. Viral genomes vary greatly in size and structure. They can be made up of either RNA or DNA, and can be single-or double-stranded. Multiple sequence alignment of viral genomes can be useful in the elucidation of novel sites of interest [9], as well as the inference of evolutionary relationships [10]. This is particularly important in studying their pathogenicity, due to the rapid rate of mutation of viruses. Viroids are plant pathogens that comprise very small, single-stranded, circular RNA. Their multiple sequence alignment could prove useful in the analysis of their secondary structures and, therefore, the mechanisms by which they infect host plant cells [11].
Naturally-occuring circular proteins are found in both prokaryotes and eukaryotes [1]. Bacteriocins are very small toxins produced by bacteria in order to compete with closely-related bacterial strains. Many of these are circular, including gassericin A, found in Lactobacillus gasseri LA39 [12], and circularin A, found in Clostridium beijerinckii [13]. An interesting phenomenon known to occur naturally in linear protein structures is circular permutation [14]. This can be exemplified by swaposins: proteins highly-similar to saposins, resulting from circularly permuted linear peptide sequences [15]. The ability to align linear sequences from circular proteins can significantly speed up and enhance their analyses, and could also lead to the discovery of novel pairs of circularly permuted proteins.

Our problem
Conventional tools, designed for linear sequences, could yield an incorrectly high genetic distance between closely related circular sequences. Indeed, when sequencing molecules, the position where a circular sequence starts can be totally arbitrary. Due to this arbitrariness, a suitable rotation of one sequence would give much better results for a pairwise alignment, and hence highlight a similarity that any linear alignment would miss. A practical example of the benefit this can bring to sequence analysis is the following. Linearized human (NC_001807) and chimpanzee (NC_001643) MtDNA sequences, obtained from GenBank [16], do not start in the same region. Their pairwise sequence alignment using EMBOSS Needle [17] (default parameters) gives a similarity of 85.1 % and consists of 1195 gaps. However, taking different rotations of these sequences into account yields a much more significant alignment with a similarity of 91 % and only 77 gaps. This example motivates the design of efficient algorithms that are specifically devoted to the comparison of circular sequences [18][19][20].
In this paper, we consider the pairwise circular sequence comparison problem. Under the edit distance model, it consists in finding an optimal linear alignment of two circular strings. This problem, for two strings x and y of length m and n ≥ m, respectively, can be solved under the edit distance model in time O(nm log m) [21]. Several other super-quadratic [22] and approximate quadratic-time [23] algorithms exist. Trivially, for molecular biology applications, the same problem can be solved in time O(nm 2 ), if extending the problem with scoring matrices and affine gap penalty scores. A direct application of pairwise circular sequence comparison is progressive multiple circular sequence alignment [11,24,25]. Multiple circular sequence alignment has also been considered in [26] under the Hamming distance model.
To the best of our knowledge, there is no fast (that is, with sub-quadratic time complexity) and exact (or at least very accurate) algorithm for circular sequence comparison under some realistic model (that is, allowing indels). Taking into account edit distance rather than Hamming distance is computationally challenging as the search space for seeking similarity is wider. Algorithms that speed up the process of string matching, by filtering out candidate positions in which a particular string can never occur, are known as filters. Filters that work for Hamming distance do not work in general for edit distance [27] as well. An exception to this are the q-gram filtering techniques [28] that have successfully been used for string matching under the edit distance model (e.g. [29][30][31]), as well as for multiple local alignments, both under the Hamming [32] and edit [31] distance models.

Our contribution
We present new efficient q-gram-based methods for pairwise circular sequence comparison. Specifically, our contribution is threefold.
1. We introduce the β-blockwise q-gram distance between two strings x and y, that is, a more powerful generalization of the q-gram distance introduced as a string distance measure in [28]. Intuitively, and similarly to [29][30][31], this generalization comprises partitioning x and y in β blocks each, as evenly as possible, computing the q-gram distance between the corresponding block pairs, and then summing up the distances computed blockwise. 2. We present an algorithm based on the suffix array [33] that finds the rotation of x such that the β-blockwise q-gram distance between the rotated x and y is minimal, in time and space O(βm + n) , where m = |x| and n = |y|, thereby solving exactly the circular sequence comparison problem under the β-blockwise q-gram distance measure. We also present a simple heuristic algorithm to solve an approximate version of the problem. 3. We present an experimental study, using real and synthetic data, which demonstrates orders-of-magnitude superiority of our approach, in terms of efficiency, while maintaining an accuracy very competitive to the optimal obtained after considering all rotations of x against y using EMBOSS Needle. The paper is organized as follows. "Definitions and properties" section gives some preliminary definitions, notation, and properties. "Algorithms" section describes two algorithms, one is a heuristic approach and the other is an exact algorithm for circular sequence comparison under the β-blockwise q-gram distance measure. "Implementation" section provides details of the implementation of the algorithms. "Experimental results" section presents the experimental results of the performance and accuracy of the algorithms. Finally, "Conclusions" section gives some concluding remarks and future proposals. A preliminary version describing a subset of the results in this paper appeared in [34].

Definitions and properties
We begin with a few definitions, following [35]. We think of a string x of length m as an array . We refer to any string x ∈ q as a q-gram. The empty string of length 0 is denoted by ε. A string x is a factor of a string y if there exist two strings u and v, such that y = uxv. Let x be a non-empty string and y be a string. We say that there is an occurrence of x in y, or, simply, that x occurs in y, when x is a factor of y. The Parikh vector associated with a string w ∈ * is denoted by P(w) and represents a vector of size | |, where each component denotes the number of occurrences in w of the corresponding letter from .
Consider the strings x, y, u, and v, such that y = uxv. If u = ε, then x is a prefix of y. If v = ε, then x is a suffix of y. We denote by SA the suffix array of y of length n, that is, an integer array of size n storing the starting positions of all lexicographically sorted suffixes of y, i.e. for all 1 ≤ r < n ,  [36].
A circular string of length m can be viewed as a traditional linear string which has the left-and right-most letters wrapped around and glued together in some way. Under this notion, the same circular string can be seen as m different linear strings, which would all be considered equivalent. Given a string x of length m, we denote by the ith rotation of x and x 0 = x. For instance, the string x = x 0 = abababbc has the following rotations: x 1 = bababbca, x 2 = ababbcab, and so on.
We give some further definitions following [28]. The qgram profile of a string x is the vector G q (x), where q > 0 and G q (x)[v] denotes the total number of occurrences of q-gram v ∈ q in x. The q-gram distance between two strings x and y is defined as Note that D q is a pseudo-metric as D q (x, y) can be 0 even if x � = y. D q has the following properties [28] for all x, y, z ∈ * of length at least q.
Example 1 Let x = GGAGTCTA, y = TTCTAGCG, and q = 3. Table 1 shows the q-gram profiles of strings x and y and the q-gram distance between them. Each row represents the frequency of a q-gram in the given string. For succinctness of presentation, only those rows with frequency greater than zero (in either string) are shown, as well as rows representing AAA, CCC, GGG, and TTT as points of reference.
For a given integer parameter β ≥ 1, we define a generalization of the q-gram distance in (1) by partitioning x and y in β blocks as evenly as possible, and computing the q-gram distance between each pair of blocks, one from x and one from y. The rationale is to enforce locality in the resulting overall distance. For the sake of presentation in the rest of the paper, we assume that the lengths |x| = m and |y| = n are both multiples of β, so that x and y are conceptually partitioned into β blocks, each of size m/β for x and n/β for y.

Definition 1
Given strings x of length m and y of length n ≥ m and integers β ≥ 1 and q > 0, the β-blockwise qgram distance D β,q (x, y) is defined as Example 2 Following Example 1, let x = GGAGTCTA and y = TTCTAGCG, q = 3, and β = 2. Further let x 1 = GGAG, x 2 = TCTA and y 1 = TTCT, y 2 = AGCG be the two blocks of x and y, respectively. Table 2 shows the q-gram profiles of strings x 1 , x 2 , y 1 , and y 2 ; and the q-gram distance between x 1 and y 1 and the q-gram distance between x 2 and y 2 . Table 1 q-gram profiles of strings x and y and q-gram distance D q (x, y) = 8 between them Table 2 q-gram profiles of strings x 1 , x 2 , y 1 , and y 2 ; q-gram distance between x 1 and y 1 ; and q-gram distance between x 2 and y 2 , giving D β,q (x, y) = 8  11:12 In this paper, we consider the following problem, where we search for the ith rotation of x that minimizes its blockwise distance from y as defined in (2). Ties are broken arbitrarily.
Circular Sequence Comparison (CSC) Input: strings x and y of lengths m and n ≥ m, respectively, and integers β ≥ 1 and q < m Output: i such that D β,q (x i , y) is minimal

Algorithms
We use the following result to first give a naïve solution to the CSC problem.
We then apply Lemma 1 to each pair of blocks of x and y separately.
The naïve algorithm, denoted by nCSC, computes for x ′ = xx the values for all 0 ≤ i < m; we report position i such that δ i is minimal. This requires the application of Lemma 2, m times. Therefore, we obtain the following.

Algorithm hCSC: a Heuristic algorithm
Here we give a simple heuristic algorithm, denoted by hCSC, to solve the CSC problem faster than nCSC, and return an approximation of the best rotation.
Step 1: We split x ′ = xx in 2β non-overlapping string blocks of length m/β. We obtain strings x 0 , , for all 0 ≤ i < 2β . We split y in β non-overlapping string blocks of length n/β. We obtain strings y 0 , y 1 , . . . , y β−1 , such that Step 2: For a given sequence x j , . . . , x j+β−1 of strings and y, we compute the β-blockwise q-gram distance as follows We compute δ j , for all 0 ≤ j ≤ β. We choose j best = j such that δ j is minimal, for all 0 ≤ j ≤ β. In other words, we have found a window of length m starting at position j best , such that (j best + 1) mod (m/β) = 0, consisting of β blocks of length m/β each, that minimizes its β-blockwise q-gram distance from y.
Step 3: To perform a refinement on the position of the window, we consider all starting positions included in TTT 0 the two blocks starting at positions j best and j best − m/β . This includes 2m/β − 1 starting positions in totalwe do not need to consider position j best − m/β as this was already considered by another window in Step 2. Similarly to Step 2, we obtain the β-blockwise q-gram distance δ i between the window starting at position i and y, for all j best − m/β < i ≤ j best + m/β − 1. We report position i best = i such that δ i is minimal, for all Step 2 can be done in time O(β(m + n)). In Step 3, the blockwise q-gram distance δ i between a single window and y can be com- Algorithm saCSC: an exact suffix-array-based algorithm The above heuristic hCSC does not guarantee to find the exact value i, for which δ i = D β,q (x i , y) is minimal. In particular, when we identify j best in Step 2, that is, the j for which δ j is minimal, we take into account only the values of j such that (j + 1) mod (m/β) = 0. Thus, Step 3 cannot guarantee that i best , the local minimum obtained by shifting the window m/β positions to the right and left of j best , is minimal for all 0 ≤ i < m. In this section, we give a fast and exact algorithm, denoted by saCSC, to find i such that δ i = D β,q (x i , y) is minimal, based on the suffix array (see "Definitions and properties" section). We partially follow the idea from [37]. This work investigates the string matching problem in the setting of k-abelian equivalences: two strings are considered k-abelian equivalent for some positive integer k, if they have the same length and share the same factors of length at most k, including multiplicities. Note that if k is greater than or equal to the string's length, then the strings must be equal. A version of this result, called extended k-abelian equivalence, focuses only on the factors of length k. By setting k = q, it is quite straightforward to notice the equivalence with q-grams. Therefore, in order to avoid confusion we will refer to the former notion from now on as q-abelian equivalence.
In [37], the authors propose a linear-time algorithm to solve the string matching problem when looking at q-abelian equivalent strings: given a string x of length m, a string y of length n ≥ m, and a positive integer q < m, all factors of y that are q-abelian equivalent to x can be found in time and space O(m + n). The idea of the algorithm in [37] consists in constructing the suffix array of the string xy, and ranking sets of identical q-length prefixes of suffixes in the suffix array in the order of their appearance. Then it constructs new strings based on this ranking, and solves the problem as in the jumbled matching case [38], i.e. identifying all factors of y that have the same Parikh vector as x.
We first describe our algorithm for a single block (β = 1) and then address the general case (β ≥ 1). Basic algorithm for β = 1. We construct the suffix array of the string xxy and assign a rank to the prefix with length q of each suffix with length at least q, based on its order in the suffix array. That is, the first i 0 suffixes, of length at least q, in the suffix array, all sharing the same prefix of length q, will get rank 0; the next i 1 suffixes, of length at least q, sharing the same prefix of length q, different from the previous one, will get rank 1, and so on. Next, based on this ranking, we construct two new strings x ′ of length 2m − q + 1 and y ′ of length n − q + 1 , such that x ′ [i] = j, if j is the rank of the q-length prefix of the (i + 1)th suffix of xx in the suffix array of xxy (the same goes for y). It is not difficult to see that the ranks go up at most to value m + n − q + 1. However, we can reduce this value to m + 2 by introducing two new ranks a x and a y : we can conceptually replace by a x every letter of x ′ that does not occur in y ′ , and by a y every letter of y ′ that does not occur in x ′ . Hence we can consider that the new strings x ′ and y ′ are defined over an integer alphabet of size at most min(n − q + 1, m) + 2 ≤ m + 2.
Example 3 Let x = GAGTCTA, y = TCTAGCG, and q = 3. We denote xxy by z.  We observe that when identifying the q-gram distance between two blocks, we can apply the idea in [37], with the only difference that we should also maintain a Parikh vector that stores the differences between the number of occurrences of q-grams (in fact the new letters given by the ranks) in the current block of xx and y. Moreover, at the time of the construction of y ′ , we also construct a Parikh vector P(y ′ ), storing for each letter of y ′ , the number of its occurrences in y ′ . Notice that |P(y ′ )| ≤ m + 2. Later on, when computing the q-gram distances, we can construct another vector diff to store the letter differences between P(y ′ ) and the Parikh vector covering the m − q + 1 letters of x ′ associated with a window of length m on the string xx. This gives us the current Parikh difference and, in fact, represents the q-gram distance between the two analyzed blocks, where |diff| ≤ m + 2. Apart from these, we only need another vector δ of size m, which stores at each position i the actual q-gram distance δ i between y and the window starting at position i in xx, which is the ith rotation x i of x.
We use a sliding window of length m to maintain the above information. When the window is shifted one position to the right, we have to add to the differencevector diff the previous first element of the window, and deduct from it the current last element of it. The distance δ i between y ′ and the factor of x ′ starting at position i is thus updated using, in addition, the value of the q-gram distance δ i−1 as follows. If, after adding the previous first element to the vector, we have a non-positive value at this position, we update the distance by decreasing the previous value by 1; otherwise, we increase it by 1. If, after deducting the current last element to the vector, we have a non-negative value at this position, we update the distance by decreasing the previous value by 1; otherwise, we increase it by 1. The distance will never be less than the number of occurrences of a y . Furthermore, if the previous first element was a x , the new distance decreases by 1, and for every newly added a x , it increases by 1. As these operations require constant time, after going once through x ′ with y ′ , we obtain the list of distances δ i from y to each rotation x i in linear time.
We are now able to give a more formal description of the steps to solve the CSC problem for β = 1, which follow a dynamic programming scheme.
Step 1: Construct the SA, iSA, and LCP of xxy. Rank the q-length prefixes of suffixes using LCP-array queries. Construct x ′ and y ′ , as well as P(y ′ ), the Parikh vector storing, for each letter of y ′ , the number of its occurrences in y ′ ; making proper use of letters a x and a y , the ranks that do not occur in either y ′ or x ′ , respectively. Further, create diff = P(y ′ ) and δ 0 = |P(y ′ )|−1 i=0

z[i] G A G T C T A G A G T C T A T C T A G C G
x ′ [i] a x a x a x 2 0 1 a x a x a x a x 2 0 y ′ [i] 2 0 1 a y a y The table below represents vector diff, right after the execution of Step 1, which implies that δ 0 = 5. Step 2: Read the first m − q + 1 letters of x ′ , which constitute our sliding window of length m on the string xx. When reading letter x ′ [i], update diff by decreasing by 1 the value of the newly read letter, and update δ 0 , by either increasing the current value of the distance when there were read too many of the current letters, or decreasing it, when more of these letters still occur in y ′ Example 5 Following Example 4, the table below represents vector diff, right after the execution of Step 2, which implies that δ 0 = 6. Step 3: Let i be the current position in x ′ and repeat this step, one position at a time. Shift the window to the right, update the information for diff and calculate δ i+1 , based on this information, sequentially applying the two following rules Example 6 Following Example 5, the table below represents vector diff at iteration i ′ = 3 of Step 3, which implies that δ 0 = 4. This is in fact the best rotation of x, that is, Correctness Steps 1 and 2 are trivially correct as at the end of them we have that diff is the difference between P(y ′ ) and the vector corresponding to the window. These operations follow directly from the definitions of SA and LCP, and are followed by a simple traversal of the suffix array in order to obtain the ranks and create the P(y ′ ) and diff vectors. Also, δ 0 , which was initially the number of letters in y ′ , is decreasing as long as the difference between the vectors for a specific letter is non-negative (thus, we still have more occurrences of that letter in y ′ compared to the window), and increasing otherwise. In Step The value of δ i never goes below the number of occurrences of a y in y ′ (it is equal to that, when all other elements of diff are 0) and represents the q-gram distance between y and x i , the corresponding window of length m starting at position i in xx. Analysis In Step 1, constructing SA, iSA, and LCP of xxy can be done in time and extra space O(m + n) ("Definitions and properties" section). Furthermore, the construction of x ′ , y ′ , P(y ′ ), diff, and δ 0 is done with the same time and space cost. In Step 2, updating diff and δ 0 after reading each letter takes constant time, as we execute two operations, thus O(m) in total. Constant time is required for each iteration in Step 3 to compute the value of δ i , 1 ≤ i < m, and update diff, since a constant number of operations are executed, thus O(m) in total. Hence, we can solve the CSC problem for β = 1 in time and space O(m + n). General algorithm for β ≥ 1. We can now generalize this algorithm to solve the CSC problem for any β ≥ 1, which gives algorithm saCSC. We maintain a Parikh vector for each block, and apply the above basic algorithm for the jth block in each string, computing their q-gram distance. If we denote by P j (y ′ ) and diff j , for all 0 ≤ j < β , the β Parikh vectors of y ′ and of the q-gram distances, respectively, as well as by δ i,j the q-gram distance between the jth block of y and x i , then the updates will be given by the formulae below. Hence, at each position i < m, we can update all of the β Parikh vectors corresponding to the blocks, as previously described, in time O(β). As an example, see here the modification of the previous Step 3, with the other two steps being easily adapted in a similar fashion.
Step 3': When shifting the window one position to the right from position i, update the information for every diff j , where 0 ≤ j < β, as follows and calculate δ i+1,j , based on this information, sequentially applying the two following rules Therefore, we obtain the following result.
Theorem 1 Algorithm s solves the CSC problem in aCSCO(βm + n) time and space.

Implementation
We implemented algorithms nCSC, hCSC, and saCSC as the program CSC. Given one of the three methods, two sequences x and y in (Multi)FASTA format, the number β of blocks, and the length q of the q-grams, CSC finds the rotation of x (or an approximation of it) that minimizes its β-blockwise q-gram distance from y. The implementation is distributed under the GNU General Public License (GPL), and it is available freely at http://www. github.com/solonas13/csc.
For comparison purposes, we implemented a naïve algorithm that compares all rotations of x against y using the Needleman-Wunsch algorithm [39] with substitution matrices and affine gap penalty scores [40]; we denote this implementation by cNW. We also implemented the following heuristics. We first use the Smith-Waterman local alignment algorithm [41] to search for the best local alignment of x and y and then use a central match from this local alignment to anchor the global alignment (see also [11]); we denote this implementation by hSW.

Refining algorithm saCSC
The application of the β-blockwise q-gram distance via algorithm saCSC suggests that an optimal or a closeto-optimal rotation of x can be found when compared to cNW. Due to the locality property offered by the newly introduced distance notion, it is reasonable to assume that the close-to-optimal rotation returned by saCSC may be refined via some quick heuristics that take into consideration the blocks at both ends.
Let x i be the close-to-optimal rotation of x returned by saCSC. We introduce a new input parameter 0 < p ≤ β 3 , which defines the length L of the prefixes and suffixes of x i and y to be considered in the refinement as follows: rotation of x ′′ is compared to y ′′ excluding when a $ letter is found at index 0 of the rotation of x ′′ . We measure the similarity between the strings for which equality between letters are positively valued; inequalities, insertions, and deletions are negatively valued; and comparisons involving $ are neither positively nor negatively valued. The goal of rotating x ′′ serves to find the rotation that maximizes the similarity to y ′′ and, to this end, we make use of the Needleman-Wunsch algorithm. The rotation of x ′′ which results in the maximum score is chosen as the best rotation, and hence, the final rotation x i ′ of x is computed based on this rotation of x ′′ . Ties are broken arbitrarily. We denote this new algorithm, consisting of saCSC and the refinement stage, by saCSCr.
The application of the Needleman-Wunsch algorithm on strings of length 3L has a time complexity of O(L 2 ). Considering all rotations of x ′′ results in a time complexity of O(L 3 ) for the refinement step. Overall, saCSCr takes time O(βm + n + L 3 ).

Example 7
Consider the following pair of strings obtained from saCSC We take p block(s) of the prefix of x i , concatenate it with a string of equal length L comprised only of letter $, where $ / ∈ �, and concatenate that with p block(s) of the suffix of x i to form a new string x ′′ of length 3L. We do the same with y to form a new string y ′′ .
The refinement algorithm works by taking all rotations of x ′′ and comparing their similarity to y ′′ . Each x i = GACACCCCCCACAGTTTATGTAGCTT…ACCCCGAACCAACCAAACCCCAAA y = GTTTATGTAGCTTACCTCCCCAAAGC…CAAACCCCAAAGACACCCCACACA and the following pair of strings formed for L = 25.
The Needleman-Wunsch algorithm for all rotations of x ′′ and string y ′′ gives the following optimal alignment which tells us that a refined rotation is in fact

Experimental results
The following experiments were conducted on a desktop computer using one core of Intel ® Core TM i7-2600 CPU at 3.4GHz and 12GB of RAM under 64-bit GNU/Linux. All programs were compiled with gcc version 4.7.3. We used both synthetic data and real data. All input datasets referred to in this section are publicly maintained at the same web-site. First, in "Accuracy"-"Time performance" sections, we establish the quality (accuracy and performance) of our methods. Then, in "Application to synthetic data"-"Application to real data" sections, we show applications of our methods.

Accuracy
We began with simulating three DNA sequence datasets using INDELible [42], with each dataset consisting of 12 sequences (denoted by α), each of length approximately 2500 bp (denoted by γ). INDELible produces linear sequences with substitutions, insertions, and deletions at rates defined by the user. Three unique substitution rates (denoted by θ) were set, per dataset, using the substitution model JC69 (Jukes-Cantor, 69): 5, 20, and 35 %. The insertion and deletion rates were set, respectively, to 4 and 6 % (denoted by κ and ω), relative to substitution rate of one, similar to those observed in MtDNA in primates and mammals [25]. We refer to these datasets as Original.
To allow for comparison of the performance of the algorithms in realigning randomly rotated sequences, which should be similar to those obtained from sequencing circular DNA structures, such as MtDNA, one random rotation was generated in each sequence in all datasets, creating new datasets which will be referred to as Random. Using the three Random datasets allowed us to test the accuracy of hCSC and saCSC; notice that nCSC and saCSC always return the same rotation. For each Random dataset, an all-against-all sequence comparison was performed. That is, all 66 possible pairs of sequences in each dataset were given as input to both hCSC and saCSC. β was set to ⌈ √ m⌉ = 50 and q was set to ⌈log | | m⌉ = 6. The resultant re-rotated sequences were aligned using EMBOSS Needle (default parameters) and the similarity scores were compared to those of the Original and Random datasets, which were input directly to EMBOSS Needle (default parameters). The results can be found in Fig. 1.
The results show that: (a) hCSC and saCSC yield significantly improved similarity scores compared to those obtained from giving Random datasets as input directly to EMBOSS Needle; and (b) hCSC and saCSC yield similarity scores that are identical or almost identicalnotice that the black (Original), green (hCSC), and blue (nCSC/saCSC) points coincide-to those obtained from giving Original datasets as input directly to EMBOSS Needle. This implies that algorithms hCSC, nCSC, and saCSC return the rotation maximizing the similarity score for all pairwise comparisons.
Hence, we establish here that the introduced distance measure coupled with the respective algorithms consistently yield a very high accuracy, compared to the standard measure [17,39,40], for both low and high substitution rates.

Time performance
We then compared the time performance of the algorithms. Each algorithm was given a pair of randomly generated sequences starting from m = n = 50 bp and doubling 8 times to a length of m = n = 12, 800 bp. It was expected that the slowest algorithm would be cNW which runs in time O(nm 2 ). Then it would be algorithm nCSC which runs in time O(m(m + n)), then algorithm hCSC, which runs in time O β(m + n) + m(m+n) β , and lastly algorithm saCSC, which runs in time O(βm + n).
Initially, β was set to ⌈ √ m⌉ and q was set to ⌈log | | m⌉ . The results in Fig. 2 demonstrate orders-of-magnitude superiority of saCSC compared to cNW and nCSC, confirming our theoretical findings. Algorithm hCSC is the second fastest. Although β was set to ⌈ √ m⌉, saCSC clearly outperforms hCSC, due to the use of a highly optimized implementation of the suffix-array construction [43], thus highlighting the importance of suitably implemented data structures such as suffix arrays.
Since the time complexities of hCSC and saCSC depend on β, we repeated the same experiment with these two algorithms setting β to ⌈m/25⌉ and q to ⌈log | | m⌉-  More algorithms could have been included in the comparison but their (at least) quadratic time complexity [22,23] prevents them from competing with saCSC.

Application to synthetic data
For evaluating the proposed methods for circular sequence comparison in some relevant application, we also implemented the following pipeline for distancebased phylogenetic reconstruction of a dataset with N circular sequences.
1. For each pair (x, y) of the N sequences, we use one method for circular sequence comparison to compute the best rotation x i . 2. A similarity score for (x i , y) is then computed using EMBOSS Needle (default parameters) and stored in cell [x, y] of an N × N similarity score matrix. 3. The similarity score matrix is transformed into a distance matrix by converting each score into a distance relative to the maximum score in the similarity score matrix. 4. Neighbour joining clustering is performed on the distance matrix, using NINJA [44], to produce a phylogenetic tree.
Phylogenetic trees were constructed by NINJA [44], for the aforementioned Random datasets, using output from the following algorithms: cNW (EMBOSS default parameters), hSW (see introduction of "Implementation" section, EMBOSS default parameters), and saCSCr (β = 50, q = 5, and p = 1). Notice that, output from cNW should be the same as from EMBOSS Needle (default parameters) with the Original datasets as input. In terms of accuracy, the Robinson-Foulds (RF) distance metric [45,46] was used to compare the three resultant phylogenetic trees with the tree resulting from EMBOSS Needle (default parameters) on the Original and Random datasets, denoted by NW(o) and NW(r), respectively. RF distance can be defined as the number of operations required to transform one tree in to another. If two isomorphic trees share the same labelling then they have an RF distance of 0. The results displayed in Table 3 clearly show that saCSCr and cNW produce the most accurate results with these nine datasets. As also shown in [11], hSW followed by EMBOSS Needle (default parameters) can often result in sub-optimal global alignments. In terms of time performance, the elapsed time required for each method to process each dataset was recorded and the results are displayed in Table 4. It is clear, from the results presented heretofore, that saCSCr outperforms all other algorithms by at least one order of magnitude.

Application to real data
We have concluded thus far that using β = ⌈ √ m⌉ and q = ⌈log | | m⌉ results in a reasonable trade-off between running time and accuracy. In the following section, where necessary, we adopt these values and multiply or divide them by a constant factor (factor of two), depending on the length of the input sequences.

DNA sequences
Pairwise sequence comparison. As the input dataset, we used two real sequences from GenBank: human  Table 3 RF distances between the tree obtained from the NW(o) and those obtained from NW(r), cNW, hSW, and saCSCr The number of sequences in the dataset is denoted by α; γ denotes their lengths; θ denotes the substitution rate; κ and ω denote the relative insertion and deletion rates, respectively  11:12 (NC_001807) and chimpanzee (NC_001643) MtDNA sequences. The MtDNA genome size for human is 16,571 bp and for chimpanzee is 16,554 bp. Their pairwise sequence alignment using EMBOSS Needle (default parameters) gives a similarity of 85.1 %. We used cNW (EMBOSS default parameters) to obtain the rotation of NC_001807 that maximizes its similarity score with NC_001643. This experiment took approximately 28 h and the resultant rotation 578 of NC_001807 improved the similarity score to 91 %. This result was then compared to those obtained from saCSC (equivalent to saCSCr with p = 0) and saCSCr with varying parameters, displayed in Table 5.
The convergence of the results after the additional step of refinement (see Table 5 in italics) demonstrates the convenience and necessity of saCSCr.
For clarity of presentation hereafter, instead of using β , we denote by ℓ the length of the block chosen in algorithm saCSCr.
We repeated this experiment with the human and gorilla (NC_011120) MtDNA sequences. The MtDNA genome size for gorilla is 16,412 bp. Their pairwise sequence alignment using EMBOSS Needle (default parameters) gives a similarity of 83.5 %. After using saC-SCr to rotate sequence NC_001807 (ℓ = 50, q = 5, and p = 1), EMBOSS Needle (default parameters) gave a significantly improved similarity of 88.4 %.
Finally, note that the experiments which used saCSC and saCSCr each took a fraction of a second to run.
Distance-based phylogenetic reconstruction Three datasets of 16 primate, 12 mammalian and 19 mixed mammalian and primate MtDNA sequences, of average length 16,500 bp, were obtained from GenBank. We followed the same pipeline as described in "Application to synthetic data" section. The RF distance between the trees produced by cNW (EMBOSS default parameters), and the trees produced by saCSCr (ℓ = ⌈ √ m⌉ = 129, q = 5, and p = 1) followed by EMBOSS Needle (default parameters), was 0.

RNA sequences
Eighteen viroid sequences were obtained from RefSeq, a database of curated molecular biological sequences [47]. Their lengths and target hosts vary, ranging from 348 to 371 bp and infecting peppers and citrus fruits, respectively. We followed the same pipeline as described in "Application to synthetic data" section. The RF distance between the tree produced by cNW (EMBOSS default parameters), and the tree produced by saCSCr (ℓ = ⌈ √ m⌉ = 19, q = ⌈log | | m⌉ = 5 , and p = 1) followed by EMBOSS Needle (default parameters), was 0.

Protein sequences
Linear, circularly-permuted protein sequences Eight sequences of proteins, of average length 950 amino acids, belonging to β-glucosidase family [48] were obtained from the UniProt protein database [49]. We followed the same pipeline as described in "Application to synthetic data" section. The RF distance between the tree produced by cNW (EMBOSS default parameters), and the tree produced by saCSCr (ℓ = ⌈ √ m⌉ = 31, q = ⌈log | | m⌉ = 5, and p = 1) followed by EMBOSS Needle (default parameters), was 0.
Naturally-occurring circular proteins Ten bacteriocin protein sequences, of average length 20 amino acids, were obtained from Cybase [50], a database of cyclical protein sequences. We followed the same pipeline as described in "Application to synthetic data" section. The RF distance between the tree produced by cNW (EMBOSS default parameters), and the tree produced by saCSCr (ℓ = 2⌈ √ m⌉ = 10, q = 2⌈log | | m⌉ = 6, and p = 1) followed by EMBOSS Needle (default parameters), was 0.

Table 4 Elapsed-time comparison (in seconds) for algorithms cNW, hSW, and saCSCr
The number of sequences in the dataset is denoted by α; γ denotes their lengths; θ denotes the substitution rate; κ and ω denote the relative insertion and deletion rates, respectively  Table 5 Rotations of GenBank sequence NC_001807 obtained when compared to NC_001643 with varying parameters of saCSCr