Skip to content

Commit

Permalink
refactor getSequences usage in SeqSetCitationsController (#3193)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabian Engelniederhammer <92720311+fengelniederhammer@users.noreply.github.com>
  • Loading branch information
fhennig and fengelniederhammer authored Nov 11, 2024
1 parent 93f9902 commit 284fed3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.loculus.backend.api.CitedBy
import org.loculus.backend.api.ResponseSeqSet
import org.loculus.backend.api.SeqSet
import org.loculus.backend.api.SeqSetRecord
import org.loculus.backend.api.Status.APPROVED_FOR_RELEASE
import org.loculus.backend.api.SubmittedSeqSet
import org.loculus.backend.api.SubmittedSeqSetRecord
import org.loculus.backend.api.SubmittedSeqSetUpdate
Expand Down Expand Up @@ -92,11 +91,10 @@ class SeqSetCitationsController(

@Operation(description = "Get count of user sequences cited by SeqSets")
@GetMapping("/get-user-cited-by-seqset")
fun getUserCitedBySeqSet(@HiddenParam authenticatedUser: AuthenticatedUser): CitedBy {
val statusFilter = listOf(APPROVED_FOR_RELEASE)
val userSequences = submissionDatabaseService.getSequences(authenticatedUser, null, null, statusFilter)
return seqSetCitationsService.getUserCitedBySeqSet(userSequences.sequenceEntries)
}
fun getUserCitedBySeqSet(@HiddenParam authenticatedUser: AuthenticatedUser): CitedBy =
seqSetCitationsService.getUserCitedBySeqSet(
submissionDatabaseService.getApprovedUserAccessionVersions(authenticatedUser),
)

@Operation(description = "Get count of SeqSet cited by publications")
@GetMapping("/get-seqset-cited-by-publication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.loculus.backend.api.ResponseSeqSet
import org.loculus.backend.api.SeqSet
import org.loculus.backend.api.SeqSetCitationsConstants
import org.loculus.backend.api.SeqSetRecord
import org.loculus.backend.api.SequenceEntryStatus
import org.loculus.backend.api.Status.APPROVED_FOR_RELEASE
import org.loculus.backend.api.SubmittedSeqSetRecord
import org.loculus.backend.auth.AuthenticatedUser
Expand Down Expand Up @@ -367,7 +366,7 @@ class SeqSetCitationsDatabaseService(
)
}

fun getUserCitedBySeqSet(userAccessions: List<SequenceEntryStatus>): CitedBy {
fun getUserCitedBySeqSet(accessionVersions: List<AccessionVersion>): CitedBy {
log.info { "Get user cited by seqSet" }

data class SeqSetWithAccession(
Expand All @@ -377,9 +376,9 @@ class SeqSetCitationsDatabaseService(
val createdAt: Timestamp,
)

val userAccessionStrings = userAccessions.flatMap {
listOf(it.accession.plus('.').plus(it.version), it.accession)
}
val userAccessionStrings = accessionVersions
.flatMap { listOf(it.accession, it.displayAccessionVersion()) }
.toSet()

val maxSeqSetVersion = SeqSetsTable.seqSetVersion.max().alias("max_version")
val maxVersionPerSeqSet = SeqSetsTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import org.loculus.backend.api.ProcessedData
import org.loculus.backend.api.SequenceEntryStatus
import org.loculus.backend.api.SequenceEntryVersionToEdit
import org.loculus.backend.api.Status
import org.loculus.backend.api.Status.APPROVED_FOR_RELEASE
import org.loculus.backend.api.SubmissionIdMapping
import org.loculus.backend.api.SubmittedProcessedData
import org.loculus.backend.api.UnprocessedData
Expand Down Expand Up @@ -623,7 +624,7 @@ open class SubmissionDatabaseService(

fun getSequences(
authenticatedUser: AuthenticatedUser,
organism: Organism?,
organism: Organism,
groupIdsFilter: List<Int>?,
statusesFilter: List<Status>?,
warningsFilter: WarningsFilter? = null,
Expand Down Expand Up @@ -663,12 +664,9 @@ open class SubmissionDatabaseService(
DataUseTermsTable.restrictedUntilColumn,
)
.where { groupCondition }
.andWhere { SequenceEntriesView.organismIs(organism) }
.orderBy(SequenceEntriesView.accessionColumn)

if (organism != null) {
baseQuery.andWhere { SequenceEntriesView.organismIs(organism) }
}

val statusCounts: Map<Status, Int> = Status.entries.associateWith { status ->
baseQuery.count { it[SequenceEntriesView.statusColumn] == status.name }
}
Expand Down Expand Up @@ -950,6 +948,24 @@ open class SubmissionDatabaseService(
)
}

/**
* Returns AccessionVersions submitted by groups that the given user is part of
* and that are approved for release.
*/
fun getApprovedUserAccessionVersions(authenticatedUser: AuthenticatedUser): List<AccessionVersion> =
SequenceEntriesView.select(
SequenceEntriesView.accessionColumn,
SequenceEntriesView.versionColumn,
)
.where(SequenceEntriesView.statusIs(APPROVED_FOR_RELEASE))
.groupBy(getGroupCondition(null, authenticatedUser))
.map {
AccessionVersion(
it[SequenceEntriesView.accessionColumn],
it[SequenceEntriesView.versionColumn],
)
}

private fun originalMetadataFilter(
authenticatedUser: AuthenticatedUser,
organism: Organism,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.loculus.backend.api.DataUseTerms
import org.loculus.backend.api.GetSequenceResponse
import org.loculus.backend.api.SequenceEntryStatus
import org.loculus.backend.api.Status
import org.loculus.backend.api.AccessionVersion
import org.loculus.backend.controller.EndpointTest
import org.loculus.backend.controller.expectUnauthorizedResponse
import org.loculus.backend.service.submission.AccessionPreconditionValidator
Expand Down Expand Up @@ -46,8 +43,8 @@ class CitationEndpointsTest(@Autowired private val client: SeqSetCitationsContro
@Test
fun `WHEN calling get user cited by seqSet of non-existing user THEN returns empty results`() {
every {
submissionDatabaseService.getSequences(any(), any(), any(), any())
} returns GetSequenceResponse(sequenceEntries = emptyList(), statusCounts = emptyMap())
submissionDatabaseService.getApprovedUserAccessionVersions(any())
} returns listOf()

client.getUserCitedBySeqSet()
.andExpect(status().isOk)
Expand All @@ -72,22 +69,8 @@ class CitationEndpointsTest(@Autowired private val client: SeqSetCitationsContro
@Test
fun `WHEN calling get seqSet cited by publication of existing seqSet THEN returns results`() {
every {
submissionDatabaseService.getSequences(any(), any(), any(), any())
} returns GetSequenceResponse(
sequenceEntries = listOf(
SequenceEntryStatus(
accession = "mock-sequence-accession",
version = 1L,
status = Status.APPROVED_FOR_RELEASE,
groupId = 123,
submitter = "mock-submitter",
isRevocation = false,
submissionId = "mock-submission-id",
dataUseTerms = DataUseTerms.Open,
),
),
statusCounts = mapOf(Status.APPROVED_FOR_RELEASE to 1),
)
submissionDatabaseService.getApprovedUserAccessionVersions(any())
} returns listOf(AccessionVersion("mock-sequence-accession", 1L))

val seqSetResult = client.createSeqSet()
.andExpect(status().isOk)
Expand Down

0 comments on commit 284fed3

Please sign in to comment.