Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate dash count on first line of each sequence. #211

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ class SequenceLengthValidationError(
class SequenceEmptyValidationError : SequenceValidationError {
override val message: String
get() = "Empty query. Query must have 1 or more sequences."
}

class SequenceDashesValidationError(val sequence: Int) : SequenceValidationError {
override val message: String
get() = "The first line of input sequence #$sequence contained too many dashes."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mb.api.service.valid

import org.veupathdb.lib.blast.BlastTool
import java.util.*
import kotlin.math.min

interface SequenceValidator {
/**
Expand Down Expand Up @@ -32,6 +33,8 @@ interface SequenceValidator {
* @return Whether all characters in the input {@code CharSequence} are valid.
*/
fun validate(sequence: Int, seq: String): SequenceValidationError? {
validateFirstLine(sequence, seq)?.let { return it }

// Input scanner
val scan = Scanner(seq)
// Current line number
Expand Down Expand Up @@ -65,6 +68,54 @@ interface SequenceValidator {
return null
}

/**
* Validates the first line in a sequence to ensure that it passes the first
* line check performed by the BLAST+ CLI tool:
* ```
* if (bad >= good / 3 &&
* (len_to_check > 3 || good == 0 || bad > good))
* {
* FASTA_ERROR( LineNumber(),
* "CFastaReader: Near line " << LineNumber()
* << ", there's a line that doesn't look like plausible data, "
* "but it's not marked as defline or comment.",
* CObjReaderParseException::eFormat);
* }
* ```
*/
private fun validateFirstLine(sequence: Int, seq: String): SequenceValidationError? {
val scan = Scanner(seq)

while (scan.hasNextLine()) {
val line = scan.nextLine()

if (line.isNotBlank() && line[0] != '>') {
var dashes = 0
var nonDashes = 0

// Count the dashes + nondashes in the line
for (i in 0 .. min(line.length, 70)) {
when (line[i]) {
'-' -> dashes++
else -> nonDashes++
}
}

val checked = dashes+nonDashes

return if (
dashes >= nonDashes / 3
&& (checked > 3 || nonDashes == 0 || dashes > nonDashes)
)
SequenceDashesValidationError(sequence)
else
null
}
}

return SequenceEmptyValidationError()
}

companion object {
fun getValidator(tool: BlastTool): SequenceValidator {
return when (tool) {
Expand Down