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

feat: SequenceMetadata can have name and length looked up by key #1002

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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 @@ -46,9 +46,9 @@
val AlternateLocus : String = "AH"
val Assembly : String = SAMSequenceRecord.ASSEMBLY_TAG
val Description : String = SAMSequenceRecord.DESCRIPTION_TAG
private[fasta] val Length : String = SAMSequenceRecord.SEQUENCE_LENGTH_TAG
val Length : String = SAMSequenceRecord.SEQUENCE_LENGTH_TAG
val Md5 : String = SAMSequenceRecord.MD5_TAG
private[fasta] val Name : String = SAMSequenceRecord.SEQUENCE_NAME_TAG
val Name : String = SAMSequenceRecord.SEQUENCE_NAME_TAG
val Species : String = SAMSequenceRecord.SPECIES_TAG
val Topology : String = "TP"
val Uri : String = SAMSequenceRecord.URI_TAG
Expand Down Expand Up @@ -84,7 +84,7 @@
species: Option[String] = None,
topology: Option[Topology] = None,
uri: Option[String] = None,
customAttributes: Map[String, String] = Map.empty
customAttributes: Map[String, String] = Map.empty
): SequenceMetadata = {
Keys.values.find(customAttributes.contains).foreach { key =>
throw new IllegalArgumentException(s"Attributes contains a standard key: $key")
Expand Down Expand Up @@ -138,6 +138,10 @@
}

/** Stores information about a single Sequence (ex. chromosome, contig)
*
* Important: when retrieving attributes using the `apply`, `get`, and `getOrElse` methods, all values will be
* returned as `String`s. Use the named accessors for attributes that have non-`String` types (i.e. `length`,
* `aliases`, `alternate`, and `topology`).
*
* @param name the primary name of the sequence
* @param length the length of the sequence, or zero if unknown
Expand All @@ -156,10 +160,20 @@
require(!attributes.contains(Keys.Name), f"`${Keys.Name}` should not given in the list of attributes")
require(!attributes.contains(Keys.Length), s"`${Keys.Length}` should not given in the list of attributes")

@inline final def apply(key: String): String = this.attributes(key)
@inline final def get(key: String): Option[String] = this.attributes.get(key)
@inline final def getOrElse(key: String, default: String): String = this.attributes.getOrElse(key, default)
@inline final def contains(key: String): Boolean = this.attributes.contains(key)
@inline final def apply(key: String): String = {
if (key == Keys.Name) this.name
else if (key == Keys.Length) s"${this.length}"
else this.attributes(key)
}
@inline final def get(key: String): Option[String] = {
if (key == Keys.Name) Some(this.name)
else if (key == Keys.Length) Some(s"${this.length}")
else this.attributes.get(key)
}
@inline final def getOrElse(key: String, default: String): String = this.get(key).getOrElse(default)

Check warning on line 173 in src/main/scala/com/fulcrumgenomics/fasta/SequenceDictionary.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/com/fulcrumgenomics/fasta/SequenceDictionary.scala#L173

Added line #L173 was not covered by tests
@inline final def contains(key: String): Boolean = {
this.attributes.contains(key) || key == Keys.Name || key == Keys.Length
}
lazy val aliases: Seq[String] = this.get(Keys.Aliases).map(_.split(',').toSeq).getOrElse(Seq.empty[String])
/** All names, including aliases */
@inline final def allNames: Seq[String] = name +: aliases
Expand Down
Loading