File: CatalogClass.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 (294 lines) | stat: -rw-r--r-- 10,423 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
//
// Copyright 2010-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("Locus.php");

class Catalog {
    var $db;       // Hash of database statement handles.
    var $display;  // An array of form variables necessary to write URLs.
    var $params;   // An array of parameters to fetch our results from the database.
    var $queries;  // An array of SQL queries.
    var $loci;     // Array of groups objects.
    var $num_loci; // Total number of groups, after filtering

    function Catalog($db, $batch_id, $display_params) {
	$this->db       = $db;
	$this->batch    = $batch_id;
	$this->display  = $display_params;
	$this->queries  = array();
	$this->params   = array();
	$this->loci     = array();
	$this->num_loci = 0;

	$this->prepare_queries();
    }

    function prepare_queries() {

        $query = 
            "SELECT allele FROM catalog_alleles " . 
            "WHERE batch_id=? AND tag_id=?";
        if (!($this->db['allele_sth'] = $this->db['dbh']->prepare($query)))
            write_db_error($this->db['dbh'], __FILE__, __LINE__);

        $query = 
            "SELECT col, rank_1, rank_2 FROM catalog_snps " . 
            "WHERE batch_id=? AND tag_id=? ORDER BY col";
        if (!($this->db['snp_sth'] = $this->db['dbh']->prepare($query)))
            write_db_error($this->db['dbh'], __FILE__, __LINE__);

        $query = 
            "SELECT samples.id, samples.sample_id, samples.type, file, tag_id, allele " . 
            "FROM matches " . 
            "JOIN samples ON (matches.sample_id=samples.id) " . 
            "WHERE matches.batch_id=? AND catalog_id=? ORDER BY samples.id";
        if (!($this->db['mat_sth'] = $this->db['dbh']->prepare($query)))
            write_db_error($this->db['dbh'], __FILE__, __LINE__);

        $query = 
            "SELECT COUNT(tag_id) as count FROM catalog_index " . 
            "WHERE batch_id=?";
        $query .= $this->apply_query_filters(); 
        $this->queries['tag_count'] = $query;

        $query = 
            "SELECT catalog_index.tag_id as tag_id, chr, bp, snps, alleles, parents, progeny, valid_progeny, " . 
            "seq, marker, max_pct, ratio, ests, pe_radtags, blast_hits, external_id " .
            "FROM catalog_index " .
            "JOIN catalog_tags ON (catalog_index.cat_id=catalog_tags.id) " . 
            "LEFT JOIN catalog_annotations ON " . 
            "(" . 
            "catalog_index.batch_id=catalog_annotations.batch_id AND " . 
            "catalog_index.tag_id=catalog_annotations.catalog_id" . 
            ") " .
            "WHERE catalog_index.batch_id=?";
        $query .= $this->apply_query_filters();

        $this->queries['tag'] = $query;
    }

    function prepare_filter_parameters() {
        $this->params[] = &$this->batch;
	$typestr = "i";

	$filters = $this->display['filter_type'];

	if (!isset($filters)) {
	    array_unshift($param, $typestr);
	    return;
	}

	foreach ($filters as $filter) {

            if ($filter == "snps") {
                $this->params[] = &$this->display['filter_snps_l'];
                $this->params[] = &$this->display['filter_snps_u'];
		$typestr .= "ii";

            } else if ($filter == "alle") {
                $this->params[] = &$this->display['filter_alle_l'];
                $this->params[] = &$this->display['filter_alle_u'];
		$typestr .= "ii";

            } else if ($filter == "pare") {
                $this->params[] = &$this->display['filter_pare_l'];
                $this->params[] = &$this->display['filter_pare_u'];
		$typestr .= "ii";

            } else if ($filter == "prog") {
                $this->params[] = &$this->display['filter_prog'];
		$typestr .= "i";

            } else if ($filter == "vprog") {
                $this->params[] = &$this->display['filter_vprog'];
		$typestr .= "i";

            } else if ($filter == "lnl") {
		$this->params[] = &$this->display['filter_lnl_l'];
		$this->params[] = &$this->display['filter_lnl_u'];
		$typestr .= "ii";

	    } else if ($filter == "cata") {
                $this->params[] = &$this->display['filter_cata'];
		$typestr .= "i";

            } else if ($filter == "gcnt") {
	        $this->params[] = &$this->display['filter_gcnt'];
		$typestr .= "i";

            } else if ($filter == "ref") {
		$this->params[] = &$this->display['filter_ref'];
		$typestr .= "i";

	    } else if ($filter == "loc") {
		$this->params[] = &$this->display['filter_chr'];
		$this->params[] = &$this->display['filter_sbp'];
		$this->params[] = &$this->display['filter_ebp'];
		$typestr .= "sii";
	
	    } else if ($filter == "mark") {
		$this->params[] = &$this->display['filter_mark'];
            }
	}

	array_unshift($this->params, $typestr);
    }

