tripal_feature.module 80 KB

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