object_diseases.php 20.7 KB
Newer Older
1 2 3 4 5 6
<?php
/*******************************************************************************
 *
 * LEIDEN OPEN VARIATION DATABASE (LOVD)
 *
 * Created     : 2010-07-28
7
 * Modified    : 2019-09-05
8
 * For LOVD    : 3.0-22
9
 *
10
 * Copyright   : 2004-2019 Leiden University Medical Center; http://www.LUMC.nl/
11 12 13
 * Programmers : Ivo F.A.C. Fokkema <I.F.A.C.Fokkema@LUMC.nl>
 *               Ivar C. Lugtenburg <I.C.Lugtenburg@LUMC.nl>
 *               Daan Asscheman <D.Asscheman@LUMC.nl>
14
 *               M. Kroon <m.kroon@lumc.nl>
15
 *               Juny Kesumadewi <juny.kesumadewi@unimelb.edu.au>
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
 *
 *
 * This file is part of LOVD.
 *
 * LOVD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LOVD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LOVD.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************/

// Don't allow direct access.
if (!defined('ROOT_PATH')) {
    exit;
}
// Require parent class definition.
require_once ROOT_PATH . 'class/objects.php';

Ivo Fokkema's avatar
Ivo Fokkema committed
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
// List of tissues selectable for being affected by a disease.
$_SETT['disease_tissues'] = array(
    'optgroup_brain' => 'Brain',
    'brain' => 'brain',
    'lateral ventricle' => 'lateral ventricle',
    'cerebral cortex' => 'cerebral cortex',
    'cerebellum' => 'cerebellum',
    'hippocampus' => 'hippocampus',

    'optgroup_face' => 'Face',
    'face' => 'face',
    'eyes' => 'eyes',
    'ears' => 'ears',
    'nose' => 'nose',
    'mouth' => 'mouth',

    'optgroup_head_internal' => 'Head (internal)',
    'nasopharynx' => 'nasopharynx',
    'oral mucosa' => 'oral mucosa',
    'salivary gland' => 'salivary gland',
    'tonsil' => 'tonsil',
    'thyroid gland' => 'thyroid gland',
    'parathyroid gland' => 'parathyroid gland',

    'optgroup_limbs' => 'Limbs',
    'shoulders' => 'shoulders',
    'arms' => 'arms',
    'elbows' => 'elbows',
    'wrist' => 'wrist',
    'hands' => 'hands',
    'fingers' => 'fingers',
    'legs' => 'legs',
    'knees' => 'knees',
    'ankles' => 'ankles',
    'feet' => 'feet',
    'toes' => 'toes',

    'optgroup_trunk_internal' => 'Trunk (internal)',
    'spine' => 'spine',
    'hips' => 'hips',
    'bronchus' => 'bronchus',
    'lung' => 'lung',
    'esophagus' => 'esophagus',
    'heart muscle' => 'heart muscle',
    'lymph node' => 'lymph node',
    'stomach' => 'stomach',
    'liver' => 'liver',
    'adrenal gland' => 'adrenal gland',
    'spleen' => 'spleen',
    'kidney' => 'kidney',
    'gallbladder' => 'gallbladder',
    'pancreas' => 'pancreas',
    'duodenum' => 'duodenum',
    'small intestine' => 'small intestine',
    'colon' => 'colon',
    'appendix' => 'appendix',
    'urinary bladder' => 'urinary bladder',
    'rectum' => 'rectum',

    'optgroup_feminine' => 'Feminine tissues',
    'breast' => 'breast',
    'placenta' => 'placenta',
    'fallopian tube' => 'fallopian tube',
    'ovary' => 'ovary',
    'uterus' => 'uterus',
    'cervix, uterine' => 'cervix, uterine',
    'vagina' => 'vagina',

    'optgroup_masculine' => 'Masculine tissues',
    'seminal vesicle' => 'seminal vesicle',
    'prostate' => 'prostate',
    'epididymis' => 'epididymis',
    'testis' => 'testis',

    'optgroup_other' => 'Other',
    'skeletal muscle' => 'skeletal muscle',
    'smooth muscle' => 'smooth muscle',
    'hair' => 'hair',
    'skin' => 'skin',
    'bone marrow' => 'bone marrow',
    'bones (skeleton)' => 'bones (skeleton)',
    'soft tissue' => 'soft tissue',
);
125

126 127 128 129




