forked from yshin1209/Computational-Medicine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two-Gene Network Simulation using Unity and Function Nanoservice
125 lines (104 loc) · 4.08 KB
/
Two-Gene Network Simulation using Unity and Function Nanoservice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Computational Medicine
// Two-Gene Network Simulation using Unity and Function Nanoservice
// Yong-Jun Shin (2019)using System.Collections;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class CellScript : MonoBehaviour
{
float x; // x protein concentration in uM (scalar)
float y; // y protein concentration in uM (scalar)
float Pxy; // production parameter
float Py; // degradation parameter
float r; // red color intensity representing y protein concentration (0 - 1)
float yMax; // maximal y protein concentration in uM
string url = "https://yongjunshin3.azurewebsites.net/api/SimpleTwoGeneRegulatoryNetworkService"; // service URL
// Start is called before the first frame update
void Start()
{
Pxy = 0.8f;
Py = 0.95f;
x = 10.0f; // constanx x protein concentration (= 10 uM)
y = 0.0f; // initial y protein concentration (= 0 uM)
yMax = 2 * Pxy * x / (1 - Py); // assume yMax equals 2 times steady state level
}
// FixedUpdate is called once per frame.
// 0.02 seconds (50 calls per second) is the default time between calls.
async Task FixedUpdate()
{
Time.fixedDeltaTime = 0.05f; // the time interval in seconds at which physics and other fixed
// fixed frame updates are performed.
// y = Pxy * x + Py * y; // difference equation
// data to be sent to the server
RequestData requestData = new RequestData();
requestData.x = x;
requestData.y = y;
requestData.Pxy = Pxy;
requestData.Py = Py;
using (HttpClient client = new HttpClient())
{
y = await PostAsync(client, requestData, url); // make a web service request
}
r = y / yMax; // compute r
GetComponent<Renderer>().material.color = new Color(r, 0, 0); // update the red color intensity
Debug.Log("y: " + y); // display y value in the Unity console window
Debug.Log("r: " + r); // display r value in the Unity console window
}
static async Task<float> PostAsync(HttpClient client, RequestData data, string url)
{
var request = new HttpRequestMessage(HttpMethod.Post, url);
string jsonRequestBody = JsonConvert.SerializeObject(data);
request.Content = new StringContent(jsonRequestBody, Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
ResponseData responseData = JsonConvert.DeserializeObject<ResponseData>(content);
return responseData.y;
}
}
public class RequestData
{
public float x;
public float y;
public float Pxy;
public float Py;
}
// server returns x and y
public class ResponseData
{
public float x;
public float y;
}
Server-side Azure Function code
// Computational Medicine
// Simple 2-Gene Regulatory Network Simulation using Function Nanoservice
// Yong-Jun Shin (2019)
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static async Task<Response> Run(HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
double x = data.x; // x protein (scalar)
double y = data.y; // y protein (scalar)
double Pxy = data.Pxy; // production parameter
double Py = data.Py; // degradation parameter
y = Pxy*x + Py*y; // difference equation
log.LogInformation($"The new y protein concentration is {y}");
//return y; // return new y value to the client
// return x and y (return multiple data)
Response r = new Response();
r.x = x;
r.y = y;
return r;
}
// create a new class to bundle response data
public class Response
{
public double x; // x protein (scalar)
public double y; // y protein (scalar)
}