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: 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:
<?php
namespace AnalyseModule\Models;
class ConservancyComparator extends \AnalyseModule\Models\BaseTool {
private $tmpDir;
private $sequenceList = array();
private $listFile;
private $winsize = 40;
private function createTmpDir() {
$dirName = (session_id()) ? md5(session_id() . time()) : md5(time() . rand());
$this->tmpDir = TEMP_PATH . '/conservancycomparator/' . $dirName;
mkdir($this->tmpDir);
return $this->tmpDir;
}
private function createFastas($sequences) {
foreach ($sequences as $sequence) {
if (isset($sequence->predictions[0])) {
$file = new \BaseModule\Models\FileModel($this->tmpDir . DIRECTORY_SEPARATOR . $sequence->accession . '.fst');
$file->appendContent('>' . $sequence->accession . "\n");
$file->appendContent($sequence->sequence . "\n");
$file->appendContent(\DispatchModule\Sequence::makeSimplePrediction($sequence->predictions[0]['structure']) . "\n");
$this->sequenceList[$sequence->accession] = $file;
}
}
}
private function createList($sequences) {
$this->listFile = new \BaseModule\Models\FileModel($this->tmpDir . DIRECTORY_SEPARATOR . 'list.txt');
foreach ($sequences as $sequence) {
$this->listFile->appendContent($sequence->accession . "\n");
}
}
private function buildCommand($dir) {
$command = 'compare.py ' . $dir;
$command = $command . ' -w ' . $this->winsize;
$command = $command . ' -s stats.csv';
$command = $command . ' 2>&1';
return $command;
}
public function execute($accessions, $winsize = null) {
$this->deleteOldDirs();
if ($winsize !== null) {
$this->winsize = $winsize;
}
$dir = $this->createTmpDir();
$sequences = $this->loadSequencesFromDb($accessions);
$this->createFastas($sequences);
$this->createList($sequences);
$command = $this->buildCommand($dir);
$r = $code = null;
exec($command, $r, $code);
if ($code !== 0) {
throw new \BaseModule\Exceptions\ToolException('Error while processing conservancy comparison - possibly missing structure [accession: ' . implode(',', $accessions) . '].');
}
return $sequences;
}
public function export($tmpPath, $svgs, $winsize) {
$statFile = $tmpPath . 'stats.csv';
$exportPath = $tmpPath . 'export';
$zipPath = $tmpPath . 'rP_CC_exp_' . date('Y-m-d-H-i') . '.zip';
$accessions = array();
if (!file_exists($exportPath)) {
mkdir($exportPath);
}
foreach ($svgs as $svgArr) {
$accRecord = explode(':', $svgArr[0]);
file_put_contents($exportPath . DIRECTORY_SEPARATOR . $accRecord[0] . '.svg', $svgArr[1]);
$accessions[] = $svgArr[0];
copy($tmpPath . $accRecord[0] . '.fst', $exportPath . DIRECTORY_SEPARATOR . $accRecord[0] . '.fst');
}
$text = $this->prepareExportText($accessions, $winsize, $statFile);
file_put_contents($exportPath . DIRECTORY_SEPARATOR . 'info.txt', $text);
$zip = new \ZipArchive();
$zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($exportPath), \RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strrpos($filePath, DIRECTORY_SEPARATOR) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return $zipPath;
}
private function prepareExportText($accessions, $winsize, $statFile) {
$stats = array();
if (file_exists($statFile)) {
$statHandle = fopen($statFile, 'r');
while (($row = fgetcsv($statHandle, 0, ',')) !== false) {
$stats[$row[0]] = array('total' => $row[2], 'avg' => $row[5]);
}
}
$sequences = $this->loadSequencesFromDb($accessions);
$text = 'This is description file for rPredictorDB Secondary structure comparison' . "\r\n";
$text .= '-----------------------' . "\r\n";
$text .= 'Export time: ' . date('d.m.Y H:i') . "\r\n";
$text .= 'Window size: ' . $winsize . "\r\n";
$text .= 'Structures compared:' . "\r\n";
foreach ($sequences as $sequence) {
$text .= $sequence->accession . ' (' . $sequence->name . '); average conservancy: ' . (1 - $stats[$sequence->accession]['avg']) . ')' . "\r\n";
}
$text .= '-----------------------' . "\r\n";
$text .= 'http://rpredictordb.elixir-czech.cz ' . "\r\n";
$text .= 'For detailed information about rPredictorDB Secondary structure comparisons visit http://rpredictordb.elixir-czech.cz/documentation/ ' . "\r\n";
return $text;
}
public static function deleteOldDirs() {
$path = TEMP_PATH . '/conservancycomparator/';
$files = scandir($path);
foreach ($files as $f) {
if ($f == "." || $f == "..") {
continue;
}
$timeDif = time() - filemtime($path . $f);
if ($timeDif > 24 * 60 * 60) {
\BaseModule\Models\FileModel::deleteDir($path . $f);
}
}
}
public function getTmpDir() {
return $this->tmpDir;
}
public function getWinSize() {
return $this->winsize;
}
}