-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunblast.php
executable file
·243 lines (211 loc) · 7.65 KB
/
runblast.php
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
<?php
require "globals.inc.php";
require "libBlastApp.inc.php";
// Take data from $_SESSION, loaded in search.php, if empty back to front page
if (!isset($_SESSION['queryData']) or !$_SESSION['queryData']){
header('Location:index.php');
}
// prepare FASTA file
// Identify file format, ">" as first char indicates FASTA
$p = strpos($_SESSION['queryData'], '>');
# strpos returns False if not found and "0" if first character in the string
# When is not already FASTA, add header + new line
if (!$p and ( $p !== 0)) {
$_SESSION['queryData'] = ">User-provided sequence\n".$_SESSION['queryData'];
}
// Set temporary file name to a unique value to protect from concurrent runs
#$tmp_file = $tmp_dir . "/test";
$_SESSION['uniqueId'] = uniqId('ID');
$tmp_file = $tmp_dir . "/" . $_SESSION['uniqueId'];
$tmp_input = $tmp_file . ".query.fasta";
$tmp_output = $tmp_file . ".blast.out";
// Open temporary file and store query FASTA
$ff = fopen("$tmp_input", 'wt');
fwrite($ff, $_SESSION['queryData']);
fclose($ff);
// Load the options for the blast database
foreach (array_values($_SESSION['dataBase']) as $dbindex){
$blast_search_db[] = $blast_db_dir . "/" . $blast_db[$dbindex];
$blast_search_db_names[] = $blast_db[$dbindex];
}
$db_query_text = join(' ', $blast_search_db);
$db_query_text_names = join(' ', $blast_search_db_names);
// Load the options for the E-value threshold
$evalue = $_SESSION['eValue'];
if (!preg_match('/0.[0-9]+/', $evalue) and !preg_match('/[1-9]E-[0-9]+/', $evalue)){
if (preg_match('/E-[0-9]+/', $evalue)){
$evalue = "1".$evalue;
}
else{
errorPage("format error", "Sorry, the BLAST search produced no results. Make sure that your E-value threshold is in a valid format.");
}
}
// Load the options for the count limit
$count_limit= $_SESSION['countLimit'];
// Execute standalone blast and keep a command line log
$blast_cmd_line = $blast_exe . " -d " . "\"" . $db_query_text . "\"" . " -p blastp -e " . $evalue . " -v " . $count_limit . " -b 0";
$command_line = $blast_cmd_line . " -i " . $tmp_input . " -o " . $tmp_output;
exec($command_line);
$command_line_log = "blastall -d \"" . $db_query_text_names . "\"" ." -p blastp -e " . $evalue . " -v " . $count_limit . " -b 0" . " -i " . "query.fasta" . " -o " . "blast.out";
// Read results file and parse hits onto $result[]
// file() produces an array each position of which contains a line
$blast = file($tmp_output);
$i = 0;
$total_hits = 0;
$first_reference = True;
$results_boolean = False;
$hits_boolean = False;
foreach (array_values($blast) as $bb){
$i++;
if ($i == 1){
$blast_version = rtrim($bb,"\n");
}
if (preg_match('/Reference/', $bb) and $first_reference){
$references_boolean = True;
}
if ($references_boolean == True and !preg_match('/Query/',$bb)){
$references[] = $bb;
}
if (preg_match('/Query\s?=\s?(.*)\n/', $bb, $match)){
$references_boolean = False;
$first_reference = False;
list ($r , $query_id) = $match;
}
if (preg_match('/Sequences producing/', $bb)){
$results_boolean = True;
}
if (preg_match("/(....)_(.) mol:([^ ]*) length:([0-9]+) (.*)/", $bb) or preg_match("/(sp\|)(......)\|(.*)/", $bb)){
$hits_boolean = True;
$result[$query_id][] = $bb;
$total_hits++;
}
if ($results_boolean == True and (preg_match('/BLASTP/', $bb) or preg_match('/Database/', $bb))){
$results_boolean = False;
}
}
if ($hits_boolean == False){
errorPage("error", "Sorry, the BLAST search produced no results. Make sure that your input is in a valid FASTA format.");
}
else {
print myheader("results browser");
?>
<p class="button"><a href="index.php?new=1">New Search</a></p>
<p class="button"><a href="download.php">Download Results</a><p>
<table border="0" cellspacing="2" cellpadding="4" align="center">
<col width="200"><col width="750">
<tbody>
<tr>
<td><b>Version:</b></td>
<td><p><?php print ($blast_version) ?></p></td>
</tr>
<tr>
<td valign="top"><b>References:</b></td>
<td>
<?php
foreach (array_values($references) as $ref){
print $ref;
}
?> </td>
</tr>
<tr>
<td valign="top"><b>Query sequences:</b></td>
<td>
<?php
foreach ($result as $key => $ss){
$query_keys[] = $key;
$printable = join('; ',$query_keys);
}?>
<p><?php print $printable . "."?></p>
</td>
</tr>
<tr>
<td><b>Command-line log:</b></td>
<td><?php print $command_line_log?></td>
</tr>
<tr>
<td valign="top"><b>Databases consulted:</b></td>
<td> <?php print join(" and ", $_SESSION['dataBase'])?></td>
</tr>
<tr>
<td><b>Total number of hits:</b></td>
<td><?php print $total_hits ?> </td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="1" cellpadding="1" width="100%" id="blastTable">
<thead>
<tr>
<th>Protein Id</th>
<th>Query Seq</th>
<th>Protein Description</th>
<th>Score</th>
<th>E-value</th>
</tr>
</thead>
<tbody>
<?php
// parsing hit following specific format, note that this format is not standard. It comes from the
// headers used to generate BLAST databases, this is from PDB
foreach ($result as $key => $ss){
$query_id = $key;
foreach (array_values($ss) as $rr){
if (strlen(rtrim($rr,'\n')) > 1) {
$rr2 = $rr;
preg_match("/(....)_(.) mol:([^ ]*) length:([0-9]+) (.*)/", $rr, $hits);
if ($hits){
$entry_type = 'pdb';
list ($r, $idCode, $sub, $type, $l, $rest) = $hits;
$rest_array = preg_split( '/\s+/', $rest, -1, PREG_SPLIT_NO_EMPTY);
$e_value = array_pop($rest_array);
$score = array_pop($rest_array);
$descr = join(' ', $rest_array);
}
else {
$entry_type ='sprot';
$rr2_array = preg_split( '/\s+/', $rr2, -1, PREG_SPLIT_NO_EMPTY);
$e_value = array_pop($rr2_array);
$score = array_pop($rr2_array);
$rr3 = join(' ', $rr2_array);
preg_match("/(sp\|)(......)\|(.*)/", $rr3, $new_hits);
list ($r, $tail, $idCode, $rest_array) = $new_hits;
$descr = $rest_array;
}
}?>
<tr>
<?php
if ($entry_type == 'pdb'){ ?>
<td><a href="http://www.rcsb.org/pdb/explore/explore.do?structureId=<?php print $idCode ?>" target="_blank"><?php print $idCode . "_$sub" ?></a></td> <?php }
else { ?>
<td><a href="http://www.uniprot.org/uniprot/<?php print $idCode ?>" target="_blank"><?php print "sp|" . $idCode ?></a></td>
<?php } ?>
<td><?php print $query_id?></td>
<td><?php print $descr ?></td>
<td><?php print $score ?></td>
<td><?php print trans_numeric($e_value) ?></td>
</tr>
<?php
}
}
}
?>
</tbody>
</table>
<?php
// Cleaning temporary files
if (file_exists($tmp_input)){
unlink($tmp_input);
}
// if (file_exists($tmp_output)){
// unlink($tmp_output);
// }
print myfooter();
?>
<script type="text/javascript">
$(document).ready(function(){
$('#blastTable').DataTable({
/* "order": [[6, "asc"]] */
"paging": true
/* "colReorder": true */
});
});
</script>