tripal_feature.module 78 KB

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