Skip to content

Commit

Permalink
#21 Add backend for SpectraList
Browse files Browse the repository at this point in the history
  • Loading branch information
David Rauh committed May 4, 2023
1 parent 357297e commit e725ee5
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 7 deletions.
58 changes: 58 additions & 0 deletions cmd/mb3server/src/api-impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,64 @@ func GetBrowseOptions() (*BrowseOptions, error) {
Count: int32(val.Count),
})
}
for _, val := range vals.Contributor {
result.Contributor = append(result.Contributor, StringCountInner{
Value: val.Val,
Count: int32(val.Count),
})
}
for _, val := range vals.CompoundStart {
result.CompoundStart = append(result.CompoundStart, StringCountInner{
Value: val.Val,
Count: int32(val.Count),
})
}

return &result, nil
}

func GetRecords(limit int32, offset int32) (*SearchResult, error) {
if limit <= 0 {
limit = 20
}
if offset <= 0 {
offset = 0
}
if err := initDB(); err != nil {
return nil, err
}
var filters = database.Filters{
InstrumentType: nil,
Splash: "",
MsType: nil,
IonMode: "",
CompoundName: "",
Mass: nil,
MassEpsilon: nil,
Formula: "",
Peaks: nil,
PeakDifferences: nil,
InchiKey: "",
Contributor: "",
IntensityCutoff: nil,
Limit: int64(limit),
Offset: int64(offset),
}
records, err := db.GetRecords(filters)
if err != nil {
return nil, err
}
var result = SearchResult{}
for _, record := range records {
var val = SearchResultDataInner{
Data: map[string]interface{}{},
Name: record.Compound.Names[1].String,
Formula: record.Compound.Formula.String,
Mass: record.Compound.Mass.Value,
Smiles: record.Compound.Smiles.String,
Spectra: []SearchResultDataInnerSpectraInner{{record.RecordTitle.String, record.Accession.String}},
}
result.Data = append(result.Data, val)
}
return &result, nil
}
11 changes: 5 additions & 6 deletions cmd/mb3server/src/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ func (s *DefaultApiService) GetRecord(ctx context.Context, accession string) (Im

// GetRecords - Get a list of records
func (s *DefaultApiService) GetRecords(ctx context.Context, instrumentType []string, splash string, msType []string, ionMode string, compoundName string, exactMass string, massTolerance float64, formula string, peaks []string, intensity int32, peakDifferences []string, peakList []string, limit int32, page int32, intensityCutoff int32, inchiKey string, contributor string) (ImplResponse, error) {
// TODO - update GetRecords with the required logic for this service method.
// Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, SearchResult{}) or use other options such as http.Ok ...
//return Response(200, SearchResult{}), nil
result, err := GetRecords(limit, (page-1)*limit)
if err != nil {
return Response(http.StatusInternalServerError, nil), errors.New("Could not get results")
}
return Response(200, result), nil

return Response(http.StatusNotImplemented, nil), errors.New("GetRecords method not implemented")
}
33 changes: 33 additions & 0 deletions cmd/mb3server/src/model_string_count_inner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MassBank3 API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 3.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/

package mb3server

type StringCountInner struct {
Value string `json:"value,omitempty"`

Count int32 `json:"count,omitempty"`
}

// AssertStringCountInnerRequired checks if the required fields are not zero-ed
func AssertStringCountInnerRequired(obj StringCountInner) error {
return nil
}

// AssertRecurseStringCountInnerRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of StringCountInner (e.g. [][]StringCountInner), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseStringCountInnerRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aStringCountInner, ok := obj.(StringCountInner)
if !ok {
return ErrTypeAssertionError
}
return AssertStringCountInnerRequired(aStringCountInner)
})
}
13 changes: 13 additions & 0 deletions pkg/database/db_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type MBMinMaxValues struct {
}

