Skip to content

Calculate the Number of Gap Positions in a Sequence Alignment (and Coverage) #491

Closed

Description

@lafita

I have not found a way to easily calculate the number of gap positions in a sequence alignment. I was using the function AlignedSequence.getNumGaps(), which is said to return the number of gaps in the sequence, but actually returns the number of gap openings (without taking the length of the gap into account), which I found it a bit confusing.

I think it could be useful to have a function AlignedSequence.getNumGapPositions() that returns the total number of gaps (with symbol -) in the aligned sequence and documents properly the differences with the other function.

The applications of such a function would be to calculate the coverage of the sequence alignment on one of the aligned sequences. I also could not find a function to calculate coverage, but it should be easily calculated from the original sequence length, the alignment length and the number of gap positions:

Coverage = ( alignment length - number of gap positions ) / sequence length

Activity

  1. andreasprlic commented on May 23, 2016

    @andreasprlic
    Member

    Here some code to calculate alignment overlaps and over scores. It would be good to add a couple of utility methods for this. In particular the replaceAll("-",""); on the aligned sequence representation is rather ugly...

                SequencePair<ProteinSequence, AminoAcidCompound> alignment = smithWaterman.getPair();
    
                int numIdenticals = alignment.getNumIdenticals();
    
                int aligLength = alignment.getLength();
    
                float percentIdenticals = (numIdenticals / (float) aligLength);
    
                // test overlaps
    
                int l1 = prot1.getLength();
                int l2 = prot2.getLength();
    
                int size = alignment.getLength();
    
                AlignedSequence alignedSequence1 = alignment.getAlignedSequence(1);
                AlignedSequence alignedSequence2 = alignment.getAlignedSequence(2);
    
                String alignedSeq1 = alignedSequence1.getSequenceAsString().replaceAll("-","");
                String alignedSeq2 = alignedSequence2.getSequenceAsString().replaceAll("-","");
    
                int size1 = alignedSeq1.length();
                int size2 = alignedSeq2.length();
    
                float overlap1 = size1 / (float) l1;
                float overlap2 = size2 / (float) l2;
  2. lafita commented on May 24, 2016

    @lafita
    MemberAuthor

    Thanks @andreasprlic. I ended up using the method AlignedSequence.getSequenceAsString() and counting the number of "-" positions, which is similar to what you proposed here, but it would be nicer (and optimal) to have a method that counts internally the number of gap positions (and maybe others that calculate coverage and sequence identity from AlignedSequence directly).

    Another concern in using the getSequenceAsString and counting "-" is that there might be an implementation of AlignedSequence where gaps are represented by another symbol, so it is not completely safe. Internally the Compound GAP is used, so this is safe to count those.

    I will make a pull request with the new methods and create some tests.

  3. added this to the BioJava 5.0.0 milestone on May 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationJavadoc relatedenhancementImprovement of existing code or methodnew featureNew method or data structure

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions