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->aid));
  344. $blast_object->ana_nid = $ana_nid;
  345. $blast_object->ana_time = $analysis->time;
  346. $blast_object->ana_name = $analysis->name;
  347. // Load the file. This XML file should be an extract
  348. // of the original XML file with only a single iteration.
  349. // An iteration is essentially all the hits for a single
  350. // query sequence.
  351. $xml_output = simplexml_load_string($xml_string);
  352. $iteration = '';
  353. // new XML file parser has added the feature name within <Iteration_query-def> tags.
  354. if ($xml_output->getName() == 'Iteration') {
  355. foreach ($xml_output->children() as $xml_tag) {
  356. if ($xml_tag->getName() == 'Iteration_query-def') {
  357. // Here we show the feature name again to check if we pull the correct data
  358. $blast_object->xml_tag = $xml_tag;
  359. } else if ($xml_tag->getName() == 'Iteration_hits') {
  360. $iteration = $xml_tag;
  361. }
  362. }
  363. // This is for the file parsed by the old parser
  364. } else {
  365. $iteration = $xml_output;
  366. }
  367. $number_hits = 0;
  368. foreach($iteration->children() as $hits){
  369. $number_hits ++;
  370. }
  371. // add the links for updating blast info using Ajax
  372. $blast_object->max = $max;
  373. $blast_object->number_hits = $number_hits;
  374. $blast_object->feature_id = $feature_id;
  375. $hits_array = array();
  376. $hit_count = 0;
  377. foreach($iteration->children() as $hits){
  378. $hsp_array = array();
  379. $counter = 0;
  380. foreach($hits->children() as $hit){
  381. $best_evalue = 0;
  382. $best_identity = 0;
  383. $best_len = 0;
  384. $element_name = $hit->getName();
  385. if($element_name == 'Hit_id'){
  386. // if parsing "name, acc, desc" from three tags (1/3)
  387. if ($is_genbank) {
  388. $hit_name = $hit;
  389. }
  390. } else if($element_name == 'Hit_def'){
  391. if($is_genbank){
  392. $description = $hit;
  393. } else {
  394. $accession = preg_replace($regex_hit_accession,"$1",$hit);
  395. $hit_name = preg_replace($regex_hit_id,"$1",$hit);
  396. $description = preg_replace($regex_hit_def,"$1",$hit);
  397. }
  398. } else if($element_name == 'Hit_accession'){
  399. // if parsing "name, acc, desc" from three tags (3/3)
  400. if ($is_genbank){
  401. $accession = $hit;
  402. }
  403. // now run through each HSP for this hit
  404. } else if($element_name == 'Hit_hsps'){
  405. foreach($hit->children() as $hsp){
  406. foreach($hsp->children() as $hsp_info){
  407. $element_name = $hsp_info->getName();
  408. if($element_name == 'Hsp_num'){
  409. $hsp_num = $hsp_info;
  410. }
  411. if($element_name == 'Hsp_bit-score'){
  412. $hsp_bit_score = $hsp_info;
  413. }
  414. if($element_name == 'Hsp_score'){
  415. $hsp_score = $hsp_info;
  416. }
  417. if($element_name == 'Hsp_evalue'){
  418. $hsp_evalue = $hsp_info;
  419. // use the first evalue for this set of HSPs
  420. // as the best evalue. This get's shown as
  421. // info for the overall match.
  422. if(!$best_evalue){
  423. $best_evalue = $hsp_evalue;
  424. }
  425. }
  426. if($element_name == 'Hsp_query-from'){
  427. $hsp_query_from = $hsp_info;
  428. }
  429. if($element_name == 'Hsp_query-to'){
  430. $hsp_query_to = $hsp_info;
  431. }
  432. if($element_name == 'Hsp_hit-from'){
  433. $hsp_hit_from = $hsp_info;
  434. }
  435. if($element_name == 'Hsp_hit-to'){
  436. $hsp_hit_to = $hsp_info;
  437. }
  438. if($element_name == 'Hsp_query-frame'){
  439. $hsp_query_frame = $hsp_info;
  440. }
  441. if($element_name == 'Hsp_identity'){
  442. $hsp_identity = $hsp_info;
  443. // use the first evalue for this set of HSPs
  444. // as the best evalue. This get's shown as
  445. // info for the overall match.
  446. if(!$best_identity){
  447. $best_identity = $hsp_identity;
  448. }
  449. }
  450. if($element_name == 'Hsp_positive'){
  451. $hsp_positive = $hsp_info;
  452. }
  453. if($element_name == 'Hsp_align-len'){
  454. $hsp_align_len = $hsp_info;
  455. // use the first evalue for this set of HSPs
  456. // as the best evalue. This get's shown as
  457. // info for the overall match.
  458. if(!$best_len){
  459. $best_len = $hsp_align_len;
  460. }
  461. }
  462. if($element_name == 'Hsp_qseq'){
  463. $hsp_qseq = $hsp_info;
  464. }
  465. if($element_name == 'Hsp_hseq'){
  466. $hsp_hseq = $hsp_info;
  467. }
  468. if($element_name == 'Hsp_midline'){
  469. $hsp_midline = $hsp_info;
  470. }
  471. }
  472. $hsp_content = array();
  473. $hsp_content['hsp_num'] = $hsp_num;
  474. $hsp_content['bit_score'] = $hsp_bit_score;
  475. $hsp_content['score'] = $hsp_score;
  476. $hsp_content['evalue'] = $hsp_evalue;
  477. $hsp_content['query_frame'] = $hsp_query_frame;
  478. $hsp_content['qseq'] = $hsp_qseq;
  479. $hsp_content['midline'] = $hsp_midline;
  480. $hsp_content['hseq'] = $hsp_hseq;
  481. $hsp_content['hit_from'] = $hsp_hit_from;
  482. $hsp_content['hit_to'] = $hsp_hit_to;
  483. $hsp_content['identity'] = $hsp_identity;
  484. $hsp_content['align_len'] = $hsp_align_len;
  485. $hsp_content['positive'] = $hsp_positive;
  486. $hsp_content['query_from'] = $hsp_query_from;
  487. $hsp_content['query_to'] = $hsp_query_to;
  488. $hsp_array[$counter] = $hsp_content;
  489. $counter ++;
  490. }
  491. }
  492. }
  493. $arrowr_url = url(drupal_get_path('theme', 'tripal')."/images/arrow_r.png");
  494. $hits_array[$hit_count]['arrowr_url'] = $arrowr_url;
  495. $hits_array[$hit_count]['accession'] = $accession;
  496. $hits_array[$hit_count]['hit_name'] = $hit_name;
  497. if($accession && $db->urlprefix){
  498. $hits_array[$hit_count]['hit_url'] = "$db->urlprefix$accession";
  499. } else {
  500. // Test if this is another feature in the database
  501. $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
  502. $previous_db = db_set_active('chado');
  503. $hit_feature_id = db_result(db_query($sql, $hit_name));
  504. db_set_active($previous_db);
  505. // If it is, add link to that feature
  506. if ($hit_feature_id) {
  507. $hits_array[$hit_count]['hit_url'] = "ID$hit_feature_id";
  508. }
  509. }
  510. $hits_array[$hit_count]['best_evalue'] = $best_evalue;
  511. $percent_identity = number_format($best_identity/$best_len*100, 2);
  512. $hits_array[$hit_count]['percent_identity'] = $percent_identity;
  513. $hits_array[$hit_count]['description'] = $description;
  514. $hits_array[$hit_count]['hsp'] = $hsp_array;
  515. $hit_count ++;
  516. // if we've hit the maximum number of hits then return
  517. if($max > 0 && $hit_count >= $max){
  518. break;
  519. }
  520. }
  521. $blast_object->hits_array = $hits_array;
  522. return $blast_object;
  523. }
  524. /********************************************************************************
  525. * Parse the best hit to generate the best hit homology report
  526. */
  527. function tripal_analysis_blast_parse_best_hit ($analysis_id) {
  528. // Select all features for this blast analysis, and save them to the 'featureSet' array
  529. $sql = "SELECT feature_id
  530. FROM {analysisfeature} AF
  531. WHERE analysis_id = %d";
  532. $previous_db = tripal_db_set_active('chado');
  533. $result = db_query($sql, $analysis_id);
  534. $featureSet = array ();
  535. $counter = 0;
  536. while ($feature = db_fetch_object($result)) {
  537. $featureSet [$counter] = $feature->feature_id;
  538. $counter ++;
  539. }
  540. // Get analysis information including 'Time', 'Name', and 'DB Settings'
  541. $sql = "SELECT value, name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
  542. FROM {analysis} A
  543. INNER JOIN {analysisprop} AP ON A.analysis_id = AP.analysis_id
  544. WHERE A.analysis_id = %d
  545. AND type_id= (SELECT cvterm_id
  546. FROM {cvterm}
  547. WHERE name = 'analysis_blast_settings')";
  548. $analysis = db_fetch_object(db_query($sql, $analysis_id));
  549. // Parse the blast settings
  550. $blastsettings = explode("|", $analysis->value);
  551. $db_id = $blastsettings [0];
  552. // Get the xml description parser using db_id
  553. tripal_db_set_active($previous_db);
  554. $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
  555. $parser = db_fetch_object(db_query($sql, $db_id));
  556. $db_name = $parser->displayname;
  557. $is_genbank = $parser->genbank_style;
  558. $regex_hit_id = $parser->regex_hit_id;
  559. $regex_hit_def = $parser->regex_hit_def;
  560. $regex_hit_accession = $parser->regex_hit_accession;
  561. // set default description parser if regular expressions have not been specified
  562. if(!$regex_hit_id){
  563. $regex_hit_id = '/^(.*?)\s.*$/';
  564. } else {
  565. $regex_hit_id = '/'.$regex_hit_id.'/';
  566. }
  567. if(!$regex_hit_def){
  568. $regex_hit_def = '/^.*?\s(.*)$/';
  569. } else {
  570. $regex_hit_def = '/'.$regex_hit_def.'/';
  571. }
  572. if(!$regex_hit_accession){
  573. $regex_hit_accession = '/^(.*?)\s.*$/';
  574. } else {
  575. $regex_hit_accession = '/'.$regex_hit_accession.'/';
  576. }
  577. $interval = intval($counter * 0.01);
  578. for ($i = 0; $i < $counter; $i ++) {
  579. if ($i !=0 && $i % $interval == 0) {
  580. $percentage = (int) ($i / $counter * 100);
  581. tripal_job_set_progress($job_id, $percentage);
  582. print $percentage."% ";
  583. }
  584. $sql = "SELECT value
  585. FROM {analysisfeatureprop} AFP
  586. INNER JOIN {analysisfeature} AF ON AFP.analysisfeature_id = AF.analysisfeature_id
  587. WHERE analysis_id = %d
  588. AND feature_id = %d
  589. 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'))";
  590. $previous_db = tripal_db_set_active('chado');
  591. $xml_output = simplexml_load_string(db_result(db_query($sql, $analysis_id, $featureSet[$i])));
  592. $iteration = '';
  593. // new XML file parser has added the feature name within <Iteration_query-def> tags.
  594. if ($xml_output->getName() == 'Iteration') {
  595. $query = "";
  596. foreach ($xml_output->children() as $xml_tag) {
  597. if ($xml_tag->getName() == 'Iteration_query-def') {
  598. // Here we show the feature name again to check if we pull the correct data
  599. $query = $xml_tag;
  600. } else if ($xml_tag->getName() == 'Iteration_hits') {
  601. $iteration = $xml_tag;
  602. }
  603. }
  604. // This is for the file parsed by the old parser
  605. } else {
  606. $iteration = $xml_output;
  607. }
  608. $number_hits = 0;
  609. foreach($iteration->children() as $hits){
  610. $number_hits ++;
  611. }
  612. $query = explode(" ", $query) ;
  613. $query = $query [0];
  614. if ($number_hits == 0) {
  615. continue;
  616. }
  617. // now run through the blast hits/hsps of this iteration
  618. // and generate the rows of the table
  619. foreach($iteration->children() as $hits){
  620. $hit_count++;
  621. foreach($hits->children() as $hit){
  622. $best_evalue = 0;
  623. $best_identity = 0;
  624. $best_len = 0;
  625. $element_name = $hit->getName();
  626. if($element_name == 'Hit_id'){
  627. // if parsing "name, acc, desc" from three tags (1/3)
  628. if ($is_genbank) {
  629. $hit_name = $hit;
  630. }
  631. } else if($element_name == 'Hit_def'){
  632. if($is_genbank){
  633. $description = $hit;
  634. } else {
  635. $accession = preg_replace($regex_hit_accession,"$1",$hit);
  636. $hit_name = preg_replace($regex_hit_id,"$1",$hit);
  637. $description = preg_replace($regex_hit_def,"$1",$hit);
  638. }
  639. } else if($element_name == 'Hit_accession'){
  640. // if parsing "name, acc, desc" from three tags (3/3)
  641. if ($is_genbank){
  642. $accession = $hit;
  643. }
  644. // now run through each HSP for this hit
  645. } else if($element_name == 'Hit_hsps'){
  646. foreach($hit->children() as $hsp){
  647. foreach($hsp->children() as $hsp_info){
  648. $element_name = $hsp_info->getName();
  649. if($element_name == 'Hsp_num'){
  650. $hsp_num = $hsp_info;
  651. }
  652. if($element_name == 'Hsp_bit-score'){
  653. $hsp_bit_score = $hsp_info;
  654. }
  655. if($element_name == 'Hsp_score'){
  656. $hsp_score = $hsp_info;
  657. }
  658. if($element_name == 'Hsp_evalue'){
  659. $hsp_evalue = $hsp_info;
  660. // use the first evalue for this set of HSPs
  661. // as the best evalue. This get's shown as
  662. // info for the overall match.
  663. if(!$best_evalue){
  664. $best_evalue = $hsp_evalue;
  665. }
  666. }
  667. if($element_name == 'Hsp_query-from'){
  668. $hsp_query_from = $hsp_info;
  669. }
  670. if($element_name == 'Hsp_query-to'){
  671. $hsp_query_to = $hsp_info;
  672. }
  673. if($element_name == 'Hsp_hit-from'){
  674. $hsp_hit_from = $hsp_info;
  675. }
  676. if($element_name == 'Hsp_hit-to'){
  677. $hsp_hit_to = $hsp_info;
  678. }
  679. if($element_name == 'Hsp_query-frame'){
  680. $hsp_query_frame = $hsp_info;
  681. }
  682. if($element_name == 'Hsp_identity'){
  683. $hsp_identity = $hsp_info;
  684. // use the first evalue for this set of HSPs
  685. // as the best evalue. This get's shown as
  686. // info for the overall match.
  687. if(!$best_identity){
  688. $best_identity = $hsp_identity;
  689. }
  690. }
  691. if($element_name == 'Hsp_positive'){
  692. $hsp_positive = $hsp_info;
  693. }
  694. if($element_name == 'Hsp_align-len'){
  695. $hsp_align_len = $hsp_info;
  696. // use the first evalue for this set of HSPs
  697. // as the best evalue. This get's shown as
  698. // info for the overall match.
  699. if(!$best_len){
  700. $best_len = $hsp_align_len;
  701. }
  702. }
  703. if($element_name == 'Hsp_qseq'){
  704. $hsp_qseq = $hsp_info;
  705. }
  706. if($element_name == 'Hsp_hseq'){
  707. $hsp_hseq = $hsp_info;
  708. }
  709. if($element_name == 'Hsp_midline'){
  710. $hsp_midline = $hsp_info;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. // Get analysisfeature_id
  717. $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE analysis_id = %d AND feature_id = %d";
  718. $af_id = db_result(db_query($sql, $analysis_id, $featureSet[$i]));
  719. // Get type_id
  720. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = '%s' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal')";
  721. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_query'));
  722. $sql_test ="SELECT analysisfeatureprop_id FROM {analysisfeatureprop} WHERE analysisfeature_id = $af_id AND type_id = %d";
  723. $test_afpid = db_result(db_query($sql_test, $type_id));
  724. //Insert only if this blast query not exists.
  725. if (!$test_afpid) {
  726. $afp_sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank) VALUES (%d, %d, '%s', 0)";
  727. //$query;
  728. db_query($afp_sql, $af_id, $type_id, $query);
  729. //$hit_name;
  730. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_match'));
  731. db_query($afp_sql, $af_id, $type_id, $hit_name);
  732. //$description;
  733. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_description'));
  734. db_query($afp_sql, $af_id, $type_id, $description);
  735. //$best_evalue;
  736. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_evalue'));
  737. $e_digit = explode("e-", $best_evalue);
  738. if (count($e_digit) == 2) {
  739. $evalue_shown = number_format($e_digit [0],1);
  740. $best_evalue = $evalue_shown."e-".$e_digit[1];
  741. }
  742. db_query($afp_sql, $af_id, $type_id, $best_evalue);
  743. //$best_identity;
  744. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_identity'));
  745. $percent_identity = number_format($best_identity/$best_len*100, 1);
  746. db_query($afp_sql, $af_id, $type_id, $percent_identity);
  747. //$best_len;
  748. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_length'));
  749. db_query($afp_sql, $af_id, $type_id, $best_len);
  750. // Otherwise, update all instead
  751. } else {
  752. $afp_sql = "UPDATE {analysisfeatureprop} SET analysisfeature_id = %d, type_id = %d, value = '%s', rank = 0 WHERE analysisfeatureprop_id = %d";
  753. //$query;
  754. db_query($afp_sql, $af_id, $type_id, $query, $test_afpid);
  755. //$hit_name;
  756. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_match'));
  757. $test_afpid = db_result(db_query($sql_test, $type_id));
  758. db_query($afp_sql, $af_id, $type_id, $hit_name, $test_afpid);
  759. //$description;
  760. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_description'));
  761. $test_afpid = db_result(db_query($sql_test, $type_id));
  762. db_query($afp_sql, $af_id, $type_id, $description, $test_afpid);
  763. //$best_evalue;
  764. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_evalue'));
  765. $test_afpid = db_result(db_query($sql_test, $type_id));
  766. $e_digit = explode("e-", $best_evalue);
  767. if (count($e_digit) == 2) {
  768. $evalue_shown = number_format($e_digit [0],1);
  769. $best_evalue = $evalue_shown."e-".$e_digit[1];
  770. }
  771. db_query($afp_sql, $af_id, $type_id, $best_evalue, $test_afpid);
  772. //$best_identity;
  773. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_identity'));
  774. $test_afpid = db_result(db_query($sql_test, $type_id));
  775. $percent_identity = number_format($best_identity/$best_len*100, 1);
  776. db_query($afp_sql, $af_id, $type_id, $percent_identity, $test_afpid);
  777. //$best_len;
  778. $type_id = db_result(db_query($sql, 'analysis_blast_besthit_length'));
  779. $test_afpid = db_result(db_query($sql_test, $type_id));
  780. db_query($afp_sql, $af_id, $type_id, $best_len, $test_afpid);
  781. }
  782. tripal_db_set_active($previous_db);
  783. break;
  784. }
  785. }
  786. print "100%\n";
  787. return;
  788. }