tripal_feature.module 80 KB

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