tripal_feature.module 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. <?php
  2. require_once "admin.php";
  3. require_once "syncFeatures.php";
  4. require_once "indexFeatures.php";
  5. require_once "fasta_loader.php";
  6. require_once "tripal_feature.api.inc";
  7. require_once "tripal_feature-secondary_tables.inc";
  8. require_once "tripal_feature-properties.inc";
  9. require_once "tripal_feature-relationships.inc";
  10. require_once "tripal_feature-db_references.inc";
  11. /*************************************************************************
  12. *
  13. */
  14. function tripal_feature_init(){
  15. // add the jGCharts JS and CSS
  16. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js');
  17. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js');
  18. }
  19. /*************************************************************************
  20. * Implements hook_views_api()
  21. * Purpose: Essentially this hook tells drupal that there is views support for
  22. * for this module which then includes tripal_db.views.inc where all the
  23. * views integration code is
  24. */
  25. function tripal_feature_views_api() {
  26. return array(
  27. 'api' => 2.0,
  28. );
  29. }
  30. /************************************************************************
  31. * Display help and module information
  32. * @param path which path of the site we're displaying help
  33. * @param arg array that holds the current path as would be returned from arg() function
  34. * @return help text for the path
  35. */
  36. function tripal_feature_help($path, $arg) {
  37. $output = '';
  38. switch ($path) {
  39. case "admin/help#tripal_feature":
  40. $output='<p>'.t("Displays links to nodes created on this date").'</p>';
  41. break;
  42. }
  43. return $output;
  44. }
  45. /************************************************************************
  46. * Provide information to drupal about the node types that we're creating
  47. * in this module
  48. */
  49. function tripal_feature_node_info() {
  50. $nodes = array();
  51. $nodes['chado_feature'] = array(
  52. 'name' => t('Feature'),
  53. 'module' => 'chado_feature',
  54. 'description' => t('A feature from the chado database'),
  55. 'has_title' => FALSE,
  56. 'title_label' => t('Feature'),
  57. 'has_body' => FALSE,
  58. 'body_label' => t('Feature Description'),
  59. 'locked' => TRUE
  60. );
  61. return $nodes;
  62. }
  63. /************************************************************************
  64. * Set the permission types that the chado module uses. Essentially we
  65. * want permissionis that protect creation, editing and deleting of chado
  66. * data objects
  67. */
  68. function tripal_feature_perm(){
  69. return array(
  70. 'access chado_feature content',
  71. 'create chado_feature content',
  72. 'delete chado_feature content',
  73. 'edit chado_feature content',
  74. );
  75. }
  76. /************************************************************************
  77. * Set the permission types that the module uses.
  78. */
  79. function chado_feature_access($op, $node, $account) {
  80. if ($op == 'create') {
  81. return user_access('create chado_feature content', $account);
  82. }
  83. if ($op == 'update') {
  84. if (user_access('edit chado_feature content', $account)) {
  85. return TRUE;
  86. }
  87. }
  88. if ($op == 'delete') {
  89. if (user_access('delete chado_feature content', $account)) {
  90. return TRUE;
  91. }
  92. }
  93. if ($op == 'view') {
  94. if (user_access('access chado_feature content', $account)) {
  95. return TRUE;
  96. }
  97. }
  98. return FALSE;
  99. }
  100. /************************************************************************
  101. * Menu items are automatically added for the new node types created
  102. * by this module to the 'Create Content' Navigation menu item. This function
  103. * adds more menu items needed for this module.
  104. */
  105. function tripal_feature_menu() {
  106. $items = array();
  107. // the administative settings menu
  108. $items['admin/tripal/tripal_feature'] = array(
  109. 'title' => 'Features',
  110. 'description' => 'Settings for Chado Features',
  111. 'page callback' => 'drupal_get_form',
  112. 'page arguments' => array('tripal_feature_admin'),
  113. 'access arguments' => array('administer site configuration'),
  114. 'type' => MENU_NORMAL_ITEM,
  115. );
  116. $items['admin/tripal/fasta_loader'] = array(
  117. 'title' => 'Import a multi-FASTA file',
  118. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  119. 'page callback' => 'drupal_get_form',
  120. 'page arguments' => array('tripal_feature_fasta_load_form'),
  121. 'access arguments' => array('administer site configuration'),
  122. 'type' => MENU_NORMAL_ITEM,
  123. );
  124. // Adding Secondary Properties-----------------
  125. $items['node/%/properties'] = array(
  126. 'title' => t('Add Properties & Synonyms'),
  127. 'description' => t('Settings for Features'),
  128. 'page callback' => 'tripal_feature_add_ALL_property_page',
  129. 'page arguments' => array(1),
  130. 'access arguments' => array('create chado_feature content'),
  131. 'type' => MENU_CALLBACK
  132. );
  133. $items['node/%/db_references'] = array(
  134. 'title' => t('Add Database References'),
  135. 'description' => t('Settings for Features'),
  136. 'page callback' => 'tripal_feature_add_ALL_dbreferences_page',
  137. 'page arguments' => array(1),
  138. 'access arguments' => array('create chado_feature content'),
  139. 'type' => MENU_CALLBACK
  140. );
  141. $items['node/%/relationships'] = array(
  142. 'title' => t('Add Relationships'),
  143. 'description' => t('Settings for Features'),
  144. 'page callback' => 'tripal_feature_add_ALL_relationships_page',
  145. 'page arguments' => array(1),
  146. 'access arguments' => array('create chado_feature content'),
  147. 'type' => MENU_CALLBACK
  148. );
  149. //Edit/Deleting Secondary Properties-------------
  150. $items['node/%/edit_properties'] = array(
  151. 'title' => t('Edit Properties'),
  152. 'description' => t('Settings for Features'),
  153. 'page callback' => 'tripal_feature_edit_ALL_properties_page',
  154. 'page arguments' => array(1),
  155. 'access arguments' => array('edit chado_feature content'),
  156. 'type' => MENU_LOCAL_TASK,
  157. 'weight' => 8,
  158. );
  159. $items['node/%/edit_relationships'] = array(
  160. 'title' => t('Edit Relationships'),
  161. 'description' => t('Settings for Feature'),
  162. 'page callback' => 'tripal_feature_edit_ALL_relationships_page',
  163. 'page arguments' => array(1),
  164. 'access arguments' => array('edit chado_feature content'),
  165. 'type' => MENU_LOCAL_TASK,
  166. 'weight' => 9,
  167. );
  168. $items['node/%/edit_db_references'] = array(
  169. 'title' => t('Edit DB References'),
  170. 'description' => t('Settings for Feature'),
  171. 'page callback' => 'tripal_feature_edit_ALL_dbreferences_page',
  172. 'page arguments' => array(1),
  173. 'access arguments' => array('edit chado_feature content'),
  174. 'type' => MENU_LOCAL_TASK,
  175. 'weight' => 10,
  176. );
  177. return $items;
  178. }
  179. /************************************************************************
  180. * When a new chado_feature node is created we also need to add information
  181. * to our chado_feature table. This function is called on insert of a new node
  182. * of type 'chado_feature' and inserts the necessary information.
  183. */
  184. function chado_feature_insert($node){
  185. // remove spaces, newlines from residues
  186. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  187. // If this feature already exists then don't recreate it in chado
  188. // TODO: the unique index in chado for this also includes the type_id. If the site
  189. // ever needs to have the same feature name for different types then this will break.
  190. $feature_sql = "SELECT *
  191. FROM {Feature} F
  192. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  193. WHERE uniquename = '%s' and organism_id = %d and CVT.name = '%s'";
  194. $previous_db = tripal_db_set_active('chado');
  195. $feature = db_fetch_object(db_query($feature_sql,$node->uniquename,$node->organism_id,$node->feature_type));
  196. tripal_db_set_active($previous_db);
  197. // if the feature doesn't exist then let's create it in chado.
  198. if(!$feature){
  199. $sql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,".
  200. " is_obsolete, type_id)".
  201. " VALUES(%d,'%s','%s','%s',%d, %s, ".
  202. " (SELECT cvterm_id ".
  203. " FROM {CVTerm} CVT ".
  204. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  205. " WHERE CV.name = 'sequence' and CVT.name = '%s'))";
  206. $obsolete = 'FALSE';
  207. if($node->is_obsolete){
  208. $obsolete = 'TRUE';
  209. }
  210. // use chado database
  211. $previous_db = tripal_db_set_active('chado');
  212. db_query($sql,$node->organism_id,$node->name,$node->uniquename,
  213. $residues,strlen($residues),$obsolete,$node->feature_type);
  214. // now that we've added the feature, get the feature id for this feature
  215. $feature = db_fetch_object(db_query($feature_sql,$node->uniquename,$node->organism_id,$node->feature_type));
  216. // now use drupal database
  217. tripal_db_set_active($previous_db);
  218. }
  219. // add the genbank accession and synonyms
  220. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  221. // make sure the entry for this feature doesn't already exist in the chado_feature table
  222. // if it doesn't exist then we want to add it.
  223. $node_check_sql = "SELECT * FROM {chado_feature} ".
  224. "WHERE feature_id = '%s'";
  225. $node_check = db_fetch_object(db_query($node_check_sql,$feature->feature_id));
  226. if(!$node_check){
  227. // next add the item to the drupal table
  228. $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
  229. "VALUES (%d, %d, %d, " . time() . ")";
  230. db_query($sql,$node->nid,$node->vid,$feature->feature_id);
  231. }
  232. }
  233. /************************************************************************
  234. */
  235. function chado_feature_delete($node){
  236. // get feature_id so we can remove it from chado database
  237. $sql_drupal = "SELECT feature_id ".
  238. "FROM {chado_feature} ".
  239. "WHERE nid = %d AND vid = %d";
  240. $feature_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  241. // remove the drupal content
  242. $sql_del = "DELETE FROM {chado_feature} ".
  243. "WHERE nid = %d ".
  244. "AND vid = %d";
  245. db_query($sql_del, $node->nid, $node->vid);
  246. $sql_del = "DELETE FROM {node} ".
  247. "WHERE nid = %d ".
  248. "AND vid = %d";
  249. db_query($sql_del, $node->nid, $node->vid);
  250. $sql_del = "DELETE FROM {node_revisions} ".
  251. "WHERE nid = %d ".
  252. "AND vid = %d";
  253. db_query($sql_del, $node->nid, $node->vid);
  254. // Remove data from feature tables of chado database. This will
  255. // cause a cascade delete and remove all data in referencing tables
  256. // for this feature
  257. $previous_db = tripal_db_set_active('chado');
  258. db_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
  259. tripal_db_set_active($previous_db);
  260. drupal_set_message("The feature and all associated data were removed from ".
  261. "chado");
  262. }
  263. /************************************************************************
  264. */
  265. function chado_feature_update($node){
  266. if($node->revision){
  267. // TODO -- decide what to do about revisions
  268. } else {
  269. // get the feature for this node:
  270. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  271. $feature = db_fetch_object(db_query($sql, $node->vid));
  272. // remove spaces, newlines from residues
  273. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  274. $sql = "UPDATE {feature} ".
  275. " SET residues = '%s', ".
  276. " name = '%s', ".
  277. " uniquename = '%s', ".
  278. " seqlen = %d, ".
  279. " organism_id = %d, ".
  280. " is_obsolete = %s, ".
  281. " type_id = (SELECT cvterm_id ".
  282. " FROM {CVTerm} CVT ".
  283. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  284. " WHERE CV.name = 'sequence' and CVT.name = '%s') ".
  285. "WHERE feature_id = %d ";
  286. $obsolete = 'FALSE';
  287. if($node->is_obsolete){
  288. $obsolete = 'TRUE';
  289. }
  290. $previous_db = tripal_db_set_active('chado'); // use chado database
  291. db_query($sql,$residues,$node->name,$node->uniquename,
  292. strlen($residues),$node->organism_id,$obsolete,$node->feature_type,
  293. $feature->feature_id);
  294. tripal_db_set_active($previous_db); // now use drupal database
  295. // add the genbank accession & synonyms
  296. // chado_feature_add_gbaccession($node->gbaccession,$feature->feature_id);
  297. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  298. }
  299. }
  300. /************************************************************************
  301. *
  302. */
  303. function chado_feature_add_synonyms($synonyms,$feature_id){
  304. // make sure we only have a single space between each synonym
  305. $synonyms = preg_replace("/[\s\n\r]+/"," ",$synonyms);
  306. // split the synonyms into an array based on a space as the delimieter
  307. $syn_array = array();
  308. $syn_array = explode(" ",$synonyms);
  309. // use the chado database
  310. $previous_db = tripal_db_set_active('chado');
  311. // remove any old synonyms
  312. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
  313. if(!db_query($feature_syn_dsql,$feature_id)){
  314. $error .= "Could not remove synonyms from feature. ";
  315. }
  316. // return if we don't have any synonmys to add
  317. if(!$synonyms){
  318. tripal_db_set_active($previous_db);
  319. return;
  320. }
  321. // iterate through each synonym and add it to the database
  322. foreach($syn_array as $syn){
  323. // skip this item if it's empty
  324. if(!$syn){ break; }
  325. // check to see if we have this accession number already in the database
  326. // if so then don't add it again. it messes up drupal if the insert fails.
  327. // It is possible for the accession number to be present and not the feature
  328. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  329. "WHERE name = '%s'";
  330. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  331. if(!$synonym){
  332. $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
  333. "VALUES ('%s','%s', ".
  334. " (SELECT cvterm_id ".
  335. " FROM {CVTerm} CVT ".
  336. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  337. " WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
  338. if(!db_query($synonym_isql,$syn,$syn)){
  339. $error .= "Could not add synonym. ";
  340. }
  341. // now get the synonym we just added
  342. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  343. "WHERE name = '%s'";
  344. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  345. }
  346. // now add in our new sysnonym
  347. $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
  348. "VALUES (%d,%d,1)";
  349. if(!db_query($feature_syn_isql,$synonym->synonym_id,$feature_id)){
  350. $error .= "Could not add synonyms to feature. ";
  351. }
  352. }
  353. // return to the drupal database
  354. tripal_db_set_active($previous_db);
  355. return $error;
  356. }
  357. /************************************************************************
  358. *
  359. */
  360. function chado_feature_add_gbaccession($accession,$feature_id){
  361. // use chado database
  362. $previous_db = tripal_db_set_active('chado');
  363. // remove any old accession from genbank dbEST
  364. $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
  365. "WHERE feature_id = %d and dbxref_id IN ".
  366. " (SELECT DBX.dbxref_id FROM {dbxref} DBX ".
  367. " INNER JOIN DB ON DB.db_id = DBX.db_id ".
  368. " INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
  369. " WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
  370. if(!db_query($fdbxref_dsql,$feature_id,$feature_id)){
  371. $error .= "Could not remove accession from feature. ";
  372. }
  373. // if we don't have an accession number to add then just return
  374. if(!$accession){
  375. tripal_db_set_active($previous_db);
  376. return;
  377. }
  378. // get the db_id
  379. $db_sql = "SELECT db_id FROM {DB} ".
  380. "WHERE name = 'DB:Genbank_est'";
  381. $db = db_fetch_object(db_query($db_sql));
  382. // check to see if we have this accession number already in the database
  383. // if so then don't add it again. it messes up drupal if the insert fails.
  384. // It is possible for the accession number to be present and not the feature
  385. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  386. "WHERE db_id = %d and accession = '%s'";
  387. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  388. if(!$dbxref){
  389. // add the accession number
  390. $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
  391. " VALUES (%d, '%s') ";
  392. if(!db_query($dbxref_isql,$db->db_id,$accession)){
  393. $error .= 'Could not add accession as a database reference ';
  394. }
  395. // get the dbxref_id for the just added accession number
  396. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  397. "WHERE db_id = %d and accession = '%s'";
  398. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  399. }
  400. // associate the accession number with the feature
  401. $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
  402. " VALUES (%d, %d) ";
  403. if(!db_query($feature_dbxref_isql,$feature_id,$dbxref->dbxref_id)){
  404. $error .= 'Could not add feature database reference. ';
  405. }
  406. tripal_db_set_active($previous_db);
  407. return $error;
  408. }
  409. /************************************************************************
  410. *
  411. */
  412. function chado_feature_form ($node,$param){
  413. $type = node_get_types('type', $node);
  414. $form = array();
  415. $feature = $node->feature;
  416. $synonyms = $node->synonyms;
  417. $analyses = $node->analyses;
  418. $references = $node->references;
  419. // We need to pass above variables for preview to show
  420. $form['feature'] = array(
  421. '#type' => 'value',
  422. '#value' => $feature
  423. );
  424. // This field is read when previewing a node
  425. $form['synonyms'] = array(
  426. '#type' => 'value',
  427. '#value' => $synonyms
  428. );
  429. // This field is read when previewing a node
  430. $form['analyses'] = array(
  431. '#type' => 'value',
  432. '#value' => $analyses
  433. );
  434. // This field is read when previewing a node
  435. $form['references'] = array(
  436. '#type' => 'value',
  437. '#value' => $references
  438. );
  439. // keep track of the feature id if we have one. If we do have one then
  440. // this would indicate an update as opposed to an insert.
  441. $form['feature_id'] = array(
  442. '#type' => 'value',
  443. '#value' => $feature->feature_id,
  444. );
  445. $form['title']= array(
  446. '#type' => 'textfield',
  447. '#title' => t('Title'),
  448. '#required' => TRUE,
  449. '#default_value' => $feature->featurename,
  450. '#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.'),
  451. '#weight' => 1,
  452. '#maxlength' => 255
  453. );
  454. $form['uniquename']= array(
  455. '#type' => 'textfield',
  456. '#title' => t('Unique Feature Name'),
  457. '#required' => TRUE,
  458. '#default_value' => $feature->featurename,
  459. '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
  460. '#weight' => 1,
  461. '#maxlength' => 255
  462. );
  463. $form['name']= array(
  464. '#type' => 'textfield',
  465. '#title' => t('Feature Name'),
  466. '#required' => TRUE,
  467. '#default_value' => $feature->featurename,
  468. '#description' => t('Enter the name used by humans to refer to this feature.'),
  469. '#weight' => 1,
  470. '#maxlength' => 255
  471. );
  472. // get the list of supported feature types
  473. $ftypes = array();
  474. $ftypes[''] = '';
  475. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  476. foreach($supported_ftypes as $ftype){
  477. $ftypes["$ftype"] = $ftype;
  478. }
  479. $form['feature_type'] = array (
  480. '#title' => t('Feature Type'),
  481. '#type' => t('select'),
  482. '#description' => t("Choose the feature type."),
  483. '#required' => TRUE,
  484. '#default_value' => $feature->cvname,
  485. '#options' => $ftypes,
  486. '#weight' => 2
  487. );
  488. // get the list of organisms
  489. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  490. $previous_db = tripal_db_set_active('chado'); // use chado database
  491. $org_rset = db_query($sql);
  492. tripal_db_set_active($previous_db); // now use drupal database
  493. //
  494. $organisms = array();
  495. $organisms[''] = '';
  496. while($organism = db_fetch_object($org_rset)){
  497. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  498. }
  499. $form['organism_id'] = array (
  500. '#title' => t('Organism'),
  501. '#type' => t('select'),
  502. '#description' => t("Choose the organism with which this feature is associated "),
  503. '#required' => TRUE,
  504. '#default_value' => $feature->organism_id,
  505. '#options' => $organisms,
  506. '#weight' => 3,
  507. );
  508. // Get synonyms
  509. if ($synonyms) {
  510. if (is_array($synonyms)) {
  511. foreach ($synonyms as $synonym){
  512. $syn_text .= "$synonym->name\n";
  513. }
  514. } else {
  515. $syn_text = $synonyms;
  516. }
  517. }
  518. $form['synonyms']= array(
  519. '#type' => 'textarea',
  520. '#title' => t('Synonyms'),
  521. '#required' => FALSE,
  522. '#default_value' => $syn_text,
  523. '#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.'),
  524. '#weight' => 5,
  525. );
  526. $form['residues']= array(
  527. '#type' => 'textarea',
  528. '#title' => t('Residues'),
  529. '#required' => FALSE,
  530. '#default_value' => $feature->residues,
  531. '#description' => t('Enter the nucelotide sequences for this feature'),
  532. '#weight' => 6
  533. );
  534. $checked = '';
  535. if($feature->is_obsolete == 't'){
  536. $checked = '1';
  537. }
  538. $form['is_obsolete']= array(
  539. '#type' => 'checkbox',
  540. '#title' => t('Is Obsolete'),
  541. '#required' => FALSE,
  542. '#default_value' => $checked,
  543. '#description' => t('Check this box if this sequence should be retired and no longer included in further analysis.'),
  544. '#weight' => 8
  545. );
  546. return $form;
  547. }
  548. /************************************************************************
  549. *
  550. */
  551. function chado_feature_validate($node){
  552. $result = 0;
  553. // if this is an update, we want to make sure that a different feature for
  554. // the organism doesn't already have this uniquename. We don't want to give
  555. // two sequences the same uniquename
  556. if($node->feature_id){
  557. $sql = "SELECT *
  558. FROM {Feature} F
  559. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  560. WHERE uniquename = '%s'
  561. AND organism_id = %d AND CVT.name = '%s' AND NOT feature_id = %d";
  562. $previous_db = tripal_db_set_active('chado');
  563. $result = db_fetch_object(db_query($sql, $node->uniquename,$node->organism_id,$node->feature_type,$node->feature_id));
  564. tripal_db_set_active($previous_db);
  565. if($result){
  566. 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. "));
  567. }
  568. }
  569. // if this is an insert then we just need to make sure this name doesn't
  570. // already exist for this organism if it does then we need to throw an error
  571. else {
  572. $sql = "SELECT *
  573. FROM {Feature} F
  574. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  575. WHERE uniquename = '%s'
  576. AND organism_id = %d AND CVT.name = '%s'";
  577. $previous_db = tripal_db_set_active('chado');
  578. $result = db_fetch_object(db_query($sql, $node->uniquename,$node->organism_id,$node->feature_type));
  579. tripal_db_set_active($previous_db);
  580. if($result){
  581. 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. "));
  582. }
  583. }
  584. // we want to remove all characters except IUPAC nucleotide characters from the
  585. // the residues. however, residues are not required so if blank then we'll skip
  586. // this step
  587. if($node->residues){
  588. $residues = preg_replace("/[^\w]/",'',$node->residues);
  589. if(!preg_match("/^[ACTGURYMKSWBDHVN]+$/i",$residues)){
  590. 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 ."'"));
  591. }
  592. }
  593. // we don't allow a genbank accession number for a contig
  594. if($node->feature_type == 'contig' and $node->gbaccession){
  595. form_set_error('gbaccession',t("Contigs cannot have a genbank accession number. Please change the feature type or remove the accession number"));
  596. }
  597. }
  598. /************************************************************************
  599. * When a node is requested by the user this function is called to allow us
  600. * to add auxiliary data to the node object.
  601. */
  602. function chado_feature_load($node){
  603. // add the feature_id for this node:
  604. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  605. $map = db_fetch_object(db_query($sql, $node->vid));
  606. // get information about this feature and add it to the items in this node
  607. $sql = "SELECT F.feature_id, F.name as featurename, F.uniquename, ".
  608. "F.residues, F.seqlen, O.genus, O.species, O.common_name, ".
  609. " CVT.name as cvname, O.organism_id, F.type_id, F.is_obsolete ".
  610. "FROM {Feature} F ".
  611. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  612. " INNER JOIN CVterm CVT ON F.type_id = CVT.cvterm_id ".
  613. "WHERE F.feature_id = %d";
  614. $previous_db = tripal_db_set_active('chado'); // use chado database
  615. $feature = db_fetch_object(db_query($sql,$map->feature_id));
  616. tripal_db_set_active($previous_db); // now use drupal database
  617. $additions->feature = $feature;
  618. $additions->seqlen = $feature->seqlen;
  619. // add organism node nid
  620. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
  621. $org_nid = db_result(db_query($sql, $additions->feature->organism_id));
  622. $additions->org_nid = $org_nid;
  623. $additions->accession = variable_get('chado_feature_accession_prefix','ID') . $feature->feature_id;
  624. // add organism details
  625. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  626. $previous_db = tripal_db_set_active('chado'); // use chado database
  627. $organism = db_fetch_object(db_query($sql, $additions->feature->organism_id));
  628. tripal_db_set_active($previous_db); // now use drupal database
  629. $additions->organism = $organism;
  630. // add the feature synonyms
  631. $sql = "SELECT S.name ".
  632. "FROM {Feature_Synonym} FS ".
  633. " INNER JOIN Synonym S ".
  634. " ON FS.synonym_id = S.Synonym_id ".
  635. "WHERE FS.feature_id = %d";
  636. $previous_db = tripal_db_set_active('chado'); // use chado database
  637. $results = db_query($sql,$map->feature_id);
  638. tripal_db_set_active($previous_db); // now use drupal database
  639. $synonyms = array();
  640. $i=0;
  641. while($synonym = db_fetch_object($results)){
  642. $synonyms[$i++] = $synonym;
  643. }
  644. $additions->synonyms = $synonyms;
  645. // add feature references in external databases
  646. $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
  647. " DB.db_id, DB.name as db_name, DB.urlprefix ".
  648. "FROM {Feature} F ".
  649. " INNER JOIN Feature_dbxref FDBX on F.feature_id = FDBX.feature_id ".
  650. " INNER JOIN Dbxref DBX on DBX.dbxref_id = FDBX.dbxref_id ".
  651. " INNER JOIN DB on DB.db_id = DBX.db_id ".
  652. "WHERE F.feature_id = %d";
  653. $previous_db = tripal_db_set_active('chado'); // use chado database
  654. $results = db_query($sql,$map->feature_id);
  655. tripal_db_set_active($previous_db); // now use drupal database
  656. $references = array();
  657. $i=0;
  658. while($accession = db_fetch_object($results)){
  659. $references[$i++] = $accession;
  660. }
  661. $additions->references = $references;
  662. // add the feature relationsips
  663. $sql = "SELECT
  664. FS.name as subject_name,
  665. FS.uniquename as subject_uniquename,
  666. CVTS.name as subject_type,
  667. FS.residues as subject_residues,
  668. FR.subject_id,
  669. FR.type_id,
  670. CVT.name as rel_type,
  671. FO.name as object_name,
  672. FO.uniquename as object_uniquename,
  673. CVTO.name as object_type,
  674. FO.residues as object_residues,
  675. FR.object_id,
  676. FLS.name as src_name,
  677. FLS.feature_id as src_featureid,
  678. CVTR.name as src_cvname,
  679. CVTR.cvterm_id as src_type_id,
  680. FL.fmin,
  681. FL.fmax,
  682. FL.is_fmin_partial,
  683. FL.is_fmax_partial,
  684. FL.strand,
  685. FL.phase,
  686. FL.residue_info
  687. FROM {feature_relationship} FR
  688. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  689. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  690. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  691. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  692. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  693. LEFT JOIN {featureloc} FL on FL.feature_id = FS.feature_id
  694. LEFT JOIN {feature} FLS on FLS.feature_id = FL.srcfeature_id
  695. LEFT JOIN {cvterm} CVTR on FLS.type_id = CVTR.cvterm_id
  696. ";
  697. $osql = "$sql WHERE FR.object_id = %d ORDER BY FLS.name, FL.fmin ASC";
  698. $ssql = "$sql WHERE FR.subject_id = %d ORDER BY FLS.name, FL.fmin ASC";
  699. // first get the relationships where this feature is the object
  700. // then where it is the subject
  701. $previous_db = tripal_db_set_active('chado'); // use chado database
  702. $oresults = db_query($osql, $map->feature_id);
  703. $sresults = db_query($ssql, $map->feature_id);
  704. tripal_db_set_active($previous_db); // now use drupal database
  705. // identify the drupal node for each feature and add that to the
  706. // relationship records
  707. $srelationships = array();
  708. $orelationships = array();
  709. $i=0;
  710. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  711. while($rel = db_fetch_object($oresults)){
  712. $node = db_fetch_object(db_query($nodesql,$rel->subject_id));
  713. if($node){
  714. $rel->subject_nid = $node->nid;
  715. }
  716. $node = db_fetch_object(db_query($nodesql,$rel->object_id));
  717. if($node){
  718. $rel->object_nid = $node->nid;
  719. }
  720. $orelationships[$i++] = $rel;
  721. }
  722. $i=0;
  723. while($rel = db_fetch_object($sresults)){
  724. $node = db_fetch_object(db_query($nodesql,$rel->subject_id));
  725. if($node){
  726. $rel->subject_nid = $node->nid;
  727. }
  728. $node = db_fetch_object(db_query($nodesql,$rel->object_id));
  729. if($node){
  730. $rel->object_nid = $node->nid;
  731. }
  732. $srelationships[$i++] = $rel;
  733. }
  734. $additions->object_relationships = $orelationships;
  735. $additions->subject_relationships = $srelationships;
  736. // get the locations for this feature
  737. $sql = "SELECT F.name,F.feature_id, F.uniquename,CVT.name as cvname, CVT.cvterm_id,
  738. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand,
  739. FL.phase, FL.residue_info
  740. FROM featureloc FL
  741. INNER JOIN feature F on FL.srcfeature_id = F.feature_id
  742. INNER JOIN cvterm CVT on F.type_id = CVT.cvterm_id
  743. WHERE FL.feature_id = %d";
  744. $previous_db = tripal_db_set_active('chado'); // use chado database
  745. $flresults = db_query($sql, $map->feature_id);
  746. tripal_db_set_active($previous_db); // now use drupal database
  747. $i=0;
  748. $featurelocs = array();
  749. while($loc = db_fetch_object($flresults)){
  750. $featurelocs[$i++] = $loc;
  751. }
  752. $additions->featurelocs = $featurelocs;
  753. // get features that are aligned to this feature
  754. $sql = "SELECT F.name,F.feature_id, F.uniquename,CVT.name as cvname, CVT.cvterm_id,
  755. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand,
  756. FL.phase, FL.residue_info
  757. FROM featureloc FL
  758. INNER JOIN feature F on FL.feature_id = F.feature_id
  759. INNER JOIN cvterm CVT on F.type_id = CVT.cvterm_id
  760. WHERE FL.srcfeature_id = %d";
  761. $previous_db = tripal_db_set_active('chado'); // use chado database
  762. $flresults = db_query($sql, $map->feature_id);
  763. tripal_db_set_active($previous_db); // now use drupal database
  764. $i=0;
  765. $myfeaturelocs = array();
  766. while($loc = db_fetch_object($flresults)){
  767. $myfeaturelocs[$i++] = $loc;
  768. }
  769. $additions->myfeaturelocs = $myfeaturelocs;
  770. return $additions;
  771. }
  772. /************************************************************************
  773. * This function customizes the view of the chado_feature node. It allows
  774. * us to generate the markup.
  775. */
  776. function chado_feature_view ($node, $teaser = FALSE, $page = FALSE) {
  777. if (!$teaser) {
  778. // use drupal's default node view:
  779. $node = node_prepare($node, $teaser);
  780. // if we're building the node for searching then
  781. // we want to handle this within the module and
  782. // not allow theme customization. We don't want to
  783. // index all items (such as DNA sequence).
  784. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  785. $node->content['index_version'] = array(
  786. '#value' => theme('tripal_feature_search_index',$node),
  787. '#weight' => 1,
  788. );
  789. }
  790. else if($node->build_mode == NODE_BUILD_SEARCH_RESULT){
  791. $node->content['index_version'] = array(
  792. '#value' => theme('tripal_feature_search_results',$node),
  793. '#weight' => 1,
  794. );
  795. }
  796. else {
  797. // do nothing here, let the theme derived template handle display
  798. }
  799. }
  800. return $node;
  801. }
  802. /*******************************************************************************
  803. * Display feature information for associated organisms. This function also
  804. * provides contents for indexing
  805. */
  806. function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
  807. switch ($op) {
  808. // Note that this function only adds feature view to an organism node.
  809. // The view of a feature node is controled by the theme *.tpl file
  810. case 'view':
  811. // Set the node types for showing feature information
  812. $types_to_show = array('chado_organism', 'chado_library');
  813. // Abort if this node is not one of the types we should show.
  814. if (!in_array($node->type, $types_to_show, TRUE)) {
  815. break;
  816. }
  817. // Add feature to the content item if it's not a teaser
  818. if (!$teaser) {
  819. // Show feature browser
  820. $node->content['tripal_feature_browser'] = array(
  821. '#value' => theme('tripal_feature_browser', $node),
  822. '#weight' => 5
  823. );
  824. $node->content['tripal_feature_org_counts'] = array(
  825. '#value' => theme('tripal_feature_counts', $node),
  826. '#weight' => 4
  827. );
  828. }
  829. }
  830. }
  831. /************************************************************************
  832. * We need to let drupal know about our theme functions and their arguments.
  833. * We create theme functions to allow users of the module to customize the
  834. * look and feel of the output generated in this module
  835. */
  836. function tripal_feature_theme () {
  837. return array(
  838. 'tripal_feature_search_index' => array (
  839. 'arguments' => array('node'),
  840. ),
  841. 'tripal_feature_search_results' => array (
  842. 'arguments' => array('node'),
  843. ),
  844. 'tripal_feature_browser' => array (
  845. 'arguments' => array('node'),
  846. ),
  847. 'tripal_feature_counts' => array (
  848. 'arguments' => array('node'),
  849. )
  850. );
  851. }
  852. /*******************************************************************************
  853. * create a list of features for the organism and pie chart
  854. */
  855. function theme_tripal_feature_counts($node){
  856. // don't show the summary if the settings in the admin page is turned off
  857. $show_browser = variable_get('tripal_feature_summary_setting',array('show_feature_summary'));
  858. if(strcmp($show_browser[0],'show_feature_summary')!=0){
  859. return;
  860. }
  861. // get the feature counts. This is dependent on a materialized view
  862. // installed with the organism module
  863. $content = '';
  864. if ($node->organism_id && $node->type == 'chado_organism') {
  865. $sql = "SELECT * FROM {organism_feature_count} ".
  866. "WHERE organism_id = %d AND NOT feature_type = 'EST_match' ".
  867. "ORDER BY num_features desc";
  868. $features = array();
  869. $previous_db = tripal_db_set_active('chado'); // use chado database
  870. $results = db_query($sql,$node->organism_id);
  871. tripal_db_set_active($previous_db); // now use drupal database
  872. $feature = db_fetch_object($results); // retrieve the first result
  873. if ($feature) {
  874. $content .= "<div class=\"tripal_feature_summary-info-box\"><br>
  875. <div class=\"tripal_expandableBox\">".
  876. "<h3>Feature Summary</h3>".
  877. "</div>";
  878. $content .= "<div class=\"tripal_expandableBoxContent\">";
  879. $content .= "<table class=\"tripal_table_horz\">";
  880. $content .= " <tr>";
  881. $content .= " <th class=\"dbfieldname\">Type</th>";
  882. $content .= " <th class=\"dbfieldname\">Number</th>";
  883. $content .= " </tr>";
  884. do {
  885. $content .= "<tr>";
  886. $content .= " <td>$feature->feature_type</td>";
  887. $content .= " <td>". number_format($feature->num_features) . "</td>";
  888. $content .= "</tr>";
  889. } while($feature = db_fetch_object($results));
  890. $content .= "</table>";
  891. $content .= "
  892. <img class=\"tripal_cv_chart\" id=\"tripal_feature_cv_chart_$node->organism_id\" src=\"\" border=\"0\">
  893. ";
  894. $content .= "</div></div>";
  895. }
  896. }
  897. return $content;
  898. }
  899. /************************************************************************
  900. *
  901. */
  902. function tripal_feature_cv_chart($chart_id){
  903. // The CV module will create the JSON array necessary for buillding a
  904. // pie chart using jgChart and Google Charts. We have to pass to it
  905. // a table that contains count information, tell it which column
  906. // contains the cvterm_id and provide a filter for getting the
  907. // results we want from the table.
  908. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/","$1",$chart_id);
  909. $options = array(
  910. count_mview => 'organism_feature_count',
  911. cvterm_id_column => 'cvterm_id',
  912. count_column => 'num_features',
  913. size => '650x200',
  914. filter => "CNT.organism_id = $organism_id AND NOT feature_type = 'EST_match' ",
  915. );
  916. return $options;
  917. }
  918. /************************************************************************
  919. *
  920. */
  921. function tripal_feature_cv_tree($tree_id){
  922. // The CV module will create the JSON array necessary for buillding a
  923. // pie chart using jgChart and Google Charts. We have to pass to it
  924. // a table that contains count information, tell it which column
  925. // contains the cvterm_id and provide a filter for getting the
  926. // results we want from the table.
  927. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/","$1",$tree_id);
  928. $options = array(
  929. cv_id => tripal_cv_get_cv_id('sequence'),
  930. count_mview => 'organism_feature_count',
  931. cvterm_id_column => 'cvterm_id',
  932. count_column => 'num_features',
  933. filter => "CNT.organism_id = $organism_id",
  934. label => 'Features',
  935. );
  936. return $options;
  937. }
  938. /*******************************************************************************
  939. * create a simple paged feature browser
  940. */
  941. function theme_tripal_feature_browser($node){
  942. // don't show the browser if the settings in the admin page is turned off
  943. $show_browser = variable_get('tripal_feature_browse_setting',array('show_feature_browser'));
  944. if(strcmp($show_browser[0],'show_feature_browser')!=0){
  945. return;
  946. }
  947. if ($node->organism_id && $node->type == 'chado_organism') {
  948. # get the list of available sequence ontology terms for which
  949. # we will build drupal pages from features in chado. If a feature
  950. # is not one of the specified typse we won't build a node for it.
  951. $allowed_types = variable_get('chado_feature_types','EST contig');
  952. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  953. $so_terms = split(' ',$allowed_types);
  954. $where_cvt = "";
  955. foreach ($so_terms as $term){
  956. $where_cvt .= "CVT.name = '$term' OR ";
  957. }
  958. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  959. // get the features for this organism
  960. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  961. "FROM {feature} F ".
  962. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  963. "WHERE organism_id = $node->organism_id and ($where_cvt) ".
  964. "ORDER BY feature_id ASC";
  965. // the counting SQL
  966. $csql = "SELECT count(*) ".
  967. "FROM {feature} F".
  968. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  969. "WHERE organism_id = $node->organism_id and ($where_cvt) ".
  970. "GROUP BY organism_id ";
  971. $previous_db = tripal_db_set_active('chado'); // use chado database
  972. $features = pager_query($sql,10,0,$csql);
  973. tripal_db_set_active($previous_db); // now use drupal database
  974. $content = "<br><div id=\"tripal_feature_box\" class=\"feature-info-box\">";
  975. $content .= "<div class=\"tripal_expandableBox\">".
  976. "<h3>Browse Features</h3>".
  977. "</div>";
  978. $content .= "<div class=\"tripal_expandableBoxContent\">";
  979. $content .= "Below are the features associated with this organism.\n";
  980. $content .= "<table class=\"tripal_table_horz\">";
  981. $content .= " <tr>";
  982. $content .= " <th>Feature Name</th>";
  983. $content .= " <th>Type</th>";
  984. $content .= " </tr>";
  985. // prepare the query that will lookup node ids
  986. $sql = "SELECT nid FROM {chado_feature} ".
  987. "WHERE feature_id = %d";
  988. while($feature = db_fetch_object($features)){
  989. $node = db_fetch_object(db_query($sql,$feature->feature_id));
  990. if($node){
  991. $name= "<a href=\"" . url("node/$node->nid") . "\">$feature->name</a>";
  992. } else {
  993. $name= "$feature->name";
  994. }
  995. $content .= " <tr>";
  996. $content .= " <td>$name</td>";
  997. $content .= " <td>$feature->cvname</td>";
  998. $content .= " </tr>";
  999. }
  1000. $content .= "</table>";
  1001. $content .= theme('pager');
  1002. $content .= "</div></div>";
  1003. return $content;
  1004. }
  1005. }
  1006. /************************************************************************
  1007. * This function is an extension of the chado_feature_view by providing
  1008. * the markup for the feature object THAT WILL BE INDEXED.
  1009. */
  1010. function theme_tripal_feature_search_index ($node) {
  1011. $feature = $node->feature;
  1012. $content = '';
  1013. // get the accession prefix
  1014. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1015. $content .= "<h1>$feature->uniquename</h1>. ";
  1016. $content .= "<strong>$aprefix$feature->feature_id.</strong> ";
  1017. $content .= "$feature->cvname ";
  1018. $content .= "$feature->common_name ";
  1019. // add the synonyms of this feature to the text for searching
  1020. $synonyms = $node->synonyms;
  1021. if(count($synonyms) > 0){
  1022. foreach ($synonyms as $result){
  1023. $content .= "$result->name ";
  1024. }
  1025. }
  1026. return $content;
  1027. }
  1028. /************************************************************************
  1029. * This function is an extension of the chado_feature_view by providing
  1030. * the markup for the feature object THAT WILL BE INDEXED.
  1031. */
  1032. function theme_tripal_feature_search_results ($node) {
  1033. $feature = $node->feature;
  1034. $content = '';
  1035. // get the accession prefix
  1036. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1037. $content .= "Feature Name: <h1>$feature->uniquename</h1>. ";
  1038. $content .= "<strong>Accession: $aprefix$feature->feature_id.</strong>";
  1039. $content .= "Type: $feature->cvname. ";
  1040. $content .= "Organism: $feature->common_name. ";
  1041. // add the synonyms of this feature to the text for searching
  1042. $synonyms = $node->synonyms;
  1043. if(count($synonyms) > 0){
  1044. $content .= "Synonyms: ";
  1045. foreach ($synonyms as $result){
  1046. $content .= "$result->name, ";
  1047. }
  1048. }
  1049. return $content;
  1050. }
  1051. /************************************************************************
  1052. *
  1053. */
  1054. function tripal_feature_set_vocabulary (){
  1055. //include the file containing the required functions for adding taxonomy vocabs
  1056. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1057. // get the vocabularies so that we make sure we don't recreate
  1058. // the vocabs that already exist
  1059. $vocabularies = taxonomy_get_vocabularies();
  1060. $ft_vid = NULL;
  1061. $op_vid = NULL;
  1062. $lb_vid = NULL;
  1063. $an_vid = NULL;
  1064. // These taxonomic terms are hard coded because we
  1065. // konw we have these relationships in the chado tables
  1066. // through foreign key relationships. The tripal
  1067. // modules that correspond to these chado "modules" don't
  1068. // need to be installed for the taxonomy to work.
  1069. foreach($vocabularies as $vocab){
  1070. if($vocab->name == 'Feature Type'){
  1071. $ft_vid = $vocab->vid;
  1072. }
  1073. if($vocab->name == 'Organism'){
  1074. $op_vid = $vocab->vid;
  1075. }
  1076. if($vocab->name == 'Library'){
  1077. $lb_vid = $vocab->vid;
  1078. }
  1079. if($vocab->name == 'Analysis'){
  1080. $an_vid = $vocab->vid;
  1081. }
  1082. }
  1083. if(!$ft_vid){
  1084. $form_state = array();
  1085. $values = array(
  1086. 'name' => t('Feature Type'),
  1087. 'nodes' => array('chado_feature' => 'chado_feature'),
  1088. 'description' => t('The feature type (or SO cvterm for this feature).'),
  1089. 'help' => t('Select the term that matches the feature '),
  1090. 'tags' => 0,
  1091. 'hierarchy' => 1,
  1092. 'relations' => 1,
  1093. 'multiple' => 0,
  1094. 'required' => 0,
  1095. 'weight' => 1,
  1096. );
  1097. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1098. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1099. }
  1100. if(!$op_vid){
  1101. $form_state = array();
  1102. $values = array(
  1103. 'name' => t('Organism'),
  1104. 'nodes' => array('chado_feature' => 'chado_feature'),
  1105. 'description' => t('The organism to which this feature belongs.'),
  1106. 'help' => t('Select the term that matches the feature '),
  1107. 'tags' => 0,
  1108. 'hierarchy' => 1,
  1109. 'relations' => 1,
  1110. 'multiple' => 0,
  1111. 'required' => 0,
  1112. 'weight' => 2,
  1113. );
  1114. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1115. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1116. }
  1117. if(!$lb_vid){
  1118. $form_state = array();
  1119. $values = array(
  1120. 'name' => t('Library'),
  1121. 'nodes' => array('chado_feature' => 'chado_feature'),
  1122. 'description' => t('Chado features associated with a library are assigned the term associated with the library'),
  1123. 'help' => t('Select the term that matches the feature '),
  1124. 'tags' => 0,
  1125. 'hierarchy' => 1,
  1126. 'relations' => 1,
  1127. 'multiple' => 0,
  1128. 'required' => 0,
  1129. 'weight' => 3,
  1130. );
  1131. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  1132. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1133. }
  1134. if(!$an_vid){
  1135. $form_state = array();
  1136. $values = array(
  1137. 'name' => t('Analysis'),
  1138. 'nodes' => array('chado_feature' => 'chado_feature'),
  1139. 'description' => t('Any analysis to which this feature belongs.'),
  1140. 'help' => t('Select the term that matches the feature '),
  1141. 'tags' => 0,
  1142. 'hierarchy' => 1,
  1143. 'relations' => 1,
  1144. 'multiple' => 1,
  1145. 'required' => 0,
  1146. 'weight' => 4,
  1147. );
  1148. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1149. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1150. }
  1151. }
  1152. /************************************************************************
  1153. *
  1154. */
  1155. function tripal_feature_del_vocabulary(){
  1156. //include the file containing the required functions for adding taxonomy vocabs
  1157. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1158. // get the vocabularies
  1159. $vocabularies = taxonomy_get_vocabularies();
  1160. // These taxonomic terms are hard coded because we
  1161. // know we have these relationships in the chado tables
  1162. // through foreign key relationships. The tripal
  1163. // modules that correspond to these chado "modules" don't
  1164. // need to be installed for the taxonomy to work.
  1165. foreach($vocabularies as $vocab){
  1166. if($vocab->name == 'Feature Type'){
  1167. taxonomy_del_vocabulary($vocab->vid);
  1168. }
  1169. if($vocab->name == 'Organism'){
  1170. taxonomy_del_vocabulary($vocab->vid);
  1171. }
  1172. if($vocab->name == 'Library'){
  1173. taxonomy_del_vocabulary($vocab->vid);
  1174. }
  1175. if($vocab->name == 'Analysis'){
  1176. taxonomy_del_vocabulary($vocab->vid);
  1177. }
  1178. }
  1179. }
  1180. /************************************************************************
  1181. *
  1182. */
  1183. function tripal_features_set_taxonomy($max_sync = 0,$job_id = NULL){
  1184. // make sure our vocabularies are cleaned and reset before proceeding
  1185. tripal_feature_del_vocabulary();
  1186. tripal_feature_set_vocabulary();
  1187. // iterate through all drupal feature nodes and set the taxonomy
  1188. $results = db_query("SELECT * FROM {chado_feature}");
  1189. $nsql = "SELECT * FROM {node} ".
  1190. "WHERE nid = %d";
  1191. $i = 0;
  1192. // load into ids array
  1193. $count = 0;
  1194. $chado_features = array();
  1195. while($chado_feature = db_fetch_object($results)){
  1196. $chado_features[$count] = $chado_feature;
  1197. $count++;
  1198. }
  1199. // Iterate through features that need to be synced
  1200. $interval = intval($count * 0.01);
  1201. foreach($chado_features as $chado_feature){
  1202. // update the job status every 1% features
  1203. if($job_id and $i % $interval == 0){
  1204. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1205. }
  1206. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  1207. tripal_feature_set_taxonomy($node,$chado_feature->feature_id);
  1208. $i++;
  1209. }
  1210. }
  1211. /************************************************************************
  1212. *
  1213. */
  1214. function tripal_feature_set_taxonomy ($node,$feature_id){
  1215. // iterate through the taxonomy classes that have been
  1216. // selected by the admin user and make sure we only set those
  1217. $tax_classes = variable_get('tax_classes', '');
  1218. $do_ft = 0;
  1219. $do_op = 0;
  1220. $do_lb = 0;
  1221. $do_an = 0;
  1222. foreach($tax_classes as $class){
  1223. if(strcmp($class ,'organism')==0){
  1224. $do_op = 1;
  1225. }
  1226. if(strcmp($class,'feature_type')==0){
  1227. $do_ft = 1;
  1228. }
  1229. if(strcmp($class,'library')==0){
  1230. $do_lb = 1;
  1231. }
  1232. if(strcmp($class,'analysis')==0){
  1233. $do_an = 1;
  1234. }
  1235. }
  1236. // get the list of vocabularies and find our two vocabularies of interest
  1237. $vocabularies = taxonomy_get_vocabularies();
  1238. $ft_vid = NULL;
  1239. $op_vid = NULL;
  1240. $lb_vid = NULL;
  1241. $an_vid = NULL;
  1242. foreach($vocabularies as $vocab){
  1243. if($vocab->name == 'Feature Type'){
  1244. $ft_vid = $vocab->vid;
  1245. }
  1246. if($vocab->name == 'Organism'){
  1247. $op_vid = $vocab->vid;
  1248. }
  1249. if($vocab->name == 'Library'){
  1250. $lb_vid = $vocab->vid;
  1251. }
  1252. if($vocab->name == 'Analysis'){
  1253. $an_vid = $vocab->vid;
  1254. }
  1255. }
  1256. // get the cvterm and the organism for this feature
  1257. $sql = "SELECT CVT.name AS cvname, O.genus, O.species ".
  1258. "FROM {CVTerm} CVT ".
  1259. " INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
  1260. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  1261. "WHERE F.feature_id = $feature_id";
  1262. $previous_db = tripal_db_set_active('chado'); // use chado database
  1263. $feature = db_fetch_object(db_query($sql));
  1264. tripal_db_set_active($previous_db); // now use drupal database
  1265. // Set the feature type for this feature
  1266. if($do_ft && $ft_vid){
  1267. $tags["$ft_vid"] = "$feature->cvname";
  1268. }
  1269. // Set the organism for this feature type
  1270. if($do_op && $op_vid){
  1271. $tags["$op_vid"] = "$feature->genus $feature->species";
  1272. }
  1273. // get the library that this feature may belong to and add it as taxonomy
  1274. if($do_lb && $lb_vid){
  1275. $sql = "SELECT L.name ".
  1276. "FROM {Library} L ".
  1277. " INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
  1278. "WHERE LF.feature_id = %d ";
  1279. $previous_db = tripal_db_set_active('chado'); // use chado database
  1280. $library = db_fetch_object(db_query($sql,$feature_id));
  1281. tripal_db_set_active($previous_db); // now use drupal database
  1282. $tags["$lb_vid"] = "$library->name";
  1283. }
  1284. // now add the taxonomy to the node
  1285. $terms['tags'] = $tags;
  1286. taxonomy_node_save($node,$terms);
  1287. // print "Setting $node->name: " . implode(", ",$tags) . "\n";
  1288. // get the analysis that this feature may belong to and add it as taxonomy
  1289. // We'll add each one individually since there may be more than one analysis
  1290. if($do_an && $an_vid){
  1291. $sql = "SELECT A.name ".
  1292. "FROM {Analysis} A ".
  1293. " INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
  1294. "WHERE AF.feature_id = $feature_id ";
  1295. $results = db_query($sql);
  1296. $previous_db = tripal_db_set_active('chado'); // use chado database
  1297. $analysis_terms = array();
  1298. while($analysis=db_fetch_object($results)){
  1299. $tags2["$an_vid"] = "$analysis->name";
  1300. $terms['tags'] = $tags2;
  1301. taxonomy_node_save($node,$terms);
  1302. }
  1303. tripal_db_set_active($previous_db); // now use drupal database
  1304. }
  1305. }
  1306. /************************************************************************
  1307. *
  1308. */
  1309. function tripal_features_cleanup($dummy = NULL, $job_id = NULL) {
  1310. // build the SQL statments needed to check if nodes point to valid features
  1311. $dsql = "SELECT * FROM {node} WHERE type = 'chado_feature' order by nid";
  1312. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1313. $csql = "SELECT * FROM {chado_feature} where nid = %d ";
  1314. $cfsql= "SELECT * FROM {chado_feature}";
  1315. $tsql = "SELECT * FROM {feature} F ".
  1316. " INNER JOIN CVTerm CVT ON F.type_id = CVT.cvterm_id ".
  1317. "WHERE feature_id = %d AND (";
  1318. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  1319. foreach($supported_ftypes as $ftype){
  1320. $tsql .= " CVT.name = '$ftype' OR ";
  1321. }
  1322. $tsql .= " 0=1) "; // add a 0=1 just as a filler so we don't have to remove a trailing 'OR'
  1323. // load into nodes array
  1324. $results = db_query($dsql);
  1325. $count = 0;
  1326. $nodes = array();
  1327. while($node = db_fetch_object($results)){
  1328. $nodes[$count] = $node;
  1329. $count++;
  1330. }
  1331. // load the chado_features into an array
  1332. $results = db_query($cfsql);
  1333. $cnodes = array();
  1334. while($node = db_fetch_object($results)){
  1335. $cnodes[$count] = $node;
  1336. $count++;
  1337. }
  1338. $interval = intval($count * 0.01);
  1339. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1340. foreach($nodes as $nid){
  1341. // update the job status every 1% features
  1342. if($job_id and $i % $interval == 0){
  1343. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1344. }
  1345. // first check to see if the node has a corresponding entry
  1346. // in the chado_feature table. If not then delete the node.
  1347. $feature = db_fetch_object(db_query($csql,$nid->nid));
  1348. if(!$feature){
  1349. node_delete($nid->nid);
  1350. $message = "Missing in chado_feature table.... DELETING: $nid->nid\n";
  1351. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1352. continue;
  1353. }
  1354. // second check to see if the node is for a feature of an allowed type.
  1355. // if not, then delete the node. This check will also take care of the
  1356. // case when a node exists and an entry in the chado_feature table exists
  1357. // but no feature with a matching feature_id exists
  1358. $previous_db = tripal_db_set_active('chado'); // use chado database
  1359. $ftype = db_fetch_object(db_query($tsql,$feature->feature_id));
  1360. tripal_db_set_active($previous_db); // now use drupal database
  1361. if(!$ftype){
  1362. node_delete($nid->nid);
  1363. db_query("DELETE FROM {chado_feature} WHERE feature_id = $feature->feature_id");
  1364. $message = "Node of the wrong feature type.... DELETING: $nid->nid\n";
  1365. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1366. }
  1367. $i++;
  1368. }
  1369. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1370. foreach($cnodes as $nid){
  1371. // update the job status every 1% features
  1372. if($job_id and $i % $interval == 0){
  1373. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1374. }
  1375. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1376. if(!$node){
  1377. db_query("DELETE FROM {chado_feature} WHERE nid = $nid->nid");
  1378. $message = "chado_feature missing node.... DELETING: $nid->nid\n";
  1379. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1380. }
  1381. $i++;
  1382. }
  1383. return '';
  1384. }
  1385. /************************************************************************
  1386. *
  1387. */
  1388. function tripal_feature_return_fasta($feature,$desc){
  1389. $fasta = ">" . variable_get('chado_feature_accession_prefix','ID') . "$feature->feature_id|$feature->name";
  1390. $fasta .= " $desc\n";
  1391. $fasta .= wordwrap($feature->residues, 50, "\n", true);
  1392. $fasta .= "\n\n";
  1393. return $fasta;
  1394. }