tripal_feature.module 79 KB

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