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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/*

* Plugin Name: VCell ShortCode

* Description: Shortcode for VCell model content.

* Version: 1.1

* Author: Jim Schaff

*/
register_activation_hook(__FILE__, 'vcell_my_activation');

function vcell_my_activation() {
	vcell_log_me('my_activation() invoked ... running do_this_hourly() immeciately upon activation ... and then schedule it periodically');
	vcell_do_this_hourly();
#	if (! wp_next_scheduled('vcell_hourly_event' )) {
#		wp_schedule_event(time(), 'hourly', 'vcell_hourly_event');
#	}
}

#add_action('vcell_hourly_event', 'vcell_do_this_hourly');

function vcell_do_this_hourly(){
	vcell_log_me('hourly processing begun');
	$publications =& vcell_get_publications_object();
	vcell_log_me('inserting publications and biomodel posts as needed');
	foreach($publications as $pub){
		# make sure all publication posts are installed.
		$pubPost =& vcell_find_publication_post($pub->pubKey);
		if (is_null($pubPost)){
			vcell_insert_publication_post($pub);
			$pubPost =& vcell_find_publication_post($pub->pubKey);
		}
		if (!empty($pub->biomodelReferences)){
			foreach ($pub->biomodelReferences as $bioref){
				$bmKey = $bioref->bmKey;
//				$biomodelvcml =& vcell_get_biomodel_vcml_object($bmKey);
				$biomodel =& vcell_get_biomodel_object($bmKey);
				$bmPost =& vcell_find_biomodel_post($bmKey);
				if (is_null($bmPost)){
					vcell_insert_biomodel_post($biomodel);
				}else{
					$biomodelPostId = $bmPost->ID;
					vcell_update_biomodel_post($biomodel,$biomodelPostId);
				}
			}
		}
		vcell_log_me('updating publications post ' . $pubPost->ID);
		vcell_update_publication_post($pub, $pubPost->ID);
	}
	vcell_log_me('hourly processing complete');
}

function &vcell_find_biomodel_post($bmKey){
	$args = array(
			'meta_key' => 'bmKey',
			'meta_value' => $bmKey,
			'category_name' => 'biomodel',
			'post_type' => 'post',
			'post_status' => 'any',
			'posts_per_page' => -1
	);
	$posts = get_posts($args);
	if (is_null($posts) || empty($posts)){
		$posts = NULL;
		return $posts;
	}else if (is_array($posts)){
		return $posts[0];
	}else{
		return $posts;
	}
}

function vcell_insert_biomodel_post($biomodel){
	$biomodelCatID = get_cat_ID('biomodel');
	if ($biomodelCatID == 0){
		$biomodelCatID = wp_create_category('biomodel');
	}
	$post_arr = array(
		'post_title'   => 'biomodel-' . $biomodel->bmKey,
		'post_content' => vcell_get_biomodel_post_content($biomodel),
		'post_status'  => 'publish',
		'post_author'  => get_current_user_id(),
		'meta_input'   => array(
			'bmKey' => $biomodel->bmKey,
		),
		'post_category' => array( $biomodelCatID )
	);
	$post_id = wp_insert_post($post_arr);
	return $post_id;
}

function vcell_update_biomodel_post($biomodel, $biomodelPostId){
	$biomodelCatID = get_cat_ID('biomodel');
	if ($biomodelCatID == 0){
		$biomodelCatID = wp_create_category('biomodel');
	}
	$post_arr = array(
		'ID'		   => $biomodelPostId,
		'post_title'   => 'biomodel-' . $biomodel->bmKey,
		'post_content' => vcell_get_biomodel_post_content($biomodel),
		'post_status'  => 'publish',
		'post_author'  => get_current_user_id(),
		'meta_input'   => array(
			'bmKey' => $biomodel->bmKey,
		),
		'post_category' => array( $biomodelCatID )
	);
	$post_id = wp_update_post($post_arr);
	return $post_id;
}