130
class LOVD_Disease extends LOVD_Object {
131 132 133 134 135 136 137
    // This class extends the basic Object class and it handles the Link object.
    var $sObject = 'Disease';





138
    function __construct ()
139 140
    {
        // Default constructor.
141
        global $_AUTH, $_SETT;
142

143 144 145 146
        // SQL code for preparing load entry query.
        // Increase DB limits to allow concatenation of large number of gene IDs.
        $this->sSQLPreLoadEntry = 'SET group_concat_max_len = 200000';

147
        // SQL code for loading an entry for an edit form.
148
        $this->sSQLLoadEntry = 'SELECT d.*, d.inheritance as _inheritance, ' .
149
                               'd.tissues AS _tissues, ' .
150
                               'GROUP_CONCAT(g2d.geneid ORDER BY g2d.geneid SEPARATOR ";") AS _genes ' .
151 152 153 154
                               'FROM ' . TABLE_DISEASES . ' AS d ' .
                               'LEFT OUTER JOIN ' . TABLE_GEN2DIS . ' AS g2d ON (d.id = g2d.diseaseid) ' .
                               'WHERE d.id = ? ' .
                               'GROUP BY d.id';
155

156 157 158 159
        // SQL code for preparing view entry query.
        // Increase DB limits to allow concatenation of large number of gene IDs.
        $this->sSQLPreViewEntry = 'SET group_concat_max_len = 200000';

160
        // SQL code for viewing an entry.
161
        $this->aSQLViewEntry['SELECT']   = 'd.*, ' .
162 163
                                           '(SELECT COUNT(*) FROM ' . TABLE_INDIVIDUALS . ' AS i INNER JOIN ' . TABLE_IND2DIS . ' AS i2d ON (i.id = i2d.individualid) WHERE i2d.diseaseid = d.id' . ($_AUTH['level'] >= LEVEL_COLLABORATOR? '' : ' AND i.statusid >= ' . STATUS_MARKED) . ') AS individuals, ' .
                                           '(SELECT COUNT(*) FROM ' . TABLE_PHENOTYPES . ' AS p WHERE p.diseaseid = d.id' . ($_AUTH['level'] >= LEVEL_COLLABORATOR? '' : ' AND p.statusid >= ' . STATUS_MARKED) . ') AS phenotypes, ' .
164
                                           'GROUP_CONCAT(DISTINCT g2d.geneid ORDER BY g2d.geneid SEPARATOR ";") AS _genes, ' .
165 166 167 168 169 170 171
                                           'uc.name AS created_by_, ' .
                                           'ue.name AS edited_by_';
        $this->aSQLViewEntry['FROM']     = TABLE_DISEASES . ' AS d ' .
                                           'LEFT OUTER JOIN ' . TABLE_GEN2DIS . ' AS g2d ON (d.id = g2d.diseaseid) ' .
                                           'LEFT OUTER JOIN ' . TABLE_USERS . ' AS uc ON (d.created_by = uc.id) ' .
                                           'LEFT OUTER JOIN ' . TABLE_USERS . ' AS ue ON (d.edited_by = ue.id)';
        $this->aSQLViewEntry['GROUP_BY'] = 'd.id';
172 173

        // SQL code for viewing a list of entries.
174
        $this->aSQLViewList['SELECT']   = 'd.*, d.id AS diseaseid, ' .
175 176
                                          '(SELECT COUNT(DISTINCT i.id) FROM ' . TABLE_IND2DIS . ' AS i2d LEFT OUTER JOIN ' . TABLE_INDIVIDUALS . ' AS i ON (i2d.individualid = i.id' . ($_AUTH['level'] >= LEVEL_COLLABORATOR? '' : ' AND i.statusid >= ' . STATUS_MARKED) . ') WHERE i2d.diseaseid = d.id) AS individuals, ' .
                                          '(SELECT COUNT(*) FROM ' . TABLE_PHENOTYPES . ' AS p WHERE p.diseaseid = d.id' . ($_AUTH['level'] >= LEVEL_COLLABORATOR? '' : ' AND p.statusid >= ' . STATUS_MARKED) . ') AS phenotypes, ' .
177
                                          'COUNT(g2d.geneid) AS gene_count, ' .
178
                                          'GROUP_CONCAT(DISTINCT g2d.geneid ORDER BY g2d.geneid SEPARATOR ";") AS _genes';
179
        $this->aSQLViewList['FROM']     = TABLE_DISEASES . ' AS d ' .
180
                                          'LEFT OUTER JOIN ' . TABLE_GEN2DIS . ' AS g2d ON (d.id = g2d.diseaseid)';
181
        $this->aSQLViewList['WHERE']    = 'd.id > 0';
182 183
        $this->aSQLViewList['GROUP_BY'] = 'd.id';

184 185 186 187 188 189
        $sInheritanceLegend = '<TABLE>';
        foreach ($_SETT['diseases_inheritance'] as $sKey => $sDescription) {
            $sInheritanceLegend .= '<TR><TD>' . $sKey . '</TD><TD>: ' . $sDescription . '</TD></TR>';
        }
        $sInheritanceLegend .= '</TABLE>';

190 191 192 193 194 195
        // List of columns and (default?) order for viewing an entry.
        $this->aColumnsViewEntry =
                 array(
                        'symbol' => 'Official abbreviation',
                        'name' => 'Name',
                        'id_omim' => 'OMIM ID',
196
                        'link_HPO_' => 'Human Phenotype Ontology Project (HPO)',
197
                        'inheritance' => 'Inheritance',
198 199
                        'individuals' => 'Individuals reported having this disease',
                        'phenotypes_' => 'Phenotype entries for this disease',
200
                        'genes_' => 'Associated with',
201
                        'tissues' => 'Associated tissues',
202 203
                        'features' => 'Disease features',
                        'remarks' => 'Remarks',
204 205 206 207
                        'created_by_' => array('Created by', LEVEL_COLLABORATOR),
                        'created_date_' => array('Date created', LEVEL_COLLABORATOR),
                        'edited_by_' => array('Last edited by', LEVEL_COLLABORATOR),
                        'edited_date_' => array('Date last edited', LEVEL_COLLABORATOR),
208 209 210 211 212
                      );

        // List of columns and (default?) order for viewing a list of entries.
        $this->aColumnsViewList =
                 array(
213
                        'diseaseid' => array(
214
                                    'view' => array('ID', 45, 'style="text-align : right;"'),
215
                                    'db'   => array('d.id', 'ASC', true)),
216 217 218 219 220 221 222
                        'symbol' => array(
                                    'view' => array('Abbreviation', 110),
                                    'db'   => array('d.symbol', 'ASC', true)),
                        'name' => array(
                                    'view' => array('Name', 300),
                                    'db'   => array('d.name', 'ASC', true)),
                        'id_omim' => array(
223
                                    'view' => array('OMIM ID', 75, 'style="text-align : right;"'),
224
                                    'db'   => array('d.id_omim', 'ASC', true)),
225 226 227 228
                        'inheritance' => array(
                                    'view' => array('Inheritance', 75),
                                    'db'   => array('inheritance', 'ASC', true),
                                    'legend' => array('Abbreviations:' . strip_tags(str_replace('<TR>', "\n", preg_replace('/\s+/', ' ', $sInheritanceLegend))),
229
                                        'Values based on OMIM\'s and HPO\'s values for inheritance.<BR>' . str_replace(array("\r", "\n"), '', $sInheritanceLegend),
230
                                    )),
Ivo Fokkema's avatar
Ivo Fokkema committed
231
                        'individuals' => array(
232
                                    'view' => array('Individuals', 80, 'style="text-align : right;"'),
233
                                    'db'   => array('individuals', 'DESC', 'INT_UNSIGNED')),
234 235
                        'phenotypes' => array(
                                    'view' => array('Phenotypes', 80, 'style="text-align : right;"'),
236
                                    'db'   => array('phenotypes', 'DESC', 'INT_UNSIGNED')),
237 238
                        'genes_' => array(
                                    'view' => array('Associated with genes', 200),
239
                                    'db'   => array('_genes', false, 'TEXT')),
240 241
                        'tissues' => array(
                                    'view' => array('Associated tissues', 160),
242
                                    'db'   => array('d.tissues', false, true)),
243 244
                        'features' => array(
                                    'view' => array('Disease features', 200),
245
                                    'db'   => array('d.features', false, true)),
246 247 248
                      );
        $this->sSortDefault = 'symbol';

249 250 251
        // Because the disease information is publicly available, remove some columns for the public.
        $this->unsetColsByAuthLevel();

252
        parent::__construct();
253 254 255 256 257 258
    }





259
    function checkFields ($aData, $zData = false, $aOptions = array())
260 261
    {
        // Checks fields before submission of data.
262
        global $_AUTH, $_DB;
263

264 265 266
        $bImport = (lovd_getProjectFile() == '/import.php');
        $bCreate = ((ACTION && ACTION == 'create') || ($bImport && !$zData));

267 268 269 270 271 272
        // Mandatory fields.
        $this->aCheckMandatory =
                 array(
                        'symbol',
                        'name',
                      );
273
        $aData = parent::checkFields($aData, $zData, $aOptions);
274

275 276 277
        if (!empty($aData['id_omim']) && !preg_match('/^[1-9]\d{5}$/', $aData['id_omim'])) {
            lovd_errorAdd('id_omim', 'The OMIM ID has to be six digits long and cannot start with a \'0\'.');
        }
278 279 280 281
        // Two diseases with the same OMIM ID are not allowed.
        if (!empty($aData['id_omim']) && ($bCreate || $aData['id_omim'] != $zData['id_omim'])) {
            $bExists = $_DB->query('SELECT id FROM ' . TABLE_DISEASES . ' WHERE id_omim = ?', array($aData['id_omim']))->fetchColumn();
            if ($bExists) {
282
                // IMPORTANT: when you change this message, also change the array_search argument in import.php in the Disease section.
283 284 285 286 287 288
                lovd_errorAdd('id_omim', 'Another disease already exists with this OMIM ID!');
            }
        }
        // We don't like two diseases with the exact same name, either.
        if (!empty($aData['name']) && ($bCreate || $aData['name'] != $zData['name'])) {
            $bExists = $_DB->query('SELECT id FROM ' . TABLE_DISEASES . ' WHERE name = ?', array($aData['name']))->fetchColumn();
289
            if ($bExists && ($bCreate || $zData['id'] != $bExists)) {
290
                // IMPORTANT: when you change this message, also change the array_search argument in import.php in the Disease section.
291
                lovd_errorAdd('name', 'Another disease already exists with the same name!');
292
            }
293
        }
294

295
        if (!$bImport && $_AUTH['level'] < LEVEL_MANAGER && empty($aData['genes'])) {
296 297 298
            lovd_errorAdd('genes', 'You should at least select one of the genes you are curator of.');
        }

299
        $_POST['genes'] = array();
300
        if (is_array($aData['genes'])) {
301
            foreach ($aData['genes'] as $sGene) {
302
                if (!lovd_isAuthorized('gene', $sGene, false) && $bCreate) {
303 304 305 306 307 308
                    lovd_errorAdd('genes', 'You are not authorized to add this disease to gene ' . htmlspecialchars($sGene) . '.');
                } else {
                    $_POST['genes'][] = $sGene;
                }
            }
        }
309
        if (!$bCreate) {
310
            if (is_array($aData['genes']) && isset($zData['genes']) && is_array($zData['genes'])) {
311
                foreach ($zData['genes'] as $sGene) {
312
                    if ($sGene && !in_array($sGene, $aData['genes']) && !lovd_isAuthorized('gene', $sGene, false)) {
313 314 315
                        lovd_errorAdd('genes', 'You are not authorized to remove this disease from gene ' . htmlspecialchars($sGene) . '.');
                        $_POST['genes'][] = $sGene;
                    }
316 317 318
                }
            }
        }
319 320 321 322 323 324 325 326 327 328 329 330

        // XSS attack prevention. Deny input of HTML.
        lovd_checkXSS();
    }





