tripal_feature.module 83 KB

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