Skip to content

Commit

Permalink
fixed validation steps
Browse files Browse the repository at this point in the history
  • Loading branch information
nathandunn committed Sep 14, 2016
1 parent fbcfd7c commit 73116a4
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 4 deletions.
Expand Up @@ -149,9 +149,8 @@ class MultiSequenceProjection extends AbstractProjection {
// length - ( i - offset ) + offset
// length - i + (2 * offset)
int alteredInput = discontinuousProjection.getBufferedLength(1) - input + projectionSequence.offset
return discontinuousProjection.projectReverseValue(alteredInput) + outputOffset
}
else{
return discontinuousProjection.projectReverseValue(alteredInput) + outputOffset
} else {
return discontinuousProjection.projectReverseValue(input - inputOffset) + outputOffset
}
}
Expand Down Expand Up @@ -436,9 +435,60 @@ class MultiSequenceProjection extends AbstractProjection {
return returnMap
}

Boolean overlaps(ProjectionSequence projectionSequenceA, ProjectionSequence projectionSequenceB) {
assert projectionSequenceA.name==projectionSequenceB.name
if (projectionSequenceA.start <= projectionSequenceB.start && projectionSequenceA.end >= projectionSequenceB.start) {
return true
}
if (projectionSequenceA.start <= projectionSequenceB.end && projectionSequenceA.start >= projectionSequenceB.end) {
return true
}
return false
}

ProjectionSequence overlaps(ProjectionSequence projectionSequence) {
for (ProjectionSequence aProjSequence in getProjectedSequences()) {
if (aProjSequence.name == projectionSequence.name) {
if (overlaps(aProjSequence, projectionSequence)) {
return aProjSequence
}
}
}
return null
}

/**
* Merge ProjectionSequenceA with ProjectionSequenceB
*
* We assume that they are overlapped and belong to the same one.
*
* Return ProjectionSequenceA
*
* @param projectionSequenceA
* @param projectionSequenceB
* @return
*/
ProjectionSequence merge(ProjectionSequence projectionSequenceA, ProjectionSequence projectionSequenceB) {
if(projectionSequenceB.start < projectionSequenceA.start){
projectionSequenceA.start = projectionSequenceB.start
}
if(projectionSequenceB.end > projectionSequenceA.end){
projectionSequenceA.end = projectionSequenceB.end
}

return projectionSequenceA
}

def addProjectionSequences(List<ProjectionSequence> theseProjectionSequences) {
theseProjectionSequences.each {
sequenceDiscontinuousProjectionMap.put(it, null)
ProjectionSequence overlappingProjectionSequence = overlaps(it)
if (overlappingProjectionSequence) {
sequenceDiscontinuousProjectionMap.remove(overlappingProjectionSequence)
overlappingProjectionSequence = merge(overlappingProjectionSequence, it)
sequenceDiscontinuousProjectionMap.put(overlappingProjectionSequence, null)
} else {
sequenceDiscontinuousProjectionMap.put(it, null)
}
}
}

Expand All @@ -450,6 +500,18 @@ class MultiSequenceProjection extends AbstractProjection {
* @return
*/
Boolean isValid() {

Map<String, Boolean> reverseMap = new HashMap<>()

for (ProjectionSequence projectionSequence in getProjectedSequences()) {
if (reverseMap.containsKey(projectionSequence.name)) {
if (reverseMap.get(projectionSequence.name) != projectionSequence.reverse) {
return false
}
} else {
reverseMap.put(projectionSequence.name, projectionSequence.reverse)
}
}
return true
}
}
94 changes: 94 additions & 0 deletions test/unit/org/bbop/apollo/MultiSequenceProjectionSpec.groovy
Expand Up @@ -2129,4 +2129,98 @@ class MultiSequenceProjectionSpec extends Specification {
assert sequence1.unprojectedLength + 6 == multiSequenceProjection.projectReverseValue(10)
assert sequence1.unprojectedLength + 2 == multiSequenceProjection.projectReverseValue(13)
}
void "test non-overlap and validity"(){
given: "two non-overlapping projection sequences for different regions"
ProjectionSequence sequence1 = new ProjectionSequence(
id: 1
, name: "Sequence1"
, organism: "Human"
, order: 0
, unprojectedLength: 20
, start: 5
, end: 10
)
ProjectionSequence sequence2 = new ProjectionSequence(
id: 2
, name: "Sequence1"
, organism: "Human"
, order: 1
, unprojectedLength: 20
, start:12
, end: 14
)
MultiSequenceProjection multiSequenceProjection = new MultiSequenceProjection()
when: "when we set the same projections"
multiSequenceProjection.addProjectionSequences([sequence1, sequence2])
multiSequenceProjection.addLocation(new Location(min: 5,max:10,sequence:sequence1))
multiSequenceProjection.addLocation(new Location(min: 12,max:14,sequence:sequence2))
multiSequenceProjection.calculateOffsets()
then: "it is still valid and we have two projection sequences"
assert multiSequenceProjection.isValid()
assert multiSequenceProjection.getProjectedSequences().size()==2
assert multiSequenceProjection.getProjectedSequences().first().start==5
assert multiSequenceProjection.getProjectedSequences().first().end==10
assert multiSequenceProjection.getProjectedSequences().last().start==12
assert multiSequenceProjection.getProjectedSequences().last().end==14
when: "we change the reverse of one"
sequence1.reverse = true
then: "should not be valid "
assert !multiSequenceProjection.isValid()
when: "we change the reverse of one"
sequence2.reverse = true
then: "should be valid "
assert multiSequenceProjection.isValid()
}
void "test overlap"(){
given: "two non-overlapping projection sequences for different regions"
ProjectionSequence sequence1 = new ProjectionSequence(
id: 1
, name: "Sequence1"
, organism: "Human"
, order: 0
, unprojectedLength: 20
, start: 5
, end: 10
)
ProjectionSequence sequence2 = new ProjectionSequence(
id: 2
, name: "Sequence1"
, organism: "Human"
, order: 1
, unprojectedLength: 20
, start:8
, end: 15
)
MultiSequenceProjection multiSequenceProjection = new MultiSequenceProjection()
when: "when we set the same projections"
multiSequenceProjection.addProjectionSequences([sequence1, sequence2])
multiSequenceProjection.addLocation(new Location(min: 5,max:10,sequence:sequence1))
multiSequenceProjection.addLocation(new Location(min: 8,max:15,sequence:sequence2))
multiSequenceProjection.calculateOffsets()
then: "it is still valid and we have two projection sequences"
assert multiSequenceProjection.isValid()
assert multiSequenceProjection.getProjectedSequences().size()==1
assert multiSequenceProjection.getProjectedSequences().first().start==5
assert multiSequenceProjection.getProjectedSequences().first().end==15
when: "we change the reverse of one"
sequence1.reverse = true
then: "should be valid "
assert multiSequenceProjection.isValid()
}
}

0 comments on commit 73116a4

Please sign in to comment.