Overview

Namespaces

  • AnalyseModule
    • Models
  • BaseModule
    • Exceptions
    • Models
    • Repository
    • Services
  • DispatchModule
    • Helpers
    • Models
    • Tools
  • PredictModule
  • SearchModule

Classes

  • AnalyseModule\AnalysePresenter
  • AnalyseModule\ConservancyPresenter
  • AnalyseModule\Models\BaseTool
  • AnalyseModule\Models\ConservancyComparator
  • BaseModule\BasePresenter
  • BaseModule\DiscoveredViewReflection
  • BaseModule\ErrorPresenter
  • BaseModule\Form
  • BaseModule\HomepagePresenter
  • BaseModule\Models\BaseModel
  • BaseModule\Models\FileModel
  • BaseModule\PesekPresenter
  • BaseModule\Repository\BaseRepository
  • BaseModule\Repository\TestRepository
  • BaseModule\RouterFactory
  • BaseModule\RssPresenter
  • BaseModule\Services\Authenticator
  • BaseModule\Services\BaseService
  • BaseModule\Services\DbService
  • DispatchModule\BaseMatch
  • DispatchModule\BaseParser
  • DispatchModule\BlastMatch
  • DispatchModule\BlatMatch
  • DispatchModule\Helpers\RnaplotHelper
  • DispatchModule\Helpers\TravelerHelper
  • DispatchModule\Models\BaseModel
  • DispatchModule\Models\BlastModel
  • DispatchModule\Models\BlastXMLParser
  • DispatchModule\Models\BlatModel
  • DispatchModule\Models\Cppredict2Model
  • DispatchModule\Models\CppredictModel
  • DispatchModule\Models\FastaModel
  • DispatchModule\Models\FileModel
  • DispatchModule\PredictParser
  • DispatchModule\ResultSet
  • DispatchModule\SearchParser
  • DispatchModule\Sequence
  • DispatchModule\Tools\AnnotationDbTool
  • DispatchModule\Tools\BaseTool
  • DispatchModule\Tools\Cppredict2Tool
  • DispatchModule\Tools\DbTool
  • DispatchModule\Tools\SimilarityTool
  • DispatchModule\Tools\TaxonomyDbTool
  • PredictModule\PredictPresenter
  • SearchModule\SearchPresenter

Interfaces

  • DispatchModule\Tools\ToolInterface

Exceptions

  • BaseModule\Exceptions\BaseException
  • BaseModule\Exceptions\NotFoundException
  • BaseModule\Exceptions\ToolException
  • Overview
  • Namespace
  • Class
  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;

/**
 * ConservancyComparator handles execution of ConservancyComparator utility. It
 * allows user to compare several sequences and visualize results in SVG plots
 */
class ConservancyComparator extends \AnalyseModule\Models\BaseTool {

    /**
     * @var string path to created temporary directory
     */
    private $tmpDir;

    /**
     * @var array list of sequences to compare
     */
    private $sequenceList = array();

    /**
     * @var string path to temporary file with list of names of the fasta files to compare
     */
    private $listFile;

    /**
     * @var int window size used for ComparisonTagger
     */
    private $winsize = 40;

    /**
     * Creates temporary directory (in which fasta files, list files,
     * output svg's and other temp files are stored)
     *
     * @return \BaseModule\Models\FileModel temporary file
     */
    private function createTmpDir() {
        $dirName = (session_id()) ? md5(session_id() . time()) : md5(time() . rand());
        // Dirname needs to be given relative to cp root
        $this->tmpDir = TEMP_PATH . '/conservancycomparator/' . $dirName;
        mkdir($this->tmpDir);

        return $this->tmpDir;
    }

