tripal_feature.module 77 KB

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