|
| 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 | +} |
0 commit comments