File: hapstat_view.php

package info (click to toggle)
stacks 2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,108 kB
  • sloc: cpp: 36,453; php: 4,059; sh: 2,122; perl: 1,163; python: 497; sql: 389; makefile: 148
file content (126 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download | duplicates (3)
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
<?php
//
// Copyright 2015-2016, Julian Catchen <jcatchen@illinois.edu>
//
// This file is part of Stacks.
//
// Stacks 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.
//
// Stacks 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 Stacks.  If not, see <http://www.gnu.org/licenses/>.
//
require_once("header.php");

$database   = isset($_GET['db'])       ? $_GET['db']       : "";
$tag_id     = isset($_GET['tag_id'])   ? $_GET['tag_id']   : 0;
$batch_id   = isset($_GET['batch_id']) ? $_GET['batch_id'] : 0;
$batch_type = isset($_GET['type'])     ? $_GET['type']     : "map";

// Connect to the database
$db = db_connect($database);

// Save these variables for automatic URL formation later on.
$display = array();
$display['db']       = $database;
$display['tag_id']   = $tag_id;
$display['batch_id'] = $batch_id;

//
// Prepare some SQL queries
//
$query = 
    "SELECT pop_id, pop_name FROM populations " . 
    "WHERE batch_id=?";
if (!($db['pop_sth'] = $db['dbh']->prepare($query)))
    write_db_error($db['dbh'], __FILE__, __LINE__);

$query = 
   "SELECT pop_id, bp, n, hapcnt, gene_div, hap_div FROM hapstats " . 
   "WHERE batch_id=? AND tag_id=?";
if (!($db['stats_sth'] = $db['dbh']->prepare($query)))
    write_db_error($db['dbh'], __FILE__, __LINE__);

//
// Fetch population names if available.
//
$pop_names = array();
if ($batch_type == "population") {
    if (!$db['pop_sth']->bind_param("i", $batch_id))
        write_db_error($db['pop_sth'], __FILE__, __LINE__);
    if (!$db['pop_sth']->execute())
        write_db_error($db['pop_sth'], __FILE__, __LINE__);
    $res = $db['pop_sth']->get_result();

    while ($row = $res->fetch_assoc())
        $pop_names[$row['pop_id']] = $row['pop_name'];
}

if (!$db['stats_sth']->bind_param("ii", $batch_id, $tag_id))
    write_db_error($db['stats_sth'], __FILE__, __LINE__);
if (!$db['stats_sth']->execute())
    write_db_error($db['stats_sth'], __FILE__, __LINE__);
$res = $db['stats_sth']->get_result();

$stats = array();

while ($row = $res->fetch_assoc()) {
    $a = array('bp'       => $row['bp'],
               'n'        => $row['n'],
	           'hapcnt'   => $row['hapcnt'],
	           'gene_div' => $row['gene_div'],
	           'hap_div'  => $row['hap_div'],
	           'pop_id'   => $row['pop_id']);

    $stats[$row['pop_id']] = $a;
}

ksort($stats);

$json_str = 
  "{" .
  "\"path\": \"$root_path\"," .
  "\"batch_id\": \"$batch_id\"," .
  "\"db\": \"$database\"," .
  "\"id\": \"$tag_id\"," .
  "\"type\": \"$batch_type\",";

$json_str .= "\"hapstats\": [";

foreach ($stats as $pop_id => $stat)
    if (!isset($pop_names[$pop_id])) 
        $pop_names[$pop_id] = $pop_id;

$rows = 0;
foreach ($stats as $pop_id => $s) {

    $gdiv = $s['gene_div'] > 0 ? sprintf("%.3f", $s['gene_div']) : $s['gene_div'];
    $hdiv = $s['hap_div']  > 0 ? sprintf("%.3f", $s['hap_div'])  : $s['hap_div'];

    $json_str .=
      "{" .
      "\"pop_id\": \"" . $pop_names[$pop_id] . "\"," .
      "\"bp\": \"$s[bp]\"," .
      "\"n\": \"$s[n]\"," .
      "\"hapcnt\": \"$s[hapcnt]\"," .
      "\"genediv\": \"$gdiv\"," .
      "\"hapdiv\": \"$hdiv\"" .
      "},";
    $rows++;
}

if ($rows > 0) 
    $json_str  = substr($json_str, 0, -1);
$json_str .= 
  "]}";

echo $json_str;

?>