function &vcell_find_publication_post($pubKey){
	$args = array(
			'meta_key' => 'pubKey',
			'meta_value' => $pubKey,
			'category_name' => 'publication',
			'post_type' => 'post',
			'post_status' => 'any',
			'posts_per_page' => -1
	);
	$posts = get_posts($args);
	if (is_null($posts) || empty($posts)){
		$posts = NULL;
		return $posts;
	}else if (is_array($posts)){
		return $posts[0];
	}else{
		return $posts;
	}
}

function vcell_update_publication_post($publication, $pubPostId){
	$publicationCatID = get_cat_ID('publication');
	if ($publicationCatID == 0){
		$publicationCatID = wp_create_category('publication');
	}
	$publicationYearCatName = 'publication-' . $publication->year;
	$publicationYearCatID = get_cat_ID($publicationYearCatName);
	if ($publicationYearCatID == 0){
		$publicationYearCatID = wp_create_category($publicationYearCatName);
	}
	$post_arr = array(
		'ID'		   => $pubPostId,
		'post_title'   => vcell_get_publication_post_title($publication),
		'post_content' => vcell_get_publication_post_content($publication),
		'post_status'  => 'publish',
		'post_author'  => get_current_user_id(),
		'meta_input'   => array(
			'pubKey' => $publication->pubKey,
		),
		'post_category' => array( $publicationCatID, $publicationYearCatID )
	);
	$post_id = wp_update_post($post_arr);
	return $post_id;
}

function vcell_insert_publication_post($publication){
	$publicationCatID = get_cat_ID('publication');
	if ($publicationCatID == 0){
		$publicationCatID = wp_create_category('publication');
	}
	$publicationYearCatName = 'publication-' . $publication->year;
	$publicationYearCatID = get_cat_ID($publicationYearCatName);
	if ($publicationYearCatID == 0){
		$publicationYearCatID = wp_create_category($publicationYearCatName);
	}
	$post_arr = array(
		'post_title'   => vcell_get_publication_post_title($publication),
		'post_content' => vcell_get_publication_post_content($publication),
		'post_status'  => 'publish',
		'post_date'    => date("Y-m-d H:i:s", strtotime("Jan 1, " . $publication->year) ),
		'post_author'  => get_current_user_id(),
		'meta_input'   => array(
			'pubKey' => $publication->pubKey,
		),
		'post_category' => array( $publicationCatID, $publicationYearCatID )
	);
	$post_id = wp_insert_post($post_arr);
	return $post_id;
}

function &vcell_get_biomodel_object($bmKey) {
	$json = file_get_contents("https://vcellapi.cam.uchc.edu/biomodel/" . $bmKey);
	$biomodel = json_decode($json);
	return $biomodel;
}

function &vcell_get_biomodel_vcml_object($bmKey){
	$vcmlString = file_get_contents("https://vcellapi.cam.uchc.edu/biomodel/" . $bmKey . '/biomodel.vcml');
    vcell_log_me("bmKey is " . $bmKey . ", vcmlString is '" . $vcmlString . "'");
    $biomodel_vcml = new SimpleXMLElement($vcmlString) or die('unable to retrieve vcml');
	return $biomodel_vcml;
}

function &vcell_get_publication_object($pubKey) {
	$json = file_get_contents("https://vcellapi.cam.uchc.edu/publication/" . $pubKey);
	$publication = json_decode($json);
	return $publication;
}

function &vcell_get_publications_object() {
vcell_log_me('calling vcellapi for publications');
	$json = file_get_contents("https://vcellapi.cam.uchc.edu/publication");
vcell_log_me('vcellapi publications json = ' . $json );
	$publications = json_decode($json);
	return $publications;
}



/**

*  Post title

*/
function vcell_get_publication_post_title($publication) {
	$authorText = '';
	if (sizeof($publication->authors)>1){
		$authorText = explode(',', reset($publication->authors))[0] . ' et al., ';
	}else{
		$authorText = reset($publication->authors) . ', ';
	}
	$authorText .= $publication->year;
	return $authorText;
}

