Skip to content

Commit

Permalink
Merge branch 'master' into releases/v4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
brouillette committed Jun 17, 2023
2 parents 1994d7f + 5676f0b commit d66ed4f
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 122 deletions.
2 changes: 1 addition & 1 deletion etc/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

GOHAN_DEBUG=false
GOHAN_SERVICE_CONTACT=[email protected]
GOHAN_SEMVER=3.8.0
GOHAN_SEMVER=3.9.0
GOHAN_SERVICES="gateway api elasticsearch kibana drs authorization"

# GOOS=linux
Expand Down
17 changes: 17 additions & 0 deletions src/api/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contexts

import (
"gohan/api/models"
"gohan/api/models/constants"
"gohan/api/services"
variantsService "gohan/api/services/variants"

Expand All @@ -14,9 +15,25 @@ type (
// an elasticsearch client and other variables
GohanContext struct {
echo.Context
QueryParameters
Es7Client *es7.Client
Config *models.Config
IngestionService *services.IngestionService
VariantService *variantsService.VariantService
}

// Convenient storage for relevant http context data
QueryParameters struct {
AssemblyId constants.AssemblyId
Alleles []string
Chromosome string
Genotype constants.GenotypeQuery
SampleIds []string
PositionBounds
}

PositionBounds struct {
LowerBound int
UpperBound int
}
)
6 changes: 4 additions & 2 deletions src/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,24 @@ func main() {
gam.MandateCalibratedBounds,
gam.MandateCalibratedAlleles,
gam.MandateAssemblyIdAttribute,
gam.MandateSampleIdsPluralAttribute,
gam.CalibrateOptionalSampleIdsPluralAttribute,
gam.ValidatePotentialGenotypeQueryParameter)
e.GET("/variants/get/by/documentId", variantsMvc.VariantsGetByDocumentId)

e.GET("/variants/count/by/variantId", variantsMvc.VariantsCountByVariantId,
// middleware
gam.ValidateOptionalChromosomeAttribute,
gam.MandateCalibratedBounds,
gam.MandateCalibratedAlleles,
gam.MandateAssemblyIdAttribute,
gam.ValidatePotentialGenotypeQueryParameter)
e.GET("/variants/count/by/sampleId", variantsMvc.VariantsCountBySampleId,
// middleware
gam.ValidateOptionalChromosomeAttribute,
gam.MandateCalibratedBounds,
gam.MandateCalibratedAlleles,
gam.MandateAssemblyIdAttribute,
gam.MandateSampleIdsSingularAttribute,
gam.CalibrateOptionalSampleIdsSingularAttribute,
gam.ValidatePotentialGenotypeQueryParameter)

// TODO: refactor (deduplicate) --
Expand Down
8 changes: 7 additions & 1 deletion src/api/middleware/assemblyMiddleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package middleware

import (
"gohan/api/contexts"
"gohan/api/models/constants"
assid "gohan/api/models/constants/assembly-id"
"net/http"

Expand All @@ -19,6 +21,10 @@ func MandateAssemblyIdAttribute(next echo.HandlerFunc) echo.HandlerFunc {
return echo.NewHTTPError(http.StatusBadRequest, "Missing or unknown assemblyId!")
}

return next(c)
// forward a type-safe value down the pipeline
gc := c.(*contexts.GohanContext)
gc.AssemblyId = constants.AssemblyId(assemblyId)

return next(gc)
}
}
37 changes: 32 additions & 5 deletions src/api/middleware/calibratedAllelesMiddleware.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package middleware

