-
Notifications
You must be signed in to change notification settings - Fork 4
/
blastphp.php
136 lines (117 loc) · 3.86 KB
/
blastphp.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
<?php
# $ID: blastphp.php, v 1.0 2017/02/21 21:02:21 Ashok Kumar T. $
#
# ===========================================================================
#
# This code is for example purposes only.
#
# Please refer to https://ncbi.github.io/blast-cloud/dev/api.html
# for a complete list of allowed parameters.
#
# Please do not submit or retrieve more than one request every two seconds.
#
# Results will be kept at NCBI for 24 hours. For best batch performance,
# we recommend that you submit requests after 2000 EST (0100 GMT) and
# retrieve results before 0500 EST (1000 GMT).
#
# ===========================================================================
#
# return codes:
# 0 - success
# 1 - invalid arguments
# 2 - no hits found
# 3 - rid expired
# 4 - search failed
# 5 - unknown error
#
# ===========================================================================
// Path of the query sequence (modify)
$file = "C:\\Users\\ashok\\Desktop\\BLASTphp\\protein.fas";
// Read FASTA sequence from the HTML textbox and encode
//$encoded_query = urlencode($_POST["sequence"]);
// Read FASTA file through HTML file upload or directly and encode
function fas_read($file) {
$query = '';
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$query .= urlencode($line);
}
fclose($handle);
}
return $query;
}
//$encoded_query = fas_read($_FILES["file"]["tmp_name"]);
$encoded_query = fas_read($file);
// Build the request
$data = array('CMD' => 'Put', 'PROGRAM' => 'blastp', 'DATABASE' => 'pdb', 'QUERY' => $encoded_query);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
// Get the response from BLAST
$result = file_get_contents("https://blast.ncbi.nlm.nih.gov/blast/Blast.cgi", false, $context);
// Parse out the request ID
preg_match("/^.*RID = .*\$/m", $result, $ridm);
$rid = implode("\n", $ridm);
$rid = preg_replace('/\s+/', '', $rid);
$rid = str_replace("RID=", "", $rid);
// Parse out the estimated time to completion
preg_match("/^.*RTOE = .*\$/m", $result, $rtoem);
$rtoe = implode("\n", $rtoem);
$rtoe = preg_replace('/\s+/', '', $rtoe);
$rtoe = str_replace("RTOE=", "", $rtoe);
// Maximum execution time of webserver (optional)
//ini_set('max_execution_time', $rtoe+60);
//converting string to long (sleep() expects a long)
$rtoe = $rtoe + 0;
// Wait for search to complete
sleep($rtoe);
// Poll for results
while(true) {
sleep(10);
$opts = array(
'http' => array(
'method' => 'GET'
)
);
$contxt = stream_context_create($opts);
$reslt = file_get_contents("https://blast.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Get&FORMAT_OBJECT=SearchInfo&RID=$rid", false, $contxt);
if(preg_match('/Status=WAITING/', $reslt)) {
//print "Searching...\n";
continue;
}
if(preg_match('/Status=FAILED/', $reslt)) {
print "Search $rid failed, please report to blast-help\@ncbi.nlm.nih.gov.\n";
exit(4);
}
if(preg_match('/Status=UNKNOWN/', $reslt)) {
print "Search $rid expired.\n";
exit(3);
}
if(preg_match('/Status=READY/', $reslt)) {
if(preg_match('/ThereAreHits=yes/', $reslt)) {
//print "Search complete, retrieving results...\n";
break;
} else {
print "No hits found.\n";
exit(2);
}
}
// If we get here, something unexpected happened.
exit(5);
} // End poll loop
// Retrieve and display results
$opt = array(
'http' => array(
'method' => 'GET'
)
);
$content = stream_context_create($opt);
$output = file_get_contents("https://blast.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Get&FORMAT_TYPE=Text&RID=$rid", false, $content);
print $output;
?>