blast_ui.linkouts.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @file
  4. * Provides Link-out functionality for BLAST hits.
  5. *
  6. * Specifically, this is how the URL portion of the hit links in your users
  7. * Blast results is formed.
  8. *
  9. * To implement your own link-out type:
  10. * 1) Register your link-out type by implementing hook_blast_linkout_info().
  11. * Hook blast_linkout_info must return an array of types where each type
  12. * has a name and process function. For exmaple,
  13. * @code
  14. function blast_ui_blast_linkout_info() {
  15. $types = array();
  16. $types['my-link-type'] = array(
  17. 'name' => 'My Amazing Link Type',
  18. 'process function' => 'mymodule_generate_linkout_mylinktype',
  19. );
  20. return $types;
  21. }
  22. * @endcode
  23. *
  24. * 2) Implement the process function you specified to determine the URL
  25. * to be linked to depending on the blast hit. For a full description of
  26. * parameters available to your function, see tripal_blast_generate_linkout_link().
  27. * @code
  28. function mymodule_generate_linkout_mylinktype($url_prefix, $hit, $info, $options = array()) {
  29. // Do some simple steps to generate the suffix based on the $hit.
  30. return $url_prefix . $url_postfix;
  31. }
  32. * @endcode
  33. *
  34. * This module will automatically,
  35. * - Add your custom type to the "Link-out Type" select list on Blast Database
  36. * node add/edit forms.
  37. * - If your type is chosen by the user when the Blast Database is created,
  38. * then your process function will be used by blast_report.tpl.php to
  39. * determine the URL that should be used for each hit link.
  40. */
  41. /**
  42. * Implements hook_blast_linkout_info().
  43. * Provide information on basic link-out types: link, GBrowse, JBrowse.
  44. *
  45. * NOTE: Each item must have a 'name' and 'process function' to indicate the
  46. * human-readable name to be used in the Blast Database add/edit form and the
  47. * function to be used to determine the URL for each hit in BLAST results.
  48. */
  49. function blast_ui_blast_linkout_info() {
  50. $types = array();
  51. $types['link'] = array(
  52. // Human-readable Type name to display to users in the BLAST Database
  53. // create/edit form.
  54. 'name' => 'Generic Link',
  55. // The function used to generate the URL to be linked to.
  56. // This function will have full access to the blast hit and database
  57. // prefix information and is expected to return a URL.
  58. 'process function' => 'tripal_blast_generate_linkout_link',
  59. );
  60. $types['gbrowse'] = array(
  61. 'name' => 'GBrowse',
  62. 'process function' => 'tripal_blast_generate_linkout_gbrowse',
  63. );
  64. $types['jbrowse'] = array(
  65. 'name' => 'JBrowse',
  66. 'process function' => 'tripal_blast_generate_linkout_jbrowse',
  67. );
  68. return $types;
  69. }
  70. /**
  71. * Generate a basic link-out for a given hit.
  72. *
  73. * Essentially, concatenate the URL prefix with the extracted hit identifier
  74. * and return the URL to be displayed by the BLAST report template.
  75. *
  76. * @param $url_prefix
  77. * The URL prefix for the BLAST Database queried.
  78. * @param $hit
  79. * The blast XML hit object. This object has the following keys based on the
  80. * XML: Hit_num, Hit_id, Hit_def, Hit_accession, Hit_len and Hit_hsps.
  81. * Furthermore, a linkout_id key has beek added that contains the part of the
  82. * Hit_def extracted using a regex provided when the blastdb node was created.
  83. * @param $info
  84. * Additional information that may be useful in creating a link-out. Includes:
  85. * - query_name: the name of the query sequence.
  86. * - score: the score of the blast hit.
  87. * - e-value: the e-value of the blast hit.
  88. * @param $options
  89. * Any additional options needed to determine the type of link-out. None are
  90. * supported by this particular link-out type.
  91. *
  92. * @return
  93. * The URL string to be linked to.
  94. */
  95. function tripal_blast_generate_linkout_link($url_prefix, $hit, $info, $options = array()) {
  96. if (isset($hit->{'linkout_id'})) {
  97. return $url_prefix . $hit->{'linkout_id'};
  98. }
  99. else {
  100. return FALSE;
  101. }
  102. }
  103. /**
  104. * Generate a GBrowse link-out with location information for a given hit.
  105. *
  106. * NOTE: Assumes the hit is a backbone feature in the GBrowse linked to.
  107. * Otherwise, the basic link can be used.
  108. *
  109. * @param $url_prefix
  110. * The URL prefix for the BLAST Database queried.
  111. * @param $hit
  112. * The blast XML hit object. This object has the following keys based on the
  113. * XML: Hit_num, Hit_id, Hit_def, Hit_accession, Hit_len and Hit_hsps.
  114. * Furthermore, a linkout_id key has beek added that contains the part of the
  115. * Hit_def extracted using a regex provided when the blastdb node was created.
  116. * @param $info
  117. * Additional information that may be useful in creating a link-out. Includes:
  118. * - query_name: the name of the query sequence.
  119. * - score: the score of the blast hit.
  120. * - e-value: the e-value of the blast hit.
  121. * @param $options
  122. * Any additional options needed to determine the type of link-out. None are
  123. * supported by this particular link-out type.
  124. *
  125. * @return
  126. * The URL string to be linked to.
  127. */
  128. function tripal_blast_generate_linkout_gbrowse($url_prefix, $hit, $info, $options = array()) {
  129. $ranges = array();
  130. $coords = array();
  131. foreach($info['HSPs'] as $hsp) {
  132. $start = min($hsp['Hsp_hit-from'], $hsp['Hsp_hit-to']);
  133. $stop = max($hsp['Hsp_hit-from'], $hsp['Hsp_hit-to']);
  134. array_push($ranges, "$start..$stop");
  135. array_push($coords, $start, $stop);
  136. }
  137. $min = min($coords);
  138. $max = max($coords);
  139. $joined_ranges = join ("," , $ranges);
  140. $track_name = $hit->{'hit_name'} . '_' . $info['query_name'] . '_' . $info['e-value'];
  141. $url_postfix = 'query=';
  142. $url_postfix .= 'start=' . $min . ';stop=' . $max;
  143. $url_protfix .= ';ref=' . $hit->{'hit_name'};
  144. $url_postfix .= ';add=' . $hit->{'hit_name'} . '+BLAST+Query+' . $joined_ranges;
  145. $url_postfix .= ';h_feat=Query';
  146. return $url_prefix . $url_postfix;
  147. }
  148. /**
  149. * Generate a JBrowse link-out with location information for a given hit.
  150. *
  151. * NOTE: Assumes the hit is a backbone feature in the JBrowse linked to.
  152. * Otherwise, the basic link can be used.
  153. *
  154. * @param $url_prefix
  155. * The URL prefix for the BLAST Database queried.
  156. * @param $hit
  157. * The blast XML hit object. This object has the following keys based on the
  158. * XML: Hit_num, Hit_id, Hit_def, Hit_accession, Hit_len and Hit_hsps.
  159. * Furthermore, the following keys have been added:
  160. * -linkout_id: the part of the Hit_def extracted using a regex provided
  161. * when the blastdb node was created.
  162. * -hit_name: the name of the hit extracted in the template.
  163. * @param $info
  164. * Additional information that may be useful in creating a link-out. Includes:
  165. * - query_name: the name of the query sequence.
  166. * - score: the score of the blast hit.
  167. * - e-value: the e-value of the blast hit.
  168. * @param $options
  169. * Any additional options needed to determine the type of link-out. None are
  170. * supported by this particular link-out type.
  171. *
  172. * @return
  173. * The URL string to be linked to.
  174. */
  175. function tripal_blast_generate_linkout_jbrowse($url_prefix, $hit, $info, $options = array()) {
  176. $ranges = array();
  177. $coords = array();
  178. $hsps = array();
  179. foreach($info['HSPs'] as $hsp) {
  180. $hsp_start = $hsp['Hsp_hit-from'];
  181. $hsp_end = $hsp['Hsp_hit-to'] ;
  182. array_push($coords,$hsp['Hsp_hit-from'] , $hsp['Hsp_hit-to'] );
  183. array_push($ranges, '{"start":'.$hsp_start.',"end":'.$hsp_end.',"type":"match_part"}');
  184. }
  185. $min = min($coords);
  186. $max = max($coords);
  187. $url_postfix = '&addFeatures=[{"seq_id":"'.$hit->{'hit_name'}.'","score":"'.$info['e-value'].'","start":'.$min.',"end":'.$max.',"type":"match","name":"MyBLASTHit","subfeatures":[';
  188. $joined_ranges = join ("," , $ranges);
  189. $url_postfix = $url_postfix . $joined_ranges . ']}]&addTracks=[{"label":"BLAST","type":"JBrowse/View/Track/HTMLFeatures","store":"url"}]&loc='.$hit->{'hit_name'}.'&tracks=DNA%2CBLAST&highlight=';
  190. return $url_prefix . $url_postfix;
  191. }