Skip to content

Commit

Permalink
[ADAM-410] add alphabet class and test to address issue #410
Browse files Browse the repository at this point in the history
removed the SequenceUtils class and updated all references.  ran mvn verify and all tests pass.

ran ./scripts/format-source

make changes as suggested in the pull request:
1. unknown characters should throw illegalArgumentException instead of keyNotFound
2. case-insensitive changed to mean that it will map both lower and upper case characters to the same symbol instead of mapping lower to lower and upper to upper

clean up import

squashed commits
  • Loading branch information
bryanjj authored and fnothaft committed May 4, 2015
1 parent 9013336 commit 6ce4ef7
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 56 deletions.
107 changes: 107 additions & 0 deletions adam-core/src/main/scala/org/bdgenomics/adam/models/Alphabet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.adam.models

import scala.util.Try

/**
* Created by bryan on 4/17/15.
*
* An alphabet of symbols and related operations
*
*/
trait Alphabet {

/** the symbols in this alphabet */
val symbols: Seq[Symbol]

/**
* flag if symbols are case-sensitive.
* if true, this alphabet will treat symbols representing upper and lower case symbols as distinct
* if false, this alphabet will treat upper or lower case chars as the same for its symbols
*/
val caseSensitive: Boolean

/** map of the symbol char to the symbol */
lazy val symbolMap: Map[Char, Symbol] =
if (caseSensitive)
symbols.map(symbol => symbol.label -> symbol).toMap
else
symbols.flatMap(symbol => Seq(symbol.label.toLower -> symbol, symbol.label.toUpper -> symbol)).toMap

/**
*
* @param s Each char in this string represents a symbol on the alphabet.
* If the char is not in the alphabet then a NoSuchElementException is thrown
* @return the reversed complement of the given string.
* @throws IllegalArgumentException if the string contains a symbol which is not in the alphabet
*/
def reverseComplementExact(s: String): String = {
reverseComplement(s,
(symbol: Char) => throw new IllegalArgumentException("Character %s not found in alphabet.".format(symbol))
)
}

/**
*
* @param s Each char in this string represents a symbol on the alphabet.
* @param notFound If the char is not in the alphabet then this function is called.
* default behavior is to return a new Symbol representing the unknown character,
* so that the unknown char is treated as the complement
* @return the reversed complement of the given string.
*/
def reverseComplement(s: String, notFound: (Char => Symbol) = ((c: Char) => Symbol(c, c))) = {
s.map(x => Try(apply(x)).getOrElse(notFound(x)).complement).reverse
}

/** number of symbols in the alphabet */
def size = symbols.size

/**
* @param c char to lookup as a symbol in this alphabet
* @return the given symbol
*/
def apply(c: Char): Symbol = symbolMap(c)

}

/**
* A symbol in an alphabet
* @param label a character which represents the symbol
* @param complement acharacter which represents the complement of the symbol
*/
case class Symbol(label: Char, complement: Char)

/**
* The standard DNA alphabet with A,T,C, and G
*/
class DNAAlphabet extends Alphabet {

override val caseSensitive = false

override val symbols = Seq(
Symbol('A', 'T'),
Symbol('T', 'A'),
Symbol('G', 'C'),
Symbol('C', 'G')
)
}

object Alphabet {
val dna = new DNAAlphabet
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package org.bdgenomics.adam.models
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.bdgenomics.adam.rdd.ADAMContext._
import org.bdgenomics.adam.util.SequenceUtils
import org.bdgenomics.formats.avro.{ Strand, Feature }

/**
Expand Down Expand Up @@ -100,7 +99,7 @@ case class Transcript(id: String,
if (strand)
referenceSequence.substring(minStart, maxEnd)
else
SequenceUtils.reverseComplement(referenceSequence.substring(minStart, maxEnd))
Alphabet.dna.reverseComplement(referenceSequence.substring(minStart, maxEnd))
}

/**
Expand Down Expand Up @@ -178,7 +177,7 @@ abstract class BlockExtractable(strand: Boolean, region: ReferenceRegion)
if (strand)
referenceSequence.substring(region.start.toInt, region.end.toInt)
else
SequenceUtils.reverseComplement(referenceSequence.substring(region.start.toInt, region.end.toInt))
Alphabet.dna.reverseComplement(referenceSequence.substring(region.start.toInt, region.end.toInt))
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.adam.models

import org.bdgenomics.adam.util.ADAMFunSuite

/**
* Created by bryan on 4/18/15.
*
* Tests the alphabet class
*/
class AlphabetSuite extends ADAMFunSuite {

val testSymbols = Seq(
Symbol('a', 'z'),
Symbol('b', 'y'),
Symbol('c', 'x')
)

val csAlpha = new Alphabet {
override val caseSensitive = true
override val symbols = testSymbols
}

test("test size of a case-sensitive alphabet") {
assert(3 == csAlpha.size)
}

test("test apply of a case-sensitive alphabet") {
assert(Symbol('b', 'y') == csAlpha('b'))
val e = intercept[NoSuchElementException] {
csAlpha('B')
}
assert(e.getMessage == "key not found: B")
}

test("test reverse complement of a case-sensitive alphabet") {
assert("xyz" == csAlpha.reverseComplement("abc"))
assert("CBA" == csAlpha.reverseComplement("ABC"))
assert("" == csAlpha.reverseComplement(""))
}

test("test exact reverse complement of a case-sensitive alphabet") {
assert("zzyyxx" == csAlpha.reverseComplement("ccbbaa"))
assert("" == csAlpha.reverseComplement(""))

val e = intercept[IllegalArgumentException] {
csAlpha.reverseComplementExact("ABC")
}
assert(e.getMessage == "Character A not found in alphabet.")
}

val ciAlpha = new Alphabet {
override val caseSensitive = false
override val symbols = testSymbols
}

test("test size of a case-insensitive alphabet") {
assert(3 == ciAlpha.size)
}

test("test apply of a case-insensitive alphabet") {
assert(Symbol('b', 'y') == ciAlpha('b'))
assert(Symbol('b', 'y') == ciAlpha('B'))
}

test("test reverse complement of a case-insensitive alphabet") {
assert("xyz" == ciAlpha.reverseComplement("abc"))
assert("xyz" == ciAlpha.reverseComplement("ABC"))
assert("" == ciAlpha.reverseComplement(""))
}

test("test exact reverse complement of a case-insensitive alphabet") {
assert("zzyyxx" == ciAlpha.reverseComplement("ccbbaa"))
assert("zzyyxx" == ciAlpha.reverseComplement("cCbBaA"))
assert("" == ciAlpha.reverseComplement(""))

val e = intercept[IllegalArgumentException] {
ciAlpha.reverseComplementExact("xxx")
}
assert(e.getMessage == "Character x not found in alphabet.")
}

test("DNA alphabet") {
assert(4 == Alphabet.dna.size)
assert("CGCGATAT" == Alphabet.dna.reverseComplement("atatcgcg"))
assert("CGxATAT" == Alphabet.dna.reverseComplement("ATATxcg"))
}

}

0 comments on commit 6ce4ef7

Please sign in to comment.