type MB3Values struct {
CompoundStart []MBCountValues
Contributor []MBCountValues
InstrumentType []MBCountValues
MSType []MBCountValues
IonMode []MBCountValues
Expand All @@ -101,6 +103,15 @@ type MB3Values struct {
Peak MBMinMaxValues
}

type MB3MetaData struct {
Version string
TimeStamp string
GitCommit string
SpectraCount int
CompoundCount int
IsomerCount int
}

// MB3Database This is the Interface which has to be implemented for databases using MassBank3
//
// Any database can be used as in the backend as long as it defines the interface.
Expand Down Expand Up @@ -133,6 +144,8 @@ type MB3Database interface {
// GetUniqueValues is used to get the values for filter frontend
GetUniqueValues(filters Filters) (MB3Values, error)

GetMetaData() (*MB3MetaData, error)

// UpdateMetadata updates the metadata describing the MassBank version.
// Provides the database id of an existing entry if it is already in the
// database.
Expand Down
72 changes: 72 additions & 0 deletions pkg/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,78 @@ type Mb3MongoDB struct {
dirty bool // if true, the database connection was changed and the database will reconnect.
}

func (db *Mb3MongoDB) GetMetaData() (*MB3MetaData, error) {
var query = ``
var bdoc interface{}
if err := bson2.UnmarshalJSON([]byte(query), &bdoc); err != nil {
return nil, err
}
cur, err := db.database.Collection(mbCollection).Aggregate(context.Background(), bdoc)
if err != nil {
return nil, err
}
var temp = make([]bson.D, 0)
if err = cur.All(context.Background(), &temp); err != nil {
return nil, err
}
result := MB3MetaData{
Version: "",
TimeStamp: "",
GitCommit: "",
SpectraCount: 0,
CompoundCount: 0,
IsomerCount: 0,
}
return &result, nil
}

func (db *Mb3MongoDB) GetUniqueValues(filters Filters) (MB3Values, error) {
var query = `
[
{ "$facet": {
"CompoundStart": [
{ "$project": { "compound.names": 1}},
{ "$unwind": "$compound.names" },
{"$group": {
"_id": {"$substr": [ {"$trim": { "input": {"$toUpper":"$compound.names"}, "chars": "()+-[]{}/? ,"}},0,1]},
"Count": {"$sum": 1}
}},
{
"$project": {
"_id": 0,
"Val": "$_id",
"Count": 1
}
},
{
"$sort": {
"Val": 1
}
}
],
"Contributor": [
{
"$project": { "_id": 0, "cont": {"$arrayElemAt":[ {"$split": [ "$accession","-"]}, 1]} }
},
{
"$group": {
"_id": "$cont",
"Count": {"$sum": 1}
},
},
{
"$project": {
"_id": 0,
"Val": "$_id",
"Count": 1
}
},
{
"$sort": {
"Val": 1
}
}
],
"InstrumentType": [
{
"$group": {
Expand Down Expand Up @@ -178,16 +246,20 @@ func (db *Mb3MongoDB) GetUniqueValues(filters Filters) (MB3Values, error) {
return result, err
}
type MB3ValuesTemp struct {
CompoundStart []MBCountValues
InstrumentType []MBCountValues
MSType []MBCountValues
IonMode []MBCountValues
Contributor []MBCountValues
Intensity []MBMinMaxValues
Mass []MBMinMaxValues
Peak []MBMinMaxValues
}
var tempV MB3ValuesTemp
err = bson.Unmarshal(doc, &tempV)
result = MB3Values{
CompoundStart: tempV.CompoundStart,
Contributor: tempV.Contributor,
InstrumentType: tempV.InstrumentType,
MSType: tempV.MSType,
IonMode: tempV.IonMode,
Expand Down
8 changes: 7 additions & 1 deletion web-frontend/src/routes/contents/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
{:then filters}
<div class="card">
<h2>Filters</h2>

<h3>Contributor</h3>
{#each filters.contributor as t}
<FilterButton group="cont" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<h3>Instrument Type</h3>
{#each filters.instrument_type as t}
<FilterButton group="insttype" val={t.value}>{t.value} ({t.count})</FilterButton>
Expand All @@ -36,6 +38,10 @@
{#each filters.ion_mode as t}
<FilterButton group="ionmode" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<h3>Compound Start</h3>
{#each filters.compound_start as t}
<FilterButton group="compoundstart" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
</div>

{/await}
Expand Down

0 comments on commit e725ee5

Please sign in to comment.