parse_blast_XML.inc 30 KB

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