/**

*  Post content

*/
function vcell_get_publication_post_content($publication) {
	$authorText = '';
	$bFirst = 1;
	foreach ($publication->authors as $author){
		if ($bFirst == 1){
			$authorText = $author;
			$bFirst = 0;
		}else{
			$authorText .= ", " . $author;
		}
	}
	$modellinktext = '';

	vcell_log_me('getting publication post content for pubKey ' . $publication->pubKey);
	if (sizeof($publication->biomodelReferences)>0){
		$modellinktext .= '&nbsp;&nbsp;&nbsp;VCell BioModels referenced in publication:<br/>';
		foreach ($publication->biomodelReferences as $biomodelref){
			$bmKey = $biomodelref->bmKey;
			$bmName = $biomodelref->name;
			$ownerKey = $biomodelref->ownerKey;
			$ownerName = $biomodelref->ownerName;
			$bmPost =& vcell_find_biomodel_post($bmKey);
			if (is_null($bmPost)){
				$bmurl = '/biomodel-' . $bmKey;  # this is a placeholder
				vcell_log_me('    using fake link for publication ' . $publication->pubKey . ' biomodel ' . $bmKey);
			}else{
				$bmurl = get_permalink($bmPost->ID);
				vcell_log_me('    using real link for publication ' . $publication->pubKey . ' biomodel ' . $bmKey);
			}
			$modellinktext .= '&nbsp;&nbsp;&nbsp;user: ' . $ownerName . ', &nbsp;&nbsp;model name: <a href="' . $bmurl . '">' . $bmName . '</a><br/>';
		}
	} 

	if (sizeof($publication->mathmodelReferences)>0){
		$modellinktext .= '&nbsp;&nbsp;&nbsp;VCell MathModels referenced in publication:<br/>';
		foreach ($publication->mathmodelReferences as $mathmodelref){
			$mmKey = $mathmodelref->mmKey;
			$mmName = $mathmodelref->name;
			$ownerKey = $mathmodelref->ownerKey;
			$ownerName = $mathmodelref->ownerName;
			$mmurl = '/mathmodel-' . $mmKey;   # this won't work
			$modellinktext .= '&nbsp;&nbsp;&nbsp;user: ' . $ownerName . ', &nbsp;&nbsp;model name: <a href="' . $mmurl . '">' . $mmName . '</a><br/>';
		}
	} 

	$text = '<strong><big>' . $publication->title . '</big></strong><br/>';
	$text .= $authorText . '<br/>';
	$text .= $publication->citation . '<br/>';
	$publinktext = '';
	if (!empty($publication->pubmedid)){
		$pubmedurl = 'http://www.ncbi.nlm.nih.gov/pubmed/' . $publication->pubmedid;
		$pubmedlabel = 'PUBMED:' . $publication->pubmedid;
		$publinktext .= '<a href="' . $pubmedurl . '">' . $pubmedlabel . '</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	if (!empty($publication->doi)){
		$doiurl = 'https://dx.doi.org/' . $publication->doi;
		$doilabel = 'doi:' . $publication->doi;
		$publinktext .= '<a href="' . $doiurl . '">' . $doilabel . '</a>';
	}
	$text .= $publinktext . '<br/>';
	$text .= $modellinktext;
	return $text;
}

function vcell_date_from_timestamp($vcell_timestamp) {
	$myDate = $vcell_timestamp/1000;
	$datetext = gmdate("D, d M Y", $myDate) . '&nbsp;&nbsp;' . gmdate("G:i:s \G\M\T", $myDate);
	return $datetext;
}

