isPublic()) continue; //Skip private pathways $pathwaySpecies = $pathway->species(); if ($pathwaySpecies != $species) continue; $page_id = $pathway->getPageIdDB(); if (in_array($page_id, $taggedIds)) continue; //skip Tutorial pathways try { $xrefs = $pathway->getUniqueXrefs(); foreach ($xrefs as $xref){ $id = $xref->getId(); $db = $xref->getSystem(); if ($id && $id != '' && $id != ' '){ if (in_array($db, $databases)){ array_push($geneList, $id); } } } } catch (Exception $e) { // we can safely ignore exceptions // erroneous pathways simply won't get counted } } $geneList = array_unique($geneList); return count ($geneList); } /** * Calculates the number of pathways for each species. Unlike countUniqueGenes(), * this methods counts for all species every time. It's basically just as fast with the * current logic below. */ private static function countPathways() { $taggedIds = CurationTag::getPagesForTag('Curation:Tutorial'); $pathwaysPerSpecies = array(); //Set all species to 0 foreach(Pathway::getAvailableSpecies() as $species) { $pathwaysPerSpecies[$species] = 0; } $total = 0; $pathways = self::getAllPathways(); foreach($pathways as $pathway) { if ($pathway->isDeleted()) continue; //skip deleted pathways if(!$pathway->isPublic()) continue; //Skip private pathways $page_id = $pathway->getPageIdDB(); if (in_array($page_id, $taggedIds)) continue; // skip Tutorial pathways $species = $pathway->getSpecies(); if ($species == '') continue; //skip pathways without a species category $pathwaysPerSpecies{$species} += 1; $total += 1; } $pathwaysPerSpecies{'total'} = $total; return $pathwaysPerSpecies; } /** re-calculate the gene count for a particular species. This should be called when a pathway has been updated. */ public static function updateUniqueGenesCache ($species) { try { $data = StatisticsCache::readGeneCache(); $data[$species] = StatisticsCache::countUniqueGenes ($species); StatisticsCache::writeGeneCache ($data); return $data; } catch(Exception $e) { // likely having trouble opening files, perhaps due to permissions // files should have 664 permissions } } /** re-calculate the pathway count for all species. This should be called when a pathway has been created or deleted. */ public static function updatePathwaysCache() { try { $data = StatisticsCache::countPathways(); StatisticsCache::writePathwayCache($data); return $data; } catch(Exception $e) { // likely having trouble opening files, perhaps due to permissions // files should have 664 permissions } } private static function writeGeneCache ($data) { global $wgScriptPath; // write all data in $data back to the file again $filename = WPI_CACHE_PATH . '/UniqueGeneCounts.data'; $file = fopen($filename, 'w+'); foreach ($data as $key => $c) { fwrite ($file, "$key\t$c\n"); } fclose ($file); chmod($filename, 0666); } /** read the contents of the gene cache and return this as a set of $species => $count pairs */ private static function readGeneCache() { global $wgScriptPath; // read contents of the cache into variable $data $data = array(); $filename = WPI_CACHE_PATH . '/UniqueGeneCounts.data'; $file = fopen($filename, 'r'); if ($file) { while (!feof($file)) { if($line = trim(fgets($file))) { $explodedLine = explode("\t", $line); $data[$explodedLine[0]] = $explodedLine[1]; } } } return $data; } private static function writePathwayCache ($data) { global $wgScriptPath; // write all data in $data back to the file again $filename = WPI_CACHE_PATH . '/PathwayCounts.data'; $file = fopen($filename, 'w+'); foreach ($data as $key => $c) { fwrite ($file, "$key\t$c\n"); } fclose ($file); chmod($filename, 0666); } /** read the contents of the pathway cache and return this as a set of $species => $count pairs */ private static function readPathwayCache() { global $wgScriptPath; // read contents of the cache into variable $data $data = array(); $filename = WPI_CACHE_PATH . '/PathwayCounts.data'; $file = @fopen($filename, 'r'); if ($file) { while (!feof($file)) { if($line = trim(fgets($file))) { $explodedLine = explode("\t", $line); $data[$explodedLine[0]] = $explodedLine[1]; } } } return $data; } } ?>