    /**
     * Create fasta files needed for executing compare.py (create fasta file
     * containing dot-paren secondary structure for each sequence)
     *
     * @param array $sequences list of sequences for which the fastas will be created
     */
    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');
                // append 3 lines - header, sequence and dot-paren secondary structure
                $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;
            }
        }
    }

    /**
     * Creates helper list for comparison script (list file is basically helper
     * that tells the comparison scripts which fasta files it should use)
     *
     * @param array $sequences list of sequences that should be added to the list 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");
        }
    }

    /**
     * Build command for execution - prepare all arguments needed for compary.py
     *
     * @param string $dir path to temporary directory
     * @return string command prepared for execution
     */
    private function buildCommand($dir) {
        $command = 'compare.py ' . $dir;
        $command = $command . ' -w ' . $this->winsize;
        $command = $command . ' -s stats.csv';
        $command = $command . ' 2>&1'; // add errors to standard output
        return $command;
    }

    /**
     * Execute comparison
     *
     * @param array $accessions list of accession number that should be compared
     * @param int $winsize window size
     * @return array sequences compared (complete records loaded from database for specified accession numbers)
     * @throws \BaseModule\Exceptions\ToolException
     */
    public function execute($accessions, $winsize = null) {
        // first make sure there are no old directories
        $this->deleteOldDirs();

        // custom specified window size
        if ($winsize !== null) {
            $this->winsize = $winsize;
        }

        // prepare temporary directory
        $dir = $this->createTmpDir();

        $sequences = $this->loadSequencesFromDb($accessions);

        // prepare fasta files for loaded sequences
        $this->createFastas($sequences);

        // create list of sequences being compared (needed for comparison script)
        $this->createList($sequences);

        // prepare command
        $command = $this->buildCommand($dir);
        $r = $code = null;
        // execute command
        exec($command, $r, $code);

        // check whether there were any errors during execution of the script
        if ($code !== 0) {
            throw new \BaseModule\Exceptions\ToolException('Error while processing conservancy comparison - possibly missing structure [accession: ' . implode(',', $accessions) . '].');
        }

        // succes - return loaded sequences
        return $sequences;
    }

    /**
     * Export generated results - save all svgs (colored), fastas and info file into a zip archive
     *
     * @param string $tmpPath path to directory containig the result of the comparison
     * @param array $svgs array containing generated svgs (as a text since it includes changes made on-the-fly by D3.js)
     * @param int $winsize window size
     * @return string path to generated archive
     */
    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();
        // create export dir
        if (!file_exists($exportPath)) {
            mkdir($exportPath);
        }
        // save all svgs & copy it's fasta file
        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');
        }
        // save text info
        $text = $this->prepareExportText($accessions, $winsize, $statFile);
        file_put_contents($exportPath . DIRECTORY_SEPARATOR . 'info.txt', $text);
        // create zip archive
        $zip = new \ZipArchive();
        $zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);

        $files = new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($exportPath), \RecursiveIteratorIterator::LEAVES_ONLY
        );
        foreach ($files as $file) {
            // Skip directories (they would be added automatically)
            if (!$file->isDir()) {
                // Get real and relative path for current file
                $filePath = $file->getRealPath();
                $relativePath = substr($filePath, strrpos($filePath, DIRECTORY_SEPARATOR) + 1);

                // Add current file to archive
                $zip->addFile($filePath, $relativePath);
            }
        }
        $zip->close();
        return $zipPath;
    }

    /**
     * Prepare text for info file contained in export
     *
     * @param array $accessions list of accession numbers
     * @param int $winsize window size
     * @param string $statFile path to stats file generated by Conservancy Comparator
     * @return string text of a info file for export
     */
    private function prepareExportText($accessions, $winsize, $statFile) {
        // load stats file information
        $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]);
            }
        }
        // load sequences
        $sequences = $this->loadSequencesFromDb($accessions);

        // prepare text
        $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;
    }

    /**
     * Delete all conservancycomparator temporary directories older than 24 hours
     */
    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);
            }
        }
    }

    /**
     * Get path to temporary directory
     *
     * @return string path to temporary directory
     */
    public function getTmpDir() {
        return $this->tmpDir;
    }

    /**
     * Get window size
     *
     * @return int window size used for comparison tagger
     */
    public function getWinSize() {
        return $this->winsize;
    }

}
API documentation generated by ApiGen