function vcell_get_biomodel_post_content($biomodel){
	$bmKey = $biomodel->bmKey;
	vcell_log_me('getting biomodel post content for bmKey ' . $bmKey . ' with name "' . $biomodel->name . '"');
	
	$compartmenttext = '[vcell-biomodel-compartments bmkey="' . $bmKey . '"]';
	$nametext = 'Name: <strong>"' . $biomodel->name . '"</strong>';
	$datetext = 'Saved: <strong>' . vcell_date_from_timestamp($biomodel->savedDate) . '</strong>';
	$ownertext = 'Owned by: <strong>"' . $biomodel->ownerName . '"</strong>';
	$bmBaseUrl = 'https://vcellapi.cam.uchc.edu/biomodel/' . $bmKey;
	$bmIdentifier = 'vcell identifier: <strong>biomodel-' . $bmKey . '</strong>';
	$filename = 'VCBioModel_' . $bmKey . '.vcml';
	$downloadtext = '<a href="' . $bmBaseUrl . '/biomodel.vcml" type="application/vcml+xml" download="' . $filename . '">download model (.vcml)</a>';
	$diagramtext = '<img src="https://vcellapi.cam.uchc.edu/biomodel/' . $bmKey . '/diagram"/>';
	return $nametext . '<br/>' . $datetext . '<br/>' . $bmIdentifier . '<br>' . $ownertext . '<br/><br/>' . $downloadtext . '<br/><br/>' . $diagramtext . '<br/><br/>' . $compartmenttext;
}

function vcell_get_biomodel_compartment_content($biomodelvcml){
	#
	# parse xml for compartments, species, reactions into nice arrays (avoiding nested iterators of SimpleXMLElement)
	#
	$compArray = [];
	$reactionArray = [];
	$speciesArray = [];
	$modelChildren = $biomodelvcml->BioModel->Model->children();
	foreach($modelChildren as $modelChild){
		if ($modelChild->getName() == "Feature" OR $modelChild->getName() == "Membrane"){
			$structureElement = $modelChild;
			$compartmentType = '';
			if ($structureElement->getName() == "Feature"){
				$compartmentType = "Volume";
			}else if ($structureElement->getName() == "Membrane"){
				$compartmentType = "Membrane";
			}
			array_push($compArray, array(
				'type'		=> $compartmentType,
				'name'		=> $structureElement['Name']
				));
		}else if ($modelChild->getName() == "LocalizedCompound"){
			$speciesElement = $modelChild;
			array_push($speciesArray, array(
				'structure'		=> $speciesElement['Structure'],
				'name'			=> $speciesElement['Name']
				));
		}else if ($modelChild->getName() == "SimpleReaction" OR $modelChild->getName() == "FluxStep"){
			$reactionElement = $modelChild;
			$reactants = [];
			$products = [];
			foreach($reactionElement->children() as $reactionChild){
				if ($reactionChild->getName() == "Reactant"){
					$reactantElement = $reactionChild;
					array_push($reactants, array(
						'species'					=> $reactantElement['LocalizedCompoundRef'],
						'stoichiometry'				=> $reactantElement['Stoichiometry']
						));
				}
				if ($reactionChild->getName() == "Product"){
					$productElement = $reactionChild;
					array_push($products, array(
						'species'					=> $productElement['LocalizedCompoundRef'],
						'stoichiometry'				=> $productElement['Stoichiometry']
						));
				}
			}
			array_push($reactionArray, array(
				'structure'		=> $reactionElement['Structure'],
				'name'			=> $reactionElement['Name'],
				'reactants'		=> $reactants,
				'products'		=> $products
				));
		}
	}
	$compartmenttext = '<strong>Compartments, Species, Reactions</strong><br/><ul>';
	foreach($compArray as $compartment){
		$compartmenttext .= '<li>';
		$compartmenttext .= ' Compartment <strong>"' . $compartment['name'] . '"</strong>. ' . ' &nbsp; (type: ' . $compartment['type'] . ')';
		#
		# add text for species in this compartment
		#
		$speciestext = '';
		foreach($speciesArray as $species){
			if (strcmp($species['structure'],$compartment['name'])==0) {
				$speciestext .= '<li>Species <strong>"' . $species['name'] . '"</strong></li>';
			}
		}

		#
		# add text for reactions in this compartment
		#
		$reactiontext = '';
		$null_set_symbol = '&#x2205;';  // null character
		$arrow_symbol = '&#8594;'; // arrow character
		#$null_set_symbol = '&nbsp;&nbsp;&nbsp;&nbsp;';  // null character
		#$arrow_symbol = '--&gt;'; // arrow character
		foreach($reactionArray as $reaction){
			if (strcmp($reaction['structure'],$compartment['name'])==0) {
				$reactiontext .= '<li>';
				$reactiontext .= 'Reaction <strong>"' . $reaction['name'] . '"</strong> &nbsp; ';
				$reactiontext .= '(';
				$reactants = [];
				if (count($reaction['reactants'])>0){
					foreach ($reaction['reactants'] as $reactant){
						if ($reactant['stoichiometry'] == "1"){
							$reactiontext .= $reactant['species'];
						}else{
							$reactiontext .= $reactant['stoichiometry'] . '&nbsp;' . $reactant['species'];
						}
						if ($reactant != end($reaction['reactants'])){
							$reactiontext .= ' + ';
						}
					}
				}else{
					$reactiontext .= $null_set_symbol;
				}
				
				$reactiontext .= ' ' . $arrow_symbol . ' ';
				if (count($reaction['products'])>0){
					foreach ($reaction['products'] as $product){
						if ($product['stoichiometry'] == 1){
							$reactiontext .= $product['species'];
						}else{
							$reactiontext .= $product['stoichiometry'] . '&nbsp;' . $product['species'];
						}
						if ($product != end($reaction['products'])){
							$reactiontext .= ' + ';
						}
					}
				}else{
					$reactiontext .= $null_set_symbol;
				}
				$reactiontext .= ')</li>';
			}
		}
		if (strlen($reactiontext)>0 OR strlen($speciestext)>0){
			$compartmenttext .= '<ul>' . $speciestext . $reactiontext . '</ul>';
		}
		$compartmenttext .= '</li>';
	}
	$compartmenttext .= '</ul>';
	return $compartmenttext;
}

