blast_ui.linkouts.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. $types['custom'] = array(
  69. 'name' => 'Custom',
  70. 'process function' => 'tripal_custom_generate_linkout',
  71. );
  72. return $types;
  73. }
  74. /**
  75. * Generate a basic link-out for a given hit.
  76. *
  77. * Essentially, concatenate the URL prefix with the extracted hit identifier
  78. * and return the URL to be displayed by the BLAST report template.
  79. *
  80. * @param $url_prefix
  81. * The URL prefix for the BLAST Database queried.
  82. * @param $hit
  83. * The blast XML hit object. This object has the following keys based on the
  84. * XML: Hit_num, Hit_id, Hit_def, Hit_accession, Hit_len and Hit_hsps.
  85. * Furthermore, a linkout_id key has beek added that contains the part of the
  86. * Hit_def extracted using a regex provided when the blastdb node was created.
  87. * @param $info
  88. * Additional information that may be useful in creating a link-out. Includes:
  89. * - query_name: the name of the query sequence.
  90. * - score: the score of the blast hit.
  91. * - e-value: the e-value of the blast hit.
  92. * @param $options
  93. * Any additional options needed to determine the type of link-out. None are
  94. * supported by this particular link-out type.
  95. *
  96. * @return
  97. * The URL string to be linked to.
  98. */
  99. function tripal_blast_generate_linkout_link($url_prefix, $hit, $info, $options = array()) {
  100. if (isset($hit->{'linkout_id'})) {
  101. return $url_prefix . $hit->{'linkout_id'};
  102. }
  103. else {
  104. return FALSE;
  105. }
  106. }
  107. /**
  108. * Generate a GBrowse link-out with location information for a given hit.
  109. *
  110. * NOTE: Assumes the hit is a backbone feature in the GBrowse linked to.
  111. * Otherwise, the basic link can be used.
  112. *
  113. * @param $url_prefix
  114. * The URL prefix for the BLAST Database queried.
  115. * @param $hit
  116. * The blast XML hit object. This object has the following keys based on the
  117. * XML: Hit_num, Hit_id, Hit_def, Hit_accession, Hit_len and Hit_hsps.
  118. * Furthermore, a linkout_id key has beek added that contains the part of the
  119. * Hit_def extracted using a regex provided when the blastdb node was created.
  120. * @param $info
  121. * Additional information that may be useful in creating a link-out. Includes:
  122. * - query_name: the name of the query sequence.
  123. * - score: the score of the blast hit.
  124. * - e-value: the e-value of the blast hit.
  125. * @param $options
  126. * Any additional options needed to determine the type of link-out. None are
  127. * supported by this particular link-out type.
  128. *
  129. * @return
  130. * The URL string to be linked to.
  131. */
  132. function tripal_blast_generate_linkout_gbrowse($url_prefix, $hit, $info, $options = array()) {
  133. $ranges = array();
  134. $coords = array();
  135. foreach($info['HSPs'] as $hsp) {
  136. $start = min($hsp['Hsp_hit-from'], $hsp['Hsp_hit-to']);
  137. $stop = max($hsp['Hsp_hit-from'], $hsp['Hsp_hit-to']);
  138. array_push($ranges, "$start..$stop");
  139. array_push($coords, $start, $stop);
  140. }
  141. $min = min($coords);
  142. $max = max($coords);
  143. $joined_ranges = join ("," , $ranges);
  144. $track_name = $hit->{'hit_name'} . '_' . $info['query_name'] . '_' . $info['e-value'];
  145. $url_postfix = 'query=';
  146. $url_postfix .= 'start=' . $min . ';stop=' . $max;
  147. $url_postfix .= ';ref=' . $hit->{'hit_name'};
  148. $url_postfix .= ';add=' . $hit->{'hit_name'} . '+BLAST+Query+' . $joined_ranges;
  149. $url_postfix .= ';h_feat=Query';
  150. return $url_prefix . $url_postfix;
  151. }
  152. /**
  153. * Generate a JBrowse link-out with location information for a given hit.
  154. *
  155. * NOTE: Assumes the hit is a backbone feature in the JBrowse linked to.
  156. * Otherwise, the basic link can be used.
  157. *
  158. * @param $url_prefix
  159. * The URL prefix for the BLAST Database queried.
  160. * @param $hit
  161. * The blast XML hit object. This object has the following keys based on the
  162. * XML: Hit_num, Hit_id, Hit_def, Hit_accession, Hit_len and Hit_hsps.
  163. * Furthermore, the following keys have been added:
  164. * -linkout_id: the part of the Hit_def extracted using a regex provided
  165. * when the blastdb node was created.
  166. * -hit_name: the name of the hit extracted in the template.
  167. * @param $info
  168. * Additional information that may be useful in creating a link-out. Includes:
  169. * - query_name: the name of the query sequence.
  170. * - score: the score of the blast hit.
  171. * - e-value: the e-value of the blast hit.
  172. * @param $options
  173. * Any additional options needed to determine the type of link-out. None are
  174. * supported by this particular link-out type.
  175. *
  176. * @return
  177. * The URL string to be linked to.
  178. */
  179. function tripal_blast_generate_linkout_jbrowse($url_prefix, $hit, $info, $options = array()) {
  180. $ranges = array();
  181. $coords = array();
  182. $hsps = array();
  183. foreach($info['HSPs'] as $hsp) {
  184. $hsp_start = $hsp['Hsp_hit-from'];
  185. $hsp_end = $hsp['Hsp_hit-to'] ;
  186. array_push($coords,$hsp['Hsp_hit-from'] , $hsp['Hsp_hit-to'] );
  187. array_push($ranges, '{"start":'.$hsp_start.',"end":'.$hsp_end.',"type":"match_part"}');
  188. }
  189. $min = min($coords);
  190. $max = max($coords);
  191. $url_postfix = '&addFeatures=[{"seq_id":"'.$hit->{'hit_name'}.'","score":"'.$info['e-value'].'","start":'.$min.',"end":'.$max.',"type":"match","name":"MyBLASTHit","subfeatures":[';
  192. $joined_ranges = join ("," , $ranges);
  193. $url_postfix = $url_postfix . $joined_ranges . ']}]&addTracks=[{"label":"BLAST","type":"JBrowse/View/Track/HTMLFeatures","store":"url"}]&loc='.$hit->{'hit_name'}.'&tracks=DNA%2CBLAST&highlight=';
  194. return $url_prefix . $url_postfix;
  195. }