parse_blast_XML.inc 31 KB

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