tripal_feature.module 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup tripal_feature Feature Module
  8. * @ingroup tripal_modules
  9. * @{
  10. * Provides functions for managing chado features including creating details pages for each feature
  11. * @}
  12. */
  13. require_once "api/tripal_feature.api.inc";
  14. require_once "api/tripal_feature.schema.api.inc";
  15. require_once "includes/tripal_feature.admin.inc";
  16. require_once "includes/tripal_feature.sync_features.inc";
  17. require_once "includes/tripal_feature.fasta_loader.inc";
  18. require_once "includes/tripal_feature.gff_loader.inc";
  19. require_once "includes/tripal_feature.seq_extract.inc";
  20. require_once "includes/tripal_feature.delete.inc";
  21. require_once "includes/tripal_feature.form.inc";
  22. /**
  23. *
  24. * @ingroup tripal_feature
  25. */
  26. function tripal_feature_init() {
  27. drupal_add_css(drupal_get_path('module', 'tripal_feature') . '/theme/css/tripal_feature.css');
  28. drupal_add_js(drupal_get_path('module', 'tripal_feature') . '/theme/js/tripal_feature.js');
  29. }
  30. /**
  31. * Implements hook_views_api()
  32. *
  33. * Purpose: Essentially this hook tells drupal that there is views support for
  34. * for this module which then includes tripal_db.views.inc where all the
  35. * views integration code is
  36. *
  37. * @ingroup tripal_feature
  38. */
  39. function tripal_feature_views_api() {
  40. return array(
  41. 'api' => 2.0,
  42. );
  43. }
  44. /**
  45. * Display help and module information
  46. *
  47. * @param
  48. * path which path of the site we're displaying help
  49. * @param
  50. * arg array that holds the current path as would be returned from arg() function
  51. *
  52. * @return
  53. * help text for the path
  54. *
  55. * @ingroup tripal_feature
  56. */
  57. function tripal_feature_help($path, $arg) {
  58. $output = '';
  59. switch ($path) {
  60. case "admin/help#tripal_feature":
  61. $output='<p>' . t("Displays links to nodes created on this date") . '</p>';
  62. break;
  63. }
  64. return $output;
  65. }
  66. /**
  67. * Provide information to drupal about the node types that we're creating
  68. * in this module
  69. *
  70. * @ingroup tripal_feature
  71. */
  72. function tripal_feature_node_info() {
  73. $nodes = array();
  74. $nodes['chado_feature'] = array(
  75. 'name' => t('Feature'),
  76. 'base' => 'chado_feature',
  77. 'description' => t('A feature from the chado database'),
  78. 'has_title' => FALSE,
  79. 'title_label' => t('Feature'),
  80. 'has_body' => FALSE,
  81. 'body_label' => t('Feature Description'),
  82. 'locked' => TRUE
  83. );
  84. return $nodes;
  85. }
  86. /**
  87. * Set the permission types that the chado module uses. Essentially we
  88. * want permissionis that protect creation, editing and deleting of chado
  89. * data objects
  90. *
  91. * @ingroup tripal_feature
  92. */
  93. function tripal_feature_permissions() {
  94. return array(
  95. 'access chado_feature content' => array(
  96. 'title' => t('View Features'),
  97. 'description' => t('Allow users to view feature pages.'),
  98. ),
  99. 'create chado_feature content' => array(
  100. 'title' => t('Create Features'),
  101. 'description' => t('Allow users to create new feature pages.'),
  102. ),
  103. 'delete chado_feature content' => array(
  104. 'title' => t('Delete Features'),
  105. 'description' => t('Allow users to delete feature pages.'),
  106. ),
  107. 'edit chado_feature content' => array(
  108. 'title' => t('Edit Features'),
  109. 'description' => t('Allow users to edit feature pages.'),
  110. ),
  111. 'adminster tripal feature' => array(
  112. 'title' => t('Administer Features'),
  113. 'description' => t('Allow users to administer all features.'),
  114. ),
  115. );
  116. }
  117. /**
  118. * Implement hook_access().
  119. *
  120. * This hook allows node modules to limit access to the node types they define.
  121. *
  122. * @param $node
  123. * The node on which the operation is to be performed, or, if it does not yet exist, the
  124. * type of node to be created
  125. *
  126. * @param $op
  127. * The operation to be performed
  128. *
  129. * @param $account
  130. * A user object representing the user for whom the operation is to be performed
  131. *
  132. * @return
  133. * If the permission for the specified operation is not set then return FALSE. If the
  134. * permission is set then return NULL as this allows other modules to disable
  135. * access. The only exception is when the $op == 'create'. We will always
  136. * return TRUE if the permission is set.
  137. *
  138. * @ingroup tripal_feature
  139. */
  140. function chado_feature_node_access($node, $op, $account) {
  141. if ($op == 'create') {
  142. if (!user_access('create chado_feature content', $account)) {
  143. return FALSE;
  144. }
  145. return TRUE;
  146. }
  147. if ($op == 'update') {
  148. if (!user_access('edit chado_feature content', $account)) {
  149. return FALSE;
  150. }
  151. }
  152. if ($op == 'delete') {
  153. if (!user_access('delete chado_feature content', $account)) {
  154. return FALSE;
  155. }
  156. }
  157. if ($op == 'view') {
  158. if (!user_access('access chado_feature content', $account)) {
  159. return FALSE;
  160. }
  161. }
  162. return NULL;
  163. }
  164. /**
  165. * Menu items are automatically added for the new node types created
  166. * by this module to the 'Create Content' Navigation menu item. This function
  167. * adds more menu items needed for this module.
  168. *
  169. * @ingroup tripal_feature
  170. */
  171. function tripal_feature_menu() {
  172. $items = array();
  173. // the administative settings menu
  174. $items['find/sequences'] = array(
  175. 'title' => 'Sequence Retrieval',
  176. 'description' => 'Download a file of sequences',
  177. 'page callback' => 'tripal_feature_seq_extract_page',
  178. 'access arguments' => array('access chado_feature content'),
  179. 'type' => MENU_CALLBACK,
  180. );
  181. $items['find/sequences/ajax'] = array(
  182. 'title' => 'Sequence Retrieval',
  183. 'page callback' => 'tripal_feature_seq_extract_form_ahah_update',
  184. 'access arguments' => array('access chado_feature content'),
  185. 'type' => MENU_CALLBACK,
  186. );
  187. // the administative settings menu
  188. $items['admin/tripal/chado/tripal_feature'] = array(
  189. 'title' => 'Features',
  190. 'description' => 'A biological sequence or a section of a biological sequence, or a collection of such sections.',
  191. 'page callback' => 'tripal_feature_admin_feature_view',
  192. 'access arguments' => array('administer tripal features'),
  193. 'type' => MENU_NORMAL_ITEM,
  194. );
  195. $items['admin/tripal/chado/tripal_feature/sync'] = array(
  196. 'title' => 'Sync',
  197. 'description' => 'Sync features from Chado with Drupal',
  198. 'page callback' => 'drupal_get_form',
  199. 'page arguments' => array('tripal_feature_sync_form'),
  200. 'access arguments' => array('administer tripal features'),
  201. 'type' => MENU_LOCAL_TASK,
  202. 'weight' => 0
  203. );
  204. $items['admin/tripal/chado/tripal_feature/delete'] = array(
  205. 'title' => ' Delete',
  206. 'description' => 'Delete multiple features from Chado',
  207. 'page callback' => 'drupal_get_form',
  208. 'page arguments' => array('tripal_feature_delete_form'),
  209. 'access arguments' => array('administer tripal features'),
  210. 'type' => MENU_LOCAL_TASK,
  211. 'weight' => 2
  212. );
  213. $items['admin/tripal/chado/tripal_feature/configuration'] = array(
  214. 'title' => 'Settings',
  215. 'description' => 'Configure the Tripal Feature module.',
  216. 'page callback' => 'drupal_get_form',
  217. 'page arguments' => array('tripal_feature_admin'),
  218. 'access arguments' => array('administer tripal features'),
  219. 'type' => MENU_LOCAL_TASK,
  220. 'weight' => 5
  221. );
  222. $items['admin/tripal/chado/tripal_feature/help'] = array(
  223. 'title' => 'Help',
  224. 'description' => 'Help with the Tripal Feature module.',
  225. 'page callback' => 'theme',
  226. 'page arguments' => array('tripal_feature_help'),
  227. 'access arguments' => array('administer tripal features'),
  228. 'type' => MENU_LOCAL_TASK,
  229. 'weight' => 10
  230. );
  231. /** Loaders */
  232. $items['admin/tripal/loaders/fasta_loader'] = array(
  233. 'title' => 'Multi-FASTA file Loader',
  234. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  235. 'page callback' => 'drupal_get_form',
  236. 'page arguments' => array('tripal_feature_fasta_load_form'),
  237. 'access arguments' => array('administer tripal features'),
  238. 'type' => MENU_NORMAL_ITEM,
  239. );
  240. $items['admin/tripal/loaders/gff3_load'] = array(
  241. 'title' => 'GFF3 file Loader',
  242. 'description' => 'Import a GFF3 file into Chado',
  243. 'page callback' => 'drupal_get_form',
  244. 'page arguments' => array('tripal_feature_gff3_load_form'),
  245. 'access arguments' => array('administer tripal features'),
  246. 'type' => MENU_NORMAL_ITEM,
  247. );
  248. // the menu link for addressing any feature (by name, uniquename, synonym)
  249. $items['feature/%'] = array(
  250. 'page callback' => 'tripal_feature_match_features_page',
  251. 'page arguments' => array(1),
  252. 'access arguments' => array('access chado_feature content'),
  253. 'type' => MENU_LOCAL_TASK,
  254. );
  255. // Enable admin view
  256. $items['admin/tripal/chado/tripal_feature/views/features/enable'] = array(
  257. 'title' => 'Enable feature Administrative View',
  258. 'page callback' => 'tripal_views_admin_enable_view',
  259. 'page arguments' => array('tripal_feature_admin_features', 'admin/tripal/chado/tripal_feature'),
  260. 'access arguments' => array('administer tripal_bulk_loader'),
  261. 'type' => MENU_CALLBACK,
  262. );
  263. return $items;
  264. }
  265. /**
  266. * We need to let drupal know about our theme functions and their arguments.
  267. * We create theme functions to allow users of the module to customize the
  268. * look and feel of the output generated in this module
  269. *
  270. * @ingroup tripal_feature
  271. */
  272. function tripal_feature_theme($existing, $type, $theme, $path) {
  273. $core_path = drupal_get_path('module', 'tripal_core');
  274. $items = array(
  275. 'node__chado_feature' => array(
  276. 'template' => 'node--chado-generic',
  277. 'render element' => 'node',
  278. 'base hook' => 'node',
  279. 'path' => "$core_path/theme",
  280. ),
  281. 'tripal_feature_alignments' => array(
  282. 'variables' => array('node' => NULL),
  283. 'template' => 'tripal_feature_alignments',
  284. 'path' => "$path/theme/tripal_feature",
  285. ),
  286. 'tripal_feature_analyses' => array(
  287. 'variables' => array('node' => NULL),
  288. 'template' => 'tripal_feature_analyses',
  289. 'path' => "$path/theme/tripal_feature",
  290. ),
  291. 'tripal_feature_base' => array(
  292. 'variables' => array('node' => NULL),
  293. 'template' => 'tripal_feature_base',
  294. 'path' => "$path/theme/tripal_feature",
  295. ),
  296. 'tripal_feature_featurepos' => array(
  297. 'arguments' => array('node' => NULL),
  298. 'template' => 'tripal_feature_featurepos',
  299. 'path' => "$path/theme/tripal_feature",
  300. ),
  301. 'tripal_feature_sequence' => array(
  302. 'variables' => array('node' => NULL),
  303. 'template' => 'tripal_feature_sequence',
  304. 'path' => "$path/theme/tripal_feature",
  305. ),
  306. 'tripal_feature_proteins' => array(
  307. 'variables' => array('node' => NULL),
  308. 'template' => 'tripal_feature_proteins',
  309. 'path' => "$path/theme/tripal_feature",
  310. ),
  311. 'tripal_feature_synonyms' => array(
  312. 'variables' => array('node' => NULL),
  313. 'template' => 'tripal_feature_synonyms',
  314. 'path' => "$path/theme/tripal_feature",
  315. ),
  316. 'tripal_feature_phenotypes' => array(
  317. 'variables' => array('node' => NULL),
  318. 'template' => 'tripal_feature_phenotypes',
  319. 'path' => "$path/theme/tripal_feature",
  320. ),
  321. 'tripal_feature_references' => array(
  322. 'variables' => array('node' => NULL),
  323. 'template' => 'tripal_feature_references',
  324. 'path' => "$path/theme/tripal_feature",
  325. ),
  326. 'tripal_feature_properties' => array(
  327. 'variables' => array('node' => NULL),
  328. 'template' => 'tripal_feature_properties',
  329. 'path' => "$path/theme/tripal_feature",
  330. ),
  331. 'tripal_feature_terms' => array(
  332. 'variables' => array('node' => NULL),
  333. 'template' => 'tripal_feature_terms',
  334. 'path' => "$path/theme/tripal_feature",
  335. ),
  336. 'tripal_feature_relationships' => array(
  337. 'variables' => array('node' => NULL),
  338. 'template' => 'tripal_feature_relationships',
  339. 'path' => "$path/theme/tripal_feature",
  340. ),
  341. 'tripal_feature_help' => array(
  342. 'template' => 'tripal_feature_help',
  343. 'variables' => array(NULL),
  344. 'path' => drupal_get_path('module', 'tripal_feature') . '/theme'
  345. ),
  346. // template for the organism page
  347. 'tripal_organism_feature_browser' => array(
  348. 'variables' => array('node' => NULL),
  349. 'template' => 'tripal_organism_feature_browser',
  350. 'path' => "$path/theme/tripal_organism",
  351. ),
  352. 'tripal_organism_feature_counts' => array(
  353. 'variables' => array('node' => NULL),
  354. 'template' => 'tripal_organism_feature_counts',
  355. 'path' => "$path/theme/tripal_organism",
  356. ),
  357. // themed forms
  358. 'tripal_feature_seq_extract_form' => array(
  359. 'arguments' => array('form'),
  360. ),
  361. // themed teaser
  362. 'tripal_feature_teaser' => array(
  363. 'variables' => array('node' => NULL),
  364. 'template' => 'tripal_feature_teaser',
  365. 'path' => "$path/theme/tripal_feature",
  366. ),
  367. );
  368. return $items;
  369. }
  370. /**
  371. *
  372. *
  373. * @ingroup tripal_feature
  374. */
  375. function tripal_feature_block_info() {
  376. $blocks['references']['info'] = t('Tripal Feature References');
  377. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  378. $blocks['base']['info'] = t('Tripal Feature Details');
  379. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  380. $blocks['sequence']['info'] = t('Tripal Feature Sequence');
  381. $blocks['sequence']['cache'] = BLOCK_NO_CACHE;
  382. $blocks['featureloc_sequences']['info'] = t('Tripal Feature Annotated Sequence');
  383. $blocks['featureloc_sequences']['cache'] = BLOCK_NO_CACHE;
  384. $blocks['synonyms']['info'] = t('Tripal Feature Synonyms');
  385. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  386. $blocks['properties']['info'] = t('Tripal Feature Properties');
  387. $blocks['properties']['cache'] = BLOCK_NO_CACHE;;
  388. $blocks['terms']['info'] = t('Tripal Annotated Terms');
  389. $blocks['terms']['cache'] = BLOCK_NO_CACHE;;
  390. $blocks['alignments']['info'] = t('Tripal Feature Alignments');
  391. $blocks['alignments']['cache'] = BLOCK_NO_CACHE;
  392. $blocks['relationships']['info'] = t('Tripal Feature Relationships');
  393. $blocks['relationships']['cache'] = BLOCK_NO_CACHE;
  394. $blocks['org_feature_counts']['info'] = t('Tripal Organism Feature Counts');
  395. $blocks['org_feature_counts']['cache'] = BLOCK_NO_CACHE;
  396. $blocks['org_feature_browser']['info'] = t('Tripal Organism Feature Browser');
  397. $blocks['org_feature_browser']['cache'] = BLOCK_NO_CACHE;
  398. return $blocks;
  399. }
  400. /**
  401. *
  402. *
  403. * @ingroup tripal_feature
  404. */
  405. function tripal_feature_block_view($delta = '') {
  406. if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  407. $nid = arg(1);
  408. $node = node_load($nid);
  409. $block = array();
  410. switch ($delta) {
  411. case 'references':
  412. $block['subject'] = t('References');
  413. $block['content'] = theme('tripal_feature_references', $node);
  414. break;
  415. case 'base':
  416. $block['subject'] = t('Feature Details');
  417. $block['content'] = theme('tripal_feature_base', $node);
  418. break;
  419. case 'synonyms':
  420. $block['subject'] = t('Synonyms');
  421. $block['content'] = theme('tripal_feature_synonyms', $node);
  422. break;
  423. case 'properties':
  424. $block['subject'] = t('Properties');
  425. $block['content'] = theme('tripal_feature_properties', $node);
  426. break;
  427. case 'terms':
  428. $block['subject'] = t('Annotated Terms');
  429. $block['content'] = theme('tripal_feature_terms', $node);
  430. break;
  431. case 'sequence':
  432. $block['subject'] = t('Sequence');
  433. $block['content'] = theme('tripal_feature_sequence', $node);
  434. break;
  435. case 'featureloc_sequences':
  436. $block['subject'] = t('Formatted Sequences');
  437. $block['content'] = theme('tripal_feature_featureloc_sequences', $node);
  438. break;
  439. case 'alignments':
  440. $block['subject'] = t('Alignments');
  441. $block['content'] = theme('tripal_feature_alignments', $node);
  442. break;
  443. case 'relationships':
  444. $block['subject'] = t('Relationships');
  445. $block['content'] = theme('tripal_feature_relationships', $node);
  446. break;
  447. case 'org_feature_counts':
  448. $block['subject'] = t('Feature Type Summary');
  449. $block['content'] = theme('tripal_organism_feature_counts', array('node' => $node));
  450. break;
  451. case 'org_feature_browser':
  452. $block['subject'] = t('Feature Browser');
  453. $block['content'] = theme('tripal_organism_feature_browser', array('node' => $node));
  454. break;
  455. case 'library_feature_browser':
  456. $block['subject'] = t('Library Feature Browser');
  457. $block['content'] = theme('tripal_library_feature_browser', $node);
  458. break;
  459. case 'analysis_feature_browser':
  460. $block['subject'] = t('Analysis Feature Browser');
  461. $block['content'] = theme('tripal_analysis_feature_browser', $node);
  462. break;
  463. default :
  464. }
  465. return $block;
  466. }
  467. }
  468. /**
  469. * When a new chado_feature node is created we also need to add information
  470. * to our chado_feature table. This function is called on insert of a new node
  471. * of type 'chado_feature' and inserts the necessary information.
  472. *
  473. * @ingroup tripal_feature
  474. */
  475. function chado_feature_insert($node) {
  476. $node->uniquename = trim($node->uniquename);
  477. $node->fname = trim($node->fname);
  478. $node->feature_type = trim($node->feature_type);
  479. $node->residues = trim($node->residues);
  480. // remove spaces, newlines from residues
  481. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  482. $obsolete = 'FALSE';
  483. if ($node->is_obsolete) {
  484. $obsolete = 'TRUE';
  485. }
  486. $feature_id = '';
  487. // if there is an feature_id in the $node object then this must be a sync so
  488. // we can skip adding the feature as it is already there, although
  489. // we do need to proceed with the rest of the insert
  490. if (!property_exists($node, 'feature_id')) {
  491. $values = array(
  492. 'organism_id' => $node->organism_id,
  493. 'name' => $node->fname,
  494. 'uniquename' => $node->uniquename,
  495. 'residues' => $residues,
  496. 'seqlen' => drupal_strlen($residues),
  497. 'is_obsolete' => $obsolete,
  498. 'type_id' => $type[0]->cvterm_id,
  499. 'md5checksum' => md5($residues)
  500. );
  501. $feature = tripal_core_chado_select('feature', array('*'), $values);
  502. if (!$feature) {
  503. drupal_set_message(t('Unable to add feature.'), 'warning');
  504. watchdog('tripal_feature', 'Insert feature: Unable to create feature where values: %values',
  505. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  506. return;
  507. }
  508. $feature_id = $feature->feature_id;
  509. // add the genbank accession and synonyms
  510. chado_feature_add_synonyms($node->synonyms, $node->feature_id);
  511. }
  512. else {
  513. $feature_id = $node->feature_id;
  514. }
  515. // Make sure the entry for this feature doesn't already exist in the
  516. // chado_feature table if it doesn't exist then we want to add it.
  517. $check_org_id = chado_get_id_for_node('feature', $node->nid);
  518. if (!$check_org_id) {
  519. $record = new stdClass();
  520. $record->nid = $node->nid;
  521. $record->vid = $node->vid;
  522. $record->feature_id = $feature_id;
  523. drupal_write_record('chado_feature', $record);
  524. }
  525. }
  526. /**
  527. *
  528. *
  529. * @ingroup tripal_feature
  530. */
  531. function chado_feature_update($node) {
  532. $node->uniquename = trim($node->uniquename);
  533. $node->fname = trim($node->fname);
  534. $node->feature_type = trim($node->feature_type);
  535. $node->residues = trim($node->residues);
  536. if ($node->revision) {
  537. // there is no way to handle revisions in Chado but leave
  538. // this here just to make not we've addressed it.
  539. }
  540. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  541. $obsolete = 'FALSE';
  542. if ($node->is_obsolete) {
  543. $obsolete = 'TRUE';
  544. }
  545. // get the feature type id
  546. $values = array(
  547. 'cv_id' => array(
  548. 'name' => 'sequence'
  549. ),
  550. 'name' => $node->feature_type
  551. );
  552. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  553. $feature_id = chado_get_id_for_node('feature', $node->nid) ;
  554. if (sizeof($type) > 0) {
  555. $match = array(
  556. 'feature_id' => $feature_id,
  557. );
  558. $values = array(
  559. 'organism_id' => $node->organism_id,
  560. 'name' => $node->fname,
  561. 'uniquename' => $node->uniquename,
  562. 'residues' => $residues,
  563. 'seqlen' => drupal_strlen($residues),
  564. 'is_obsolete' => $obsolete,
  565. 'type_id' => $type[0]->cvterm_id,
  566. 'md5checksum' => md5($residues)
  567. );
  568. $options = array('return_record' => TRUE);
  569. $status = tripal_core_chado_update('feature', $match, $values, $options);
  570. // add the genbank synonyms
  571. chado_feature_add_synonyms($node->synonyms, $feature_id);
  572. }
  573. else {
  574. drupal_set_message(t('Unable to update feature.'), 'warning');
  575. watchdog('tripal_feature',
  576. 'Update feature: Unable to update feature where values: %values',
  577. array('%values' => print_r($values, TRUE)),
  578. WATCHDOG_WARNING
  579. );
  580. }
  581. }
  582. /**
  583. *
  584. *
  585. * @ingroup tripal_feature
  586. */
  587. function chado_feature_delete($node) {
  588. $feature_id = chado_get_id_for_node('feature', $node->nid);
  589. // if we don't have a library id for this node then this isn't a node of
  590. // type chado_library or the entry in the chado_library table was lost.
  591. if (!$feature_id) {
  592. return;
  593. }
  594. // remove the drupal content
  595. $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
  596. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  597. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  598. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  599. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  600. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  601. // Remove data from feature tables of chado database. This will
  602. // cause a cascade delete and remove all data in referencing tables
  603. // for this feature
  604. chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  605. drupal_set_message(t("The feature and all associated data were removed from") .
  606. "chado");
  607. }
  608. /**
  609. *
  610. *
  611. * @ingroup tripal_feature
  612. */
  613. function chado_feature_add_synonyms($synonyms, $feature_id) {
  614. // separate synomys by carriage returns
  615. $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  616. // split the synonyms into an array based on a space as the delimieter
  617. $syn_array = array();
  618. $syn_array = explode(" ", $synonyms);
  619. // remove any old synonyms
  620. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  621. if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
  622. watchdog('tripal_feature', "Could not remove synonyms from feature. ", array(), WATCHDOG_ERROR);
  623. return;
  624. }
  625. // return if we don't have any synonmys to add
  626. if (!$synonyms) {
  627. return;
  628. }
  629. // iterate through each synonym and add it to the database
  630. foreach ($syn_array as $syn) {
  631. // skip this item if it's empty
  632. if (!$syn) {
  633. break;
  634. }
  635. // check to see if we have this accession number already in the database
  636. // if so then don't add it again. it messes up drupal if the insert fails.
  637. // It is possible for the accession number to be present and not the feature
  638. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  639. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  640. if (!$synonym) {
  641. $synonym_isql = "
  642. INSERT INTO {synonym} (name, synonym_sgml, type_id)
  643. VALUES (:name, :synonym_sgml,
  644. (SELECT cvterm_id
  645. FROM {cvterm} CVT
  646. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  647. WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
  648. )
  649. ";
  650. if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
  651. watchdog('tripal_feature', "Could not add synonym. ", array(), WATCHDOG_WARNING);
  652. return;
  653. }
  654. // now get the synonym we just added
  655. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  656. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  657. }
  658. // now add in our new sysnonym
  659. $feature_syn_isql = "
  660. INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
  661. VALUES (:synonym_id, :feature_id, :pub_id)";
  662. $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id'=> 1);
  663. if (!chado_query($feature_syn_isql, $args)) {
  664. watchdog('tripal_feature', "Could not associate synonym with feature. ", array(), WATCHDOG_WARNING);
  665. return;
  666. }
  667. }
  668. }
  669. /**
  670. *
  671. *
  672. * @ingroup tripal_feature
  673. */
  674. function chado_feature_add_gbaccession($accession, $feature_id) {
  675. // use chado database
  676. // remove any old accession from genbank dbEST
  677. $fdbxref_dsql = "
  678. DELETE FROM {feature_dbxref}
  679. WHERE feature_id = :feature_id and dbxref_id IN
  680. (SELECT DBX.dbxref_id
  681. FROM {dbxref} DBX
  682. INNER JOIN DB ON DB.db_id = DBX.db_id
  683. INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id
  684. WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = :feature_id
  685. )
  686. ";
  687. if (!chado_query($fdbxref_dsql, array(':feature_id' => $feature_id))) {
  688. $error .= "Could not remove accession from feature. ";
  689. }
  690. // if we don't have an accession number to add then just return
  691. if (!$accession) {
  692. return;
  693. }
  694. // get the db_id
  695. $db_sql = "SELECT db_id FROM {DB} WHERE name = 'DB:Genbank_est'";
  696. $db = chado_query($db_sql)->fetchObject();
  697. // check to see if we have this accession number already in the database
  698. // if so then don't add it again. it messes up drupal if the insert fails.
  699. // It is possible for the accession number to be present and not the feature
  700. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = :db_id and accession = :accession";
  701. $dbxref = chado_query($dbxref_sql, array(':db_id' => $db->db_id, ':accession' => $accession))->fetchObject();
  702. if (!$dbxref) {
  703. // add the accession number
  704. $dbxref_isql = "INSERT INTO {dbxref} (db_id, accession) VALUES (:db_id, :accession) ";
  705. if (!chado_query($dbxref_isql, array(':db_id' => $db->db_id, ':accession' => $accession))) {
  706. $error .= 'Could not add accession as a database reference ';
  707. }
  708. // get the dbxref_id for the just added accession number
  709. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = :db_id and accession = :accession";
  710. $dbxref = chado_query($dbxref_sql, array(':db_id' => $db->db_id, ':accession' => $accession))->fetchObject();
  711. }
  712. // associate the accession number with the feature
  713. $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id, dbxref_id) VALUES (:feature_id, :dbxref_id) ";
  714. if (!chado_query($feature_dbxref_isql, array(':feature_id' => $feature_id, ':dbxref_id' => $dbxref->dbxref_id))) {
  715. $error .= 'Could not add feature database reference. ';
  716. }
  717. return $error;
  718. }
  719. /**
  720. * When a node is requested by the user this function is called to allow us
  721. * to add auxiliary data to the node object.
  722. *
  723. * @ingroup tripal_feature
  724. */
  725. function chado_feature_load($nodes) {
  726. foreach ($nodes as $nid => $node) {
  727. // find the feature and add in the details
  728. $feature_id = chado_get_id_for_node('feature', $nid);
  729. // build the feature variable
  730. $values = array('feature_id' => $feature_id);
  731. $feature = tripal_core_generate_chado_var('feature', $values);
  732. $nodes[$nid]->feature = $feature;
  733. // by default, the titles are saved using the unique constraint. We will
  734. // keep it the same, but remove the duplicate name if the unique name and name
  735. // are identical. This doesn't change the title saved in the database, just what is shown
  736. // to the user on the page
  737. $title_type = variable_get('chado_feature_title', 'unique_constraint');
  738. if ($title_type == 'unique_constraint') {
  739. if (strcmp($feature->name, $feature->uniquename)==0) {
  740. $node->title = $feature->name . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  741. }
  742. // in previous version of Tripal, the feature title was simply the unique name.
  743. // so, we recreate the title just to be sure all of our feature pages are consistent
  744. else {
  745. $node->title = $feature->name . ", " . $feature->uniquename . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  746. }
  747. }
  748. // set the title to be the feature name or uniquename as configured
  749. if ($title_type == 'feature_name') {
  750. $node->title = $feature->name;
  751. }
  752. if ($title_type == 'feature_unique_name') {
  753. $node->title = $feature->uniquename;
  754. }
  755. }
  756. }
  757. /**
  758. *
  759. *
  760. * @ingroup tripal_feature
  761. */
  762. function tripal_feature_load_organism($organism_id) {
  763. // add organism details
  764. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  765. $organism = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  766. return $organism;
  767. }
  768. /**
  769. *
  770. *
  771. * @ingroup tripal_feature
  772. */
  773. function tripal_feature_load_synonyms($feature_id) {
  774. $sql = "
  775. SELECT S.name
  776. FROM {feature_synonym} FS
  777. INNER JOIN {synonym} S ON FS.synonym_id = S.Synonym_id
  778. WHERE FS.feature_id = :feature_id
  779. ORDER BY S.name
  780. ";
  781. $results = chado_query($sql, array(':feature_id' => $feature_id));
  782. $synonyms = array();
  783. $i=0;
  784. while ($synonym = $results->fetchObject()) {
  785. $synonyms[$i++] = $synonym;
  786. }
  787. return $synonyms;
  788. }
  789. /**
  790. *
  791. *
  792. * @ingroup tripal_feature
  793. */
  794. function tripal_feature_load_properties($feature_id) {
  795. $sql = "
  796. SELECT
  797. CVT.name as cvname, CVT.definition, CVT.is_obsolete,
  798. FS.type_id, FS.value, FS.rank,
  799. DBX.dbxref_id,DBX.accession,DB.name as dbname,
  800. DB.urlprefix, DB.description as db_description, DB.url
  801. FROM {featureprop} FS
  802. INNER JOIN {cvterm} CVT ON FS.type_id = CVT.cvterm_id
  803. INNER JOIN {dbxref} DBX ON CVT.dbxref_id = DBX.dbxref_id
  804. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  805. WHERE
  806. FS.feature_id = :feature_id
  807. ORDER BY FS.rank ASC
  808. ";
  809. $results = chado_query($sql, array(':feature_id' => $feature_id));
  810. $i=0;
  811. $properties = array();
  812. while ($property = $results->fetchObject()) {
  813. $properties[$i++] = $property;
  814. }
  815. return $properties;
  816. }
  817. /**
  818. *
  819. *
  820. * @ingroup tripal_feature
  821. */
  822. function tripal_feature_load_references($feature_id) {
  823. $sql = "
  824. SELECT
  825. F.uniquename, F.Feature_id,
  826. DB.description as dbdesc, DB.db_id, DB.name as db_name, DB.urlprefix,
  827. DBX.accession, DBX.dbxref_id
  828. FROM {feature} F
  829. INNER JOIN {feature_dbxref} FDBX ON F.feature_id = FDBX.feature_id
  830. INNER JOIN {dbxref} DBX ON DBX.dbxref_id = FDBX.dbxref_id
  831. INNER JOIN {db} ON DB.db_id = DBX.db_id
  832. WHERE
  833. F.feature_id = :feature_id
  834. ORDER BY DB.name
  835. ";
  836. $results = chado_query($sql, array(':feature_id' => $feature_id));
  837. $references = array();
  838. $i=0;
  839. while ($accession = $results->fetchObject()) {
  840. $references[$i++] = $accession;
  841. }
  842. return $references;
  843. }
  844. /**
  845. *
  846. *
  847. * @ingroup tripal_feature
  848. */
  849. function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  850. $sql = "
  851. SELECT
  852. F.name, F.feature_id, F.uniquename,
  853. FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
  854. CVT.name as cvname, CVT.cvterm_id,
  855. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  856. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
  857. FROM {featureloc} FL
  858. INNER JOIN {feature} F ON FL.feature_id = F.feature_id
  859. INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
  860. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  861. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  862. ";
  863. if (strcmp($side, 'as_parent')==0) {
  864. $sql .= "WHERE FL.srcfeature_id = :feature_id ";
  865. }
  866. if (strcmp($side, 'as_child')==0) {
  867. $sql .= "WHERE FL.feature_id = :feature_id ";
  868. }
  869. $flresults = chado_query($sql, array(':feature_id' => $feature_id));
  870. // copy the results into an array
  871. $i=0;
  872. $featurelocs = array();
  873. while ($loc = $flresults->fetchObject()) {
  874. // if a drupal node exists for this feature then add the nid to the
  875. // results object
  876. $loc->fnid = chado_get_node_id('feature', $loc->feature_id);
  877. $loc->snid = chado_get_node_id('feature', $loc->src_feature_id);
  878. // add the result to the array
  879. $featurelocs[$i++] = $loc;
  880. }
  881. // Add the relationship feature locs if aggregate is turned on
  882. if ($aggregate and strcmp($side, 'as_parent')==0) {
  883. // get the relationships for this feature without substituting any children
  884. // for the parent. We want all relationships
  885. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  886. foreach ($relationships as $rindex => $rel) {
  887. // get the featurelocs for each of the relationship features
  888. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  889. foreach ($rel_featurelocs as $findex => $rfloc) {
  890. $featurelocs[$i++] = $rfloc;
  891. }
  892. }
  893. }
  894. usort($featurelocs, 'tripal_feature_sort_locations');
  895. return $featurelocs;
  896. }
  897. /**
  898. * used to sort the feature locs by start position
  899. *
  900. * @ingroup tripal_feature
  901. */
  902. function tripal_feature_sort_locations($a, $b) {
  903. return strnatcmp($a->fmin, $b->fmin);
  904. }
  905. /**
  906. *
  907. *
  908. * @ingroup tripal_feature
  909. */
  910. function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
  911. // get the relationships for this feature. The query below is used for both
  912. // querying the object and subject relationships
  913. $sql = "
  914. SELECT
  915. FS.name as subject_name, FS.uniquename as subject_uniquename,
  916. CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
  917. FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
  918. CVT.name as rel_type,
  919. FO.name as object_name, FO.uniquename as object_uniquename,
  920. CVTO.name as object_type, CVTO.cvterm_id as object_type_id
  921. FROM {feature_relationship} FR
  922. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  923. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  924. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  925. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  926. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  927. ";
  928. if (strcmp($side, 'as_object')==0) {
  929. $sql .= " WHERE FR.object_id = :feature_id";
  930. }
  931. if (strcmp($side, 'as_subject')==0) {
  932. $sql .= " WHERE FR.subject_id = :feature_id";
  933. }
  934. $sql .= " ORDER BY FR.rank";
  935. // get the relationships
  936. $results = chado_query($sql, array(':feature_id' => $feature_id));
  937. // iterate through the relationships, put these in an array and add
  938. // in the Drupal node id if one exists
  939. $i=0;
  940. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  941. $relationships = array();
  942. while ($rel = $results->fetchObject()) {
  943. $node = db_query($nodesql, array(':feature_id' => $rel->subject_id))->fetchObject();
  944. if ($node) {
  945. $rel->subject_nid = $node->nid;
  946. }
  947. $node = db_query($nodesql, array(':feature_id' => $rel->object_id))->fetchObject();
  948. if ($node) {
  949. $rel->object_nid = $node->nid;
  950. }
  951. $relationships[$i++] = $rel;
  952. }
  953. return $relationships;
  954. }
  955. /**
  956. *
  957. *
  958. * @ingroup tripal_feature
  959. */
  960. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  961. $levels=0, $base_type_id=NULL, $depth=0) {
  962. // we only want to recurse to as many levels deep as indicated by the
  963. // $levels variable, but only if this variable is > 0. If 0 then we
  964. // recurse until we reach the end of the relationships tree.
  965. if ($levels > 0 and $levels == $depth) {
  966. return NULL;
  967. }
  968. // first get the relationships for this feature
  969. return tripal_feature_load_relationships($feature_id, 'as_object');
  970. }
  971. /**
  972. *
  973. *
  974. * @ingroup tripal_feature
  975. */
  976. function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
  977. // if we don't have any featurelocs then no point in continuing
  978. if (!$featurelocs) {
  979. return array();
  980. }
  981. // get the list of relationships (including any aggregators) and iterate
  982. // through each one to find information needed to color-code the reference sequence
  983. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  984. if (!$relationships) {
  985. return array();
  986. }
  987. // iterate through each of the realtionships features and get their
  988. // locations
  989. foreach ($relationships as $rindex => $rel) {
  990. // get the featurelocs for each of the relationship features
  991. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  992. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  993. // keep track of this unique source feature
  994. $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
  995. // copy over the results to the relationship object. Since there can
  996. // be more than one feature location for each relationship feature we
  997. // use the '$src' variable to keep track of these.
  998. $rel->featurelocs = new stdClass();
  999. $rel->featurelocs->$src = new stdClass();
  1000. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  1001. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  1002. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  1003. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  1004. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  1005. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  1006. // keep track of the individual parts for each relationship
  1007. $start = $rel->featurelocs->$src->fmin;
  1008. $end = $rel->featurelocs->$src->fmax;
  1009. $type = $rel->subject_type;
  1010. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  1011. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  1012. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  1013. }
  1014. }
  1015. // the featurelocs array provided to the function contains the locations
  1016. // where this feature is found. We want to get the sequence for each
  1017. // location and then annotate it with the parts found from the relationships
  1018. // locations determiend above.
  1019. $floc_sequences = array();
  1020. foreach ($featurelocs as $featureloc) {
  1021. // build the src name so we can keep track of the different parts for each feature
  1022. $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
  1023. // orient the parts to the beginning of the feature sequence
  1024. if (!empty($rel_locs[$src]['parts'])) {
  1025. $parts = $rel_locs[$src]['parts'];
  1026. $rparts = array(); // we will fill this up if we're on the reverse strand
  1027. foreach ($parts as $start => $types) {
  1028. foreach ($types as $type_name => $type) {
  1029. if ($featureloc->strand >= 0) {
  1030. // this is on the forward strand. We need to convert the start on the src feature to the
  1031. // start on this feature's sequence
  1032. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  1033. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  1034. $parts[$start][$type_name]['type'] = $type_name;
  1035. }
  1036. else {
  1037. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  1038. // begining of the reverse sequence
  1039. $size = ($featureloc->fmax - $featureloc->fmin);
  1040. $start_orig = $parts[$start][$type_name]['start'];
  1041. $end_orig = $parts[$start][$type_name]['end'];
  1042. $new_start = $size - ($end_orig - $featureloc->fmin);
  1043. $new_end = $size - ($start_orig - $featureloc->fmin);
  1044. $rparts[$new_start][$type_name]['start'] = $new_start;
  1045. $rparts[$new_start][$type_name]['end'] = $new_end;
  1046. $rparts[$new_start][$type_name]['type'] = $type_name;
  1047. }
  1048. }
  1049. }
  1050. // now sort the parts
  1051. // if we're on the reverse strand we need to resort
  1052. if ($featureloc->strand >= 0) {
  1053. usort($parts, 'tripal_feature_sort_rel_parts_by_start');
  1054. }
  1055. else {
  1056. usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
  1057. $parts = $rparts;
  1058. }
  1059. $floc_sequences[$src]['src'] = $src;
  1060. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  1061. $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
  1062. $start = $featureloc->fmin + 1;
  1063. $size = $featureloc->fmax - $featureloc->fmin;
  1064. // TODO: fix the hard coded $start and $size
  1065. // the $start and $size variables are hard-coded in the SQL statement
  1066. // because the db_query function places quotes around all placeholders
  1067. // (e.g. :start & :size) and screws up the substring function
  1068. $sql = "
  1069. SELECT substring(residues from $start for $size) as residues
  1070. FROM {feature}
  1071. WHERE feature_id = :feature_id
  1072. ";
  1073. $sequence = chado_query($sql, $args)->fetchObject();
  1074. $residues = $sequence->residues;
  1075. if ($featureloc->strand < 0) {
  1076. $residues = tripal_feature_reverse_complement($residues);
  1077. }
  1078. $strand = '.';
  1079. if ($featureloc->strand == 1) {
  1080. $strand = '+';
  1081. }
  1082. elseif ($featureloc->strand == -1) {
  1083. $strand = '-';
  1084. }
  1085. $defline = $featureloc->feature_id->name . " " . $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . " " . $strand;
  1086. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $defline);
  1087. }
  1088. }
  1089. return $floc_sequences;
  1090. }
  1091. /**
  1092. *
  1093. *
  1094. * @ingroup tripal_feature
  1095. */
  1096. function tripal_feature_get_matched_alignments($feature) {
  1097. // This function is for features that align through an intermediate such
  1098. // as 'EST_match' or 'match'. This occurs in the case where two sequences
  1099. // align but where one does not align perfectly. Some ESTs may be in a contig
  1100. // but not all of the EST. Portions may overhang and not be included in the
  1101. // consensus if quality is bad.
  1102. // For example:
  1103. //
  1104. // Feature 1: Contig --------------------
  1105. // Feature 2: EST_match -------
  1106. // Feature 3: EST ---------
  1107. //
  1108. // The feature provided to the function will always be the feature 1. The
  1109. // featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  1110. // alignment of feature 3 with feature 2
  1111. //
  1112. // Features may align to more than one feature and are not matches. We do
  1113. // not want to include these, so we have to filter on the SO terms:
  1114. // match, or %_match
  1115. //
  1116. $sql = "
  1117. SELECT
  1118. FL1.featureloc_id as left_featureloc_id,
  1119. FL1.srcfeature_id as left_srcfeature_id,
  1120. FL1.feature_id as left_feature_id,
  1121. FL1.fmin as left_fmin,
  1122. FL1.is_fmin_partial as left_is_fmin_partial,
  1123. FL1.fmax as left_fmax,
  1124. FL1.is_fmax_partial as left_is_fmax_partial,
  1125. FL1.strand as left_strand,
  1126. FL1.phase as left_phase,
  1127. FL1.locgroup as left_locgroup,
  1128. FL1.rank as left_rank,
  1129. FL2.featureloc_id as right_featureloc_id,
  1130. FL2.srcfeature_id as right_srcfeature_id,
  1131. FL2.feature_id as right_feature_id,
  1132. FL2.fmin as right_fmin,
  1133. FL2.is_fmin_partial as right_is_fmin_partial,
  1134. FL2.fmax as right_fmax,
  1135. FL2.is_fmax_partial as right_is_fmax_partial,
  1136. FL2.strand as right_strand,
  1137. FL2.phase as right_phase,
  1138. FL2.locgroup as right_locgroup,
  1139. FL2.rank as right_rank
  1140. FROM {feature} F1
  1141. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  1142. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  1143. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  1144. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  1145. WHERE
  1146. F1.feature_id = :feature_id AND
  1147. (CVT2.name = 'match' or CVT2.name like '%_match')
  1148. ORDER BY FL1.fmin
  1149. ";
  1150. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  1151. // iterate through the results and add them to our featurelocs array
  1152. $featurelocs = array();
  1153. while ($fl = $results->fetchObject()) {
  1154. // ignore featurelocs where the left and right srcfeature is the same
  1155. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  1156. continue;
  1157. }
  1158. $featurelocs[] = $fl ;
  1159. }
  1160. return $featurelocs;
  1161. }
  1162. /**
  1163. *
  1164. *
  1165. * @ingroup tripal_feature
  1166. */
  1167. function tripal_feature_load_organism_feature_counts($organism) {
  1168. // don't show the browser if the settings in the admin page is turned off
  1169. // instead return the array indicating the status of the browser
  1170. $show_counts = variable_get('tripal_feature_summary_setting', 'show_feature_summary');
  1171. if (strcmp($show_counts, 'show_feature_summary')!=0) {
  1172. return array('enabled' => FALSE );
  1173. }
  1174. $args = array();
  1175. $names = array();
  1176. $order = array();
  1177. // build the where clause for the SQL statement if we have a custom term list
  1178. // we'll also keep track of the names the admin provided (if any) and the
  1179. // order that the terms should appear.
  1180. $is_custom = 0;
  1181. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1182. $where = '';
  1183. if ($temp) {
  1184. $is_custom = 1;
  1185. $temp = explode("\n", $temp);
  1186. $i = 0;
  1187. foreach ($temp as $key => $value) {
  1188. // separate the key value pairs
  1189. $temp2 = explode("=", $value);
  1190. $feature_type = rtrim($temp2[0]);
  1191. $args[] = $feature_type;
  1192. $order[] = $feature_type;
  1193. // if a new name is provided then use that otherwise just
  1194. // use the feature type
  1195. if (count($temp2) == 2) {
  1196. $names[":name$i"] = rtrim($temp2[1]);
  1197. }
  1198. else {
  1199. $names[":name$i"] = $feature_type;
  1200. }
  1201. $where .= " OFC.feature_type = :name$i OR ";
  1202. $i++;
  1203. }
  1204. if ($where) {
  1205. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1206. $where = "($where) AND";
  1207. }
  1208. }
  1209. // get the feature counts. This is dependent on a materialized view
  1210. // installed with the organism module
  1211. $sql = "
  1212. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  1213. FROM {organism_feature_count} OFC
  1214. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  1215. WHERE $where organism_id = :organism_id
  1216. ORDER BY num_features desc
  1217. ";
  1218. $args[':organism_id'] = $organism->organism_id;
  1219. $org_features = chado_query($sql, $args);
  1220. // iterate through the types
  1221. $types = array();
  1222. while ($type = $org_features->fetchObject()) {
  1223. $types[$type->feature_type] = $type;
  1224. // if we don't have an order this means we didn't go through the loop
  1225. // above to set the names, so do that now
  1226. if (!$is_custom) {
  1227. $names[] = $type->feature_type;
  1228. $order[] = $type->feature_type;
  1229. }
  1230. }
  1231. # now reorder the types
  1232. $ordered_types = array();
  1233. foreach ($order as $type) {
  1234. $ordered_types[] = $types[$type];
  1235. }
  1236. return array( 'types' => $ordered_types, 'names' => $names, 'enabled' => TRUE );
  1237. }
  1238. /**
  1239. *
  1240. *
  1241. * @ingroup tripal_feature
  1242. */
  1243. function tripal_feature_load_organism_feature_browser($organism) {
  1244. if (!$organism) {
  1245. return array();
  1246. }
  1247. // don't show the browser if the settings in the admin page is turned off
  1248. // instead return the array indicating the status of the browser
  1249. $show_browser = variable_get('tripal_feature_browse_setting', 'show_feature_browser');
  1250. if (strcmp($show_browser, 'show_feature_browser')!=0) {
  1251. return array('enabled' => FALSE);
  1252. }
  1253. // get the list of available sequence ontology terms for which
  1254. // we will build drupal pages from features in chado. If a feature
  1255. // is not one of the specified typse we won't build a node for it.
  1256. $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
  1257. $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
  1258. $so_terms = split(' ', $allowed_types);
  1259. // perform the query
  1260. $values = array(
  1261. 'organism_id' => $organism->organism_id,
  1262. 'type_id' => array(
  1263. 'name' => $so_terms
  1264. ),
  1265. );
  1266. $columns = array('feature_id', 'name', 'uniquename', 'type_id');
  1267. $options = array(
  1268. 'pager' => array('limit' => 10, 'element' => 0),
  1269. 'order_by' => array('name' => 'ASC'),
  1270. );
  1271. $features = tripal_core_chado_select('feature', $columns, $values, $options);
  1272. $pager = theme('pager');
  1273. // add the node ids and types
  1274. $nsql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  1275. $tsql = "SELECT name FROM {cvterm} WHERE cvterm_id = :cvterm_id";
  1276. foreach ($features as $feature) {
  1277. $node = db_query($nsql, array(':feature_id' => $feature->feature_id))->fetchObject();
  1278. $type = chado_query($tsql, array(':cvterm_id' => $feature->type_id))->fetchObject();
  1279. $feature->nid = $node->nid;
  1280. $feature->type_name = $type->name;
  1281. }
  1282. return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
  1283. }
  1284. /**
  1285. * This generates the Feature Browser which can optionally be included on library pages
  1286. * and shows all features belonging to the given library. This Browse can be shown/hidden
  1287. * on the Feature Configuration page.
  1288. *
  1289. * @ingroup tripal_feature
  1290. */
  1291. function tripal_feature_load_library_feature_browser($library) {
  1292. // don't show the browser if the settings in the admin page is turned off
  1293. // instead return the array indicating the status of the browser
  1294. $show_browser = variable_get('tripal_library_feature_browse_setting', 'show_feature_browser');
  1295. if (strcmp($show_browser, 'show_feature_browser')!=0) {
  1296. return array('enabled' => FALSE);
  1297. }
  1298. // get a list of feature types to include in the browser
  1299. $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
  1300. $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
  1301. $so_terms = split(' ', $allowed_types);
  1302. $where_cvt = "";
  1303. $args = array();
  1304. $i = 0;
  1305. foreach ($so_terms as $term) {
  1306. $where_cvt .= "CVT.name = :cvtname$i OR ";
  1307. $args[':cvtname$i'] = $term;
  1308. $i++;
  1309. }
  1310. $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3); # strip trailing 'OR'
  1311. // get the features for this library
  1312. $sql = "
  1313. SELECT F.name, F.feature_id, F.uniquename, CVT.name as cvname
  1314. FROM {feature} F
  1315. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  1316. INNER JOIN {library_feature} LF ON F.feature_id = LF.feature_id
  1317. INNER JOIN {library} L ON LF.library_id = L.library_id
  1318. WHERE LF.library_id = :library_id and ($where_cvt)
  1319. ORDER BY feature_id ASC
  1320. ";
  1321. $args[':library_id'] = $library->libary_id;
  1322. // the counting SQL
  1323. $csql = "
  1324. SELECT count(*)
  1325. FROM {feature} F
  1326. INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id
  1327. INNER JOIN {library_feature} LF on F.feature_id = LF.feature_id
  1328. INNER JOIN {library} L on LF.library_id = L.library_id
  1329. WHERE LF.library_id = :library_id and ($where_cvt)
  1330. GROUP BY L.library_id ";
  1331. $org_features = chado_pager_query($sql, 10, 0, $csql, $args);
  1332. $pager = theme('pager');
  1333. // prepare the query that will lookup node ids
  1334. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  1335. $i=0;
  1336. $features = array();
  1337. while ($feature = $org_features->fetchObject()) {
  1338. $node = db_query($sql, array(':feature_id' => $feature->feature_id))->fetchObject();
  1339. $feature->nid = $node->nid;
  1340. $features[$i++] = $feature;
  1341. }
  1342. return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
  1343. }
  1344. /**
  1345. * This generates the Feature Browse which can optionally be included on library pages
  1346. * and shows all features belonging to the given library. This Browse can be shown/hidden
  1347. * on the Feature Configuration page.
  1348. *
  1349. * @ingroup tripal_feature
  1350. */
  1351. function tripal_feature_load_analysis_feature_browser($analysis) {
  1352. // don't show the browser if the settings in the admin page is turned off
  1353. // instead return the array indicating the status of the browser
  1354. $show_browser = variable_get('tripal_analysis_feature_browse_setting', 'show_feature_browser');
  1355. if (strcmp($show_browser, 'show_feature_browser')!=0) {
  1356. return array('enabled' => FALSE);
  1357. }
  1358. // get a list of feature types to include in the browser
  1359. $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
  1360. $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
  1361. $so_terms = split(' ', $allowed_types);
  1362. $where_cvt = "";
  1363. $i = 0;
  1364. $args = array();
  1365. foreach ($so_terms as $term) {
  1366. $where_cvt .= "CVT.name = :aname$i OR ";
  1367. $args[":aname$i"] = $term;
  1368. $i++;
  1369. }
  1370. $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3); # strip trailing 'OR'
  1371. // get the features for this library
  1372. $sql = "
  1373. SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname
  1374. FROM {feature} F
  1375. INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id
  1376. INNER JOIN {analysisfeature} AF on F.feature_id = AF.feature_id
  1377. INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id
  1378. WHERE A.analysis_id = :analysis_id and ($where_cvt)
  1379. ORDER BY feature_id ASC
  1380. ";
  1381. $args[':analysis_id'] = $analysis->analysis_id;
  1382. // the counting SQL
  1383. $csql = "
  1384. SELECT count(*)
  1385. FROM {feature} F
  1386. INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id
  1387. INNER JOIN {analysisfeature} AF on F.feature_id = AF.feature_id
  1388. INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id
  1389. WHERE A.analysis_id = %d and ($where_cvt)
  1390. GROUP BY A.analysis_id
  1391. ";
  1392. $org_features = chado_pager_query($sql, 10, 0, $csql, $args);
  1393. $pager = theme('pager');
  1394. // prepare the query that will lookup node ids
  1395. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  1396. $i=0;
  1397. $features = array();
  1398. while ($feature = $org_features->fetchObject()) {
  1399. $node = db_query($sql, array(':feature_id' => $feature->feature_id))->fetchObject();
  1400. $feature->nid = $node->nid;
  1401. $features[$i++] = $feature;
  1402. }
  1403. return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
  1404. }
  1405. /**
  1406. * used to sort the list of relationship objects by start position
  1407. *
  1408. * @ingroup tripal_feature
  1409. */
  1410. function tripal_feature_sort_rel_objects($a, $b) {
  1411. return strnatcmp($a->fmin, $b->fmin);
  1412. }
  1413. /**
  1414. * used to sort the list of relationship parts by start position
  1415. *
  1416. * @ingroup tripal_feature
  1417. */
  1418. function tripal_feature_sort_rel_parts_by_start($a, $b) {
  1419. foreach ($a as $type_name => $details) {
  1420. $astart = $a[$type_name]['start'];
  1421. break;
  1422. }
  1423. foreach ($b as $type_name => $details) {
  1424. $bstart = $b[$type_name]['start'];
  1425. break;
  1426. }
  1427. return strnatcmp($astart, $bstart);
  1428. }
  1429. /**
  1430. * used to sort the list of relationship parts by start position
  1431. *
  1432. * @ingroup tripal_feature
  1433. */
  1434. function tripal_feature_sort_rel_parts_by_end($a, $b) {
  1435. $val = strnatcmp($b['end'], $a['end']);
  1436. if ($val == 0) {
  1437. return strcmp($a['type'], $b['type']);
  1438. }
  1439. return $val;
  1440. }
  1441. /**
  1442. *
  1443. *
  1444. * @ingroup tripal_feature
  1445. */
  1446. function tripal_feature_color_sequence($sequence, $parts, $defline) {
  1447. $types = array();
  1448. // first get the list of types so we can create a color legend
  1449. foreach ($parts as $index => $t) {
  1450. foreach ($t as $type_name => $details) {
  1451. $types[$type_name] = 1;
  1452. }
  1453. }
  1454. $newseq = "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  1455. foreach ($types as $type_name => $present) {
  1456. $newseq .= "<span id=\"tripal_feature-legend-$type_name\" class=\"tripal_feature-legend-item tripal_feature-featureloc_sequence-$type_name\" script=\"\">$type_name</span>";
  1457. }
  1458. $newseq .= "</div>Hold the cursor over a type above to highlight its positions in the sequence below. The colors in the sequence below merge when types overlap.";
  1459. // set the background color of the rows based on the type
  1460. $pos = 0;
  1461. $newseq .= "<pre id=\"tripal_feature-featureloc_sequence\">";
  1462. $newseq .= ">$defline\n";
  1463. // iterate through the parts. They should be in order.
  1464. $ends = array();
  1465. $seqcount = 0;
  1466. foreach ($parts as $index => $types) {
  1467. // get the start for this part. All types in this part start at the
  1468. // same position so we only need the first record
  1469. foreach ($types as $type => $child) {
  1470. $start = $child['start'];
  1471. break;
  1472. }
  1473. // add in the sequence up to the start of this part
  1474. for ($i = $pos; $i < $start; $i++) {
  1475. $newseq .= $sequence{$pos};
  1476. $seqcount++;
  1477. if ($seqcount % 50 == 0) {
  1478. $newseq .= "\n";
  1479. }
  1480. if (array_key_exists($pos, $ends)) {
  1481. foreach ($ends[$pos] as $end) {
  1482. $newseq .= "</span>";
  1483. }
  1484. }
  1485. $pos++;
  1486. }
  1487. // we want to sort the parts by their end. We want the span tag to
  1488. // to be added in the order the parts end.
  1489. usort($types, 'tripal_feature_sort_rel_parts_by_end');
  1490. // now add the child span for all types that start at this position
  1491. foreach ($types as $type) {
  1492. $class = "tripal_feature-featureloc_sequence-" . $type['type'];
  1493. $newseq .= "<span class=\"$class\">";
  1494. // add the end position
  1495. $end = $type['end'];
  1496. $ends[$end][] = $end;
  1497. }
  1498. }
  1499. // add in rest of the sequence
  1500. for ($i = $pos; $i < strlen($sequence); $i++) {
  1501. $newseq .= $sequence{$pos};
  1502. $seqcount++;
  1503. if ($seqcount % 50 == 0) {
  1504. $newseq .= "\n";
  1505. }
  1506. if (array_key_exists($pos, $ends)) {
  1507. foreach ($ends[$pos] as $end) {
  1508. $newseq .= "</span>";
  1509. }
  1510. }
  1511. $pos++;
  1512. }
  1513. $newseq .= "</pre>";
  1514. return $newseq;
  1515. }
  1516. /**
  1517. *
  1518. * @ingroup tripal_feature
  1519. */
  1520. function tripal_feature_node_presave($node) {
  1521. // set the title to ensure it is always unique
  1522. switch ($node->type) {
  1523. case 'chado_feature':
  1524. $values = array('organism_id' => $node->organism_id);
  1525. $organism = tripal_core_chado_select('organism', array('genus', 'species'), $values);
  1526. $node->title = $node->fname . ', ' . $node->uniquename . ' (' . $node->feature_type . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  1527. break;
  1528. }
  1529. }
  1530. /**
  1531. *
  1532. * @ingroup tripal_feature
  1533. */
  1534. function tripal_feature_node_insert($node) {
  1535. // set the URL path after inserting. We do it here because we do not
  1536. // know the feature_id in the presave
  1537. switch ($node->type) {
  1538. case 'chado_feature':
  1539. if (!$node->feature_id) {
  1540. $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
  1541. $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  1542. $node->feature_id = $chado_feature->feature_id;
  1543. }
  1544. // remove any previous alias
  1545. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1546. // set the URL for this feature page
  1547. $url_alias = tripal_feature_get_feature_url($node);
  1548. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1549. path_save($path_alias);
  1550. break;
  1551. }
  1552. }
  1553. /**
  1554. *
  1555. * @ingroup tripal_feature
  1556. */
  1557. function tripal_feature_node_view($node, $view_mode, $langcode) {
  1558. switch ($node->type) {
  1559. case 'chado_feature':
  1560. // Show feature browser and counts
  1561. if ($view_mode == 'full') {
  1562. $node->content['tripal_feature_alignments'] = array(
  1563. '#value' => theme('tripal_feature_alignments', array('node' => $node)),
  1564. );
  1565. $node->content['tripal_feature_analyses'] = array(
  1566. '#value' => theme('tripal_feature_analyses', array('node' => $node)),
  1567. );
  1568. $node->content['tripal_feature_base'] = array(
  1569. '#value' => theme('tripal_feature_base', array('node' => $node)),
  1570. );
  1571. $node->content['tripal_feature_phenotypes'] = array(
  1572. '#value' => theme('tripal_feature_phenotypes', array('node' => $node)),
  1573. );
  1574. $node->content['tripal_feature_properties'] = array(
  1575. '#value' => theme('tripal_feature_properties', array('node' => $node)),
  1576. );
  1577. $node->content['tripal_feature_references'] = array(
  1578. '#value' => theme('tripal_feature_references', array('node' => $node)),
  1579. );
  1580. $node->content['tripal_feature_relationships'] = array(
  1581. '#value' => theme('tripal_feature_relationships', array('node' => $node)),
  1582. );
  1583. $node->content['tripal_feature_seqence'] = array(
  1584. '#value' => theme('tripal_feature_sequence', array('node' => $node)),
  1585. );
  1586. $node->content['tripal_feature_synonyms'] = array(
  1587. '#value' => theme('tripal_feature_synonyms', array('node' => $node)),
  1588. );
  1589. $node->content['tripal_feature_terms'] = array(
  1590. '#value' => theme('tripal_feature_terms', array('node' => $node)),
  1591. );
  1592. }
  1593. if ($view_mode == 'teaser') {
  1594. $node->content['tripal_feature_teaser'] = array(
  1595. '#value' => theme('tripal_feature_teaser', array('node' => $node)),
  1596. );
  1597. }
  1598. break;
  1599. case 'chado_organism':
  1600. // Show feature browser and counts
  1601. if ($view_mode == 'full') {
  1602. $node->content['tripal_organism_feature_counts'] = array(
  1603. '#value' => theme('tripal_organism_feature_counts', array('node' => $node)),
  1604. );
  1605. $node->content['tripal_organism_feature_browser'] = array(
  1606. '#value' => theme('tripal_organism_feature_browser', array('node' => $node)),
  1607. );
  1608. }
  1609. break;
  1610. // TODO: handle these node types. Should we also have a feature browser?
  1611. case 'chado_library':
  1612. break;
  1613. case 'chado_stock':
  1614. break;
  1615. case 'chado_analysis':
  1616. break;
  1617. }
  1618. }
  1619. /**
  1620. *
  1621. * @ingroup tripal_feature
  1622. */
  1623. function tripal_feature_node_update($node) {
  1624. // add items to other nodes, build index and search results
  1625. switch ($node->type) {
  1626. case 'chado_feature':
  1627. // remove any previous alias
  1628. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1629. // set the URL for this feature page
  1630. $url_alias = tripal_feature_get_feature_url($node);
  1631. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1632. path_save($path_alias);
  1633. break;
  1634. }
  1635. }
  1636. /**
  1637. * @ingroup tripal_feature
  1638. */
  1639. function tripal_feature_preprocess_tripal_feature_sequence(&$variables) {
  1640. // we want to provide a new variable that contains the matched features.
  1641. $feature = $variables['node']->feature;
  1642. // get the featureloc src features
  1643. $options = array(
  1644. 'return_array' => 1,
  1645. 'include_fk' => array(
  1646. 'srcfeature_id' => array(
  1647. 'type_id' => 1
  1648. ),
  1649. ),
  1650. );
  1651. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc', $options);
  1652. // because there are two foriegn keys in the featureloc table with the feature table
  1653. // we have to access the records for each by specifying the field name after the table name:
  1654. $ffeaturelocs = $feature->featureloc->feature_id;
  1655. // now extract the sequences
  1656. $featureloc_sequences = tripal_feature_load_featureloc_sequences($feature->feature_id, $ffeaturelocs);
  1657. $feature->featureloc_sequences = $featureloc_sequences;
  1658. }
  1659. /**
  1660. *
  1661. *
  1662. * @ingroup tripal_feature
  1663. */
  1664. function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
  1665. // we want to provide a new variable that contains the matched features.
  1666. $feature = $variables['node']->feature;
  1667. if (!property_exists($feature, 'all_relationships')) {
  1668. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  1669. }
  1670. }
  1671. /**
  1672. *
  1673. *
  1674. * @ingroup tripal_feature
  1675. */
  1676. function tripal_feature_preprocess_tripal_feature_proteins(&$variables) {
  1677. // we want to provide a new variable that contains the matched features.
  1678. $feature = $variables['node']->feature;
  1679. if (!property_exists($feature, 'all_relationships')) {
  1680. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  1681. }
  1682. }
  1683. /**
  1684. *
  1685. *
  1686. * @ingroup tripal_feature
  1687. */
  1688. function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
  1689. // we want to provide a new variable that contains the matched features.
  1690. $feature = $variables['node']->feature;
  1691. $options = array(
  1692. 'return_array' => 1,
  1693. 'include_fk' => array(
  1694. 'srcfeature_id' => array(
  1695. 'type_id' => 1,
  1696. ),
  1697. 'feature_id' => array(
  1698. 'type_id' => 1
  1699. ),
  1700. )
  1701. );
  1702. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc', $options);
  1703. // get alignments as child
  1704. $cfeaturelocs = $feature->featureloc->feature_id;
  1705. if (!$cfeaturelocs) {
  1706. $cfeaturelocs = array();
  1707. }
  1708. // get alignment as parent
  1709. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  1710. if (!$pfeaturelocs) {
  1711. $pfeaturelocs = array();
  1712. }
  1713. // get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  1714. $mfeaturelocs = tripal_feature_get_matched_alignments($feature);
  1715. $feature->matched_featurelocs = $mfeaturelocs;
  1716. // combine all three alignments into a single array for printing together in
  1717. // a single list
  1718. $alignments = array();
  1719. foreach ($pfeaturelocs as $featureloc) {
  1720. // if type is a 'match' then ignore it. We will handle those below
  1721. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  1722. continue;
  1723. }
  1724. $alignment = new stdClass();
  1725. $alignment->record = $featureloc;
  1726. $alignment->name = $featureloc->feature_id->name;
  1727. $alignment->type = $featureloc->feature_id->type_id->name;
  1728. $alignment->fmin = $featureloc->fmin;
  1729. $alignment->fmax = $featureloc->fmax;
  1730. $alignment->phase = $featureloc->phase;
  1731. $alignment->strand = $featureloc->strand;
  1732. $alignments[] = $alignment;
  1733. if (property_exists($featureloc->feature_id, 'nid')) {
  1734. $alignment->nid = $featureloc->feature_id->nid;
  1735. }
  1736. }
  1737. foreach ($cfeaturelocs as $featureloc) {
  1738. // if type is a 'match' then ignore it. We will handle those below
  1739. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  1740. continue;
  1741. }
  1742. $alignment = new stdClass();
  1743. $alignment->record = $featureloc;
  1744. $alignment->name = $featureloc->srcfeature_id->name;
  1745. $alignment->type = $featureloc->srcfeature_id->type_id->name;
  1746. $alignment->fmin = $featureloc->fmin;
  1747. $alignment->is_fmin_partial = $featureloc->is_fmin_partial;
  1748. $alignment->fmax = $featureloc->fmax;
  1749. $alignment->is_fmax_partial = $featureloc->is_fmax_partial;
  1750. $alignment->phase = $featureloc->phase;
  1751. $alignment->strand = $featureloc->strand;
  1752. $alignments[] = $alignment;
  1753. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  1754. $alignment->nid = $featureloc->srcfeature_id->nid;
  1755. }
  1756. }
  1757. // in matching features, the left feature is always the feature
  1758. // provided to this function.
  1759. foreach ($mfeaturelocs as $featureloc) {
  1760. // get more information about the right feature
  1761. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  1762. $rfeature = tripal_core_generate_chado_var('feature', $select);
  1763. // now add to the list
  1764. $alignment = new stdClass();
  1765. $alignment->record = $featureloc;
  1766. $alignment->right_feature = $rfeature;
  1767. $alignment->name = $rfeature->name;
  1768. $alignment->type = $rfeature->type_id->name;
  1769. $alignment->fmin = $featureloc->left_fmin;
  1770. $alignment->is_fmin_partial = $featureloc->left_is_fmin_partial;
  1771. $alignment->fmax = $featureloc->left_fmax;
  1772. $alignment->is_fmax_partial = $featureloc->left_is_fmax_partial;
  1773. $alignment->phase = $featureloc->left_phase;
  1774. $alignment->strand = $featureloc->left_strand;
  1775. $alignment->right_fmin = $featureloc->right_fmin;
  1776. $alignment->right_is_fmin_partial = $featureloc->right_is_fmin_partial;
  1777. $alignment->right_fmax = $featureloc->right_fmax;
  1778. $alignment->right_is_fmax_partial = $featureloc->right_is_fmax_partial;
  1779. $alignment->right_phase = $featureloc->right_phase;
  1780. $alignment->right_strand = $featureloc->right_strand;
  1781. $alignments[] = $alignment;
  1782. if (property_exists($rfeature, 'nid')) {
  1783. $alignment->nid = $rfeature->nid;
  1784. }
  1785. }
  1786. $feature->all_featurelocs = $alignments;
  1787. }
  1788. /**
  1789. *
  1790. *
  1791. * @ingroup tripal_feature
  1792. */
  1793. function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables, $hook) {
  1794. $organism = $variables['node']->organism;
  1795. $organism->feature_counts = tripal_feature_load_organism_feature_counts($organism);
  1796. }
  1797. /**
  1798. *
  1799. *
  1800. * @ingroup tripal_feature
  1801. */
  1802. function tripal_feature_preprocess_tripal_organism_feature_browser(&$variables, $hook) {
  1803. $organism = $variables['node']->organism;
  1804. $organism->feature_browser = tripal_feature_load_organism_feature_browser($organism);
  1805. }
  1806. /**
  1807. * Preprocessor function for the Library Feature Browser
  1808. *
  1809. * @ingroup tripal_feature
  1810. */
  1811. function tripal_feature_preprocess_tripal_library_feature_browser(&$variables) {
  1812. $library = $variables['node']->library;
  1813. $library->feature_browser = tripal_feature_load_library_feature_browser($library);
  1814. }
  1815. /**
  1816. * Preprocessor function for the Analysis Feature Browser
  1817. *
  1818. * @ingroup tripal_feature
  1819. */
  1820. function tripal_feature_preprocess_tripal_analysis_feature_browser(&$variables) {
  1821. $analysis = $variables['node']->analysis;
  1822. $analysis->feature_browser = tripal_feature_load_analysis_feature_browser($analysis);
  1823. }
  1824. /**
  1825. *
  1826. *
  1827. * @ingroup tripal_feature
  1828. */
  1829. function tripal_feature_cv_chart($chart_id) {
  1830. // we only want the chart to show feature types setup by the admin
  1831. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1832. $where = '';
  1833. if ($temp) {
  1834. $temp = explode("\n", $temp);
  1835. foreach ($temp as $key => $value) {
  1836. $temp2 = explode("=", $value);
  1837. $feature_type = rtrim($temp2[0]);
  1838. $where .= "CNT.feature_type = '$feature_type' OR \n";
  1839. }
  1840. if ($where) {
  1841. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1842. $where = "($where) AND";
  1843. }
  1844. }
  1845. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/", "$1", $chart_id);
  1846. // The CV module will create the JSON array necessary for buillding a
  1847. // pie chart using jgChart and Google Charts. We have to pass to it
  1848. // a table that contains count information, tell it which column
  1849. // contains the cvterm_id and provide a filter for getting the
  1850. // results we want from the table.
  1851. $options = array(
  1852. count_mview => 'organism_feature_count',
  1853. cvterm_id_column => 'cvterm_id',
  1854. count_column => 'num_features',
  1855. size => '550x200',
  1856. filter => "$where CNT.organism_id = $organism_id",
  1857. );
  1858. return $options;
  1859. }
  1860. /**
  1861. *
  1862. *
  1863. * @ingroup tripal_feature
  1864. */
  1865. function tripal_feature_cv_tree($tree_id) {
  1866. // The CV module will create the JSON array necessary for buillding a
  1867. // pie chart using jgChart and Google Charts. We have to pass to it
  1868. // a table that contains count information, tell it which column
  1869. // contains the cvterm_id and provide a filter for getting the
  1870. // results we want from the table.
  1871. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/", "$1", $tree_id);
  1872. $options = array(
  1873. cv_id => tripal_cv_get_cv_id('sequence'),
  1874. count_mview => 'organism_feature_count',
  1875. cvterm_id_column => 'cvterm_id',
  1876. count_column => 'num_features',
  1877. filter => "CNT.organism_id = $organism_id",
  1878. label => 'Features',
  1879. );
  1880. return $options;
  1881. }
  1882. /**
  1883. *
  1884. *
  1885. * @ingroup tripal_feature
  1886. */
  1887. function tripal_feature_del_vocabulary() {
  1888. //include the file containing the required functions for adding taxonomy vocabs
  1889. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1890. // get the vocabularies
  1891. $vocabularies = taxonomy_get_vocabularies();
  1892. // These taxonomic terms are hard coded because we
  1893. // know we have these relationships in the chado tables
  1894. // through foreign key relationships. The tripal
  1895. // modules that correspond to these chado "modules" don't
  1896. // need to be installed for the taxonomy to work.
  1897. foreach ($vocabularies as $vocab) {
  1898. if ($vocab->name == 'Feature Type') {
  1899. taxonomy_vocabulary_delete($vocab->vid);
  1900. }
  1901. if ($vocab->name == 'Organism') {
  1902. taxonomy_vocabulary_delete($vocab->vid);
  1903. }
  1904. if ($vocab->name == 'Library') {
  1905. taxonomy_vocabulary_delete($vocab->vid);
  1906. }
  1907. if ($vocab->name == 'Analysis') {
  1908. taxonomy_vocabulary_delete($vocab->vid);
  1909. }
  1910. }
  1911. }
  1912. /**
  1913. *
  1914. *
  1915. * @ingroup tripal_feature
  1916. */
  1917. function tripal_feature_return_fasta($feature, $desc) {
  1918. $fasta = ">" . variable_get('chado_feature_accession_prefix', 'FID') . "$feature->feature_id|$feature->name";
  1919. $fasta .= " $desc\n";
  1920. $fasta .= wordwrap($feature->residues, 50, "\n", TRUE);
  1921. $fasta .= "\n\n";
  1922. return $fasta;
  1923. }
  1924. /**
  1925. *
  1926. *
  1927. * @ingroup tripal_feature
  1928. */
  1929. function tripal_feature_job_describe_args($callback, $args) {
  1930. $new_args = array();
  1931. if ($callback == 'tripal_feature_load_fasta') {
  1932. $new_args['FASTA file'] = $args[0];
  1933. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  1934. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1935. $new_args['Sequence Type'] = $args[2];
  1936. $new_args['Name Match Type'] = $args[14];
  1937. $new_args['Name RE'] = $args[4];
  1938. $new_args['Unique Name RE'] = $args[5];
  1939. // add in the relationship arguments
  1940. $new_args['Relationship Type'] = $args[8];
  1941. $new_args['Relationship Parent RE'] = $args[9];
  1942. $new_args['Relationship Parent Type'] = $args[10];
  1943. // add in the database reference arguments
  1944. if ($args[7]) {
  1945. $db = tripal_core_chado_select('db', array('name'), array('db_id' => $args[7]));
  1946. }
  1947. $new_args['Database Reference'] = $db[0]->name;
  1948. $new_args['Accession RE'] = $args[6];
  1949. $new_args['Method'] = $args[11];
  1950. // add in the analysis
  1951. if ($args[13]) {
  1952. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[13]));
  1953. }
  1954. $new_args['Analysis'] = $analysis[0]->name;
  1955. }
  1956. if ($callback == 'tripal_feature_delete_features') {
  1957. if ($args[0]) {
  1958. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  1959. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1960. }
  1961. else {
  1962. $new_args['Organism'] = '';
  1963. }
  1964. if ($args[1]) {
  1965. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[1]));
  1966. $new_args['Analysis'] = $analysis[0]->name;
  1967. }
  1968. else {
  1969. $new_args['Analysis'] = '';
  1970. }
  1971. $new_args['Sequence Type'] = $args[2];
  1972. $new_args['Is Unique Name'] = $args[3];
  1973. $new_args['Features Names'] = $args[4];
  1974. }
  1975. elseif ($callback == 'tripal_feature_load_gff3') {
  1976. $new_args['GFF File'] = $args[0];
  1977. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  1978. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1979. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[2]));
  1980. $new_args['Analysis'] = $analysis[0]->name;
  1981. $new_args['Use a Transaction'] = ($args[7] == 1) ? "Yes" : "No";
  1982. $new_args['Import only new features'] = ($args[3] == 1) ? "Yes" : "No";
  1983. $new_args['Import all and update'] = ($args[4] == 1) ? "Yes" : "No";
  1984. $new_args['Import all and replace'] = ($args[5] == 1) ? "Yes" : "No";
  1985. $new_args['Delete features'] = ($args[6] == 1) ? "Yes" : "No";
  1986. if ($args[8]) {
  1987. $target_organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[8]));
  1988. $new_args['Target organism'] = $target_organism[0]->genus . " " . $target_organism[0]->species;
  1989. }
  1990. else {
  1991. $new_args['Target organism'] = '';
  1992. }
  1993. $new_args['Target type'] = $args[9];
  1994. $new_args['Create target'] = ($args[10] == 1) ? "Yes" : "No";
  1995. $new_args['Starting line'] = $args[11];
  1996. $new_args['Landmark Type'] = $args[12];
  1997. $new_args['Alternate ID attribute'] = $args[13];
  1998. $new_args['Create Organism'] = ($args[14] == 1) ? "Yes" : "No";
  1999. }
  2000. if ($callback == 'tripal_feature_sync_features') {
  2001. if ($args[0]) {
  2002. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  2003. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  2004. }
  2005. else {
  2006. $new_args['Organism'] = '';
  2007. }
  2008. $new_args['Feature Types'] = $args[1];
  2009. }
  2010. return $new_args;
  2011. }
  2012. /**
  2013. * Implements hook_coder_ignore().
  2014. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for coder are stored
  2015. */
  2016. function tripal_feature_coder_ignore() {
  2017. return array(
  2018. 'path' => drupal_get_path('module', 'tripal_feature'),
  2019. 'line prefix' => drupal_get_path('module', 'tripal_feature'),
  2020. );
  2021. }
  2022. /*
  2023. * Uses the value provided in the $id argument to find all features that match
  2024. * that ID by name, featurename or synonym. If it matches uniquenly to a single
  2025. * feature it will redirect to that feature page, otherwise, a list of matching
  2026. * features is shown.
  2027. */
  2028. function tripal_feature_match_features_page($id) {
  2029. // if the URL alias configuration is set such that the URL
  2030. // always begins with 'feature' then we want to use the ID as it is and
  2031. // forward it on. Otherwise, try to find the matching feature.
  2032. $url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
  2033. if (!$url_alias) {
  2034. $url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
  2035. }
  2036. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  2037. if (preg_match('/^feature\//', $url_alias)) {
  2038. drupal_goto($id);
  2039. }
  2040. $sql = "
  2041. SELECT
  2042. F.name, F.uniquename, F.feature_id,
  2043. O.genus, O.species, O.organism_id,
  2044. CVT.cvterm_id, CVT.name as type_name,
  2045. CF.nid,
  2046. array_agg(S.name) as synonyms
  2047. FROM {feature} F
  2048. INNER JOIN {organism} O on F.organism_id = O.organism_id
  2049. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  2050. LEFT JOIN {feature_synonym} FS on FS.feature_id = F.feature_id
  2051. LEFT JOIN {synonym} S on S.synonym_id = FS.synonym_id
  2052. INNER JOIN public.chado_feature CF on CF.feature_id = F.feature_id
  2053. WHERE
  2054. F.uniquename = :uname or
  2055. F.name = :fname' or
  2056. S.name = :sname
  2057. GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
  2058. O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  2059. ";
  2060. $results = chado_query($sql, array(':uname' => $id, ':fname' => $id, ':sname' => $id));
  2061. $num_matches = 0;
  2062. // iterate through the matches and build the table for showing matches
  2063. $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  2064. $rows = array();
  2065. $curr_match;
  2066. while ($match = $results->fetchObject()) {
  2067. $curr_match = $match;
  2068. $synonyms = $match->synonyms;
  2069. $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
  2070. $rows[] = array(
  2071. $match->uniquename,
  2072. "<a href=\"" . url("node/" . $match->nid) . "\">" . $match->name . "</a>",
  2073. $match->type_name,
  2074. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  2075. $synonyms,
  2076. );
  2077. $num_matches++;
  2078. }
  2079. // if we have more than one match then generate the table, otherwise, redirect
  2080. // to the matched feature
  2081. if ($num_matches == 1) {
  2082. drupal_goto("node/" . $curr_match->nid);
  2083. }
  2084. if ($num_matches == 0) {
  2085. return "<p>No features matched the given name '$id'</p>";
  2086. }
  2087. $table_attrs = array(
  2088. 'class' => 'tripal-table tripal-table-horz'
  2089. );
  2090. $output = "<p>The following features match the name '$id'.</p>";
  2091. $output .= theme_table($header, $rows, $table_attrs, $caption);
  2092. return $output;
  2093. }
  2094. /**
  2095. * Implementation of hook_form_alter()
  2096. *
  2097. * @param $form
  2098. * @param $form_state
  2099. * @param $form_id
  2100. */
  2101. function tripal_feature_form_alter(&$form, &$form_state, $form_id) {
  2102. if ($form_id == "tripal_feature_seq_extract_form") {
  2103. // updating the form through the ahah callback sets the action of
  2104. // the form to the ahah callback URL. We need to set it back
  2105. // to the normal form URL
  2106. $form['#action'] = url("find/sequences");
  2107. }
  2108. // turn off preview button for insert/updates
  2109. if ($form_id == "chado_feature_node_form") {
  2110. $form['actions']['preview']['#access'] = FALSE;
  2111. }
  2112. }