Skip to content

Commit

Permalink
Adds a get-field-by-index method for ExtendedBedEntry (#42)
Browse files Browse the repository at this point in the history
* Added a get-field-by-index method for ExtendedBedEntry

* Code style fixes in BedEntryTest

Alignment, indentation, line breaks, common code extraction etc.

* Added more out-of-bounds safety for getFieldByIndex

* Added tests for getFieldByIndex

* Renamed getFieldByIndex to getField
  • Loading branch information
dievsky authored Feb 12, 2019
1 parent 342c066 commit e08955a
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 162 deletions.
30 changes: 30 additions & 0 deletions src/main/kotlin/org/jetbrains/bio/big/Bed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,34 @@ data class ExtendedBedEntry(
}
return rest
}

/**
* Returns a i-th field of a Bed entry. Since ExtendedBedEntry is format-agnostic,
* it doesn't actually know which field is i-th, so we have to provide [fieldsNumber] and [extraFieldsNumber].
* Returns an instance of a correct type ([Int], [String] etc.) or null for missing and out of bounds fields.
*/

This comment has been minimized.

Copy link
@iromeo

iromeo May 15, 2019

Contributor

Please specify parameters kdoc, as for other API methods in this class (i.e. @param ...)

This comment has been minimized.

Copy link
@dievsky

dievsky May 17, 2019

Author Contributor

Done.

fun getField(i: Int, fieldsNumber: Int = 12, extraFieldsNumber: Int? = null): Any? {
val actualExtraFieldsNumber = extraFieldsNumber ?: extraFields?.size ?: 0
return when {
i >= fieldsNumber + actualExtraFieldsNumber -> null
i >= fieldsNumber -> extraFields?.let {
if (i - fieldsNumber < it.size) it[i - fieldsNumber] else null
}
else -> when (i) {
0 -> chrom
1 -> start
2 -> end
3 -> name
4 -> score
5 -> strand
6 -> thickStart
7 -> thickEnd
8 -> itemRgb
9 -> blockCount
10 -> blockSizes
11 -> blockStarts
else -> null
}
}
}
}
Loading

0 comments on commit e08955a

Please sign in to comment.