function vcell_log_me($message) {
    if (WP_DEBUG === true) {
        if (is_array($message) || is_object($message)) {
            error_log(print_r($message, true));
        } else {
            error_log($message);
        }
    }
}

function vcell_get_model_detail($atts) {
vcell_log_me('calling shortcode vcell-biomodel-compartments with bmKey = ' . $atts['bmkey'] );
vcell_log_me($atts);
	$bmKey = $atts['bmkey'];
vcell_log_me('in shortcode vcell-biomodel-compartments using bmKey = ' . $bmKey );
	try {
        $biomodelvcml =& vcell_get_biomodel_vcml_object($bmKey);
        $text = vcell_get_biomodel_compartment_content($biomodelvcml);
    } catch (Exception $e) {
		echo 'Caught exception: ', $e->getMessage(), "\n";
		$text = "model details unavailable for this model";
	}
vcell_log_me('returning ' . $text);
	return $text;
}

add_shortcode('vcell-biomodel-compartments','vcell_get_model_detail');

/**

 *

 * testing post content in fiddle

 *

 */
/*

function test_biomodel(){

	$bmKey = 2917738;

	$biomodel =& vcell_get_biomodel_object($bmKey);

	$biomodelvcml =& vcell_get_biomodel_vcml_object($bmKey);

	return vcell_get_biomodel_post_content($biomodel, $biomodelvcml);

}



function test_model_publication(){

	$pubKey = 102336415;

	$publication =& vcell_get_publication_object($pubKey);

	return vcell_get_publication_post_title($publication) . '<br/>' . vcell_get_publication_post_content($publication);

}



#  fake functions for use in phpFiddle

function vcell_log_me($message) {

    print($message);

}



function register_activation_hook(){}

function add_shortcode(){}

function add_action(){}



print("<h1>Publication Example</h1>");

print(test_model_publication());

print("<h1>Biomodel Example</h1>");

print(test_biomodel());

*/
?>