1: <?php
2:
3: function create_job($type, $database, $additional_data = array(), $paramters = array(), $queries = array()) {
4: try {
5:
6: $pdo = connect_queue_db();
7:
8:
9:
10: $parameters_prepared = array();
11: foreach ($paramters as $key => $value) {
12: $parameters_prepared[] = $key;
13: $parameters_prepared[] = $value;
14: }
15:
16: $parameter_qmarks = count($parameters_prepared) == 0 ? 'ARRAY[]' : implode(',', array_fill(0, count($parameters_prepared) / 2, 'ARRAY[?,?]'));
17:
18: if (count($queries) == 0)
19: return(array('status' => 'error', 'message' => 'No query sequence specified!'));
20:
21:
22:
23: $query_qmarks = implode(',', array_fill(0, count($queries), '?'));
24:
25:
26: $statement_create_job = $pdo->prepare('SELECT * FROM create_job(?, ?, ?, ARRAY[' . $parameter_qmarks . '], ARRAY[' . $query_qmarks . ']);');
27:
28: $statement_create_job->execute(array_merge(
29: array($type, $database, json_encode($additional_data)), $parameters_prepared, $queries
30: ));
31:
32: if ($statement_create_job->rowCount() == 0) {
33: return(array('status' => 'error', 'message' => 'Job could not be started! Please report this error including all parameters you used.'));
34: }
35:
36: return array('status' => 'success', 'job_id' => $statement_create_job->fetchColumn());
37: } catch (\PDOException $e) {
38:
39: return(array('status' => 'error', 'message' => 'Job could not be started: ' . $e->getMessage()));
40: }
41: }
42:
43: function get_job_results($job_uid) {
44: try {
45:
46: $pdo = connect_queue_db();
47:
48: $statement_jobresults = $pdo->prepare('SELECT * FROM get_job_results(?);');
49:
50: $statement_jobresults->execute(array($job_uid));
51: $res = array();
52: while ($row = $statement_jobresults->fetch(\PDO::FETCH_ASSOC)) {
53:
54: if (empty($res)) {
55: $res['job_status'] = $row['status'];
56: $res['additional_data'] = json_decode($row['additional_data']);
57: if ($row['status'] == 'NOT_PROCESSED') {
58: $statement_queuepos = $pdo->prepare('SELECT * FROM get_queue_position(?);');
59: $statement_queuepos->execute(array($_REQUEST['jobid']));
60: $pos = $statement_queuepos->fetch(\PDO::FETCH_ASSOC);
61: $res['queue_position'] = $pos['queue_position'];
62: $res['queue_length'] = $pos['queue_length'];
63: }
64: }
65:
66: $res['processed_results'][] = array(
67: 'query' => $row['query'],
68: 'status' => $row['query_status'],
69: 'result' => $row['query_stdout'],
70: 'errors' => $row['query_stderr']
71: );
72: }
73:
74: return $res;
75: } catch (\PDOException $e) {
76:
77: return array('status' => 'ERROR');
78: }
79: }
80:
81: function get_program_databases($filter_string=null) {
82: try {
83:
84: $pdo = connect_queue_db();
85:
86: $stm = $pdo->prepare('SELECT * FROM get_programname_database(?)');
87:
88: $stm->execute(array($filter_string));
89: $ret = array();
90: while ($row = $stm->fetch(\PDO::FETCH_ASSOC)) {
91:
92: $ret[$row['programname']][] = $row['database_name'];
93: }
94:
95: return $ret;
96: } catch (\PDOException $e) {
97:
98: return array();
99: }
100: }
101:
102: 103: 104: 105: 106: 107: 108:
109:
110: function split_fasta($query, $type) {
111:
112: $fasta_allowed = array();
113: 114: 115: 116: 117: 118: 119: 120:
121: $fasta_allowed['nucl'] = 'ACGTNUKSYMWRBDHV-';
122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136:
137: $fasta_allowed['prot'] = 'ABCDEFGHIKLMNPQRSTUVWYZX*-';
138:
139: $queries = array();
140:
141:
142: if (strpos($query, '>') === FALSE) {
143: $query = trim($query);
144: if (preg_match('/^[0-9\\s' . $fasta_allowed[$type] . ']+$/im', $query))
145: if (preg_match('/(\n\n|\r\n\r\n|\n\r\n\r)/im', $query)) {
146: throw new Exception('FASTA sequence invalid! If you want to specify multiple sequences, add headers. If you want to query a single sequence, remove blank lines.');
147: } else {
148: $queries[] = $query;
149: }
150: else
151: throw new Exception('FASTA sequence invalid!');
152: } else {
153: $require_next_line_header = true;
154: $lines = explode(PHP_EOL, $query);
155: foreach ($lines as $nr => $line) {
156: $line = trim($line);
157:
158: if (strpos($line, '>') === 0) {
159: $require_next_line_header = false;
160: $queries [] = "";
161: $current = &$queries[count($queries) - 1];
162: $current.=$line;
163: }
164:
165: else if (strlen($line) > 0) {
166: if ($require_next_line_header)
167: throw new Exception(sprintf('Missing FASTA Header at line number %d', $nr));
168: if (!preg_match('/^[' . $fasta_allowed[$type] . ']+$/i', $line))
169: throw new Exception(sprintf('FASTA sequence invalid in line %d!', $nr));
170: $current.="\n" . $line;
171: }
172:
173: else {
174: $require_next_line_header = true;
175: }
176: }
177: }
178: return $queries;
179: }
180:
181: ?>
182: