parse_blast_XML.inc 32 KB

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