tripal_chado.semweb.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. <?php
  2. /**
  3. * Adds defaults to the chado_semweb table.
  4. */
  5. function tripal_chado_populate_chado_semweb_table() {
  6. // Add in all tables and fields into the chado_semweb table.
  7. $chado_tables = chado_get_table_names(TRUE);
  8. foreach ($chado_tables as $chado_table) {
  9. tripal_add_chado_semweb_table($chado_table);
  10. }
  11. // Now set defaults!
  12. tripal_chado_populate_vocab_EDAM();
  13. tripal_chado_populate_vocab_FOAF();
  14. tripal_chado_populate_vocab_IAO();
  15. tripal_chado_populate_vocab_LOCAL();
  16. tripal_chado_populate_vocab_NCBITAXON();
  17. tripal_chado_populate_vocab_OBI();
  18. tripal_chado_populate_vocab_RDFS();
  19. tripal_chado_populate_vocab_SBO();
  20. tripal_chado_populate_vocab_SCHEMA();
  21. tripal_chado_populate_vocab_SWO();
  22. tripal_chado_populate_vocab_TAXRANK();
  23. }
  24. /**
  25. * Adds the friend of a friend database and terms.
  26. */
  27. function tripal_chado_populate_vocab_FOAF() {
  28. tripal_insert_cv('foaf','Friend of a Friend');
  29. tripal_insert_db(array(
  30. 'name' => 'foaf',
  31. 'description' => 'Friend of a Friend',
  32. 'url' => 'http://www.foaf-project.org/',
  33. 'urlprefix' => 'http://xmlns.com/foaf/spec/#',
  34. ));
  35. tripal_insert_cv('foaf','Friend of a Friend. A dictionary of people-related terms that can be used in structured data).');
  36. }
  37. /**
  38. * Adds the RDFS database and terms.
  39. */
  40. function tripal_chado_populate_vocab_RDFS() {
  41. tripal_insert_db(array(
  42. 'name' => 'rdfs',
  43. 'description' => 'Resource Description Framework Schema',
  44. 'url' => 'https://www.w3.org/TR/rdf-schema/',
  45. 'urlprefix' => 'https://www.w3.org/TR/rdf-schema/#ch_',
  46. ));
  47. tripal_insert_cv('rdfs','Resource Description Framework Schema');
  48. $name = tripal_insert_cvterm(array(
  49. 'id' => 'rdfs:type',
  50. 'name' => 'type',
  51. 'cv_name' => 'rdfs',
  52. 'definition' => 'The type of resource.',
  53. ));
  54. }
  55. /**
  56. * Adds the Schema.org database and terms.
  57. */
  58. function tripal_chado_populate_vocab_SCHEMA() {
  59. tripal_insert_db(array(
  60. 'name' => 'schema',
  61. 'description' => 'Schema.org.',
  62. 'url' => 'https://schema.org/',
  63. 'urlprefix' => 'https://schema.org/{accession}',
  64. ));
  65. tripal_insert_cv('schema','Schema.org. Schema.org is sponsored by Google, Microsoft, Yahoo and Yandex. The vocabularies are developed by an open community process.');
  66. $term = tripal_insert_cvterm(array(
  67. 'id' => 'schema:name',
  68. 'name' => 'name',
  69. 'cv_name' => 'schema',
  70. 'definition' => 'The name of the item.',
  71. ));
  72. tripal_associate_chado_semweb_term(NULL, 'name', $term);
  73. $term = tripal_insert_cvterm(array(
  74. 'id' => 'schema:alternateName',
  75. 'name' => 'alternateName',
  76. 'cv_name' => 'schema',
  77. 'definition' => 'An alias for the item.',
  78. ));
  79. $term = tripal_insert_cvterm(array(
  80. 'id' => 'schema:comment',
  81. 'name' => 'comment',
  82. 'cv_name' => 'schema',
  83. 'definition' => 'Comments, typically from users.',
  84. ));
  85. tripal_associate_chado_semweb_term(NULL, 'comment', $term);
  86. $term = tripal_insert_cvterm(array(
  87. 'id' => 'schema:description',
  88. 'name' => 'description',
  89. 'cv_name' => 'schema',
  90. 'definition' => 'A description of the item.',
  91. ));
  92. tripal_associate_chado_semweb_term(NULL, 'description', $term);
  93. tripal_associate_chado_semweb_term('organism', 'comment', $term);
  94. }
  95. /**
  96. * Adds the EDAM database and terms.
  97. */
  98. function tripal_chado_populate_vocab_EDAM() {
  99. tripal_insert_db(array(
  100. 'name' => 'data',
  101. 'description' => 'Bioinformatics operations, data types, formats, identifiers and topics.',
  102. 'url' => 'http://edamontology.org/page',
  103. 'urlprefix' => 'http://edamontology.org/{db}_{accession}',
  104. ));
  105. tripal_insert_db(array(
  106. 'name' => 'EDAM',
  107. 'description' => 'Bioinformatics operations, data types, formats, identifiers and topics.',
  108. 'url' => 'http://edamontology.org/page',
  109. 'urlprefix' => 'http://edamontology.org/{db}_{accession}',
  110. ));
  111. $term = tripal_insert_cvterm(array(
  112. 'id' => 'data:1249',
  113. 'name' => 'Sequence length',
  114. 'cv_name' => 'EDAM',
  115. 'definition' => 'The size (length) of a sequence, subsequence or region in a sequence, or range(s) of lengths.',
  116. ));
  117. tripal_associate_chado_semweb_term('feature', 'seqlen', $term);
  118. $term = tripal_insert_cvterm(array(
  119. 'id' => 'data:2190',
  120. 'name' => 'Sequence checksum',
  121. 'cv_name' => 'EDAM',
  122. 'definition' => 'A fixed-size datum calculated (by using a hash function) for a molecular sequence, typically for purposes of error detection or indexing.',
  123. ));
  124. tripal_associate_chado_semweb_term('feature', 'md5checksum', $term);
  125. $term = tripal_insert_cvterm(array(
  126. 'id' => 'data:2091',
  127. 'name' => 'Accession',
  128. 'cv_name' => 'EDAM',
  129. 'definition' => 'A persistent (stable) and unique identifier, typically identifying an object (entry) from a database.',
  130. ));
  131. tripal_associate_chado_semweb_term(NULL, 'dbxref_id', $term);
  132. $term = tripal_insert_cvterm(array(
  133. 'id' => 'data:2044',
  134. 'name' => 'Sequence',
  135. 'cv_name' => 'EDAM',
  136. 'definition' => 'One or more molecular sequences, possibly with associated annotation..',
  137. ));
  138. tripal_associate_chado_semweb_term('feature', 'residues', $term);
  139. $term = tripal_insert_cvterm(array(
  140. 'id' => 'data:0842',
  141. 'name' => 'Identifier',
  142. 'cv_name' => 'EDAM',
  143. 'definition' => 'A text token, number or something else which identifies an entity, but which may not be persistent (stable) or unique (the same identifier may identify multiple things).',
  144. ));
  145. tripal_associate_chado_semweb_term(NULL, 'uniquename', $term);
  146. }
  147. /**
  148. * Adds the Information Artifact Ontology database and terms.
  149. */
  150. function tripal_chado_populate_vocab_OBI() {
  151. tripal_insert_db(array(
  152. 'name' => 'OBI',
  153. 'description' => 'The Ontology for Biomedical Investigation.',
  154. 'url' => 'http://obi-ontology.org/page/Main_Page',
  155. 'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
  156. ));
  157. tripal_insert_cv('obi','Ontology for Biomedical Investigation. The Ontology for Biomedical Investigations (OBI) is build in a collaborative, international effort and will serve as a resource for annotating biomedical investigations, including the study design, protocols and instrumentation used, the data generated and the types of analysis performed on the data. This ontology arose from the Functional Genomics Investigation Ontology (FuGO) and will contain both terms that are common to all biomedical investigations, including functional genomics investigations and those that are more domain specific.');
  158. $term = tripal_insert_cvterm(array(
  159. 'id' => 'OBI:0100026',
  160. 'name' => 'organism',
  161. 'cv_name' => 'obi',
  162. 'definition' => 'A material entity that is an individual living system, such as animal, plant, bacteria or virus, that is capable of replicating or reproducing, growth and maintenance in the right environment. An organism may be unicellular or made up, like humans, of many billions of cells divided into specialized tissues and organs.',
  163. ));
  164. tripal_associate_chado_semweb_term(NULL, 'organism_id', $term);
  165. }
  166. /**
  167. * Adds the Information Artifact Ontology database and terms.
  168. */
  169. function tripal_chado_populate_vocab_IAO() {
  170. tripal_insert_db(array(
  171. 'name' => 'IAO',
  172. 'description' => 'The Information Artifact Ontology (IAO).',
  173. 'url' => 'https://github.com/information-artifact-ontology/IAO/',
  174. 'urlprefix' => 'http://purl.obolibrary.org/obo/IAO_',
  175. ));
  176. tripal_insert_cv('IAO','Information Artifact Ontology is a new ' .
  177. 'ontology of information entities, originally driven by work by the ' .
  178. 'OBI digital entity and realizable information entity branch.');
  179. $term = tripal_insert_cvterm(array(
  180. 'id' => 'IAO:0000115',
  181. 'name' => 'definition',
  182. 'cv_name' => 'iao',
  183. 'definition' => 'The official OBI definition, explaining the meaning of ' .
  184. 'a class or property. Shall be Aristotelian, formalized and normalized. ' .
  185. 'Can be augmented with colloquial definitions.',
  186. ));
  187. tripal_associate_chado_semweb_term(NULL, 'definition', $term);
  188. $term = tripal_insert_cvterm(array(
  189. 'id' => 'IAO:0000129',
  190. 'name' => 'version number',
  191. 'cv_name' => 'IAO',
  192. 'definition' => 'A version number is an ' .
  193. 'information content entity which is a sequence of characters ' .
  194. 'borne by part of each of a class of manufactured products or its ' .
  195. 'packaging and indicates its order within a set of other products ' .
  196. 'having the same name.',
  197. ));
  198. tripal_associate_chado_semweb_term('analysis', 'programversion', $term);
  199. $term = tripal_insert_cvterm(array(
  200. 'id' => 'IAO:0000064',
  201. 'name' => 'algorithm',
  202. 'cv_name' => 'IAO',
  203. 'definition' => 'An algorithm is a set of instructions for performing a paticular calculation.',
  204. ));
  205. tripal_associate_chado_semweb_term('analysis', 'algorithm', $term);
  206. }
  207. /**
  208. * Adds terms to the 'local' database.
  209. *
  210. * These are terms where an appropriate match could not be found in any other
  211. * ontology.
  212. */
  213. function tripal_chado_populate_vocab_LOCAL() {
  214. tripal_insert_db(array(
  215. 'name' => 'local',
  216. 'description' => 'Terms created for this site.',
  217. 'url' => '/cv/lookup',
  218. 'urlprefix' => '/cv/lookup/{db}/{accession}',
  219. ));
  220. $term = tripal_insert_cvterm(array(
  221. 'id' => 'local:timelastmodified',
  222. 'name' => 'time_last_modified',
  223. 'cv_name' => 'local',
  224. 'definition' => 'The time at which the record was last modified.',
  225. ));
  226. tripal_associate_chado_semweb_term(NULL, 'timelastmodified', $term);
  227. $term = tripal_insert_cvterm(array(
  228. 'id' => 'local:timeaccessioned',
  229. 'name' => 'time_accessioned',
  230. 'cv_name' => 'local',
  231. 'definition' => 'The time at which the record was first added.',
  232. ));
  233. tripal_associate_chado_semweb_term(NULL, 'timeaccessioned', $term);
  234. $term = tripal_insert_cvterm(array(
  235. 'id' => 'local:timeexecuted',
  236. 'name' => 'time_executed',
  237. 'cv_name' => 'local',
  238. 'definition' => 'The time when the task was executed.',
  239. ));
  240. tripal_associate_chado_semweb_term(NULL, 'timeexecuted', $term);
  241. $term = tripal_insert_cvterm(array(
  242. 'id' => 'local:infraspecific_type',
  243. 'name' => 'infraspecific_type',
  244. 'definition' => 'The connector type (e.g. subspecies, varietas, forma, etc.) for the infraspecific name',
  245. 'cv_name' => 'local',
  246. ));
  247. tripal_associate_chado_semweb_term('organism', 'type_id', $term);
  248. $term = tripal_insert_cvterm(array(
  249. 'id' => 'local:abbreviation',
  250. 'name' => 'abbreviation',
  251. 'cv_name' => 'local',
  252. 'definition' => 'A shortened name (or abbreviation) for the item.'
  253. ));
  254. tripal_associate_chado_semweb_term('organism', 'abbreviation', $term);
  255. $term = tripal_insert_cvterm(array(
  256. 'id' => 'local:expression',
  257. 'name' => 'expression',
  258. 'definition' => 'Curated expression data',
  259. 'cv_name' => 'local',
  260. ));
  261. $term = tripal_insert_cvterm(array(
  262. 'id' => 'local:is_analysis',
  263. 'name' => 'is_analysis',
  264. 'definition' => 'Indicates if this feature was predicted computationally using another feature.',
  265. 'cv_name' => 'local',
  266. ));
  267. tripal_associate_chado_semweb_term('feature', 'is_analysis', $term);
  268. $term = tripal_insert_cvterm(array(
  269. 'id' => 'local:is_obsolete',
  270. 'name' => 'is_obsolete',
  271. 'definition' => 'Indicates if this record is obsolete.',
  272. 'cv_name' => 'local',
  273. ));
  274. tripal_associate_chado_semweb_term(NULL, 'is_obsolete', $term);
  275. }
  276. /**
  277. * Adds the Systems Biology Ontology database and terms.
  278. */
  279. function tripal_chado_populate_vocab_SBO() {
  280. tripal_insert_db(array(
  281. 'name' => 'SBO',
  282. 'description' => 'Systems Biology.',
  283. 'url' => 'http://www.ebi.ac.uk/sbo/main/',
  284. 'urlprefix' => 'http://purl.obolibrary.org/obo/{db}_{accession}',
  285. ));
  286. tripal_insert_cv('sbo','Systems Biology. Terms commonly used in Systems Biology, and in particular in computational modeling.');
  287. $dbxref = tripal_insert_cvterm(array(
  288. 'id' => 'SBO:0000358',
  289. 'name' => 'phenotype',
  290. 'cv_name' => 'sbo',
  291. 'definition' => 'A biochemical network can generate phenotypes or affects biological processes. Such processes can take place at different levels and are independent of the biochemical network itself.',
  292. ));
  293. $dbxref = tripal_insert_cvterm(array(
  294. 'id' => 'SBO:0000554',
  295. 'name' => 'database cross reference',
  296. 'cv_name' => 'sbo',
  297. 'definition' => 'An annotation which directs one to information contained within a database.',
  298. ));
  299. $relationship = tripal_insert_cvterm(array(
  300. 'id' => 'SBO:0000374',
  301. 'name' => 'relationship',
  302. 'cv_name' => 'sbo',
  303. 'definition' => 'Connectedness between entities and/or interactions representing their relatedness or influence.',
  304. ));
  305. }
  306. /**
  307. * Adds the Software Ontology database and terms.
  308. */
  309. function tripal_chado_populate_vocab_SWO() {
  310. tripal_insert_db(array(
  311. 'name' => 'SWO',
  312. 'description' => 'Software Ontology',
  313. 'url' => 'http://theswo.sourceforge.net/',
  314. 'urlprefix' => '',
  315. ));
  316. tripal_insert_cv('swo','Software Ontology. An ontology representation of the NCBI organismal taxonomy.');
  317. $term = tripal_insert_cvterm(array(
  318. 'id' => 'SWO:0000001',
  319. 'name' => 'software',
  320. 'cv_name' => 'schema',
  321. 'definition' => 'Computer software, or generally just software, is any ' .
  322. 'set of machine-readable instructions (most often in the form of a ' .
  323. 'computer program) that conform to a given syntax (sometimes ' .
  324. 'referred to as a language) that is interpretable by a given ' .
  325. 'processor and that directs a computer\'s processor to perform ' .
  326. 'specific operations.',
  327. ));
  328. tripal_associate_chado_semweb_term('analysis', 'program', $term);
  329. }
  330. /**
  331. * Adds the Taxonomic Rank Ontology database and terms.
  332. */
  333. function tripal_chado_populate_vocab_TAXRANK() {
  334. tripal_insert_db(array(
  335. 'name' => 'TAXRANK',
  336. 'description' => 'Taxonomic rank vocabulary.',
  337. 'url' => 'https://github.com/phenoscape/taxrank',
  338. 'urlprefix' => 'http://purl.obolibrary.org/obo/TAXRANK_',
  339. ));
  340. tripal_insert_cv('taxrank','Taxonomic rank vocabulary. A vocabulary of taxonomic ranks (species, family, phylum, etc).');
  341. $term = tripal_insert_cvterm(array(
  342. 'id' => 'TAXRANK:0000005',
  343. 'name' => 'genus',
  344. 'cv_name' => 'taxonomic_rank',
  345. 'description' => 'Taxonomic rank of Genus'
  346. ));
  347. tripal_associate_chado_semweb_term('organism', 'genus', $term);
  348. $term = tripal_insert_cvterm(array(
  349. 'id' => 'TAXRANK:0000006',
  350. 'name' => 'species',
  351. 'cv_name' => 'taxonomic_rank',
  352. 'description' => 'Taxonomic rank of species.'
  353. ));
  354. tripal_associate_chado_semweb_term('organism', 'species', $term);
  355. $term = tripal_insert_cvterm(array(
  356. 'id' => 'TAXRANK:0000045',
  357. 'name' => 'infraspecies',
  358. 'cv_name' => 'taxonomic_rank',
  359. 'description' => 'Taxonomic infraspecies name.'
  360. ));
  361. tripal_associate_chado_semweb_term('organism', 'infraspecific_name', $term);
  362. }
  363. /**
  364. * Adds the NCBI Taxon vocabulary database and terms.
  365. */
  366. function tripal_chado_populate_vocab_NCBITAXON() {
  367. tripal_insert_db(array(
  368. 'name' => 'NCBITaxon',
  369. 'description' => 'NCBI organismal classification.',
  370. 'url' => 'http://www.berkeleybop.org/ontologies/ncbitaxon/',
  371. 'urlprefix' => 'http://purl.obolibrary.org/obo/ncbitaxon#',
  372. ));
  373. tripal_insert_cv('ncbitaxon','NCBI organismal classification. An ontology representation of the NCBI organismal taxonomy.');
  374. $term = tripal_insert_cvterm(array(
  375. 'id' => 'NCBITaxon:common_name',
  376. 'name' => 'common name',
  377. 'cv_name' => 'ncbitaxon',
  378. ));
  379. tripal_associate_chado_semweb_term('organism', 'common_name', $term);
  380. }
  381. /**
  382. * Adds defaults to the chado_semweb table.
  383. */
  384. function tripal_chado_semweb_form($form, &$form_state, $chado_table = NULL) {
  385. if (array_key_exists('values', $form_state)) {
  386. $chado_table = $form_state['values']['chado_table'];
  387. }
  388. $chado_tables = chado_get_table_names(TRUE);
  389. $chado_tables = array_merge(array('Select a Chado Table'), $chado_tables);
  390. // Make sure the table name exists. If not, reset it.
  391. $chado_table = in_array($chado_table, $chado_tables) ? $chado_table: NULL;
  392. $form['chado_table'] = array(
  393. '#type' => 'select',
  394. '#title' => 'Chado Table',
  395. '#description' => t('Select a chado table to set web services terms used for its columns.'),
  396. '#options' => $chado_tables,
  397. '#default_value' => $chado_table,
  398. '#ajax' => array(
  399. 'callback' => "tripal_chado_semweb_form_ajax_callback",
  400. 'wrapper' => "tripal-chado-semweb-form",
  401. 'effect' => 'fade',
  402. 'method' => 'replace'
  403. ),
  404. );
  405. // If the user has selected a chado table, then we need to
  406. // show the columns for setting terms.
  407. if ($chado_table) {
  408. $schema = chado_get_schema($chado_table);
  409. $pk = $schema['primary key'][0];
  410. $cv_default =
  411. db_select('tripal_cv_defaults', 'tc')
  412. ->fields('tc', array('field_name'))
  413. ->condition('table_name', $chado_table)
  414. ->execute()
  415. ->fetchField();
  416. $columns = $schema['fields'];
  417. $headers = array('Field Name', 'Vocabulary', 'Term Name', 'Term Description', 'Action');
  418. $rows = array();
  419. foreach ($columns AS $column => $detail) {
  420. // Do not show column if it's the primary key or default cv
  421. if ($column != $pk && $column != $cv_default) {
  422. $cvterm_id =
  423. db_select('chado_semweb', 'cs')
  424. ->fields('cs', array('cvterm_id'))
  425. ->condition('chado_table', $chado_table)
  426. ->condition('chado_column', $column)
  427. ->execute()
  428. ->fetchField();
  429. $sw_voc = '';
  430. $sw_term = '';
  431. $sw_desc = '';
  432. if($cvterm_id) {
  433. $term = tripal_get_cvterm(array(
  434. 'cvterm_id' => $cvterm_id
  435. ));
  436. $sw_voc = $term->cv_id->name;
  437. $sw_term = $term->name;
  438. $sw_desc = $term->definition;
  439. }
  440. $rows[] = array(
  441. $column,
  442. $sw_voc,
  443. $sw_term,
  444. $sw_desc,
  445. l('Edit', '/admin/tripal/storage/chado/semweb/edit/' . $chado_table . '/' . $column) . ' | ' .
  446. l('Reset', '/admin/tripal/storage/chado/semweb/reset/' . $chado_table . '/' . $column)
  447. );
  448. }
  449. }
  450. $output = theme('table', array(
  451. 'header' => $headers,
  452. 'rows' => $rows,
  453. ));
  454. $form['table'] = array(
  455. '#markup' => $output,
  456. '#title' => 'Table',
  457. '#description' => t('Please provide any filters for limiting
  458. the records. Only those that match the filters specified
  459. below will be published. To publish all records of this
  460. type, leave all filters blank.'),
  461. );
  462. }
  463. $form['#prefix'] = '<div id="tripal-chado-semweb-form">';
  464. $form['#suffix'] = '</div>';
  465. return $form;
  466. }
  467. /**
  468. * Implements hook_form()
  469. * Edit terms used by the semantic web
  470. *
  471. * @param $form
  472. * @param $form_state
  473. * @param $table
  474. * @param $column
  475. * @return $form
  476. */
  477. function tripal_chado_semweb_edit_form($form, &$form_state, $table = NULL, $column = NULL) {
  478. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  479. $form['chado_table'] = array(
  480. '#markup' => 'Term used for the <strong>' . t($column) . '</strong> column of the chado <strong>' . t($table) . '</strong> table:',
  481. );
  482. $form['table_name'] = array(
  483. '#type' => 'value',
  484. '#value' => $table
  485. );
  486. $form['column'] = array(
  487. '#type' => 'value',
  488. '#value' => $column
  489. );
  490. // If no term has been selected yet then provide the auto complete field.
  491. $form['term_name'] = array(
  492. '#title' => t('Term'),
  493. '#type' => 'textfield',
  494. '#description' => t("The content type must be the name of a term in
  495. a controlled vocabulary and the controlled vocabulary should
  496. already be loaded into Tripal. For example, to create a content
  497. type for storing 'genes', use the 'gene' term from the
  498. Sequence Ontology (SO)."),
  499. '#required' => TRUE,
  500. '#default_value' => $term_name,
  501. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  502. );
  503. $form['select_button'] = array(
  504. '#type' => 'button',
  505. '#value' => t('Lookup Term'),
  506. '#name' => 'select_cvterm',
  507. '#ajax' => array(
  508. 'callback' => "tripal_chado_semweb_form_ajax_callback",
  509. 'wrapper' => "tripal-chado-semweb-edit-form",
  510. 'effect' => 'fade',
  511. 'method' => 'replace'
  512. ),
  513. );
  514. if ($term_name) {
  515. $form['terms_list'] = array(
  516. '#type' => 'fieldset',
  517. '#title' => t('Matching Terms'),
  518. '#description' => t('Please select the term the best matches the
  519. content type you want to create. If the same term exists in
  520. multiple vocabularies you will see more than one option below.')
  521. );
  522. $match = array(
  523. 'name' => $term_name,
  524. );
  525. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  526. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  527. $num_terms = 0;
  528. foreach ($terms as $term) {
  529. // Save the user a click by setting the default value as 1 if there's
  530. // only one matching term.
  531. $default = FALSE;
  532. $attrs = array();
  533. if ($num_terms == 0 and count($terms) == 1) {
  534. $default = TRUE;
  535. $attrs = array('checked' => 'checked');
  536. }
  537. $form['terms_list']['term-' . $term->cvterm_id] = array(
  538. '#type' => 'checkbox',
  539. '#title' => $term->name,
  540. '#default_value' => $default,
  541. '#attributes' => $attrs,
  542. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name .
  543. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  544. '<br><b>Definition:</b> ' . $term->definition,
  545. );
  546. $num_terms++;
  547. }
  548. if ($num_terms == 0) {
  549. $form['terms_list']['none'] = array(
  550. '#type' => 'item',
  551. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  552. );
  553. }
  554. // Add in the button for the cases of no terms or too many.
  555. $form['submit_button'] = array(
  556. '#type' => 'submit',
  557. '#value' => t('Use this term'),
  558. '#name' => 'use_cvterm'
  559. );
  560. }
  561. $form['cancel_button'] = array(
  562. '#type' => 'button',
  563. '#value' => t('Cancel'),
  564. '#name' => 'cancel_button',
  565. '#limit_validation_errors' => array()
  566. );
  567. $form['#prefix'] = '<div id = "tripal-chado-semweb-edit-form">';
  568. $form['#suffix'] = '</div>';
  569. return $form;
  570. }
  571. /**
  572. * Implements hook_form_validate()
  573. *
  574. * Validate function for editing the semantic web term
  575. *
  576. * @param unknown $form
  577. * @param unknown $form_state
  578. */
  579. function tripal_chado_semweb_edit_form_validate($form, &$form_state) {
  580. if (array_key_exists('clicked_button', $form_state)) {
  581. if ($form_state['clicked_button']['#name'] =='use_cvterm') {
  582. $cvterm_id = NULL;
  583. // Make sure we have a cvterm selected
  584. $num_selected = 0;
  585. foreach ($form_state['values'] as $key => $value) {
  586. $matches = array();
  587. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  588. $form_state['values']['term-' . $matches[1]]) {
  589. $cvterm_id = $matches[1];
  590. $num_selected++;
  591. }
  592. }
  593. if ($num_selected == 0) {
  594. form_set_error('', 'Please select at least one term.');
  595. }
  596. else if ($num_selected > 1) {
  597. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  598. }
  599. else {
  600. $form_state['values']['#selected_cvterm_id'] = $cvterm_id;
  601. }
  602. }
  603. else if ($form_state['clicked_button']['#name'] =='cancel_button') {
  604. $table_name = $form_state['values']['table_name'];
  605. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  606. }
  607. }
  608. }
  609. /**
  610. * Implements hook_form_submit()
  611. *
  612. * Submit function for editing the semantic web term
  613. *
  614. * @param unknown $form
  615. * @param unknown $form_state
  616. */
  617. function tripal_chado_semweb_edit_form_submit($form, &$form_state) {
  618. if (array_key_exists('clicked_button', $form_state) && $form_state['clicked_button']['#name'] =='use_cvterm') {
  619. $table_name = $form_state['values']['table_name'];
  620. $column = $form_state['values']['column'];
  621. $cvterm_id = $form_state['values']['#selected_cvterm_id'];
  622. // Check if there is already a record
  623. $record_id =
  624. db_select('chado_semweb', 'cs')
  625. ->fields('cs', array('chado_semweb_id'))
  626. ->condition('chado_table', $table_name)
  627. ->condition('chado_column', $column)
  628. ->execute()
  629. ->fetchField();
  630. // If the record exists, update it
  631. if ($record_id) {
  632. db_update('chado_semweb')
  633. ->fields(array(
  634. 'cvterm_id' => $cvterm_id
  635. ))
  636. ->condition('chado_semweb_id', $record_id)
  637. ->execute();
  638. }
  639. // Otherwise, insert a new record
  640. else {
  641. db_insert('chado_semweb')
  642. ->fields(array(
  643. 'chado_table' => $table_name,
  644. 'chado_column' => $column,
  645. 'cvterm_id' => $cvterm_id
  646. ))
  647. ->execute();
  648. }
  649. drupal_set_message('The term settings have been saved.');
  650. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  651. }
  652. }
  653. /**
  654. * Implements hook_form()
  655. * Reset term used by semantic web
  656. *
  657. * @param $form
  658. * @param $form_state
  659. * @param $table
  660. * @param $column
  661. * @return $form
  662. */
  663. function tripal_chado_semweb_reset_form($form, &$form_state, $table = NULL, $column = NULL) {
  664. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  665. $form['chado_table'] = array(
  666. '#markup' => 'Are you sure you want to remove the use of this term? ',
  667. );
  668. $form['table_name'] = array(
  669. '#type' => 'value',
  670. '#value' => $table
  671. );
  672. $form['column'] = array(
  673. '#type' => 'value',
  674. '#value' => $column
  675. );
  676. $form['submit_button'] = array(
  677. '#type' => 'submit',
  678. '#value' => t('Reset'),
  679. '#name' => 'reset_term'
  680. );
  681. $form['cancel_button'] = array(
  682. '#type' => 'button',
  683. '#value' => t('Cancel'),
  684. '#name' => 'cancel_button',
  685. '#limit_validation_errors' => array()
  686. );
  687. return $form;
  688. }
  689. /**
  690. * Implements hook_form_validate()
  691. *
  692. * Validate function for resetting the semantic web term
  693. *
  694. * @param unknown $form
  695. * @param unknown $form_state
  696. */
  697. function tripal_chado_semweb_reset_form_validate($form, &$form_state) {
  698. if (array_key_exists('clicked_button', $form_state)) {
  699. if ($form_state['clicked_button']['#name'] =='use_cvterm') {
  700. $cvterm_id = NULL;
  701. // Make sure we have a cvterm selected
  702. $num_selected = 0;
  703. foreach ($form_state['values'] as $key => $value) {
  704. $matches = array();
  705. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  706. $form_state['values']['term-' . $matches[1]]) {
  707. $cvterm_id = $matches[1];
  708. $num_selected++;
  709. }
  710. }
  711. if ($num_selected == 0) {
  712. form_set_error('', 'Please select at least one term.');
  713. }
  714. else if ($num_selected > 1) {
  715. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  716. }
  717. else {
  718. $form_state['values']['#selected_cvterm_id'] = $cvterm_id;
  719. }
  720. }
  721. else if ($form_state['clicked_button']['#name'] =='cancel_button') {
  722. $table_name = $form_state['values']['table_name'];
  723. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  724. }
  725. }
  726. }
  727. /**
  728. * Implements hook_form_submit()
  729. *
  730. * Submit function for editing the semantic web term
  731. *
  732. * @param unknown $form
  733. * @param unknown $form_state
  734. */
  735. function tripal_chado_semweb_reset_form_submit($form, &$form_state) {
  736. if (array_key_exists('clicked_button', $form_state) && $form_state['clicked_button']['#name'] =='reset_term') {
  737. $table_name = $form_state['values']['table_name'];
  738. $column = $form_state['values']['column'];
  739. // Check if there is already a record
  740. $record_id =
  741. db_select('chado_semweb', 'cs')
  742. ->fields('cs', array('chado_semweb_id'))
  743. ->condition('chado_table', $table_name)
  744. ->condition('chado_column', $column)
  745. ->execute()
  746. ->fetchField();
  747. // If the record exists, reset it
  748. if ($record_id) {
  749. db_update('chado_semweb')
  750. ->fields(array(
  751. 'cvterm_id' => NULL
  752. ))
  753. ->condition('chado_semweb_id', $record_id)
  754. ->execute();
  755. }
  756. drupal_set_message('The term settings have been reset.');
  757. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  758. }
  759. }
  760. /**
  761. *
  762. */
  763. function tripal_chado_semweb_form_ajax_callback($form, $form_state) {
  764. return $form;
  765. }