tripal_chado.semweb.inc 29 KB

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