-
Notifications
You must be signed in to change notification settings - Fork 0
/
helium_exporter.module
292 lines (244 loc) · 8.78 KB
/
helium_exporter.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
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
<?php
/**
* @file
* The main functionality for this module.
*/
// Include general API and helper functions.
module_load_include('inc', 'helium_exporter', 'api/helium_exporter.api');
/**
* Implements hook_menu().
*/
function helium_exporter_menu() {
// Download Data.
$items['helium-exporter'] = array(
'title' => t(''),
'page callback' => 'drupal_get_form',
'page arguments' => array('helium_exporter_form'),
'access arguments' => array('access helium_exporter'),
'file' => 'includes/helium_exporter.form.inc',
'type' => MENU_NORMAL_ITEM,
);
// Configure Helium Exporter.
$items['admin/tripal/extension/helium_exporter/configure'] = array(
'title' => t('Helium Exporter Configuration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('helium_exporter_settings_form'),
'access arguments' => array('administer tripal'),
'file' => 'includes/helium_exporter_settings.form.inc',
'type' => MENU_NORMAL_ITEM,
);
// Tripal Download Implementation.
$items['helium/exporter/download'] = array(
'title' => 'Helium Exporter Download',
'page callback' => 'trpdownload_download_page',
'page arguments' => array('helium_exporter_download'),
'access arguments' => array('access helium_exporter'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function helium_exporter_permission() {
return array(
'access helium_exporter' => array(
'title' => t('Access Helium Export Page'),
)
);
}
/**
* Implements hook_theme().
*/
function helium_exporter_theme($existing, $type, $theme, $path) {
$items['helium_exporter_form'] = array(
'render element' => 'form',
'template' => 'helium_exporter_page',
'path' => $path . '/theme',
);
return $items;
}
/**
* Implement hook_preprocess_HOOK().
*/
function helium_exporter_preprocess_helium_exporter_form(&$variables, $hook) {
$path = drupal_get_path('module', 'helium_exporter');
// Attach script and style.
drupal_add_css($path . '/theme/css/style-helium-exporter-page.css');
drupal_add_js($path . '/theme/script/script-helium-exporter-page.js', array('type' => 'file', 'scope' => 'footer'));
// Attach library autocomplete:
drupal_add_library('system', 'ui.autocomplete');
// Define template variables.
$variables['path_module'] = $GLOBALS['base_url'] . '/' . $path;
$config = variable_get('helium_exporter_pedigree_relations');
$is_configured = ($config == '0') ? FALSE : TRUE;
$variables['is_configured'] = $is_configured;
// Help url configuration.
$help_url = variable_get('helium_exporter_help_url');
$variables['help_url'] = trim($help_url);
$variables['helium_resource'] = array(
'download' => 'https://github.com/cardinalb/helium-docs/wiki/Download-Helium',
'information' => 'https://github.com/cardinalb/helium-docs/wiki',
'help' => 'https://github.com/cardinalb/helium-docs/wiki/Data-Formats',
'web-base' => 'https://helium.hutton.ac.uk',
);
}
// TRIPAL DOWNLOAD API IMPLEMENTATION
/**
* Implements hook_register_tripaldownload_type().
*/
function helium_exporter_register_trpdownload_type() {
$types = array();
// The key is the machine name of my download type.
$types['helium_exporter_download'] = array(
// A human readable name to show in an administrative interface one day.
'type_name' => 'Helium Exporter Data Download',
// A human readable description of the format.
'format' => '',
// An array of functions that the API will use to customize your experience.
'functions' => array(
// The function that tripal jobs will call to generate the file.
'generate_file' => 'helium_exporter_trpdownload_generate_file',
// OPTIONAL: provide a summary to the user on the download page.
'summarize' => 'helium_exporter_trpdownload_summarize_download',
// OPTIONAL: determine your own filename.
'get_filename' => 'helium_exporter_trpdownload_get_filename',
// OPTIONAL: Change the file suffix (defaults to .txt)
'get_file_suffix' => 'helium_exporter_trpdownload_get_suffix',
// OPTIONAL: determine the human-readable format based on a function.
'get_format' => 'helium_exporter_trpdownload_get_readable_format',
),
);
return $types;
}
/**
* Generate a readable and unique filename for the file to be generated.
*/
function helium_exporter_trpdownload_get_filename($vars) {
// Filename.
$filename = 'He_pedigree_' . date('YMd') .'_'. time();
return $filename;
}
/**
* determine the human-readable format based on a function.
*/
function helium_exporter_trpdownload_get_readable_format($vars) {
$format = 'Helium (.helium)';
return $format;
}
/**
* Determine the file suffix for the file to be generated.
*/
function helium_exporter_trpdownload_get_suffix($vars) {
return 'helium';
}
/**
* Create summary.
*/
function helium_exporter_trpdownload_summarize_download($vars) {
$code = '';
foreach($vars['q'] as $j => $m) {
if ($j == 'code') {
$code = $m;
break;
}
}
$code = base64_decode($code);
parse_str($code, $values);
$path = file_directory_temp() . '/';
$He_file = $path . $values['he'];
$querystring = file_get_contents($He_file);
unset($values);
parse_str($querystring, $values);
$path = variable_get('trpdownload_relpath');
$file_link = l($values['f'], file_create_url($path) . '/' . $values['f'],
array('attributes' => array('target' => '_blank', 'download' => $values['f']))
);
$file_icon = theme_image(array(
'path' => drupal_get_path('module','trpdownload_api').'/theme/icons/file_generic.128.png',
'alt' => 'download environment data file',
'attributes' => array()
));
$output = '<div style="position: absolute; left: 0; width: 100%; margin-top: 90px; padding: 10px;">
<div style="margin: 0 20px 0 0;">
<div class="download-pane" style="margin: 0 20px;">'
. $file_icon .
'<div class="inner-pane file">
<h2>File:</h2>
<div class="file-link">' . $file_link . '</div>
<div class="file-format">Format: Helium (.helium)</div>
</div>
</div>
</div>
</div>';
// Link to download a different set.
$exporter = l('Go back Helium Exporter', '/helium-exporter');
return 'Download Pedigree and Categorical Data <br />' . $exporter . $output;
}
/**
* Function callback: generate .helum file.
*/
function helium_exporter_trpdownload_generate_file($vars, $job_id = NULL) {
// Use this character to separate entries in a string.
$delimiter = '+';
$code = '';
foreach($vars['q'] as $j => $m) {
if ($j == 'code') {
$code = $m;
break;
}
}
// User selected values parsed from the querystring.
$code = base64_decode($code);
parse_str($code, $values);
$path = file_directory_temp() . '/';
$He_file = $path . $values['he'];
$querystring = file_get_contents($He_file);
unset($values);
parse_str($querystring, $values);
$experiment = $values['e'];
// Get experiment assets to match traits and germplasm only
// belonging to the same experiment.
$experiment_assets = helium_exporter_get_experiment_assets($experiment);
// Germplasm and traits selected.
$germplasm = explode(' ', $values['g']);
$traits = explode(' ', $values['t']);
// Source directory for files.
$path_tripal_download = variable_get('trpdownload_fullpath');
// Lineage option.
$lineage = $values['l'];
// PEDIGREE:
$file_pedigree = $path_tripal_download . $vars['filename'];
$FILE = fopen($file_pedigree, 'w') or die ('Unable to read file to write pedigree to.');
// Write metadata about this file in the header followed
// by column headers.
$header = '# heliumInput = PEDIGREE : ' . $experiment_assets['experiment'];
fwrite($FILE, $header . "\n");
fputcsv($FILE, array('LineName', 'Parent', 'ParentType'), "\t");
// Datapoints that will map to every column headers.
// For each germplasm selected, fetch pedigree information.
$rows = [];
foreach($germplasm as $line) {
// Get selections of etc. and parents.
$relations = helium_exporter_get_parents($line, $lineage);
foreach($relations as $rel) {
fputcsv($FILE, $rel, "\t");
}
}
fclose($FILE);
// CATEGORICAL/PHENOTYPES:
$file_phenotypes = file_create_url($path_tripal_download . $values['f']);
$FILE = fopen($file_phenotypes, 'w') or die ('Unable to read file to write phenotypes to.');
// Write metadata about this file in the header followed
// by column headers.
$header = '# heliumInput = PHENOTYPE : ' . $experiment_assets['experiment'];
fwrite($FILE, $header . "\n");
$phenotypes = helium_exporter_get_phenotypes($experiment, $traits, $germplasm);
if ($phenotypes) {
fputcsv($FILE, $phenotypes['headers'], "\t");
foreach($phenotypes['rows'] as $data) {
fputcsv($FILE, $data, "\t");
}
}
fclose($FILE);
}