    function getForm ()
    {
        // Build the form.
331
        global $_AUTH, $_DB, $_SETT;
Ivo Fokkema's avatar
Ivo Fokkema committed
332 333 334 335 336 337

        // If we've built the form before, simply return it. Especially imports will repeatedly call checkFields(), which calls getForm().
        if (!empty($this->aFormData)) {
            return parent::getForm();
        }

338
        // Get list of genes, to connect disease to gene.
339 340 341 342
        if ($_AUTH['level'] == LEVEL_CURATOR) {
            $aGenes = $_AUTH['curates'];
            if (ACTION == 'edit') {
                global $zData;
343 344
                // 2016-09-08; 3.0-17; Don't forget to run array_values() since a missing key results in a query error... :|
                $aGenes = array_values(array_unique(array_merge($aGenes, $zData['genes'])));
345 346 347 348 349
            }
            $aGenesForm = $_DB->query('SELECT id, name FROM ' . TABLE_GENES . ' WHERE id IN (?' . str_repeat(', ?', count($aGenes) - 1) . ') ORDER BY id', $aGenes)->fetchAllCombine();
        } else {
            $aGenesForm = $_DB->query('SELECT id, name FROM ' . TABLE_GENES . ' ORDER BY id')->fetchAllCombine();
        }
350 351 352 353 354
        $nData = count($aGenesForm);
        foreach ($aGenesForm as $sID => $sGene) {
            $aGenesForm[$sID] = $sID . ' (' . lovd_shortenString($sGene, 50) . ')';
        }
        if (!$nData) {
355
            $aGenesForm = array('' => 'No gene entries available');
356
        }
357
        $nFieldSize = (count($aGenesForm) < 15? count($aGenesForm) : 15);
358 359 360 361

        // Array which will make up the form table.
        $this->aFormData =
                 array(
362
                        array('POST', '', '', '', '35%', '14', '65%'),
363
                        array('', '', 'print', '<B>Disease information</B>'),
364
                        'hr',
365 366
                        array('Disease abbreviation', '', 'text', 'symbol', 15),
                        array('Disease name', '', 'text', 'name', 40),
367
                        array('OMIM ID (optional)', '', 'text', 'id_omim', 10),
368
                        array('Inheritance', '', 'select', 'inheritance', count($_SETT['diseases_inheritance']), $_SETT['diseases_inheritance'], false, true, false),
369
                        array('Associated tissues (optional)', '', 'select', 'tissues', 10, $_SETT['disease_tissues'],
370
                              false, true, false),
371 372
                        array('Disease features (optional)', '', 'textarea', 'features', 50, 5),
                        array('Remarks (optional)', '', 'textarea', 'remarks', 50, 5),
373
                        'hr',
374
                        'skip',
375
                        array('', '', 'print', '<B>Relation to genes (optional)</B>'),
376
                        'hr',
377
            'aGenes' => array('This disease has been linked to these genes', '', 'select', 'genes', $nFieldSize, $aGenesForm, false, true, false),
378 379
                        'hr',
                        'skip',
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
                  );

        return parent::getForm();
    }





