tripal_feature.module 80 KB

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