-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkp_searches.module
222 lines (196 loc) · 7.18 KB
/
kp_searches.module
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
<?php
/**
* @file
* General functions relating to this module.
* This file will not include the searches themselves.
*/
require_once 'includes/KPSEARCH.inc';
require_once 'includes/GeneticMarkerSearch.inc';
require_once 'includes/SequenceVariantSearch.inc';
require_once 'includes/GermplasmSearch.inc';
require_once 'includes/BreedingCrossSearch.inc';
require_once 'includes/CultivarSearch.inc';
require_once 'includes/RILSearch.inc';
require_once 'includes/GeneticMapSearch.inc';
require_once 'includes/ResearchStudySearch.inc';
require_once 'includes/GenomeAssemblySearch.inc';
/**
* Implement hook_chado_custom_search().
*
* This hook simply lists the machine name of the searches so that we can find
* the info hook. We've done this for performance reasons.
*/
function kp_searches_chado_custom_search() {
$searches = [];
// EXAMPLE: $searches['BreedingCrossSearch'] = 'Breeding Cross Search';
$searches['GeneticMarkerSearch'] = 'Genetic Marker Search';
$searches['SequenceVariantSearch'] = 'Sequence Variant Search';
$searches['GermplasmSearch'] = 'Germplasm Search';
$searches['BreedingCrossSearch'] = 'Breeding Cross Search';
$searches['CultivarSearch'] = 'Cultivar Search';
$searches['RILSearch'] = 'Recombinant Inbred Line (RIL) Search';
$searches['GeneticMapSearch'] = 'Genetic Map Search';
$searches['ResearchStudySearch'] = 'Research Study Search';
$searches['GenomeAssemblySearch'] = 'Genome Assembly Search';
return $searches;
}
/**
* Implements hook_menu().
*/
function kp_searches_menu() {
$items = [];
$items['search/germplasm/autocomplete.json/%/%/%'] = array(
'page callback' => 'autocomplete_kp_searches_germplasm_name',
'page arguments' => array(3,4,5),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Save options used in search form using variable_get/set().
*/
function kp_searches_cache_options() {
// Crops.
//-------------------------
// These are hard-coded since we do not want the full list of organisms.
$crop_options = [
'Cicer' => [
'title' => 'Chickpea',
'genus' => 'Cicer',
'crop-species' => 'arietinum',
'image' => '/sites/all/modules/custom/kp_frontpage/images/kphome/crops-chickpea.jpg',
],
'Lens' => [
'title' => 'Lentil',
'genus' => 'Lens',
'crop-species' => 'culinaris',
'image' => '/sites/all/modules/custom/kp_frontpage/images/kphome/crops-cultivated-lentil.jpg',
],
'Phaseolus' => [
'title' => 'Dry Bean',
'genus' => 'Phaseolus',
'crop-species' => 'vulgaris',
'image' => '/sites/all/modules/custom/kp_frontpage/images/kphome/crops-dry-bean.jpg',
],
'Vicia' => [
'title' => 'Faba Bean',
'genus' => 'Vicia',
'crop-species' => 'faba',
'image' => '/sites/all/modules/custom/kp_frontpage/images/kphome/crops-faba-bean.jpg',
],
'Pisum' => [
'title' => 'Field Pea',
'genus' => 'Pisum',
'crop-species' => 'sativum',
'image' => '/sites/all/modules/custom/kp_frontpage/images/kphome/crops-pea.jpg',
],
];
// Now add in the rest of the species.
$sql = 'SELECT genus, species FROM {organism} ORDER BY genus, species';
$resource = chado_query($sql);
foreach ($resource as $r) {
if (isset($crop_options[ $r->genus ])) {
$crop_options[ $r->genus ]['species'][ $r->species ] = $r->species;
}
}
variable_set('kp_searches_crop_options', serialize($crop_options));
// Germplasm Types.
//-------------------------
$options = db_query("SELECT cb.type_id, tb.label
FROM chado_bundle cb
LEFT JOIN tripal_bundle tb ON tb.id=cb.bundle_id
WHERE cb.data_table = 'stock'")->fetchAllKeyed(0,1);
variable_set('kp_searches_germplasm_type_options', serialize($options));
//-------------------------
// Genetic Marker Types.
// We want ot use ND Genotypes list but that is broken down into partition.
// Thus we are first going to get a list of all partitions and then merge
// each list. We are going this route b/c it's non-trivial to determine which
// type featureprops are for markers and which are for variants.
// -- Retrieve a list of all partitions.
$query = "WITH RECURSIVE t AS (
(SELECT partition
FROM {mview_ndg_germplasm_genotyped}
ORDER BY partition
LIMIT 1)
UNION ALL
SELECT
(SELECT partition
FROM {mview_ndg_germplasm_genotyped}
WHERE partition > t.partition
ORDER BY partition
LIMIT 1)
FROM t
WHERE t.partition IS NOT NULL
)
SELECT t.partition FROM t WHERE t.partition IS NOT NULL";
$partitions = chado_query($query)->fetchCol();
// -- Retieve marker type options for all partitions.
$marker_types = [];
$variant_types = [];
foreach ($partitions as $partition_name) {
// Markers:
$curr_partition_marker_types = variable_get(
'nd_genotypes_'.$partition_name.'_marker_types',
'a:0{}'
);
if (!empty($curr_partition_marker_types)) {
$marker_types = array_merge($marker_types, unserialize($curr_partition_marker_types));
}
// Variants:
$curr_partition_variant_types = variable_get(
'nd_genotypes_'.$partition_name.'_variant_types',
'a:0{}'
);
if (!empty($curr_partition_variant_types)) {
$variant_types = array_merge($variant_types, unserialize($curr_partition_variant_types));
}
}
// -- Finally, Save the options!
variable_set('kp_searches_marker_types', serialize($marker_types));
variable_set('kp_searches_variant_types', serialize($variant_types));
}
/**
* AUTOCOMPLETE: Germplasm Names respecting access of the user.
*/
function autocomplete_kp_searches_germplasm_name($genus, $type_id = 'all', $keyword = '') {
global $user;
$where = [
'(s.name~*:keyword OR s.uniquename~*:keyword)',
'o.genus = :genus'];
$args = [':genus' => $genus, ':keyword' => $keyword];
// Restrict by type (if supplied).
if (is_numeric($type_id)) {
$where[] = 's.type_id = :type_id';
$args[':type_id'] = $type_id;
}
// Retrict by access.
// Only use access control for non administrators. This is put in place to
// allow administrators to see content "on the cutting room floor"
// (i.e. not of any given type).
$administrator = array_search('administrator', $user->roles);
if (!$administrator) {
// See https://github.com/tripal/tripal/blob/7.x-3.x/tripal/includes/tripal.entity.inc#L495
$entity_types = db_query("SELECT cb.type_id, cb.bundle_id FROM chado_bundle cb
WHERE cb.data_table = 'stock'")->fetchAllKeyed(0,1);
// Only includes bundles the current user has access to.
$available_types = [];
foreach($entity_types as $type_id => $bundle_id) {
$bundle_name = 'bio_data_' . $bundle_id;
if (user_access('view ' . $bundle_name)) {
$available_types[] = $type_id;
}
}
$where[] = 's.type_id IN (:access_type_ids)';
$args[':access_type_ids'] = $available_types;
}
$results = chado_query('SELECT s.name
FROM {stock} s
LEFT JOIN {organism} o ON o.organism_id=s.organism_id
WHERE ' . implode(' AND ', $where)
. ' LIMIT 10',
$args)->fetchAllKeyed(0,0);
// Return the results.
drupal_json_output($results);
}