tripal_feature.module 62 KB

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