tripal_feature.module 88 KB

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