    function apply_query_filters() {
        $sql_filters =
            array("cata"  => "(catalog_index.tag_id = ?)", 
		  "alle"  => "(alleles >= ? AND alleles <= ?)", 
		  "snps"  => "(snps >= ? AND snps <= ?)",
		  "pare"  => "(parents >= ? AND parents <= ?)",
                  "prog"  => "(progeny >= ?)",
                  "vprog" => "(valid_progeny >= ?)",
		  "lnl"   => "(lnl >= ? AND lnl <= ?)",
                  "mark"  => "(marker LIKE ?)", 
                  "est"   => "(ests > 0)",
                  "pe"    => "(pe_radtags > 0)",
                  "blast" => "(blast_hits > 0)",
		  "gcnt"  => "(geno_cnt >= ?)",
		  "ref"   => "(catalog_index.type = ?)",
		  "loc"   => "(catalog_index.chr = ? && catalog_index.bp >= ? && catalog_index.bp <= ?)");

        $filters = $this->display['filter_type'];

        if (count($filters) > 0) {
            $query = " AND ";

            while (count($filters) > 0) {
                $filter = array_shift($filters);
                $query .= $sql_filters[$filter];
                $query .= count($filters) > 0 ? " AND " : "";
            }
        }

	return $query;
    }

    function &loci() {
	return $this->loci;
    }

    function &locus($id) {
	return $this->loci[$id];
    }

    function num_loci() {
	return $this->num_loci;
    }

    function determine_count() {

	if (!($this->db['tag_count_sth'] = $this->db['dbh']->prepare($this->queries['tag_count'])))
	    write_db_error($this->db['dbh'], __FILE__, __LINE__);

	$this->prepare_filter_parameters();

	array_unshift($this->params, $this->db['tag_count_sth']);
	call_user_func_array("mysqli_stmt_bind_param", $this->params);
	array_shift($this->params);
	if (!$this->db['tag_count_sth']->execute())
	    write_db_error($this->db['tag_count_sth'], __FILE__, __LINE__);
	$res = $this->db['tag_count_sth']->get_result();

	$row = $res->fetch_assoc();
	$this->num_loci = $row['count'];
    }

    function populate($start_group, $num_groups) {
	//
	// We only want to load genes between $start_gene and $end_gene. 
	//
	$this->queries['tag'] .= " LIMIT " . $start_group . ", " . $num_groups;
	
	if (!($this->db['tag_sth'] = $this->db['dbh']->prepare($this->queries['tag'])))
	    write_db_error($this->db['dbh'], __FILE__, __LINE__);

	$this->prepare_filter_parameters();

	//
	// Fetch the results and populate the array of groups.
	//
	array_unshift($this->params, $this->db['tag_sth']);
	call_user_func_array("mysqli_stmt_bind_param", $this->params);
	array_shift($this->params);
	if (!$this->db['tag_sth']->execute())
	    write_db_error($this->db['tag_sth'], __FILE__, __LINE__);
	$res = $this->db['tag_sth']->get_result();

	while ($row = $res->fetch_assoc()) {
            $locus = new Locus();
            $locus->id         = $row['tag_id'];
            $locus->annotation = $row['external_id'];
            $locus->chr        = $row['chr'];
            $locus->bp         = $row['bp'];
            $locus->marker     = $row['marker'];
            $locus->seq        = $row['seq'];

            $locus->num_alleles   = $row['alleles'];
            $locus->num_snps      = $row['snps'];
            $locus->num_parents   = $row['parents'];
            $locus->num_progeny   = $row['progeny'];
            $locus->valid_progeny = $row['valid_progeny'];
            $locus->num_ests      = $row['ests'];
            $locus->num_pe_tags   = $row['pe_radtags'];
            $locus->num_blast     = $row['blast_hits'];

            //
            // Fetch SNPs and Alleles
            //
	    if (!$this->db['snp_sth']->bind_param("ii", $this->batch, $locus->id))
		write_db_error($db['snp_sth'], __FILE__, __LINE__);
	    if (!$this->db['snp_sth']->execute())
		write_db_error($db['snp_sth'], __FILE__, __LINE__);
	    $snp_res = $this->db['snp_sth']->get_result();

            while ($snp_row = $snp_res->fetch_assoc()) {
                $locus->snps .= $snp_row['col'] . "," . $snp_row['rank_1'] . ">" . $snp_row['rank_2'] . ";";
            }
            $locus->snps = substr($locus->snps, 0, -1);

	    if (!$this->db['all_sth']->bind_param("ii", $this->batch, $locus->id))
		write_db_error($db['all_sth'], __FILE__, __LINE__);
	    if (!$this->db['all_sth']->execute())
		write_db_error($db['all_sth'], __FILE__, __LINE__);
	    $all_res = $this->db['all_sth']->get_result();

            while ($all_row = $all_res->fetch_assoc()) {
                $locus->alleles .= $all_row['allele'] . ";";
            }
            $locus->alleles = substr($locus->alleles, 0, -1);

            //
            // Add genotypes
            //
	    if (!$this->db['mat_sth']->bind_param("ii", $this->batch, $locus->id))
		write_db_error($db['mat_sth'], __FILE__, __LINE__);
	    if (!$this->db['mat_sth']->execute())
		write_db_error($db['mat_sth'], __FILE__, __LINE__);
	    $gen_res = $this->db['mat_sth']->get_result();

            while ($gen_row = $gen_res->fetch_assoc())
                $locus->add_genotype($gen_row['id'], $gen_row['file'], $gen_row['allele']);

	    $this->loci[$row['tag_id']] = $locus;
	}
    }
}