forked from bigdatagenomics/adam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADAM-410] add alphabet class and test to address issue bigdatagenomi…
…cs#410 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
Showing
4 changed files
with
215 additions
and
56 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
adam-core/src/main/scala/org/bdgenomics/adam/models/Alphabet.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
adam-core/src/main/scala/org/bdgenomics/adam/util/SequenceUtils.scala
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
adam-core/src/test/scala/org/bdgenomics/adam/models/AlphabetSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
|
||
} | ||
|