    function prepareData ($zData = '', $sView = 'list')
    {
        // Prepares the data by "enriching" the variable received with links, pictures, etc.

        if (!in_array($sView, array('list', 'entry'))) {
            $sView = 'list';
        }

        // Makes sure it's an array and htmlspecialchars() all the values.
        $zData = parent::prepareData($zData, $sView);

        if ($sView == 'list') {
            $zData['row_id'] = $zData['id'];
            $zData['symbol'] = '<A href="' . $zData['row_link'] . '" class="hide">' . $zData['symbol'] . '</A>';
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
            $zData['genes_'] = '';
            $i = 0;
            if (count($zData['genes']) > 22) {
                // Don't show all genes.
                foreach ($zData['genes'] as $key => $sID) {
                    $zData['genes_'] .= (!$key? '' : ', ') . $sID;
                    $i++;
                    if ($i >= 20) {
                        break;
                    }
                }
                $zData['genes_'] .= ', ' . ($zData['gene_count'] - $i) . ' more';
            } else {
                $zData['genes_'] = implode(', ', $zData['genes']);
            }
418 419
        } else {
            if (!empty($zData['id_omim'])) {
420 421
                $zData['link_HPO_'] = '<A href="' . lovd_getExternalSource('hpo_disease',
                        $zData['id_omim'], true) . '" target="_blank">HPO</A>';
422
                $zData['id_omim'] = '<A href="' . lovd_getExternalSource('omim', $zData['id_omim'], true) . '" target="_blank">' . $zData['id_omim'] . '</A>';
423 424 425
            } else {
                // Cannot link to HPO without OMIM ID, hide this row.
                unset($this->aColumnsViewEntry['link_HPO_']);
426
            }
427 428 429 430
            $zData['phenotypes_'] = $zData['phenotypes'];
            if ($zData['phenotypes']) {
                $zData['phenotypes_'] = '<A href="phenotypes/disease/' . $zData['id'] . '">' . $zData['phenotypes'] . '</A>';
            }
431 432
            // Provide links to gene symbols this disease is associated with.
            $this->aColumnsViewEntry['genes_'] .= ' ' . count($zData['genes']) . ' gene' . (count($zData['genes']) == 1? '' : 's');
433
            $zData['genes_'] = '';
434 435 436 437 438 439 440
            $zData['genes_short_'] = '';
            $i = 0;
            foreach ($zData['genes'] as $key => $sID) {
                $zData['genes_'] .= (!$key? '' : ', ') . '<A href="genes/' . $sID . '">' . $sID . '</A>';
                if ($i < 20) {
                    $zData['genes_short_'] .= (!$key? '' : ', ') . '<A href="genes/' . $sID . '">' . $sID . '</A>';
                    $i++;
441
                }
442
            }
443 444 445 446
            if (count($zData['genes']) > 22) {
                // Replace long gene list by shorter one, allowing expand.
                $zData['genes_'] = '<SPAN>' . $zData['genes_short_'] . ', <A href="#" onclick="$(this).parent().hide(); $(this).parent().next().show(); return false;">' . (count($zData['genes']) - $i) . ' more...</A></SPAN><SPAN style="display : none;">' . $zData['genes_'] . '</SPAN>';
            }
447 448 449 450 451
        }

        return $zData;
    }
}
452
?>