tripal_chado.semweb.inc 30 KB

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