tripal_chado.semweb.inc 29 KB

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