tripal_feature.module 80 KB

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