diff --git a/tools/grin/compose.yml b/tools/grin/compose.yml new file mode 100644 index 00000000..6a56f0a8 --- /dev/null +++ b/tools/grin/compose.yml @@ -0,0 +1,8 @@ +services: + mongo: + image: docker.io/library/mongo + ports: + - '27017:27017' + volumes: + - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + - /data/db \ No newline at end of file diff --git a/tools/grin/docker-entrypoint-initdb.d/load.sh b/tools/grin/docker-entrypoint-initdb.d/load.sh new file mode 100644 index 00000000..a00d0d0f --- /dev/null +++ b/tools/grin/docker-entrypoint-initdb.d/load.sh @@ -0,0 +1 @@ +gzip -dc /docker/entrypoint-initdb.d/observations-Glycine.json.gz | mongoimport -c observations --jsonArray \ No newline at end of file diff --git a/tools/grin/docker-entrypoint-initdb.d/observations-Glycine.json.gz b/tools/grin/docker-entrypoint-initdb.d/observations-Glycine.json.gz new file mode 100644 index 00000000..b8d59b58 Binary files /dev/null and b/tools/grin/docker-entrypoint-initdb.d/observations-Glycine.json.gz differ diff --git a/tools/grin/index.html b/tools/grin/index.html index cb3eaee7..7c3a0670 100644 --- a/tools/grin/index.html +++ b/tools/grin/index.html @@ -106,6 +106,7 @@ + @@ -143,7 +144,7 @@

GRIN Data Explorer

