parse_blast_XML.inc 30 KB

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