Question: I am currently learning python. I do not want to use Biopython, or really any imported modules, other than maybe regex so I can understand what the code is doing.

From a genetic sequence alignment, I would like to find the location of the start and end positions of gaps/indels "-" that are next to each other within my sequences, the number of gap regions, and calculate the length of gap regions. For example:

```
>Seq1
ATC----GCTGTA--A-----T

```

I would like an output that may look something like this:

```
Number of gaps = 3

Index Position of Gap region 1 = 3 to 6
Length of Gap region 1 = 4

Index Position of Gap region 2 = 13 to 14
Length of Gap region 2 = 2

Index Position of Gap region 3 = 16 to 20
Length of Gap region 3 = 5

```

I have tried to figure this out on larger sequence alignments but I have not been able to even remotely figure out how to do this.

Answer: 
Here is my suggestion of code, quite straight-forward, short and easy to understand, without any other imported package other than `re`: 

```
import re

def findGaps(aSeq):
    # Get and print the list of gaps present into the sequence
    gaps = re.findall('[-]+', aSeq)
    print('Number of gaps = {0} \n'.format(len(gaps)))
    # Get and print start index, end index and length for each gap
    for i,gap in enumerate(gaps,1):
        startIndex = aSeq.index(gap)
        endIndex = startIndex + len(gap) - 1
        print('Index Position of Gap region {0} = {1} to {2}'.format(i, startIndex, endIndex))
        print('Length of Gap region {0} = {1} \n'.format(i, len(gap)))
        aSeq = aSeq.replace(gap,'*' * len(gap), 1)

findGaps("ATC----GCTGTA--A-----T")

```
A bit of a longer-winded way about this than with regex, but you could find the index of the hyphens and group them by using first-differences:

```
>>> def get_seq_gaps(seq):
...     gaps = np.array([i for i, el in enumerate(seq) if el == '-'])
...     diff = np.cumsum(np.append([False], np.diff(gaps) != 1))
...     un = np.unique(diff)
...     yield len(un)
...     for i in un:
...         subseq = gaps[diff == i]
...         yield i + 1, len(subseq), subseq.min(), subseq.max()

>>> def report_gaps(seq):
...     gaps = get_seq_gaps(seq)
...     print('Number of gaps = %s\n' % next(gaps), sep='')
...     for (i, l, mn, mx) in gaps:
...         print('Index Position of Gap region %s = %s to %s' % (i, mn, mx))
...         print('Length of Gap Region %s = %s\n' % (i, l), sep='')

>>> seq = 'ATC----GCTGTA--A-----T'
>>> report_gaps(seq)
Number of gaps = 3

Index Position of Gap region 1 = 3 to 6
Length of Gap Region 1 = 4

Index Position of Gap region 2 = 13 to 14
Length of Gap Region 2 = 2

Index Position of Gap region 3 = 16 to 20
Length of Gap Region 3 = 5

```

First, this forms an array of the indices at which you have hyphens:

```
>>> gaps
array([ 3,  4,  5,  6, 13, 14, 16, 17, 18, 19, 20])

```

Places where first differences are not 1 indicate breaks. Throw on another False to maintain length.

```
>>> diff
array([0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2])

```

Now take the unique elements of these groups, constrain `gaps` to the corresponding indices, and find its min/max.