Skip to content

Commit

Permalink
Merge pull request #5 from federico-rosatelli/main
Browse files Browse the repository at this point in the history
SRA Analysis
  • Loading branch information
federico-rosatelli committed Jun 9, 2023
2 parents 8be0ed3 + c3a94c5 commit 0da5998
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 4 deletions.
108 changes: 108 additions & 0 deletions service/api/analysis.go
Original file line number Diff line number Diff line change
@@ -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) {

// }
1 change: 1 addition & 0 deletions service/api/api-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions service/api/routing-functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
61 changes: 61 additions & 0 deletions service/database/analysis.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 5 additions & 4 deletions service/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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{
Expand All @@ -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
}
21 changes: 21 additions & 0 deletions service/structures/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 0da5998

Please sign in to comment.