Skip to content

Commit ad075cf

Browse files
committed
Add cell lines count per tissue
rename tests -> test update readme add temp drugs count function with endpoint handler (to be updated in next commit
1 parent 130b20f commit ad075cf

File tree

6 files changed

+127
-3
lines changed

6 files changed

+127
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Application currently has two main components: [`api`](./api) and [`web-applicat
1212
## Table of Contents
1313

1414
- [**API**](#api)
15-
- [Endpoints](#endpoints)
16-
- [Running the API Locally](#running-the-api-locally)
15+
- [**Endpoints**](#endpoints)
16+
- [**Running the API Locally**](#running-the-api-locally)
1717
- [**Web Application**](#web-application)
1818
- [**Contributing**](#contributing)
1919
- [**License**](#license)

api/handlers.go

+24
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,27 @@ func CellDatasetIntersection(c *gin.Context) {
648648
}
649649
RenderJSON(c, indent, experiments)
650650
}
651+
652+
// TissueCellStats is a handler for '/stats/cell_tissues' endpoint.
653+
// Lists all tissues, along with the number of cell lines in each tissue.
654+
func TissueCellStats(c *gin.Context) {
655+
indent, _ := strconv.ParseBool(c.DefaultQuery("indent", "true"))
656+
data, err := CountCellsPerTissue()
657+
if err != nil {
658+
LogInternalServerError(c)
659+
return
660+
}
661+
RenderJSON(c, indent, data)
662+
}
663+
664+
// DatasetDrugStats is a handler for '/stats/dataset_drugs' endpoint.
665+
// Lists all datasets, along with the number of drugs tested in each dataset.
666+
func DatasetDrugStats(c *gin.Context) {
667+
indent, _ := strconv.ParseBool(c.DefaultQuery("indent", "true"))
668+
data, err := CountDrugsPerDataset()
669+
if err != nil {
670+
LogInternalServerError(c)
671+
return
672+
}
673+
RenderJSON(c, indent, data)
674+
}

api/routes.go

+3
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,7 @@ var routes = Routes{
5050
Route{GET, "/intersections", IndexIntersection},
5151
Route{GET, "/intersections/1/:cell_id/:drug_id", CellDrugIntersection},
5252
Route{GET, "/intersections/2/:cell_id/:dataset_id", CellDatasetIntersection},
53+
54+
Route{GET, "/stats/tissue_cells", TissueCellStats},
55+
Route{GET, "/stats/dataset_drugs", DatasetDrugStats},
5356
}

api/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Init(c *Context) {
2121

2222
router.StaticFile("/favicon.ico", "./static/images/favicon.png")
2323

24-
v := router.Group(Version())
24+
v := router.Group(Version() + "/")
2525
for _, route := range routes {
2626
v.Handle(route.Method, route.Endpoint, route.Handler)
2727
}

api/stats.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package api
2+
3+
import "strconv"
4+
5+
// TissueCellCount models the number of cell lines per tissue.
6+
type TissueCellCount struct {
7+
Tissue Tissue `json:"tissue"`
8+
Count int `json:"cell_lines_count"`
9+
}
10+
11+
// TissueCellCounts is a collection of TissueCellsCount.
12+
type TissueCellCounts []TissueCellCount
13+
14+
// DatasetDrugCount models the number of drugs tested per dataset.
15+
type DatasetDrugCount struct {
16+
Dataset Dataset `json:"dataset"`
17+
Count int `json:"drugs_count"`
18+
}
19+
20+
// DatasetDrugCounts is a collection of DatasetDrugCount.
21+
type DatasetDrugCounts []DatasetDrugCount
22+
23+
// CountCellsPerTissue returns a list of all tissues, along with the number of
24+
// cell lines of each tissue type.
25+
func CountCellsPerTissue() (TissueCellCounts, error) {
26+
var (
27+
tissueCellCount TissueCellCount
28+
tissueCellCounts TissueCellCounts
29+
)
30+
db, err := InitDB()
31+
defer db.Close()
32+
if err != nil {
33+
return tissueCellCounts, err
34+
}
35+
query := "SELECT t.tissue_id, t.tissue_name, COUNT(*) AS cell_lines_count FROM tissues t JOIN cells c ON c.tissue_id = t.tissue_id GROUP BY(c.tissue_id);"
36+
rows, err := db.Query(query)
37+
defer rows.Close()
38+
if err != nil {
39+
LogPrivateError(err)
40+
return tissueCellCounts, err
41+
}
42+
for rows.Next() {
43+
err = rows.Scan(&tissueCellCount.Tissue.ID, &tissueCellCount.Tissue.Name, &tissueCellCount.Count)
44+
if err != nil {
45+
LogPrivateError(err)
46+
return tissueCellCounts, err
47+
}
48+
tissueCellCounts = append(tissueCellCounts, tissueCellCount)
49+
}
50+
return tissueCellCounts, nil
51+
}
52+
53+
// CountDrugsPerDataset returns a list of all datasets, along with the number of
54+
// drugs tested in each dataset.
55+
func CountDrugsPerDataset() (DatasetDrugCounts, error) {
56+
type DDD struct {
57+
ID int
58+
Count int
59+
}
60+
var (
61+
datasetDrugCount DatasetDrugCount
62+
datasetDrugCounts DatasetDrugCounts
63+
DD DDD
64+
DDs []DDD
65+
)
66+
db, err := InitDB()
67+
defer db.Close()
68+
if err != nil {
69+
return datasetDrugCounts, err
70+
}
71+
query := "SELECT dataset_id, COUNT(DISTINCT drug_id) AS drugs_count FROM experiments GROUP BY dataset_id;"
72+
rows, err := db.Query(query)
73+
defer rows.Close()
74+
if err != nil {
75+
LogPrivateError(err)
76+
return datasetDrugCounts, err
77+
}
78+
for rows.Next() {
79+
err = rows.Scan(&DD.ID, &DD.Count)
80+
if err != nil {
81+
LogPrivateError(err)
82+
return datasetDrugCounts, err
83+
}
84+
DDs = append(DDs, DD)
85+
}
86+
for _, a := range DDs {
87+
var dataset Dataset
88+
err = dataset.Find(strconv.Itoa(a.ID), "id")
89+
if err != nil {
90+
return datasetDrugCounts, err
91+
}
92+
datasetDrugCount.Dataset = dataset
93+
datasetDrugCount.Count = a.Count
94+
datasetDrugCounts = append(datasetDrugCounts, datasetDrugCount)
95+
}
96+
return datasetDrugCounts, nil
97+
}

tests/.keep renamed to test/.keep

File renamed without changes.

0 commit comments

Comments
 (0)