parse_blast_XML.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. <?php
  2. /*******************************************************************************
  3. * Parse NCBI Blast results for indexing so that user can use blast results to
  4. * find corresponding features
  5. */
  6. function parse_NCBI_Blast_XML_index_version($xml_string,$db,$feature_id) {
  7. // Get the parser using db_id
  8. $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
  9. $parser = db_fetch_object(db_query($sql, $db->db_id));
  10. $db_name = $parser->displayname;
  11. $is_genbank = $parser->genbank_style;
  12. $regex_hit_id = $parser->regex_hit_id;
  13. $regex_hit_def = $parser->regex_hit_def;
  14. $regex_hit_accession = $parser->regex_hit_accession;
  15. // set default if regular expressions have not been specified
  16. if(!$regex_hit_id){
  17. $regex_hit_id = '/^(.*?)\s.*$/';
  18. } else {
  19. $regex_hit_id = '/'.$regex_hit_id.'/';
  20. }
  21. if(!$regex_hit_def){
  22. $regex_hit_def = '/^.*?\s(.*)$/';
  23. } else {
  24. $regex_hit_def = '/'.$regex_hit_def.'/';
  25. }
  26. if(!$regex_hit_accession){
  27. $regex_hit_accession = '/^(.*?)\s.*$/';
  28. } else {
  29. $regex_hit_accession = '/'.$regex_hit_accession.'/';
  30. }
  31. $html_out .= "<h3>$db_name</h3>";
  32. // Load the file. This XML file should be an extract
  33. // of the original XML file with only a single iteration.
  34. // An iteration is essentially all the hits for a single
  35. // query sequence.
  36. $xml_output = simplexml_load_string($xml_string);
  37. $iteration = '';
  38. // new XML file parser has added the feature name within <Iteration_query-def> tags.
  39. if ($xml_output->getName() == 'Iteration') {
  40. foreach ($xml_output->children() as $xml_tag) {
  41. if ($xml_tag->getName() == 'Iteration_query-def') {
  42. // Here we show the feature name again to check if we pull the correct data
  43. $html_out .= "Query: $xml_tag<br>";
  44. } else if ($xml_tag->getName() == 'Iteration_hits') {
  45. $iteration = $xml_tag;
  46. }
  47. }
  48. // This is for the file parsed by the old parser
  49. } else {
  50. $iteration = $xml_output;
  51. }
  52. // now run through the blast hits/hsps of this iteration
  53. // and generate the rows of the table
  54. foreach($iteration->children() as $hits){
  55. $best_evalue = 0;
  56. foreach($hits->children() as $hit){
  57. $best_evalue = 0;
  58. $element_name = $hit->getName();
  59. if($element_name == 'Hit_id'){
  60. // if parsing "name, acc, desc" from three tags (1/3)
  61. if ($is_genbank) {
  62. $hit_name = $hit;
  63. }
  64. } else if($element_name == 'Hit_def'){
  65. if($is_genbank){
  66. $description = $hit;
  67. } else {
  68. $accession = preg_replace($regex_hit_accession,"$1",$hit);
  69. $hit_name = preg_replace($regex_hit_id,"$1",$hit);
  70. $description = preg_replace($regex_hit_def,"$1",$hit);
  71. }
  72. } else if($element_name == 'Hit_accession'){
  73. // if parsing "name, acc, desc" from three tags (3/3)
  74. if ($is_genbank){
  75. $accession = $hit;
  76. }
  77. // now run through each HSP for this hit
  78. }
  79. }
  80. $html_out .= "<p>$hit_name<br>";
  81. $html_out .= "$accession<br>";
  82. $html_out .= "<b>$description</b></br>";
  83. $hsp_html_out = '';
  84. }
  85. return $html_out;
  86. }
  87. /*******************************************************************************
  88. * Parse Blast XML Output file into analysisfeatureprop table
  89. */
  90. function tripal_analysis_blast_parseXMLFile ($analysis_id, $blastdb, $blastfile,
  91. $no_parsed, $blastfile_ext, $query_re, $query_type, $query_uniquename,
  92. $is_concat,$job_id,$is_concat) {
  93. // Prepare log
  94. $filename = preg_replace("/.*\/(.*)/", "$1", $blastfile);
  95. $logfile = file_directory_path() . "/tripal/tripal_analysis_blast/load_$filename.log";
  96. $log = fopen($logfile, 'a'); // append parsing results to log file
  97. // If user input a file (e.g. blast.xml)
  98. if (is_file($blastfile)) {
  99. tripal_analysis_blast_parseXML($analysis_id, $blastdb, $blastfile,
  100. $no_parsed, $blastfile_ext, $query_re, $query_type, $query_uniquename,
  101. $job_id,1,$log,$is_concat);
  102. }
  103. // Otherwise, $blastfile is a directory. Iterate through all xml files in it
  104. else {
  105. if(!$blastfile_ext){
  106. $blastfile_ext = 'xml';
  107. }
  108. $dir_handle = @opendir($blastfile) or die("Unable to open $blastfile");
  109. $pattern = sql_regcase($blastfile . "/*.$blastfile_ext");
  110. $total_files = count(glob($pattern));
  111. print "$total_files file(s) to be parsed.\n";
  112. $interval = intval($total_files * 0.01);
  113. $no_file = 0;
  114. // Parsing all files in the directory
  115. while ($file = readdir($dir_handle)) {
  116. if(preg_match("/^.*\.$blastfile_ext/i",$file)){
  117. tripal_analysis_blast_parseXML($analysis_id, $blastdb, "$blastfile/$file",
  118. $no_parsed, $blastfile_ext, $query_re, $query_type, $query_uniquename,
  119. $job_id,0,$log);
  120. // Set job status
  121. if ($no_file % $interval == 0) {
  122. $percentage = (int) (($no_file / $total_files) * 100);
  123. tripal_job_set_progress($job_id, $percentage);
  124. print $percentage."% ";
  125. }
  126. }
  127. $no_file ++;
  128. }
  129. }
  130. print "Done.\nSuccessful and failed entries have been saved in the log file:\n $logfile\n";
  131. fwrite($log, "\n");
  132. fclose($log);
  133. return;
  134. }
  135. /********************************************************************************
  136. *
  137. */
  138. function tripal_analysis_blast_parseXML($analysis_id, $blastdb, $blastfile,
  139. $no_parsed, $blastfile_ext, $query_re, $query_type, $query_uniquename,
  140. $job_id,$set_progress,$log,$is_concat){
  141. // Parsing started
  142. print "Parsing File:".$blastfile." ...\n";
  143. fwrite($log, date("D M j G:i:s Y").". Loading $blastfile\n");
  144. if ($no_parsed == 'all') {
  145. print "Parsing all hits...\n";
  146. } else {
  147. print "Parsing top $no_parsed hits...\n";
  148. }
  149. // Get cvterm_id for 'analysis_blast_output_iteration_hits' which is required
  150. // for inserting into the analysisfeatureprop table
  151. $previous_db = tripal_db_set_active('chado'); // use chado database
  152. $sql = "SELECT CVT.cvterm_id ".
  153. "FROM {cvterm} CVT ".
  154. " INNER JOIN {cv} ON cv.cv_id = CVT.cv_id ".
  155. "WHERE CVT.name = 'analysis_blast_output_iteration_hits' ".
  156. " AND CV.name = 'tripal'";
  157. $type_id = db_result(db_query($sql));
  158. // Load the XML file.
  159. if (!is_readable($blastfile)) {
  160. exit("Could not open the XML file '$blastfile'. Check that file exists and that permissions are correct.\n");
  161. }
  162. // if the file is a set of concatenated files then we want to split it up
  163. // and run each one individually
  164. if($is_concat){
  165. // generate a temporary file name
  166. $temp = tempnam(sys_get_temp_dir(),'blast_');
  167. print "Blast XML file is concatenated. Breaking apart and parsing each individually: $temp\n";
  168. $out_fh = fopen($temp,"w");
  169. // run through the lines of the XML file
  170. $in_fh = fopen($blastfile,"r");
  171. while(!feof($in_fh)){
  172. $line = fgets($in_fh);
  173. $line = trim($line);
  174. if(!$line){
  175. continue;
  176. }
  177. fwrite($out_fh,"$line\n");
  178. // if the line begins a set of blast output XML then parse the
  179. // preceeding set.
  180. if(preg_match("/<\/BlastOutput>/",$line)){
  181. // close the temp file
  182. fclose($out_fh);
  183. // now parse this new temp file
  184. tripal_analysis_blast_parseXML($analysis_id, $blastdb, $temp,
  185. $no_parsed, $blastfile_ext, $query_re, $query_type, $query_uniquename,
  186. $job_id,$set_progress,$log,0);
  187. // reopen the file for the next set of results
  188. $out_fh = fopen($temp,"w");
  189. }
  190. }
  191. fclose($in_fh);
  192. return;
  193. }
  194. $blastoutput = simplexml_load_file($blastfile);
  195. if(!$blastoutput){
  196. exit("Could not read the XML file '$blastfile'. Check that the XML file is not corrupted.\n");
  197. }
  198. $no_iterations = 0;
  199. foreach($blastoutput->children() as $tmp) {
  200. if ($tmp->getName() == 'BlastOutput_iterations') {
  201. foreach($tmp->children() as $itr) {
  202. if ($itr->getName() == 'Iteration') {
  203. $no_iterations ++;
  204. }
  205. }
  206. }
  207. }
  208. print "$no_iterations iterations to be processed.\n";
  209. $interval = intval($no_iterations * 0.01);
  210. $idx_iterations = 0;
  211. foreach ($blastoutput->children() as $blastoutput_tags) {
  212. if ($blastoutput_tags->getName() == 'BlastOutput_iterations') {
  213. foreach($blastoutput_tags->children() as $iterations) {
  214. if ($iterations->getName() == 'Iteration') {
  215. // Set job status
  216. $idx_iterations ++;
  217. if ($set_progress and $idx_iterations % $interval == 0) {
  218. $percentage = (int) (($idx_iterations / $no_iterations) * 100);
  219. tripal_job_set_progress($job_id, $percentage);
  220. print $percentage."% ";
  221. }
  222. // now run through the blast hits/hsps of this iteration
  223. // and generate the rows of the table
  224. $feature_id = 0;
  225. foreach($iterations->children() as $iteration_tags) {
  226. // Match chado feature uniquename with <Iteration_query-def>
  227. // and get the feature_id
  228. $featurenaem_xml = '';
  229. if($iteration_tags->getName() == 'Iteration_query-def'){
  230. // If the Iteration_query-def in the format provided by the
  231. // user's regular expression
  232. if ($query_re and preg_match("/$query_re/", $iteration_tags, $matches)) {
  233. $feature = $matches[1];
  234. }
  235. // If not in above format then pull up to the first space
  236. else {
  237. if (preg_match('/^(.*?)\s.*$/', $iteration_tags, $matches)) {
  238. $feature = $matches[1];
  239. }
  240. // if no match up to the first space then just use the entire string
  241. else {
  242. $feature = $iteration_tags;
  243. }
  244. }
  245. if(!$feature and $query_re){
  246. print "ERROR: cannot find feature for $iteration_tags using the regular expression: $query_re\n";
  247. exit;
  248. }
  249. // now find the feature in chado
  250. $select = array();
  251. if($query_uniquename){
  252. $select['uniquename'] = $feature;
  253. } else {
  254. $select['name'] = $feature;
  255. }
  256. if($query_type){
  257. $select['type_id'] = array(
  258. 'cv_id' => array(
  259. 'name' => 'sequence'
  260. ),
  261. 'name' => $query_type,
  262. );
  263. }
  264. $feature_arr = tripal_core_chado_select('feature',array('feature_id'),$select);
  265. if(count($feature_arr) > 1){
  266. fwrite($log, "Ambiguous: '$feature' matches more than one feature and is being skipped.\n");
  267. continue;
  268. }
  269. if(count($feature_arr) == 0){
  270. fwrite($log, "Failed: '$feature' cannot find a matching feature in the database.\n");
  271. continue;
  272. }
  273. $feature_id = $feature_arr[0]->feature_id;
  274. fwrite($log, "Matched: '$feature' => feature id:".$feature_id);
  275. $featurename_xml = $iteration_tags->asXML();
  276. }
  277. // Insert Iteration_hits into analysisfeatureprop and analysisfeature tables
  278. else if($iteration_tags->getName() == 'Iteration_hits'){
  279. if ($feature_id) {
  280. // Make sure this iteration doesn't exist in analysisfeatureprop. If it does, update but not insert
  281. $sql = "SELECT analysisfeatureprop_id FROM {analysisfeatureprop} AFP ".
  282. "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
  283. "WHERE feature_id=%d ".
  284. "AND analysis_id=%d ".
  285. "AND type_id=%d ";
  286. $result = db_query($sql, $feature_id, $analysis_id, $type_id);
  287. $analysisfeatureprop = db_fetch_object($result);
  288. $xml_content = "<Iteration>\n".$featurename_xml."\n";
  289. // parse all hits
  290. if ($no_parsed == 'all') {
  291. $xml_content .= $iteration_tags->asXML();
  292. // parse only top hits
  293. } else {
  294. $counter = 0;
  295. $xml_content .= "<Iteration_hits>\n";
  296. foreach ($iteration_tags->children() As $hit) {
  297. if ($counter < $no_parsed) {
  298. $xml_content .= $hit->asXML();
  299. } else {
  300. break;
  301. }
  302. $counter ++;
  303. }
  304. $xml_content .= "</Iteration_hits>";
  305. }
  306. $xml_content .= "\n</Iteration>";
  307. // If this Iteration_hits already exists, update it
  308. if ($analysisfeatureprop) {
  309. $sql = "UPDATE {analysisfeatureprop} ".
  310. "SET value = '%s' ".
  311. "WHERE analysisfeatureprop_id = %d ";
  312. db_query($sql, $xml_content, $analysisfeatureprop->analysisfeatureprop_id);
  313. fwrite($log, " (Update)\n"); // write to log
  314. // Otherwise, insert the Iteration_hits into analysisfeature and analysisfeatureprop tables
  315. } else {
  316. //------------------------------------------------------
  317. // Insert into analysisfeature table
  318. //------------------------------------------------------
  319. $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
  320. "VALUES (%d, %d)";
  321. db_query ($sql, $feature_id, $analysis_id);
  322. // Get the newly inserted analysisfeature_id
  323. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
  324. $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
  325. //------------------------------------------------------
  326. // Insert into analysisfeatureprop table
  327. //------------------------------------------------------
  328. $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
  329. "VALUES (%d, %d, '%s', %d)";
  330. db_query($sql, $analysisfeature_id, $type_id, $xml_content, '0');
  331. fwrite($log, " (Insert)\n"); // write to log
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. tripal_db_set_active ($previous_db); // Use drupal database
  341. }
  342. /********************************************************************************
  343. *
  344. */
  345. function tripal_analysis_blast_get_result_object($xml_string,$db,$max,$feature_id, $analysis) {
  346. $blast_object = new stdClass();
  347. // Get the parser using db_id
  348. $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
  349. $parser = db_fetch_object(db_query($sql, $db->db_id));
  350. $db_name = $parser->displayname;
  351. $is_genbank = $parser->genbank_style;
  352. $regex_hit_id = $parser->regex_hit_id;
  353. $regex_hit_def = $parser->regex_hit_def;
  354. $regex_hit_accession = $parser->regex_hit_accession;
  355. // set default if regular expressions have not been specified
  356. if(!$regex_hit_id){
  357. $regex_hit_id = '/^(.*?)\s.*$/';
  358. } else {
  359. $regex_hit_id = '/'.$regex_hit_id.'/';
  360. }
  361. if(!$regex_hit_def){
  362. $regex_hit_def = '/^.*?\s(.*)$/';
  363. } else {
  364. $regex_hit_def = '/'.$regex_hit_def.'/';
  365. }
  366. if(!$regex_hit_accession){
  367. $regex_hit_accession = '/^(.*?)\s.*$/';
  368. } else {
  369. $regex_hit_accession = '/'.$regex_hit_accession.'/';
  370. }
  371. // Get analysis information
  372. $blast_object->analysis = $analysis;
  373. $blast_object->db = $db;
  374. if (!$db_name) {
  375. $blast_object->title = $analysis->name;
  376. } else {
  377. $blast_object->title = $db_name;
  378. }
  379. // Find node id for the analysis
  380. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $analysis->analysis_id));
  381. $analysis->nid = $ana_nid;
  382. // Load the file. This XML file should be an extract
  383. // of the original XML file with only a single iteration.
  384. // An iteration is essentially all the hits for a single
  385. // query sequence.
  386. $xml_output = simplexml_load_string($xml_string);
  387. $iteration = '';
  388. // new XML file parser has added the feature name within <Iteration_query-def> tags.
  389. if ($xml_output->getName() == 'Iteration') {
  390. foreach ($xml_output->children() as $xml_tag) {
  391. if ($xml_tag->getName() == 'Iteration_query-def') {
  392. // Here we show the feature name again to check if we pull the correct data
  393. $blast_object->xml_tag = $xml_tag;
  394. } else if ($xml_tag->getName() == 'Iteration_hits') {
  395. $iteration = $xml_tag;
  396. }
  397. }
  398. // This is for the file parsed by the old parser
  399. } else {
  400. $iteration = $xml_output;
  401. }
  402. $number_hits = 0;
  403. foreach($iteration->children() as $hits){
  404. $number_hits ++;
  405. }
  406. // add the links for updating blast info using Ajax
  407. $blast_object->max = $max;
  408. $blast_object->number_hits = $number_hits;
  409. $blast_object->feature_id = $feature_id;
  410. $hits_array = array();
  411. $hit_count = 0;
  412. foreach($iteration->children() as $hits){
  413. $hsp_array = array();
  414. $counter = 0;
  415. foreach($hits->children() as $hit){
  416. $best_evalue = 0;
  417. $best_identity = 0;
  418. $best_len = 0;
  419. $element_name = $hit->getName();
  420. if($element_name == 'Hit_id'){
  421. // if parsing "name, acc, desc" from three tags (1/3)
  422. if ($is_genbank) {
  423. $hit_name = $hit;
  424. }
  425. } else if($element_name == 'Hit_def'){
  426. if($is_genbank){
  427. $description = $hit;
  428. } else {
  429. $accession = preg_replace($regex_hit_accession,"$1",$hit);
  430. $hit_name = preg_replace($regex_hit_id,"$1",$hit);
  431. $description = preg_replace($regex_hit_def,"$1",$hit);
  432. }
  433. } else if($element_name == 'Hit_accession'){
  434. // if parsing "name, acc, desc" from three tags (3/3)
  435. if ($is_genbank){
  436. $accession = $hit;
  437. }
  438. // now run through each HSP for this hit
  439. } else if($element_name == 'Hit_hsps'){
  440. foreach($hit->children() as $hsp){
  441. foreach($hsp->children() as $hsp_info){
  442. $element_name = $hsp_info->getName();
  443. if($element_name == 'Hsp_num'){
  444. $hsp_num = $hsp_info;
  445. }
  446. if($element_name == 'Hsp_bit-score'){
  447. $hsp_bit_score = $hsp_info;
  448. }
  449. if($element_name == 'Hsp_score'){
  450. $hsp_score = $hsp_info;
  451. }
  452. if($element_name == 'Hsp_evalue'){
  453. $hsp_evalue = $hsp_info;
  454. // use the first evalue for this set of HSPs
  455. // as the best evalue. This get's shown as
  456. // info for the overall match.
  457. if(!$best_evalue){
  458. $best_evalue = $hsp_evalue;
  459. }
  460. }
  461. if($element_name == 'Hsp_query-from'){
  462. $hsp_query_from = $hsp_info;
  463. }
  464. if($element_name == 'Hsp_query-to'){
  465. $hsp_query_to = $hsp_info;
  466. }
  467. if($element_name == 'Hsp_hit-from'){
  468. $hsp_hit_from = $hsp_info;
  469. }
  470. if($element_name == 'Hsp_hit-to'){
  471. $hsp_hit_to = $hsp_info;
  472. }
  473. if($element_name == 'Hsp_query-frame'){
  474. $hsp_query_frame = $hsp_info;
  475. }
  476. if($element_name == 'Hsp_identity'){
  477. $hsp_identity = $hsp_info;
  478. // use the first evalue for this set of HSPs
  479. // as the best evalue. This get's shown as
  480. // info for the overall match.
  481. if(!$best_identity){
  482. $best_identity = $hsp_identity;
  483. }
  484. }
  485. if($element_name == 'Hsp_positive'){
  486. $hsp_positive = $hsp_info;
  487. }
  488. if($element_name == 'Hsp_align-len'){
  489. $hsp_align_len = $hsp_info;
  490. // use the first evalue for this set of HSPs
  491. // as the best evalue. This get's shown as
  492. // info for the overall match.
  493. if(!$best_len){
  494. $best_len = $hsp_align_len;
  495. }
  496. }
  497. if($element_name == 'Hsp_qseq'){
  498. $hsp_qseq = $hsp_info;
  499. }
  500. if($element_name == 'Hsp_hseq'){
  501. $hsp_hseq = $hsp_info;
  502. }
  503. if($element_name == 'Hsp_midline'){
  504. $hsp_midline = $hsp_info;
  505. }
  506. }
  507. $hsp_content = array();
  508. $hsp_content['hsp_num'] = $hsp_num;
  509. $hsp_content['bit_score'] = $hsp_bit_score;
  510. $hsp_content['score'] = $hsp_score;
  511. $hsp_content['evalue'] = $hsp_evalue;
  512. $hsp_content['query_frame'] = $hsp_query_frame;
  513. $hsp_content['qseq'] = $hsp_qseq;
  514. $hsp_content['midline'] = $hsp_midline;
  515. $hsp_content['hseq'] = $hsp_hseq;
  516. $hsp_content['hit_from'] = $hsp_hit_from;
  517. $hsp_content['hit_to'] = $hsp_hit_to;
  518. $hsp_content['identity'] = $hsp_identity;
  519. $hsp_content['align_len'] = $hsp_align_len;
  520. $hsp_content['positive'] = $hsp_positive;
  521. $hsp_content['query_from'] = $hsp_query_from;
  522. $hsp_content['query_to'] = $hsp_query_to;
  523. $hsp_array[$counter] = $hsp_content;
  524. $counter ++;
  525. }
  526. }
  527. }
  528. $arrowr_url = url(drupal_get_path('theme', 'tripal')."/images/arrow_r.png");
  529. $hits_array[$hit_count]['arrowr_url'] = $arrowr_url;
  530. $hits_array[$hit_count]['accession'] = $accession;
  531. $hits_array[$hit_count]['hit_name'] = $hit_name;
  532. if($accession && $db->urlprefix){
  533. $hits_array[$hit_count]['hit_url'] = "$db->urlprefix$accession";
  534. } else {
  535. // Test if this is another feature in the database
  536. $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
  537. $previous_db = db_set_active('chado');
  538. $hit_feature_id = db_result(db_query($sql, $hit_name));
  539. db_set_active($previous_db);
  540. // If it is, add link to that feature
  541. if ($hit_feature_id) {
  542. $hits_array[$hit_count]['hit_url'] = "ID$hit_feature_id";
  543. }
  544. }
  545. $hits_array[$hit_count]['best_evalue'] = $best_evalue;
  546. $percent_identity = number_format($best_identity/$best_len*100, 2);
  547. $hits_array[$hit_count]['percent_identity'] = $percent_identity;
  548. $hits_array[$hit_count]['description'] = $description;
  549. $hits_array[$hit_count]['hsp'] = $hsp_array;
  550. $hit_count ++;
  551. // if we've hit the maximum number of hits then return
  552. if($max > 0 && $hit_count >= $max){
  553. break;
  554. }
  555. }
  556. $blast_object->hits_array = $hits_array;
  557. return $blast_object;
  558. }
  559. /********************************************************************************
  560. * Parse the best hit to generate the best hit homology report
  561. */
  562. function tripal_analysis_blast_parse_best_hit ($analysis_id) {
  563. // Select all features for this blast analysis, and save them to the 'featureSet' array
  564. $sql = "SELECT feature_id
  565. FROM {analysisfeature} AF
  566. WHERE analysis_id = %d";
  567. $previous_db = tripal_db_set_active('chado');
  568. $result = db_query($sql, $analysis_id);
  569. $featureSet = array ();
  570. $counter = 0;
  571. while ($feature = db_fetch_object($result)) {
  572. $featureSet [$counter] = $feature->feature_id;
  573. $counter ++;
  574. }
  575. // Get analysis information including 'Time', 'Name', and 'DB Settings'
  576. $sql = "SELECT value, name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  577. FROM {analysis} A
  578. INNER JOIN {analysisprop} AP ON A.analysis_id = AP.analysis_id
  579. WHERE A.analysis_id = %d
  580. AND type_id= (SELECT cvterm_id
  581. FROM {cvterm}
  582. WHERE name = 'analysis_blast_settings')";
  583. $analysis = db_fetch_object(db_query($sql, $analysis_id));
  584. // Parse the blast settings
  585. $blastsettings = explode("|", $analysis->value);
  586. $db_id = $blastsettings [0];
  587. // Get the xml description parser using db_id
  588. tripal_db_set_active($previous_db);
  589. $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
  590. $parser = db_fetch_object(db_query($sql, $db_id));
  591. $db_name = $parser->displayname;
  592. $is_genbank = $parser->genbank_style;
  593. $regex_hit_id = $parser->regex_hit_id;
  594. $regex_hit_def = $parser->regex_hit_def;
  595. $regex_hit_accession = $parser->regex_hit_accession;
  596. // set default description parser if regular expressions have not been specified
  597. if(!$regex_hit_id){
  598. $regex_hit_id = '/^(.*?)\s.*$/';
  599. } else {
  600. $regex_hit_id = '/'.$regex_hit_id.'/';
  601. }
  602. if(!$regex_hit_def){
  603. $regex_hit_def = '/^.*?\s(.*)$/';
  604. } else {
  605. $regex_hit_def = '/'.$regex_hit_def.'/';
  606. }
  607. if(!$regex_hit_accession){
  608. $regex_hit_accession = '/^(.*?)\s.*$/';
  609. } else {
  610. $regex_hit_accession = '/'.$regex_hit_accession.'/';
  611. }
  612. $interval = intval($counter * 0.01);
  613. for ($i = 0; $i < $counter; $i ++) {
  614. if ($i !=0 && $i % $interval == 0) {
  615. $percentage = (int) ($i / $counter * 100);
  616. tripal_job_set_progress($job_id, $percentage);
  617. print $percentage."% ";
  618. }
  619. $sql = "SELECT value
  620. FROM {analysisfeatureprop} AFP
  621. INNER JOIN {analysisfeature} AF ON AFP.analysisfeature_id = AF.analysisfeature_id
  622. WHERE analysis_id = %d
  623. AND feature_id = %d
  624. AND type_id = (SELECT cvterm_id FROM cvterm WHERE name='analysis_blast_output_iteration_hits' AND cv_id = (SELECT cv_id FROM cv WHERE name='tripal'))";
  625. $previous_db = tripal_db_set_active('chado');
  626. $xml_output = simplexml_load_string(db_result(db_query($sql, $analysis_id, $featureSet[$i])));
  627. $iteration = '';
  628. // new XML file parser has added the feature name within <Iteration_query-def> tags.
  629. if ($xml_output->getName() == 'Iteration') {
  630. $query = "";
  631. foreach ($xml_output->children() as $xml_tag) {
  632. if ($xml_tag->getName() == 'Iteration_query-def') {
  633. // Here we show the feature name again to check if we pull the correct data
  634. $query = $xml_tag;
  635. } else if ($xml_tag->getName() == 'Iteration_hits') {
  636. $iteration = $xml_tag;
  637. }
  638. }
  639. // This is for the file parsed by the old parser
  640. } else {
  641. $iteration = $xml_output;
  642. }
  643. $number_hits = 0;
  644. foreach($iteration->children() as $hits){
  645. $number_hits ++;
  646. }
  647. $query = explode(" ", $query) ;
  648. $query = $query [0];
  649. if ($number_hits == 0) {
  650. continue;
  651. }
  652. // now run through the blast hits/hsps of this iteration
  653. // and generate the rows of the table
  654. foreach($iteration->children() as $hits){
  655. $hit_count++;
  656. foreach($hits->children() as $hit){
  657. $best_evalue = 0;
  658. $best_identity = 0;
  659. $best_len = 0;
  660. $element_name = $hit->getName();
  661. if($element_name == 'Hit_id'){
  662. // if parsing "name, acc, desc" from three tags (1/3)
  663. if ($is_genbank) {
  664. $hit_name = $hit;
  665. }
  666. } else if($element_name == 'Hit_def'){
  667. if($is_genbank){
  668. $description = $hit;
  669. } else {
  670. $accession = preg_replace($regex_hit_accession,"$1",$hit);
  671. $hit_name = preg_replace($regex_hit_id,"$1",$hit);
  672. $description = preg_replace($regex_hit_def,"$1",$hit);
  673. }
  674. } else if($element_name == 'Hit_accession'){
  675. // if parsing "name, acc, desc" from three tags (3/3)
  676. if ($is_genbank){
  677. $accession = $hit;
  678. }
  679. // now run through each HSP for this hit
  680. } else if($element_name == 'Hit_hsps'){
  681. foreach($hit->children() as $hsp){
  682. foreach($hsp->children() as $hsp_info){
  683. $element_name = $hsp_info->getName();
  684. if($element_name == 'Hsp_num'){
  685. $hsp_num = $hsp_info;
  686. }
  687. if($element_name == 'Hsp_bit-score'){
  688. $hsp_bit_score = $hsp_info;
  689. }
  690. if($element_name == 'Hsp_score'){
  691. $hsp_score = $hsp_info;
  692. }
  693. if($element_name == 'Hsp_evalue'){
  694. $hsp_evalue = $hsp_info;
  695. // use the first evalue for this set of HSPs
  696. // as the best evalue. This get's shown as
  697. // info for the overall match.
  698. if(!$best_evalue){
  699. $best_evalue = $hsp_evalue;
  700. }
  701. }
  702. if($element_name == 'Hsp_query-from'){
  703. $hsp_query_from = $hsp_info;
  704. }
  705. if($element_name == 'Hsp_query-to'){
  706. $hsp_query_to = $hsp_info;
  707. }
  708. if($element_name == 'Hsp_hit-from'){
  709. $hsp_hit_from = $hsp_info;
  710. }
  711. if($element_name == 'Hsp_hit-to'){
  712. $hsp_hit_to = $hsp_info;
  713. }
  714. if($element_name == 'Hsp_query-frame'){
  715. $hsp_query_frame = $hsp_info;
  716. }
  717. if($element_name == 'Hsp_identity'){
  718. $hsp_identity = $hsp_info;
  719. // use the first evalue for this set of HSPs
  720. // as the best evalue. This get's shown as
  721. // info for the overall match.
  722. if(!$best_identity){
  723. $best_identity = $hsp_identity;
  724. }
  725. }
  726. if($element_name == 'Hsp_positive'){
  727. $hsp_positive = $hsp_info;
  728. }
  729. if($element_name == 'Hsp_align-len'){
  730. $hsp_align_len = $hsp_info;
  731. // use the first evalue for this set of HSPs
  732. // as the best evalue. This get's shown as
  733. // info for the overall match.
  734. if(!$best_len){
  735. $best_len = $hsp_align_len;
  736. }
  737. }
  738. if($element_name == 'Hsp_qseq'){
  739. $hsp_qseq = $hsp_info;
  740. }
  741. if($element_name == 'Hsp_hseq'){
  742. $hsp_hseq = $hsp_info;
  743. }
  744. if($element_name == 'Hsp_midline'){
  745. $hsp_midline = $hsp_info;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. // Get analysisfeature_id
  752. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE analysis_id = %d AND feature_id = %d";
  753. $af_id = db_result(db_query($sql, $analysis_id, $featureSet[$i]));
  754. // Get type_id
  755. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = '%s' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal')";
  756. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_query'));
  757. $sql_test ="SELECT analysisfeatureprop_id FROM {analysisfeatureprop} WHERE analysisfeature_id = $af_id AND type_id = %d";
  758. $test_afpid = db_result(db_query($sql_test, $type_id));
  759. //Insert only if this blast query not exists.
  760. if (!$test_afpid) {
  761. $afp_sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank) VALUES (%d, %d, '%s', 0)";
  762. //$query;
  763. db_query($afp_sql, $af_id, $type_id, $query);
  764. //$hit_name;
  765. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_match'));
  766. db_query($afp_sql, $af_id, $type_id, $hit_name);
  767. //$description;
  768. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_description'));
  769. db_query($afp_sql, $af_id, $type_id, $description);
  770. //$best_evalue;
  771. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_evalue'));
  772. $e_digit = explode("e-", $best_evalue);
  773. if (count($e_digit) == 2) {
  774. $evalue_shown = number_format($e_digit [0],1);
  775. $best_evalue = $evalue_shown."e-".$e_digit[1];
  776. }
  777. db_query($afp_sql, $af_id, $type_id, $best_evalue);
  778. //$best_identity;
  779. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_identity'));
  780. $percent_identity = number_format($best_identity/$best_len*100, 1);
  781. db_query($afp_sql, $af_id, $type_id, $percent_identity);
  782. //$best_len;
  783. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_length'));
  784. db_query($afp_sql, $af_id, $type_id, $best_len);
  785. // Otherwise, update all instead
  786. } else {
  787. $afp_sql = "UPDATE {analysisfeatureprop} SET analysisfeature_id = %d, type_id = %d, value = '%s', rank = 0 WHERE analysisfeatureprop_id = %d";
  788. //$query;
  789. db_query($afp_sql, $af_id, $type_id, $query, $test_afpid);
  790. //$hit_name;
  791. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_match'));
  792. $test_afpid = db_result(db_query($sql_test, $type_id));
  793. db_query($afp_sql, $af_id, $type_id, $hit_name, $test_afpid);
  794. //$description;
  795. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_description'));
  796. $test_afpid = db_result(db_query($sql_test, $type_id));
  797. db_query($afp_sql, $af_id, $type_id, $description, $test_afpid);
  798. //$best_evalue;
  799. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_evalue'));
  800. $test_afpid = db_result(db_query($sql_test, $type_id));
  801. $e_digit = explode("e-", $best_evalue);
  802. if (count($e_digit) == 2) {
  803. $evalue_shown = number_format($e_digit [0],1);
  804. $best_evalue = $evalue_shown."e-".$e_digit[1];
  805. }
  806. db_query($afp_sql, $af_id, $type_id, $best_evalue, $test_afpid);
  807. //$best_identity;
  808. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_identity'));
  809. $test_afpid = db_result(db_query($sql_test, $type_id));
  810. $percent_identity = number_format($best_identity/$best_len*100, 1);
  811. db_query($afp_sql, $af_id, $type_id, $percent_identity, $test_afpid);
  812. //$best_len;
  813. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_length'));
  814. $test_afpid = db_result(db_query($sql_test, $type_id));
  815. db_query($afp_sql, $af_id, $type_id, $best_len, $test_afpid);
  816. }
  817. tripal_db_set_active($previous_db);
  818. break;
  819. }
  820. }
  821. print "100%\n";
  822. return;
  823. }