import (
"fmt"
"gohan/api/contexts"
"gohan/api/models/dtos/errors"
"gohan/api/utils"
"net/http"
"strings"

Expand All @@ -10,24 +13,48 @@ import (

func MandateCalibratedAlleles(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if len(c.QueryParam("alleles")) > 0 {
allelesQP := strings.Split(c.QueryParam("alleles"), ",")
gc := c.(*contexts.GohanContext)

var alleles []string
var allelesQP string = c.QueryParam("alleles")
if len(allelesQP) > 0 {
alleles = strings.Split(allelesQP, ",")

// ensure the allele query is properly formatted
if allelesQP[len(allelesQP)-1] == "" {
if alleles[len(alleles)-1] == "" {
return echo.NewHTTPError(
http.StatusBadRequest,
errors.CreateSimpleBadRequest("Found an empty allele! Please double check your request!"))
}

// ensure no more than 2 alleles are provided at once
if len(allelesQP) > 2 {
if len(alleles) > 2 {
return echo.NewHTTPError(
http.StatusBadRequest,
errors.CreateSimpleBadRequest("Too many alleles! Please only provide 1 or 2"))
}

// check validity of each provided character
for i, allele := range alleles {
// - accept lower cases, but transform to upper for elasticsearch (case sensitive)
upperAllele := strings.ToUpper(allele)
// - check validity of the rest
for _, nuc := range upperAllele {
if !utils.StringInSlice(string(nuc), utils.AcceptedNucleotideCharacters) {
// return status 400 if any allele is incorrect
return echo.NewHTTPError(
http.StatusBadRequest,
errors.CreateSimpleBadRequest(fmt.Sprintf("Nucleotide %s unacceptable! Please double check and try again.", string(nuc))))
}
}
// - replace 'N' with uppercase '?' for elasticsearch
upperAllele = strings.Replace(upperAllele, "N", "?", -1)

alleles[i] = upperAllele
}
}

return next(c)
gc.Alleles = alleles
return next(gc)
}
}
7 changes: 6 additions & 1 deletion src/api/middleware/calibratedBoundsMiddleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"gohan/api/contexts"
"net/http"
"strconv"

Expand All @@ -9,6 +10,8 @@ import (

func MandateCalibratedBounds(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
gc := c.(*contexts.GohanContext)

var (
lowerBound int
upperBound int
Expand Down Expand Up @@ -50,6 +53,8 @@ func MandateCalibratedBounds(next echo.HandlerFunc) echo.HandlerFunc {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid lower and upper bounds!")
}

return next(c)
gc.LowerBound = lowerBound
gc.UpperBound = upperBound
return next(gc)
}
}
12 changes: 11 additions & 1 deletion src/api/middleware/chromosomeMiddleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"gohan/api/contexts"
"gohan/api/models/constants/chromosome"
"net/http"

Expand All @@ -12,6 +13,8 @@ Echo middleware to ensure a valid `chromosome` HTTP query parameter was provided
*/
func ValidateOptionalChromosomeAttribute(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
gc := c.(*contexts.GohanContext)

// check for chromosome query parameter
chromQP := c.QueryParam("chromosome")

Expand All @@ -22,6 +25,13 @@ func ValidateOptionalChromosomeAttribute(next echo.HandlerFunc) echo.HandlerFunc
return echo.NewHTTPError(http.StatusBadRequest, "Please provide a valid 'chromosome' (either 1-23, X, Y, or M)")
}

return next(c)
if len(chromQP) == 0 {
// if no chromosome is provided, assume "wildcard" search
gc.Chromosome = "*"
} else {
gc.Chromosome = chromQP
}

return next(gc)
}
}
13 changes: 11 additions & 2 deletions src/api/middleware/genotypeMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/http"

"gohan/api/contexts"
"gohan/api/models/constants"
gq "gohan/api/models/constants/genotype-query"

"github.com/labstack/echo"
Expand All @@ -14,18 +16,25 @@ Echo middleware to ensure the validity of the optionally provided `genotype` HTT
*/
func ValidatePotentialGenotypeQueryParameter(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
gc := c.(*contexts.GohanContext)

// check for a genotype query parameter
var (
genotype constants.GenotypeQuery
genotypeErr error
)
genotypeQP := c.QueryParam("genotype")
if len(genotypeQP) > 0 {
// validate that the genotype string
// converts to a valid GenotypeQuery
// (skips "UNCALLED" values)
_, genotypeErr := gq.CastToGenoType(genotypeQP)
genotype, genotypeErr = gq.CastToGenoType(genotypeQP)
if genotypeErr != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid genotype query %s, %s", genotypeQP, genotypeErr))
}
}

return next(c)
gc.Genotype = genotype
return next(gc)
}
}
35 changes: 22 additions & 13 deletions src/api/middleware/sampleMiddleware.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
package middleware

import (
"net/http"
"gohan/api/contexts"
"strings"

"github.com/labstack/echo"
)

/*
Echo middleware to ensure a singular `id` HTTP query parameter was provided
Echo middleware to prepare the context for an optionall provided singular `id` HTTP query parameter
*/
func MandateSampleIdsSingularAttribute(next echo.HandlerFunc) echo.HandlerFunc {
func CalibrateOptionalSampleIdsSingularAttribute(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
gc := c.(*contexts.GohanContext)

// check for id query parameter
sampleId := c.QueryParam("id")
if len(sampleId) == 0 {
// if no id was provided return an error
return echo.NewHTTPError(http.StatusBadRequest, "Missing 'id' query parameter for sample id querying!")
sampleId = "*" // wildcard
}

return next(c)
gc.SampleIds = append(gc.SampleIds, sampleId)
return next(gc)
}
}

/*
Echo middleware to ensure a pluralized `id` (spelled `ids`) HTTP query parameter was provided
Echo middleware to prepare the context for an optionally provided pluralized `id` (spelled `ids`) HTTP query parameter
*/
func MandateSampleIdsPluralAttribute(next echo.HandlerFunc) echo.HandlerFunc {
func CalibrateOptionalSampleIdsPluralAttribute(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
gc := c.(*contexts.GohanContext)

// check for id's query parameter
sampleIds := strings.Split(c.QueryParam("ids"), ",")
if len(sampleIds) == 0 {
// if no ids were provided return an error
return echo.NewHTTPError(http.StatusBadRequest, "Missing 'ids' query parameter for sample id querying!")
var (
sampleIdQP = c.QueryParam("ids")
sampleIds []string
)
if len(sampleIdQP) > 0 {
sampleIds = strings.Split(sampleIdQP, ",")
} else {
sampleIds = append(sampleIds, "*") // wildcard
}

return next(c)
gc.SampleIds = append(gc.SampleIds, sampleIds...)
return next(gc)
}
}
17 changes: 7 additions & 10 deletions src/api/mvc/genes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,26 +322,23 @@ func GetAllGeneIngestionRequests(c echo.Context) error {

func GenesGetByNomenclatureWildcard(c echo.Context) error {
fmt.Printf("[%s] - GenesGetByNomenclatureWildcard hit!\n", time.Now())
cfg := c.(*contexts.GohanContext).Config
es := c.(*contexts.GohanContext).Es7Client
gc := c.(*contexts.GohanContext)
cfg := gc.Config
es := gc.Es7Client

// Chromosome search term
chromosomeSearchTerm := c.QueryParam("chromosome")
if len(chromosomeSearchTerm) == 0 {
// if no chromosome is provided, assume "wildcard" search
chromosomeSearchTerm = "*"
}
chromosomeSearchTerm := gc.Chromosome

// Name search term
term := c.QueryParam("term")

// Assembly ID
// perform wildcard search if empty/random parameter is passed
// - set to Unknown to trigger it
assId := assemblyId.Unknown
if assemblyId.CastToAssemblyId(c.QueryParam("assemblyId")) != assemblyId.Unknown {
var assId constants.AssemblyId
if gc.AssemblyId != assemblyId.Unknown {
// retrieve passed parameter if is valid
assId = assemblyId.CastToAssemblyId(c.QueryParam("assemblyId"))
assId = gc.AssemblyId
}

// Size
Expand Down
Loading

0 comments on commit d66ed4f

Please sign in to comment.