From c3a94c5d6e3cdcde816436fd3a58ffabbc2e9b0a Mon Sep 17 00:00:00 2001 From: federico-rosatelli Date: Fri, 9 Jun 2023 11:11:58 +0200 Subject: [PATCH] SRA Analysis --- service/api/analysis.go | 108 +++++++++++++++++++++++++++++++ service/api/api-handler.go | 1 + service/api/routing-functions.go | 13 ++++ service/database/analysis.go | 61 +++++++++++++++++ service/database/database.go | 9 +-- service/structures/structures.go | 21 ++++++ 6 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 service/api/analysis.go create mode 100644 service/database/analysis.go diff --git a/service/api/analysis.go b/service/api/analysis.go new file mode 100644 index 0000000..beacbbe --- /dev/null +++ b/service/api/analysis.go @@ -0,0 +1,108 @@ +package api + +import ( + "github.com/BITSapienza/Bio-Server/service/ErrManager" + str "github.com/BITSapienza/Bio-Server/service/structures" +) + +func (rt *_router) GetAnalysis() ([]str.SraBioProject, ErrManager.Errors) { + var an []str.SraBioProject + nProj, err := rt.db.FindAnalysis() + if err != nil { + return an, err + } + var sraProjects []str.SraBioProject + srr := make(map[string]str.SraBioProject) + for _, pro := range nProj { + projId := pro["BioProject"].(string) + val, ok := srr[projId] + if !ok { + + sra := str.SraBioProject{ + TaxId: pro["TaxId"].(string), + + BioProjectId: pro["BioProject"].(string), + BioSamples: []struct { + BioSampleId string + Experiments []struct { + ExperimentId string + Runs []string + } + }{}, + } + if _, ok := pro["ScientificName"].(string); ok { + sra.ScientificName = pro["ScientificName"].(string) + } + if _, ok := pro["Abstract"].(string); ok { + sra.Comment = pro["Abstract"].(string) + } + sra.BioSamples = append(sra.BioSamples, struct { + BioSampleId string + Experiments []struct { + ExperimentId string + Runs []string + } + }{ + BioSampleId: pro["BioSample"].(string), + }) + sra.BioSamples[0].Experiments = append(sra.BioSamples[0].Experiments, struct { + ExperimentId string + Runs []string + }{ExperimentId: pro["ExperimentCode"].(string)}) + sra.BioSamples[0].Experiments[0].Runs = append(sra.BioSamples[0].Experiments[0].Runs, pro["Run"].(string)) + srr[projId] = sra + } else { + isInsideSample := false + + for k, sample := range val.BioSamples { + if sample.BioSampleId == pro["BioSample"].(string) { + isInsideSample = true + isInsideExample := false + for j, expreiment := range sample.Experiments { + if expreiment.ExperimentId == pro["ExperimentCode"].(string) { + isInsideExample = true + srr[projId].BioSamples[k].Experiments[j].Runs = append(srr[projId].BioSamples[k].Experiments[j].Runs, pro["Run"].(string)) + } + } + if !isInsideExample { + srr[projId].BioSamples[k].Experiments = append(srr[projId].BioSamples[k].Experiments, struct { + ExperimentId string + Runs []string + }{ + ExperimentId: pro["ExperimentCode"].(string), + }) + srr[projId].BioSamples[k].Experiments[len(srr[projId].BioSamples[k].Experiments)-1].Runs = append(srr[projId].BioSamples[k].Experiments[len(srr[projId].BioSamples[k].Experiments)-1].Runs, pro["Run"].(string)) + } + + } + } + if !isInsideSample { + prov := struct { + BioSampleId string + Experiments []struct { + ExperimentId string + Runs []string + } + }{BioSampleId: pro["BioSample"].(string)} + + prov.Experiments = append(prov.Experiments, struct { + ExperimentId string + Runs []string + }{ExperimentId: pro["ExperimentCode"].(string)}) + prov.Experiments[0].Runs = append(prov.Experiments[0].Runs, pro["Run"].(string)) + value := srr[projId] + value.BioSamples = append(value.BioSamples, prov) + srr[projId] = value + } + + } + } + for _, key := range srr { + sraProjects = append(sraProjects, key) + } + return sraProjects, nil +} + +// func (a myAnalysis) mutateStruct() (str.SraBioProject, ErrManager.Errors) { + +// } diff --git a/service/api/api-handler.go b/service/api/api-handler.go index 0c4a54b..3daf789 100644 --- a/service/api/api-handler.go +++ b/service/api/api-handler.go @@ -14,5 +14,6 @@ func (rt *_router) Handler() http.Handler { rt.router.GET("/organism/:id/nucleotides/:locus", rt.Get_Nucleotide_From_Locus) rt.router.GET("/organism/:id/proteins", rt.Get_Proteins_Id) rt.router.GET("/organism/:id/proteins/:locus", rt.Get_Protein_From_Locus) + rt.router.GET("/analysis", rt.Get_Analysis) return rt.router } diff --git a/service/api/routing-functions.go b/service/api/routing-functions.go index 8b0feef..726e0b4 100644 --- a/service/api/routing-functions.go +++ b/service/api/routing-functions.go @@ -135,3 +135,16 @@ func (rt *_router) Get_Protein_From_Locus(w http.ResponseWriter, r *http.Request } } + +func (rt *_router) Get_Analysis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + w.Header().Set("content-type", "application/json") + c, err := rt.GetAnalysis() + if err != nil { + http.Error(w, err.Error(), err.Type()) + return + } + if errJson := json.NewEncoder(w).Encode(c); errJson != nil { + http.Error(w, errJson.Error(), http.StatusBadRequest) + return + } +} diff --git a/service/database/analysis.go b/service/database/analysis.go new file mode 100644 index 0000000..0e71043 --- /dev/null +++ b/service/database/analysis.go @@ -0,0 +1,61 @@ +package database + +import ( + "context" + + errorM "github.com/BITSapienza/Bio-Server/service/ErrManager" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" +) + +// filter = { +// "STUDY.center_name": "BioProject", +// "SAMPLE.IDENTIFIERS.EXTERNAL_ID.namespace": "BioSample", +// } + +// proj = { +// "_id":0, +// "TaxId":"$SAMPLE.SAMPLE_NAME.TAXON_ID.value", +// "BioProject":"$STUDY.alias", +// "BioSample":"$SAMPLE.IDENTIFIERS.EXTERNAL_ID.value", +// "ExperimentCode":"$EXPERIMENT.accession", +// "SRR":"$RUN_SET.RUN.accession", +// "Abstract":"$STUDY.DESCRIPTOR.STUDY_ABSTRACT.value", +// "LinkSRA":"$RUN_SET.RUN.SRAFiles" +// } + +func (db *appDB) FindAnalysis() ([]map[string]interface{}, errorM.Errors) { + filter := bson.D{{ + Key: "$match", Value: bson.M{ + "STUDY.center_name": "BioProject", + "SAMPLE.IDENTIFIERS.EXTERNAL_ID.namespace": "BioSample", + }, + }} + proj := bson.D{{ + Key: "$project", Value: bson.M{ + "_id": 0, + "ScientificName": "$SAMPLE.SAMPLE_NAME.SCIENTIFIC_NAME.value", + "TaxId": "$SAMPLE.SAMPLE_NAME.TAXON_ID.value", + "BioProject": "$STUDY.alias", + "BioSample": "$SAMPLE.IDENTIFIERS.EXTERNAL_ID.value", + "ExperimentCode": "$EXPERIMENT.accession", + "Run": "$RUN_SET.RUN.accession", + "Abstract": "$STUDY.DESCRIPTOR.STUDY_ABSTRACT.value", + }, + }} + dataSeq, errM := db.sequences_data.Aggregate(context.TODO(), mongo.Pipeline{filter, proj}) + if errM != nil { + return nil, errorM.NewError(errM.Error(), errorM.StatusInternalServerError) + } + var seq_data []map[string]interface{} + for dataSeq.Next(context.TODO()) { + var seq map[string]interface{} + err := dataSeq.Decode(&seq) + if err != nil { + return nil, errorM.NewError("Can't Decode Result", errorM.StatusInternalServerError) + } + seq_data = append(seq_data, seq) + } + return seq_data, nil +} diff --git a/service/database/database.go b/service/database/database.go index 5ad0906..a6dc296 100644 --- a/service/database/database.go +++ b/service/database/database.go @@ -24,6 +24,7 @@ type AppDatabase interface { FindProteinsId(taxonId string) (str.TableBasic, errorM.Errors) FindProteinTableByLocus(taxId string, locus string) (str.TableComplete, errorM.Errors) FindProteinByLocus(locus string) (str.Protein, errorM.Errors) + FindAnalysis() ([]map[string]interface{}, errorM.Errors) } type appDB struct { @@ -34,7 +35,7 @@ type appDB struct { table_complete *mongo.Collection taxonomy_data *mongo.Collection taxonomy_tree *mongo.Collection - markdown *mongo.Collection + sequences_data *mongo.Collection } func InitDatabase(client *mongo.Client) (AppDatabase, error) { @@ -62,8 +63,8 @@ func InitDatabase(client *mongo.Client) (AppDatabase, error) { if collectionTaxonomyTree == nil { return nil, errors.New("error Creating users Collection") } - collectionMarkDown := client.Database("Biologia").Collection("markdown") - if collectionMarkDown == nil { + collectionSequences := client.Database("Biologia").Collection("sequences_data") + if collectionSequences == nil { return nil, errors.New("error Creating users Collection") } return &appDB{ @@ -74,6 +75,6 @@ func InitDatabase(client *mongo.Client) (AppDatabase, error) { table_complete: collectionTableComplete, taxonomy_data: collectionTaxonomy, taxonomy_tree: collectionTaxonomyTree, - markdown: collectionMarkDown, + sequences_data: collectionSequences, }, nil } diff --git a/service/structures/structures.go b/service/structures/structures.go index 1747924..487b057 100644 --- a/service/structures/structures.go +++ b/service/structures/structures.go @@ -124,3 +124,24 @@ type TableComplete struct { CountryName string `bson:"CountryName"` } `bson:"Country"` } + +type Genome struct { + TaxId string + ScientificName string + BioProjects []SraBioProject +} + +type SraBioProject struct { + ScientificName string + TaxId string + BioProjectId string + Comment string + ImagesPath []string + BioSamples []struct { + BioSampleId string + Experiments []struct { + ExperimentId string + Runs []string + } + } +}