-
+
- + diff --git a/tools/grin/results.html b/tools/grin/results.html new file mode 100644 index 00000000..409ed7e9 --- /dev/null +++ b/tools/grin/results.html @@ -0,0 +1,109 @@ + + + + + + + + + GRIN Data Explorer - Results + + +
+
+
+
+
+ + + + + +
+ GRIN Data Explorer - Results +
+ +
+ +
+
+
+
+ +
+ +
+
+
+ + \ No newline at end of file diff --git a/tools/grin/results.js b/tools/grin/results.js new file mode 100644 index 00000000..1e2f1d88 --- /dev/null +++ b/tools/grin/results.js @@ -0,0 +1,249 @@ +// Import necessary libraries +const { MongoClient } = require('mongodb'); + +// Configuration +const uri = 'mongodb://localhost:27017'; // MongoDB connection URI +const dbName = 'soybase'; +const client = new MongoClient(uri); + +(async () => { + try { + await client.connect(); + console.log('Connected to MongoDB'); + + const db = client.db(dbName); + + // Step 1: Get distinct cultivars + const distinctCultivars = await db + .collection('germplasm_grin_descriptor_data') + .distinct('cultivar_name'); + + // Step 2: Process requested cultivars + let requestedCultivarsRaw = ''; // Replace with input from the request + let requestedCultivars = requestedCultivarsRaw + .split('\n') + .map(cultivar => cultivar.trim()) + .filter(cultivar => cultivar !== ''); + + let requestedCultivarsString = requestedCultivars + .map(cultivar => `'${cultivar}'`) + .join(','); + + // Step 3: Check for alternate cultivar names + if (requestedCultivars.length > 0) { + const alternateNames = await db + .collection('germplasm_grin_othernames') + .find({ alternate_name: { $in: requestedCultivars } }) + .toArray(); + + if (alternateNames.length > 0) { + alternateNames.forEach(alternate => { + const index = requestedCultivars.indexOf(alternate.alternate_name); + if (index !== -1) { + requestedCultivars[index] = alternate.grin_name; + } + }); + + requestedCultivarsString = requestedCultivars + .map(cultivar => `'${cultivar}'`) + .join(','); + } + } + + // Step 4: Find missing cultivars + const missingCultivars = requestedCultivars.filter( + cultivar => !distinctCultivars.includes(cultivar) + ); + + requestedCultivars = requestedCultivars.filter( + cultivar => !missingCultivars.includes(cultivar) + ); + + // Step 5: Get request specifics + const categories = [ + 'Disease', + 'Insect', + 'Nematode', + 'Stress', + 'Morphology', + 'Growth', + 'Root', + 'Phenology', + 'Chemical', + 'Production', + 'User Submitted' + ]; + + const requestSpecifics = await db + .collection('germplasm_grin_descriptor_definitions') + .find() + .toArray(); + + // Mock user input for selected descriptors + requestSpecifics.forEach(specific => { + specific.selected = 'no'; // Replace with actual user input logic + }); + + const chosenDescriptors = requestSpecifics + .filter(specific => specific.selected === 'yes') + .map(specific => specific.descriptor_shortname); + + const relevantDescriptorsList = chosenDescriptors + .map(descriptor => `'${descriptor}'`) + .join(','); + + // Step 6: Get distinct cultivars matching criteria + const distinctCultivarResults = await db + .collection('germplasm_grin_descriptor_data') + .aggregate([ + { $match: { cultivar_name: { $in: requestedCultivars } } }, + { $group: { _id: { cultivar_name: '$cultivar_name', grin_acc_id: '$grin_acc_id' } } } + ]) + .toArray(); + + // Map results to key-value format + const maximumResults = {}; + distinctCultivarResults.forEach(result => { + const key = `${result._id.cultivar_name}_${result._id.grin_acc_id}`; + maximumResults[key] = { + cultivar_name: result._id.cultivar_name, + grin_acc_id: result._id.grin_acc_id + }; + }); + + // Step 7: Fetch descriptor values + const descriptorValues = await db + .collection('germplasm_grin_descriptor_data') + .find({ + descriptor_shortname: { $in: chosenDescriptors }, + cultivar_name: { $in: requestedCultivars } + }) + .toArray(); + + // Populate maximumResults with descriptor values + descriptorValues.forEach(value => { + const key = `${value.cultivar_name}_${value.grin_acc_id}`; + if (maximumResults[key]) { + maximumResults[key][value.descriptor_shortname] = value.descriptor_value; + } + }); + + console.log('Final Results:', maximumResults); + } catch (err) { + console.error('Error:', err); + } finally { + await client.close(); + console.log('MongoDB connection closed'); + } +})(); + + + + + + + + + + + GRIN Data Explorer - Results + + +
+
+
+
+
+ + + + + +
+ GRIN Data Explorer - Results +
+ +
+ +
+
+
+
+ +
+ +
+
+
+ + \ No newline at end of file diff --git a/tools/grin/results.php b/tools/grin/results.php index 2a81a2cd..61804626 100644 --- a/tools/grin/results.php +++ b/tools/grin/results.php @@ -6,7 +6,7 @@ // FIX - Get an array of all possible distinct cultivars $arrDistinctCultivarResults = array(); - $strSQLDistinctCultivars = "SELECT DISTINCT(cultivar_name) FROM germplasm_grin_descriptor_data"; + $strSQLDistinctCultivars = "SELECT DISTINCT(cultivar_name) FROM germplasm_grin_descriptor_data"; $prepSQLDistinctCultivars = $dbConnectionSoybase->prepare($strSQLDistinctCultivars); $prepSQLDistinctCultivars->execute(); $arrDistinctCultivars = $prepSQLDistinctCultivars->fetchAll(PDO::FETCH_ASSOC); @@ -35,7 +35,7 @@ $prepSQLAlternateNames = $dbConnectionSoybase->prepare($strSQLAlternateNames); $prepSQLAlternateNames->execute(); $arrAlternateNames = $prepSQLAlternateNames->fetchAll(PDO::FETCH_ASSOC); - if (!empty($arrAlternateNames)){ + if (!empty($arrAlternateNames)) $intAlternateNamesCount = $prepSQLAlternateNames->rowCount(); $intCultivarsCount = $prepSQLAlternateNames->rowCount(); if($intAlternateNamesCount > 0){ diff --git a/tools/grin/script/jsonToMongo.py b/tools/grin/script/jsonToMongo.py new file mode 100644 index 00000000..f6986a17 --- /dev/null +++ b/tools/grin/script/jsonToMongo.py @@ -0,0 +1,16 @@ +import pymongo +import json +from pymongo import MongoClient, InsertOne + +client = pymongo.MongoClient() +db = client. +collection = db. +requesting = [] + +with open(r"") as f: + for jsonObj in f: + myDict = json.loads(jsonObj) + requesting.append(InsertOne(myDict)) + +result = collection.bulk_write(requesting) +client.close() \ No newline at end of file diff --git a/tools/grin/scripts.js b/tools/grin/scripts.js index a4076bbf..aefb1edc 100644 --- a/tools/grin/scripts.js +++ b/tools/grin/scripts.js @@ -168,4 +168,49 @@ function funCultivarFileChanged(){ })(f); reader.readAsText(f); +} + +function fetchData(){ + fetch('http://localhost:3000/data') // Replace with your Express server URL + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); // Assuming your server sends JSON data + }) + .then(data => { + // Do something with the fetched data + console.log(data); + }) + .catch(error => { + // Handle errors + console.error('There has been a problem with your fetch operation:', error); + }); +} + +function fetchDataPost(){ + fetch('http://localhost:3000/data1', { + method: "POST", + body: JSON.stringify({ + id: 2916156, + + }), + headers: { + "Content-type": "application/json; charset=UTF-8" + } + }) // Replace with your Express server URL + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); // Assuming your server sends JSON data + }) + .then(data => { + // Do something with the fetched data + console.log(data); + }) + .catch(error => { + // Handle errors + console.error('There has been a problem with your fetch operation:', error); + }); } \ No newline at end of file diff --git a/tools/grin/server/server.js b/tools/grin/server/server.js new file mode 100644 index 00000000..577ce363 --- /dev/null +++ b/tools/grin/server/server.js @@ -0,0 +1,44 @@ +const express = require('express'); +const {MongoClient} = require('mongodb'); +const cors = require('cors') + +const app = express(); +const PORT = process.env.PORT || 3000; +const MONGODB_URI = 'mongodb://localhost:27017/'; + +app.use(express.json()); +app.use(cors()); + +// Connect to MongoDB +MongoClient.connect(MONGODB_URI) + .then(client => { + const db = client.db('grin'); + const collection = db.collection('glycine'); + + // Example API endpoint to get data from MongoDB + app.get('/data', async (req, res) => { + console.log("/data") + try { + const result = await collection.find({observationDbId: '615056'}).toArray(); + res.json(result); + } catch (err) { + console.error(err); + res.status(500).send('Error fetching data from MongoDB'); + } + }); + + app.post('/data1', async (req, res) => { + console.log(req.body.id) + + }); + + + + app.listen(PORT, () => { + console.log(`Server listening on port ${PORT}`); + }); + }) + .catch(err => { + console.error('Error connecting to MongoDB:', err); + }); +