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