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 .= '   VCell BioModels referenced in publication:
'; 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 .= '   user: ' . $ownerName . ',   model name: ' . $bmName . '
'; } } if (sizeof($publication->mathmodelReferences)>0){ $modellinktext .= '   VCell MathModels referenced in publication:
'; 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 .= '   user: ' . $ownerName . ',   model name: ' . $mmName . '
'; } } $text = '' . $publication->title . '
'; $text .= $authorText . '
'; $text .= $publication->citation . '
'; $publinktext = ''; if (!empty($publication->pubmedid)){ $pubmedurl = 'http://www.ncbi.nlm.nih.gov/pubmed/' . $publication->pubmedid; $pubmedlabel = 'PUBMED:' . $publication->pubmedid; $publinktext .= '' . $pubmedlabel . '      '; } if (!empty($publication->doi)){ $doiurl = 'https://dx.doi.org/' . $publication->doi; $doilabel = 'doi:' . $publication->doi; $publinktext .= '' . $doilabel . ''; } $text .= $publinktext . '
'; $text .= $modellinktext; return $text; } function vcell_date_from_timestamp($vcell_timestamp) { $myDate = $vcell_timestamp/1000; $datetext = gmdate("D, d M Y", $myDate) . '  ' . 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: "' . $biomodel->name . '"'; $datetext = 'Saved: ' . vcell_date_from_timestamp($biomodel->savedDate) . ''; $ownertext = 'Owned by: "' . $biomodel->ownerName . '"'; $bmBaseUrl = 'https://vcellapi.cam.uchc.edu/biomodel/' . $bmKey; $bmIdentifier = 'vcell identifier: biomodel-' . $bmKey . ''; $filename = 'VCBioModel_' . $bmKey . '.vcml'; $downloadtext = 'download model (.vcml)'; $diagramtext = ''; return $nametext . '
' . $datetext . '
' . $bmIdentifier . '
' . $ownertext . '

' . $downloadtext . '

' . $diagramtext . '

' . $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 = 'Compartments, Species, Reactions
'; 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) . '
' . 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("

Publication Example

"); print(test_model_publication()); print("

Biomodel Example

"); print(test_biomodel()); */ ?>