tripal_chado.fields.inc 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. <?php
  2. /**
  3. * Implements hook_field_create_info().
  4. *
  5. * This is a Tripal defined hook that supports integration with the
  6. * TripalEntity field.
  7. */
  8. function tripal_chado_bundle_create_fields($entity_type, $bundle) {
  9. $chado_bundle = db_select('chado_bundle', 'cb')
  10. ->fields('cb')
  11. ->condition('bundle_id', $bundle->id)
  12. ->execute()
  13. ->fetchObject();
  14. // Get the details about the mapping of this bundle to the Chado table:
  15. $details = array(
  16. 'chado_cvterm_id' => $chado_bundle->type_id,
  17. 'chado_table' => $chado_bundle->data_table,
  18. 'chado_type_table' => $chado_bundle->type_linker_table,
  19. 'chado_type_column' => $chado_bundle->type_column,
  20. );
  21. $info = array();
  22. // Create the fields for each column in the table.
  23. tripal_chado_bundle_create_fields_base($info, $details, $entity_type, $bundle);
  24. // Create custom fields.
  25. tripal_chado_bundle_create_fields_custom($info, $details, $entity_type, $bundle);
  26. // Create fields for linking tables.
  27. tripal_chado_bundle_create_fields_linker($info, $details, $entity_type, $bundle);
  28. return $info;
  29. }
  30. /**
  31. *
  32. * @param unknown $details
  33. */
  34. function tripal_chado_bundle_create_fields_base(&$info, $details, $entity_type, $bundle) {
  35. $table_name = $details['chado_table'];
  36. $type_table = $details['chado_type_table'];
  37. $type_field = $details['chado_type_column'];
  38. $cvterm_id = $details['chado_cvterm_id'];
  39. // Iterate through the columns of the table and see if fields have been
  40. // created for each one. If not, then create them.
  41. $schema = chado_get_schema($table_name);
  42. if (!$schema) {
  43. return;
  44. }
  45. $pkey = $schema['primary key'][0];
  46. // Get the list of columns for this table and create a new field for each one.
  47. $columns = $schema['fields'];
  48. foreach ($columns as $column_name => $details) {
  49. // Don't create base fields for the primary key and the type_id field.
  50. if ($column_name == $pkey or $column_name == $type_field) {
  51. continue;
  52. }
  53. $cvterm = tripal_get_chado_semweb_term($table_name, $column_name, array('return_object' => TRUE));
  54. if (!$cvterm) {
  55. tripal_report_error('tripal', TRIPAL_ERROR,
  56. 'Cannot create field for "%table_name.%column_name". Missing an appropriate vocabulary term',
  57. array('%table_name' => $table_name, '%column_name' => $column_name));
  58. drupal_set_message(t('Cannot create field for "%table_name.%column_name". Missing an appropriate vocabulary term',
  59. array('%table_name' => $table_name, '%column_name' => $column_name)), 'error');
  60. continue;
  61. }
  62. $field_name = strtolower($cvterm->dbxref_id->db_id->name . '__' . preg_replace('/ /', '_', $cvterm->name));
  63. $field_name = substr($field_name, 0, 32);
  64. // Skip the primary key field.
  65. if ($column_name == $schema['primary key'][0]) {
  66. continue;
  67. }
  68. // Skip the type field that will always be custom
  69. if (($table_name == $type_table and $column_name == $type_field) or
  70. $column_name == 'type_id') {
  71. continue;
  72. }
  73. // Set some defaults for the field.
  74. $base_info = array(
  75. 'field_name' => $field_name,
  76. 'type' => '',
  77. 'cardinality' => 1,
  78. 'locked' => FALSE,
  79. 'storage' => array(
  80. 'type' => 'field_chado_storage',
  81. ),
  82. );
  83. // Alter the field info array depending on the column details.
  84. switch($details['type']) {
  85. case 'char':
  86. $base_info['type'] = 'text';
  87. $base_info['settings']['max_length'] = $details['length'];
  88. break;
  89. case 'varchar':
  90. $base_info['type'] = 'text';
  91. $base_info['settings']['max_length'] = $details['length'];
  92. break;
  93. case 'text':
  94. $base_info['type'] = 'text';
  95. $base_info['settings']['max_length'] = 17179869184;
  96. $base_info['settings']['text_processing'] = 1;
  97. break;
  98. case 'blob':
  99. // not sure how to support a blob field.
  100. continue;
  101. break;
  102. case 'int':
  103. $base_info['type'] = 'number_integer';
  104. break;
  105. case 'float':
  106. $base_info['type'] = 'number_float';
  107. $base_info['settings']['precision'] = 10;
  108. $base_info['settings']['scale'] = 2;
  109. $base_info['settings']['decimal_separator'] = '.';
  110. break;
  111. case 'numeric':
  112. $base_info['type'] = 'number_decimal';
  113. break;
  114. case 'serial':
  115. // Serial fields are most likely not needed as a field.
  116. break;
  117. case 'boolean':
  118. $base_info['type'] = 'list_boolean';
  119. $base_info['settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  120. break;
  121. case 'datetime':
  122. // Use the Drupal Date and Date API to create the field/widget
  123. $base_info['type'] = 'datetime';
  124. break;
  125. }
  126. // Set some default semantic web information
  127. if ($column_name == 'uniquename') {
  128. $base_info['settings']['text_processing'] = 0;
  129. }
  130. //
  131. // PUB TABLE
  132. //
  133. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  134. $base_info['type'] = 'text';
  135. $base_info['settings']['text_processing'] = 0;
  136. }
  137. //
  138. // ANALYSIS TABLE
  139. //
  140. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  141. $base_info['type'] = 'text';
  142. $base_info['settings']['text_processing'] = 0;
  143. }
  144. $info[$field_name] = $base_info;
  145. }
  146. }
  147. /**
  148. *
  149. * @param unknown $details
  150. */
  151. function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type, $bundle) {
  152. $table_name = $details['chado_table'];
  153. $type_table = $details['chado_type_table'];
  154. $type_field = $details['chado_type_column'];
  155. $cvterm_id = $details['chado_cvterm_id'];
  156. $schema = chado_get_schema($table_name);
  157. // BASE TYPE_ID
  158. // Exculde the type_id field on the organism table that will be handled
  159. // by the infraspecific type field.
  160. if (array_key_exists('type_id', $schema['fields']) and $table_name != 'organism') {
  161. $field_name = 'schema__additional_type';
  162. $field_type = 'schema__additional_type';
  163. $info[$field_name] = array(
  164. 'field_name' => $field_name,
  165. 'type' => $field_type,
  166. 'cardinality' => 1,
  167. 'locked' => FALSE,
  168. 'storage' => array(
  169. 'type' => 'field_chado_storage',
  170. ),
  171. );
  172. }
  173. // BASE ORGANISM_ID
  174. if ($table_name != 'organism' and
  175. array_key_exists('organism_id', $schema['fields'])) {
  176. $field_name = 'obi__organism';
  177. $field_type = 'obi__organism';
  178. $info[$field_name] = array(
  179. 'field_name' => $field_name,
  180. 'type' => $field_type,
  181. 'cardinality' => 1,
  182. 'locked' => FALSE,
  183. 'storage' => array(
  184. 'type' => 'field_chado_storage',
  185. ),
  186. );
  187. }
  188. // BASE DBXREF
  189. if (array_key_exists('dbxref_id', $schema['fields'])) {
  190. $field_name = 'data__accession';
  191. $field_type = 'data__accession';
  192. $info[$field_name] = array(
  193. 'field_name' => $field_name,
  194. 'type' => $field_type,
  195. 'cardinality' => 1,
  196. 'locked' => FALSE,
  197. 'storage' => array(
  198. 'type' => 'field_chado_storage',
  199. ),
  200. );
  201. }
  202. // FEATURE MD5CHECKSUM
  203. if ($table_name == 'feature') {
  204. $field_name = 'data__sequence_checksum';
  205. $field_type = 'data__sequence_checksum';
  206. $info[$field_name] = array(
  207. 'field_name' => $field_name,
  208. 'type' => $field_type,
  209. 'cardinality' => 1,
  210. 'locked' => FALSE,
  211. 'storage' => array(
  212. 'type' => 'field_chado_storage',
  213. ),
  214. );
  215. }
  216. // FEATURE RESIDUES
  217. if ($table_name == 'feature') {
  218. $field_name = 'data__sequence';
  219. $field_type = 'data__sequence';
  220. $info[$field_name] = array(
  221. 'field_name' => $field_name,
  222. 'type' => $field_type,
  223. 'cardinality' => 1,
  224. 'locked' => FALSE,
  225. 'storage' => array(
  226. 'type' => 'field_chado_storage',
  227. ),
  228. );
  229. }
  230. // FEATURE SEQLEN
  231. if ($table_name == 'feature') {
  232. $field_name = 'data__sequence_length';
  233. $field_type = 'data__sequence_length';
  234. $info[$field_name] = array(
  235. 'field_name' => $field_name,
  236. 'type' => $field_type,
  237. 'cardinality' => 1,
  238. 'locked' => FALSE,
  239. 'storage' => array(
  240. 'type' => 'field_chado_storage',
  241. ),
  242. );
  243. }
  244. // GENE TRANSCRIPTS
  245. $rel_table = $table_name . '_relationship';
  246. if (chado_table_exists($rel_table) and $bundle->label == 'gene') {
  247. $field_name = 'so__transcript';
  248. $field_type = 'so__transcript';
  249. $info[$field_name] = array(
  250. 'field_name' => $field_name,
  251. 'type' => $field_type,
  252. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  253. 'locked' => FALSE,
  254. 'storage' => array(
  255. 'type' => 'field_chado_storage',
  256. ),
  257. );
  258. }
  259. // ORGANISM TYPE_ID
  260. // if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
  261. // $field_name = 'taxarank__infraspecific_taxon';
  262. // $field_type = 'taxarank__infraspecific_taxon';
  263. // $info[$field_name] = array(
  264. // 'field_name' => $field_name,
  265. // 'type' => $field_type,
  266. // 'cardinality' => 1,
  267. // 'locked' => FALSE,
  268. // 'storage' => array(
  269. // 'type' => 'field_chado_storage',
  270. // ),
  271. // 'settings' => array(
  272. // ),
  273. // );
  274. // }
  275. // FEATUREMAP UNITTYPE_ID
  276. if ($table_name == 'featuremap') {
  277. $field_name = 'uo__unit';
  278. $field_type = 'uo__unit';
  279. $info[$field_name] = array(
  280. 'field_name' => $field_name,
  281. 'type' => $field_type,
  282. 'cardinality' => 1,
  283. 'locked' => FALSE,
  284. 'storage' => array(
  285. 'type' => 'field_chado_storage',
  286. ),
  287. );
  288. }
  289. }
  290. /**
  291. *
  292. * @param unknown $details
  293. */
  294. function tripal_chado_bundle_create_fields_linker(&$info, $details, $entity_type, $bundle) {
  295. $table_name = $details['chado_table'];
  296. $type_table = $details['chado_type_table'];
  297. $type_field = $details['chado_type_column'];
  298. $cvterm_id = $details['chado_cvterm_id'];
  299. // CONTACTS
  300. $contact_table = $table_name . '_contact';
  301. if (chado_table_exists($contact_table)) {
  302. $schema = chado_get_schema($contact_table);
  303. $pkey = $schema['primary key'][0];
  304. $field_name = $table_name . '_contact';
  305. $field_type = 'chado_linker__contact';
  306. $info[$field_name] = array(
  307. 'field_name' => $field_name,
  308. 'type' => $field_type,
  309. 'cardinality' => 1,
  310. 'locked' => FALSE,
  311. 'storage' => array(
  312. 'type' => 'field_chado_storage',
  313. ),
  314. );
  315. }
  316. // DBXREF
  317. $dbxref_table = $table_name . '_dbxref';
  318. if (chado_table_exists($dbxref_table)) {
  319. $field_name = 'sbo__database_cross_reference';
  320. $field_type = 'sbo__database_cross_reference';
  321. $info[$field_name] = array(
  322. 'field_name' => $field_name,
  323. 'type' => $field_type,
  324. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  325. 'locked' => FALSE,
  326. 'storage' => array(
  327. 'type' => 'field_chado_storage',
  328. ),
  329. );
  330. }
  331. // EXPRESSION
  332. $expression_table = $table_name . '_expression';
  333. if (chado_table_exists($expression_table)) {
  334. $field_name = 'go__gene_expression';
  335. $field_type = 'go__gene_expression';
  336. $info[$field_name] = array(
  337. 'field_name' => $field_name,
  338. 'type' => $field_type,
  339. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  340. 'locked' => FALSE,
  341. 'storage' => array(
  342. 'type' => 'field_chado_storage',
  343. ),
  344. );
  345. }
  346. // FEATURELOC
  347. if ($table_name == 'feature') {
  348. $field_name = 'data__sequence_coordinates';
  349. $field_type = 'data__sequence_coordinates';
  350. $info[$field_name] = array(
  351. 'field_name' => $field_name,
  352. 'type' => $field_type,
  353. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  354. 'locked' => FALSE,
  355. 'storage' => array(
  356. 'type' => 'field_chado_storage',
  357. ),
  358. );
  359. }
  360. // FEATUREPOS
  361. if ($table_name == 'feature') {
  362. $field_name = 'ogi__location_on_map';
  363. $field_type = 'ogi__location_on_map';
  364. $info[$field_name] = array(
  365. 'field_name' => $field_name,
  366. 'type' => $field_type,
  367. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  368. 'locked' => FALSE,
  369. 'storage' => array(
  370. 'type' => 'field_chado_storage',
  371. ),
  372. );
  373. }
  374. // GENOTYPE
  375. $genotype_table = $table_name . '_genotype';
  376. if (chado_table_exists($genotype_table)) {
  377. $field_name = 'so__genotype';
  378. $field_type = 'so__genotype';
  379. $info[$field_name] = array(
  380. 'field_name' => $field_name,
  381. 'type' => $field_type,
  382. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  383. 'locked' => FALSE,
  384. 'storage' => array(
  385. 'type' => 'field_chado_storage',
  386. ),
  387. );
  388. }
  389. // PHENOTYPE
  390. $phenotype_table = $table_name . '_phenotype';
  391. if (chado_table_exists($phenotype_table)) {
  392. $field_name = 'sbo__phenotype';
  393. $field_type = 'sbo__phenotype';
  394. $info[$field_name] = array(
  395. 'field_name' => $field_name,
  396. 'type' => $field_type,
  397. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  398. 'locked' => FALSE,
  399. 'storage' => array(
  400. 'type' => 'field_chado_storage',
  401. ),
  402. );
  403. }
  404. // PROPERTIES
  405. $prop_table = $table_name . 'prop';
  406. if (chado_table_exists($prop_table)) {
  407. // Get the list of existing property types for this table.
  408. $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
  409. $props = chado_query($sql);
  410. while ($prop = $props->fetchObject()) {
  411. $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));
  412. $field_name = strtolower(preg_replace('/[^\w]/','_', $term->dbxref_id->db_id->name . '__' . $term->name));
  413. $field_name = substr($field_name, 0, 32);
  414. $field_type = 'chado_linker__prop';
  415. $info[$field_name] = array(
  416. 'field_name' => $field_name,
  417. 'type' => $field_type,
  418. 'cardinality' => 1,
  419. 'locked' => FALSE,
  420. 'storage' => array(
  421. 'type' => 'field_chado_storage',
  422. ),
  423. );
  424. }
  425. }
  426. // PUBLICATIONS
  427. $pub_table = $table_name . '_pub';
  428. if (chado_table_exists($pub_table)) {
  429. $field_name = 'schema__publication';
  430. $field_type = 'schema__publication';
  431. $info[$field_name] = array(
  432. 'field_name' => $field_name,
  433. 'type' => $field_type,
  434. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  435. 'locked' => FALSE,
  436. 'storage' => array(
  437. 'type' => 'field_chado_storage',
  438. ),
  439. );
  440. }
  441. // PUBLICATIONS (in reverse)
  442. // We want to be able to show all of the content that a publication links
  443. // to. The sio__references field does that.
  444. if ($table_name == 'pub') {
  445. $field_name = 'sio__references';
  446. $field_type = 'sio__references';
  447. $info[$field_name] = array(
  448. 'field_name' => $field_name,
  449. 'type' => $field_type,
  450. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  451. 'locked' => FALSE,
  452. 'storage' => array(
  453. 'type' => 'field_chado_storage',
  454. ),
  455. );
  456. }
  457. // RELATIONSHIPS
  458. // If the linker table does not exists then we don't want to add attach.
  459. $rel_table = $table_name . '_relationship';
  460. if (chado_table_exists($rel_table)) {
  461. $field_name = 'sbo__relationship';
  462. $field_type = 'sbo__relationship';
  463. $info[$field_name] = array(
  464. 'field_name' => $field_name,
  465. 'type' => $field_type,
  466. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  467. 'locked' => FALSE,
  468. 'storage' => array(
  469. 'type' => 'field_chado_storage',
  470. ),
  471. );
  472. }
  473. // SYNONYMS
  474. $syn_table = $table_name . '_synonym';
  475. if (chado_table_exists($syn_table)) {
  476. $field_name = 'schema__alternate_name';
  477. $field_type = 'schema__alternate_name';
  478. $info[$field_name] = array(
  479. 'field_name' => $field_name,
  480. 'type' => $field_type,
  481. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  482. 'locked' => FALSE,
  483. 'storage' => array(
  484. 'type' => 'field_chado_storage',
  485. ),
  486. 'settings' => array(
  487. ),
  488. );
  489. }
  490. }
  491. /**
  492. * Impelments hook_create_tripalfield_instance().
  493. *
  494. * This is a Tripal defined hook that supports integration with the
  495. * TripalEntity field.
  496. */
  497. function tripal_chado_bundle_create_instances($entity_type, $bundle) {
  498. $chado_bundle = db_select('chado_bundle', 'cb')
  499. ->fields('cb')
  500. ->condition('bundle_id', $bundle->id)
  501. ->execute()
  502. ->fetchObject();
  503. $details = array(
  504. 'chado_cvterm_id' => $chado_bundle->type_id,
  505. 'chado_table' => $chado_bundle->data_table,
  506. 'chado_type_table' => $chado_bundle->type_linker_table,
  507. 'chado_type_column' => $chado_bundle->type_column,
  508. );
  509. $info = array();
  510. tripal_chado_bundle_create_instances_base($info, $entity_type, $bundle, $details);
  511. tripal_chado_bundle_create_instances_custom($info, $entity_type, $bundle, $details);
  512. tripal_chado_bundle_create_instances_linker($info, $entity_type, $bundle, $details);
  513. return $info;
  514. }
  515. /**
  516. * Helper function for the hook_create_tripalfield_instance().
  517. *
  518. * Add the field instances that corresond to the columns of the base table.
  519. *
  520. * @param $entity_type
  521. * @param $bundle
  522. * @param $details
  523. */
  524. function tripal_chado_bundle_create_instances_base(&$info, $entity_type, $bundle, $details) {
  525. $fields = array();
  526. // Get Chado information
  527. $table_name = $details['chado_table'];
  528. $type_table = $details['chado_type_table'];
  529. $type_field = $details['chado_type_column'];
  530. $cvterm_id = $details['chado_cvterm_id'];
  531. // Iterate through the columns of the table and see if fields have been
  532. // created for each one. If not, then create them.
  533. $schema = chado_get_schema($table_name);
  534. if (!$schema) {
  535. return;
  536. }
  537. $pkey = $schema['primary key'][0];
  538. $columns = $schema['fields'];
  539. foreach ($columns as $column_name => $details) {
  540. // Don't create base fields for the primary key and the type_id field.
  541. if ($column_name == $pkey or $column_name == $type_field) {
  542. continue;
  543. }
  544. // Skip the type field that will always be custom
  545. if (($table_name == $type_table and $column_name == $type_field) or
  546. $column_name == 'type_id') {
  547. continue;
  548. }
  549. $cvterm = tripal_get_chado_semweb_term($table_name, $column_name, array('return_object' => TRUE));
  550. if (!$cvterm) {
  551. // We already provided an error when creating the base field. So
  552. // don't create another one here.
  553. continue;
  554. }
  555. $field_name = strtolower($cvterm->dbxref_id->db_id->name . '__' . preg_replace('/ /', '_', $cvterm->name));
  556. $field_name = substr($field_name, 0, 32);
  557. // Skip the primary key field.
  558. if ($column_name == $schema['primary key'][0]) {
  559. continue;
  560. }
  561. // Skip the type field.
  562. if ($table_name == $type_table and $column_name == $type_field) {
  563. continue;
  564. }
  565. $base_info = array(
  566. 'field_name' => $field_name,
  567. 'entity_type' => 'TripalEntity',
  568. 'bundle' => $bundle->name,
  569. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  570. 'description' => '',
  571. 'required' => FALSE,
  572. 'settings' => array(
  573. 'auto_attach' => TRUE,
  574. 'term_vocabulary' => $cvterm->dbxref_id->db_id->name,
  575. 'term_name' => $cvterm->name,
  576. 'term_accession' => $cvterm->dbxref_id->accession,
  577. 'chado_table' => $table_name,
  578. 'chado_column' => $column_name,
  579. 'base_table' => $table_name,
  580. ),
  581. 'widget' => array(
  582. 'settings' => array(
  583. 'display_label' => 1,
  584. ),
  585. ),
  586. 'display' => array(
  587. 'default' => array(
  588. 'label' => 'inline',
  589. 'settings' => array(),
  590. ),
  591. ),
  592. );
  593. // Determine if the field is required.
  594. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  595. $base_info['required'] = TRUE;
  596. }
  597. // Alter the field info array depending on the column details.
  598. switch($details['type']) {
  599. case 'char':
  600. $base_info['widget']['type'] = 'text_textfield';
  601. break;
  602. case 'varchar':
  603. $base_info['widget']['type'] = 'text_textfield';
  604. break;
  605. case 'text':
  606. $base_info['display']['default']['label'] = 'above';
  607. $base_info['widget']['type'] = 'text_textarea';
  608. $base_info['settings']['text_processing'] = '1';
  609. $base_info['settings']['format'] = 'full_html';
  610. break;
  611. case 'blob':
  612. // not sure how to support a blob field.
  613. continue;
  614. break;
  615. case 'int':
  616. $base_info['widget']['type'] = 'number';
  617. break;
  618. case 'float':
  619. $base_info['widget']['type'] = 'number';
  620. break;
  621. case 'numeric':
  622. $base_info['widget']['type'] = 'number';
  623. break;
  624. case 'serial':
  625. // Serial fields are most likely not needed as a field.
  626. break;
  627. case 'boolean':
  628. $base_info['widget']['type'] = 'options_onoff';
  629. $base_info['required'] = FALSE;
  630. break;
  631. case 'datetime':
  632. $base_info['widget']['type'] = 'date_select';
  633. $base_info['widget']['settings']['increment'] = 1;
  634. $base_info['widget']['settings']['tz_handling'] = 'none';
  635. $base_info['widget']['settings']['collapsible'] = TRUE;
  636. // TODO: Add settings so that the minutes increment by 1.
  637. // And turn off the timezone, as the Chado field doesn't support it.
  638. break;
  639. }
  640. // Set some default semantic web information
  641. if ($column_name == 'uniquename') {
  642. $base_info['label'] = 'Identifier';
  643. $base_info['widget_type'] = 'text_textfield';
  644. }
  645. elseif ($base_info['label'] == 'Timeaccessioned') {
  646. $base_info['label'] = 'Time Accessioned';
  647. $base_info['description'] = 'Please enter the time that this record was first added to the database.';
  648. }
  649. elseif ($base_info['label'] == 'Timelastmodified') {
  650. $base_info['label'] = 'Time Last Modified';
  651. $base_info['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  652. }
  653. //
  654. // ORGANISM TABLE
  655. //
  656. elseif ($table_name == 'organism' and $column_name == 'comment') {
  657. $base_info['label'] = 'Description';
  658. }
  659. //
  660. // PUB TABLE
  661. //
  662. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  663. $base_info['widget_type'] = 'text_textfield';
  664. }
  665. //
  666. // ANALYSIS TABLE
  667. //
  668. elseif ($table_name == 'analysis' and $column_name == 'program') {
  669. $base_info['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
  670. $base_info['label'] = 'Program, Pipeline, Workflow or Method Name';
  671. }
  672. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  673. $base_info['widget_type'] = 'text_textfield';
  674. $base_info['label'] = 'Source URL';
  675. $base_info['description'] = 'The URL where the original source data was derived. Ideally, this should link to the page where more information about the source data can be found.';
  676. }
  677. elseif ($table_name == 'analysis' and $column_name == 'sourcename') {
  678. $base_info['label'] = 'Source Name';
  679. $base_info['description'] = 'The name of the source data. This could be a file name, data set or a small description for how the data was collected. For long descriptions use the larger description field.';
  680. }
  681. elseif ($table_name == 'analysis' and $column_name == 'sourceversion') {
  682. $base_info['label'] = 'Source Version';
  683. $base_info['description'] = 'If the source data set has a version include it here.';
  684. }
  685. elseif ($table_name == 'analysis' and $column_name == 'algorithm') {
  686. $base_info['label'] = 'Source Version';
  687. $base_info['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  688. }
  689. elseif ($table_name == 'analysis' and $column_name == 'programversion') {
  690. $base_info['label'] = 'Program Version';
  691. $base_info['description'] = 'The version of the program used to perform this analysis. (e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter "n/a" if no version is available or applicable.';
  692. }
  693. //
  694. // PROJECT TABLE
  695. //
  696. elseif ($table_name == 'project' and $column_name == 'description') {
  697. $base_info['label'] = 'Short Description';
  698. }
  699. $info[$field_name] = $base_info;
  700. }
  701. }
  702. /**
  703. * Helper function for the hook_create_tripalfield_instance().
  704. *
  705. * Adds custom fields for base fields. These override the settings provided
  706. * in the tripal_chado_create_tripalfield_instance_base() function.
  707. *
  708. * @param $entity_type
  709. * @param $bundle
  710. * @param $details
  711. */
  712. function tripal_chado_bundle_create_instances_custom(&$info, $entity_type, $bundle, $details) {
  713. $table_name = $details['chado_table'];
  714. $type_table = $details['chado_type_table'];
  715. $type_field = $details['chado_type_column'];
  716. $cvterm_id = $details['chado_cvterm_id'];
  717. $schema = chado_get_schema($table_name);
  718. // BASE TYPE_ID
  719. // Exculde the type_id field on the organism table that will be handled
  720. // by the infraspecific type field.
  721. if (array_key_exists('type_id', $schema['fields']) and $table_name != 'organism') {
  722. $field_name = 'schema__additional_type';
  723. $is_required = FALSE;
  724. if (array_key_exists('not null', $schema['fields']['type_id']) and
  725. $schema['fields']['type_id']['not null']) {
  726. $is_required = TRUE;
  727. }
  728. $info[$field_name] = array(
  729. 'field_name' => $field_name,
  730. 'entity_type' => $entity_type,
  731. 'bundle' => $bundle->name,
  732. 'label' => 'Type',
  733. 'description' => 'Select a type for this record.',
  734. 'required' => $is_required,
  735. 'settings' => array(
  736. 'auto_attach' => TRUE,
  737. 'chado_table' => $table_name,
  738. 'chado_column' => 'type_id',
  739. 'base_table' => $table_name,
  740. ),
  741. 'widget' => array(
  742. 'type' => 'schema__additional_type_widget',
  743. 'settings' => array(
  744. 'display_label' => 1,
  745. ),
  746. ),
  747. 'display' => array(
  748. 'default' => array(
  749. 'label' => 'inline',
  750. 'type' => 'schema__additional_type_formatter',
  751. 'settings' => array(),
  752. ),
  753. ),
  754. );
  755. }
  756. // BASE ORGANISM_ID
  757. if ($table_name != 'organism' and array_key_exists('organism_id', $schema['fields'])) {
  758. $field_name = 'obi__organism';
  759. $is_required = FALSE;
  760. if (array_key_exists('not null', $schema['fields']['organism_id']) and
  761. $schema['fields']['organism_id']['not null']) {
  762. $is_required = TRUE;
  763. }
  764. $info[$field_name] = array(
  765. 'field_name' => $field_name,
  766. 'entity_type' => $entity_type,
  767. 'bundle' => $bundle->name,
  768. 'label' => 'Organism',
  769. 'description' => 'Select an organism.',
  770. 'required' => $is_required,
  771. 'settings' => array(
  772. 'auto_attach' => TRUE,
  773. 'chado_table' => $table_name,
  774. 'chado_column' => 'organism_id',
  775. 'base_table' => $table_name,
  776. ),
  777. 'widget' => array(
  778. 'type' => 'obi__organism_widget',
  779. 'settings' => array(
  780. 'display_label' => 1,
  781. ),
  782. ),
  783. 'display' => array(
  784. 'default' => array(
  785. 'label' => 'inline',
  786. 'type' => 'obi__organism_formatter',
  787. 'settings' => array(),
  788. ),
  789. ),
  790. );
  791. }
  792. // BASE DBXREF
  793. if (array_key_exists('dbxref_id', $schema['fields'])) {
  794. $field_name = 'data__accession';
  795. $info[$field_name] = array(
  796. 'field_name' => $field_name,
  797. 'entity_type' => $entity_type,
  798. 'bundle' => $bundle->name,
  799. 'label' => 'Accession',
  800. 'description' => 'This field specifies the unique stable accession (ID) for
  801. this record. It requires that this site have a database entry.',
  802. 'required' => FALSE,
  803. 'settings' => array(
  804. 'auto_attach' => TRUE,
  805. 'chado_table' => $table_name,
  806. 'chado_column' => 'dbxref_id',
  807. 'base_table' => $table_name,
  808. ),
  809. 'widget' => array(
  810. 'type' => 'data__accession_widget',
  811. 'settings' => array(
  812. 'display_label' => 1,
  813. ),
  814. ),
  815. 'display' => array(
  816. 'default' => array(
  817. 'label' => 'inline',
  818. 'type' => 'data__accession_formatter',
  819. 'settings' => array(),
  820. ),
  821. ),
  822. );
  823. }
  824. // FEATURE MD5CHECKSUM
  825. if ($table_name == 'feature') {
  826. $field_name = 'data__sequence_checksum';
  827. $info[$field_name] = array(
  828. 'field_name' => $field_name,
  829. 'entity_type' => $entity_type,
  830. 'bundle' => $bundle->name,
  831. 'label' => 'Sequence Checksum',
  832. 'description' => 'The MD5 checksum for the sequence. The checksum here
  833. will always be unique for the raw unformatted sequence. To verify that the
  834. sequence has not been corrupted, download the raw sequence and use an MD5 tool
  835. to calculate the value. If the value calculated is identical the one shown
  836. here, then the downloaded sequence is uncorrupted.',
  837. 'required' => FALSE,
  838. 'settings' => array(
  839. 'auto_attach' => TRUE,
  840. 'chado_table' => $table_name,
  841. 'chado_column' => 'md5checksum',
  842. 'base_table' => $table_name,
  843. ),
  844. 'widget' => array(
  845. 'type' => 'data__sequence_checksum_widget',
  846. 'settings' => array(
  847. 'display_label' => 1,
  848. 'md5_fieldname' => 'feature__md5checksum',
  849. ),
  850. ),
  851. 'display' => array(
  852. 'default' => array(
  853. 'label' => 'inline',
  854. 'type' => 'data__sequence_checksum_formatter',
  855. 'settings' => array(),
  856. ),
  857. ),
  858. );
  859. }
  860. // FEATURE RESIDUES
  861. if ($table_name == 'feature') {
  862. $field_name = 'data__sequence';
  863. $info[$field_name] = array(
  864. 'field_name' => $field_name,
  865. 'entity_type' => $entity_type,
  866. 'bundle' => $bundle->name,
  867. 'label' => 'Sequence',
  868. 'description' => 'All available sequences for this record.',
  869. 'required' => FALSE,
  870. 'settings' => array(
  871. 'auto_attach' => FALSE,
  872. 'chado_table' => $table_name,
  873. 'chado_column' => 'residues',
  874. 'base_table' => $table_name,
  875. ),
  876. 'widget' => array(
  877. 'type' => 'data__sequence_widget',
  878. 'settings' => array(
  879. 'display_label' => 1,
  880. ),
  881. ),
  882. 'display' => array(
  883. 'default' => array(
  884. 'label' => 'above',
  885. 'type' => 'data__sequence_formatter',
  886. 'settings' => array(),
  887. ),
  888. ),
  889. );
  890. }
  891. // FEATURE SEQLEN
  892. if ($table_name == 'feature') {
  893. $field_name = 'data__sequence_length';
  894. $info[$field_name] = array(
  895. 'field_name' => $field_name,
  896. 'entity_type' => $entity_type,
  897. 'bundle' => $bundle->name,
  898. 'label' => 'Sequence Length',
  899. 'description' => 'The number of residues in the raw sequence. This length
  900. is only for the assigned raw sequence and does not represent the length of any
  901. sequences derived from alignments. If this value is zero but aligned sequences
  902. are present then this record has no official assigned sequence.',
  903. 'required' => FALSE,
  904. 'settings' => array(
  905. 'auto_attach' => TRUE,
  906. 'chado_table' => $table_name,
  907. 'chado_column' => 'seqlen',
  908. 'base_table' => $table_name,
  909. ),
  910. 'widget' => array(
  911. 'type' => 'data__sequence_length_widget',
  912. 'settings' => array(
  913. 'display_label' => 1,
  914. ),
  915. ),
  916. 'display' => array(
  917. 'default' => array(
  918. 'label' => 'inline',
  919. 'type' => 'data__sequence_length_formatter',
  920. 'settings' => array(),
  921. ),
  922. ),
  923. );
  924. }
  925. // GENE TRANSCRIPTS
  926. $rel_table = $table_name . '_relationship';
  927. if (chado_table_exists($rel_table) and $bundle->label == 'gene') {
  928. $field_name = 'so__transcript';
  929. $info[$field_name] = array(
  930. 'field_name' => $field_name,
  931. 'entity_type' => $entity_type,
  932. 'bundle' => $bundle->name,
  933. 'label' => 'Transcripts',
  934. 'description' => 'Transcripts that are part of this gene.',
  935. 'required' => FALSE,
  936. 'settings' => array(
  937. 'auto_attach' => FALSE,
  938. 'chado_table' => $rel_table,
  939. 'chado_column' => '',
  940. 'base_table' => $table_name,
  941. ),
  942. 'widget' => array(
  943. 'type' => 'so__transcript_widget',
  944. 'settings' => array(
  945. 'display_label' => 1,
  946. ),
  947. ),
  948. 'display' => array(
  949. 'default' => array(
  950. 'label' => 'above',
  951. 'type' => 'so__transcript_formatter',
  952. 'settings' => array(),
  953. ),
  954. ),
  955. );
  956. }
  957. // ORGANISM TYPE_ID
  958. // if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
  959. // $field_name = 'taxarank__infraspecific_taxon';
  960. // $info[$field_name] = array(
  961. // 'field_name' => $field_name,
  962. // 'entity_type' => $entity_type,
  963. // 'bundle' => $bundle->name,
  964. // 'label' => 'Infraspecific Taxon',
  965. // 'description' => 'The Infraspecific Taxon.',
  966. // 'required' => FALSE,
  967. // 'settings' => array(
  968. // 'auto_attach' => TRUE,
  969. // 'chado_table' => 'organism',
  970. // 'chado_column' => 'type_id',
  971. // 'base_table' => 'organism',
  972. // ),
  973. // 'widget' => array(
  974. // 'type' => 'taxarank__infraspecific_taxon_widget',
  975. // 'settings' => array(
  976. // 'display_label' => 1,
  977. // ),
  978. // ),
  979. // 'display' => array(
  980. // 'default' => array(
  981. // 'label' => 'inline',
  982. // 'type' => 'taxarank__infraspecific_taxon_formatter',
  983. // 'settings' => array(),
  984. // ),
  985. // ),
  986. // );
  987. // }
  988. // FEATURE SEQLEN
  989. if ($table_name == 'featuremap') {
  990. $field_name = 'uo__unit';
  991. $info[$field_name] = array(
  992. 'field_name' => $field_name,
  993. 'entity_type' => $entity_type,
  994. 'bundle' => $bundle->name,
  995. 'label' => 'Units',
  996. 'description' => 'The map\'s unit type.',
  997. 'required' => TRUE,
  998. 'settings' => array(
  999. 'auto_attach' => TRUE,
  1000. 'chado_table' => $table_name,
  1001. 'chado_column' => 'unittype_id',
  1002. 'base_table' => $table_name,
  1003. ),
  1004. 'widget' => array(
  1005. 'type' => 'uo__unit_widget',
  1006. 'settings' => array(
  1007. 'display_label' => 1,
  1008. ),
  1009. ),
  1010. 'display' => array(
  1011. 'default' => array(
  1012. 'label' => 'inline',
  1013. 'type' => 'uo__unit_formatter',
  1014. 'settings' => array(),
  1015. ),
  1016. ),
  1017. );
  1018. }
  1019. }
  1020. /**
  1021. *
  1022. * @param unknown $entity_type
  1023. * @param unknown $bundle
  1024. * @param unknown $details
  1025. */
  1026. function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bundle, $details) {
  1027. $table_name = $details['chado_table'];
  1028. $type_table = $details['chado_type_table'];
  1029. $type_field = $details['chado_type_column'];
  1030. $cvterm_id = $details['chado_cvterm_id'];
  1031. // CONTACTS
  1032. $contact_table = $table_name . '_contact';
  1033. if (chado_table_exists($contact_table)) {
  1034. $field_name = $table_name . '_contact';
  1035. $info[$field_name] = $info[$field_name] = array(
  1036. 'field_name' => $field_name,
  1037. 'entity_type' => $entity_type,
  1038. 'bundle' => $bundle->name,
  1039. 'label' => 'Contact',
  1040. 'description' => 'Associates an indviddual or organization with this record',
  1041. 'required' => FALSE,
  1042. 'settings' => array(
  1043. 'auto_attach' => FALSE,
  1044. 'chado_table' => $contact_table,
  1045. 'base_table' => $table_name,
  1046. 'chado_column' => 'contact_id',
  1047. ),
  1048. 'widget' => array(
  1049. 'type' => 'local__contact_widget',
  1050. 'settings' => array(
  1051. 'display_label' => 1,
  1052. ),
  1053. ),
  1054. 'display' => array(
  1055. 'default' => array(
  1056. 'label' => 'above',
  1057. 'type' => 'local__contact_formatter',
  1058. 'settings' => array(),
  1059. ),
  1060. ),
  1061. );
  1062. }
  1063. // DBXREF
  1064. $dbxref_table = $table_name . '_dbxref';
  1065. if (chado_table_exists($dbxref_table)) {
  1066. $field_name = 'sbo__database_cross_reference';
  1067. $schema = chado_get_schema($dbxref_table);
  1068. $pkey = $schema['primary key'][0];
  1069. $info[$field_name] = array(
  1070. 'field_name' => $field_name,
  1071. 'entity_type' => $entity_type,
  1072. 'bundle' => $bundle->name,
  1073. 'label' => 'Database Cross Reference',
  1074. 'description' => 'The IDs where this record may be available in other external online databases.',
  1075. 'required' => FALSE,
  1076. 'settings' => array(
  1077. 'auto_attach' => FALSE,
  1078. 'chado_table' => $dbxref_table,
  1079. 'chado_column' => $pkey,
  1080. 'base_table' => $table_name,
  1081. ),
  1082. 'widget' => array(
  1083. 'type' => 'sbo__database_cross_reference_widget',
  1084. 'settings' => array(
  1085. 'display_label' => 1,
  1086. ),
  1087. ),
  1088. 'display' => array(
  1089. 'default' => array(
  1090. 'label' => 'inline',
  1091. 'type' => 'sbo__database_cross_reference_formatter',
  1092. 'settings' => array(),
  1093. ),
  1094. ),
  1095. );
  1096. }
  1097. // EXPRESSION
  1098. $expression_table = $table_name . '_expression';
  1099. if (chado_table_exists($expression_table)) {
  1100. $field_name = 'go__gene_expression';
  1101. $schema = chado_get_schema($expression_table);
  1102. $pkey = $schema['primary key'][0];
  1103. $info[$field_name] = array(
  1104. 'field_name' => $field_name,
  1105. 'entity_type' => $entity_type,
  1106. 'bundle' => $bundle->name,
  1107. 'label' => 'Gene expression',
  1108. 'description' => 'Information about the expression of this record.',
  1109. 'required' => FALSE,
  1110. 'settings' => array(
  1111. 'auto_attach' => FALSE,
  1112. 'chado_table' => $expression_table,
  1113. 'chado_column' => $pkey,
  1114. 'base_table' => $table_name,
  1115. ),
  1116. 'widget' => array(
  1117. 'type' => 'go__gene_expression_widget',
  1118. 'settings' => array(
  1119. 'display_label' => 1,
  1120. ),
  1121. ),
  1122. 'display' => array(
  1123. 'default' => array(
  1124. 'label' => 'above',
  1125. 'type' => 'go__gene_expression_formatter',
  1126. 'settings' => array(),
  1127. ),
  1128. ),
  1129. );
  1130. }
  1131. // FEATURELOC
  1132. if ($table_name == 'feature') {
  1133. $field_name = 'data__sequence_coordinates';
  1134. $schema = chado_get_schema('featureloc');
  1135. $pkey = $schema['primary key'][0];
  1136. $info[$field_name] = array(
  1137. 'field_name' => $field_name,
  1138. 'entity_type' => $entity_type,
  1139. 'bundle' => $bundle->name,
  1140. 'label' => 'Sequence Coordinates',
  1141. 'description' => 'The locations on other genomic sequences where this
  1142. record has been aligned.',
  1143. 'required' => FALSE,
  1144. 'settings' => array(
  1145. 'auto_attach' => FALSE,
  1146. 'chado_table' => 'featureloc',
  1147. 'chado_column' => $pkey,
  1148. 'base_table' => 'feature',
  1149. ),
  1150. 'widget' => array(
  1151. 'type' => 'data__sequence_coordinates_widget',
  1152. 'settings' => array(
  1153. 'display_label' => 1,
  1154. ),
  1155. ),
  1156. 'display' => array(
  1157. 'default' => array(
  1158. 'label' => 'above',
  1159. 'type' => 'data__sequence_coordinates_formatter',
  1160. 'settings' => array(),
  1161. ),
  1162. ),
  1163. );
  1164. }
  1165. // FEATUREPOS
  1166. if ($table_name == 'feature') {
  1167. $field_name = 'ogi__location_on_map';
  1168. $schema = chado_get_schema('featurepos');
  1169. $pkey = $schema['primary key'][0];
  1170. $info[$field_name] = array(
  1171. 'field_name' => $field_name,
  1172. 'entity_type' => $entity_type,
  1173. 'bundle' => $bundle->name,
  1174. 'label' => 'Location on Map',
  1175. 'description' => 'The positions on a genetic map.',
  1176. 'required' => FALSE,
  1177. 'settings' => array(
  1178. 'auto_attach' => FALSE,
  1179. 'chado_table' => 'featurepos',
  1180. 'chado_column' => $pkey,
  1181. 'base_table' => 'feature',
  1182. ),
  1183. 'widget' => array(
  1184. 'type' => 'ogi__location_on_map_widget',
  1185. 'settings' => array(
  1186. 'display_label' => 1,
  1187. ),
  1188. ),
  1189. 'display' => array(
  1190. 'default' => array(
  1191. 'label' => 'above',
  1192. 'type' => 'ogi__location_on_map_formatter',
  1193. 'settings' => array(),
  1194. ),
  1195. ),
  1196. );
  1197. }
  1198. // GENOTYPE
  1199. $genotype_table = $table_name . '_genotype';
  1200. if (chado_table_exists($genotype_table)) {
  1201. $field_name = 'so__genotype';
  1202. $schema = chado_get_schema($genotype_table);
  1203. $pkey = $schema['primary key'][0];
  1204. $info[$field_name] = array(
  1205. 'field_name' => $field_name,
  1206. 'entity_type' => $entity_type,
  1207. 'bundle' => $bundle->name,
  1208. 'label' => 'Genotype',
  1209. 'description' => 'The genotypes associated with this record.',
  1210. 'required' => FALSE,
  1211. 'settings' => array(
  1212. 'auto_attach' => FALSE,
  1213. 'chado_table' => $genotype_table,
  1214. 'chado_column' => $pkey,
  1215. 'base_table' => $table_name,
  1216. ),
  1217. 'widget' => array(
  1218. 'type' => 'so__genotype_widget',
  1219. 'settings' => array(
  1220. 'display_label' => 1,
  1221. ),
  1222. ),
  1223. 'display' => array(
  1224. 'default' => array(
  1225. 'label' => 'above',
  1226. 'type' => 'so__genotype_formatter',
  1227. 'settings' => array(),
  1228. ),
  1229. ),
  1230. );
  1231. }
  1232. // PHENOTYPE
  1233. $phenotype_table = $table_name . '_phenotype';
  1234. if (chado_table_exists($phenotype_table)) {
  1235. $field_name = 'sbo__phenotype';
  1236. $schema = chado_get_schema($phenotype_table);
  1237. $pkey = $schema['primary key'][0];
  1238. $info[$field_name] = array(
  1239. 'field_name' => $field_name,
  1240. 'entity_type' => $entity_type,
  1241. 'bundle' => $bundle->name,
  1242. 'label' => 'Phenotype',
  1243. 'description' => 'The phenotypes associated with this record.',
  1244. 'required' => FALSE,
  1245. 'settings' => array(
  1246. 'auto_attach' => FALSE,
  1247. 'chado_table' => $phenotype_table,
  1248. 'chado_column' => $pkey,
  1249. 'base_table' => $table_name,
  1250. ),
  1251. 'widget' => array(
  1252. 'type' => 'sbo__phenotype_widget',
  1253. 'settings' => array(
  1254. 'display_label' => 1,
  1255. ),
  1256. ),
  1257. 'display' => array(
  1258. 'default' => array(
  1259. 'label' => 'above',
  1260. 'type' => 'sbo__phenotype_formatter',
  1261. 'settings' => array(),
  1262. ),
  1263. ),
  1264. );
  1265. }
  1266. // PROPERTIES
  1267. $prop_table = $table_name . 'prop';
  1268. if (chado_table_exists($prop_table)) {
  1269. $tschema = chado_get_schema($table_name);
  1270. $schema = chado_get_schema($prop_table);
  1271. $tpkey = $tschema['primary key'][0];
  1272. $pkey = $schema['primary key'][0];
  1273. // Get the list of existing property types for this table.
  1274. $args = array();
  1275. $sql = "
  1276. SELECT DISTINCT P.type_id
  1277. FROM {" . $prop_table . "} P
  1278. ";
  1279. if ($type_field) {
  1280. $sql .= "
  1281. INNER JOIN {" . $table_name . "} T on T.$tpkey = P.$tpkey
  1282. WHERE T.type_id = :cvterm_id
  1283. ";
  1284. $args[':cvterm_id'] = $cvterm_id;
  1285. }
  1286. $props = chado_query($sql, $args);
  1287. while ($prop = $props->fetchObject()) {
  1288. $term = chado_generate_var('cvterm', array('cvterm_id' => $prop->type_id));
  1289. $field_name = strtolower(preg_replace('/[^\w]/','_', $term->dbxref_id->db_id->name . '__' . $term->name));
  1290. $field_name = substr($field_name, 0, 32);
  1291. $info[$field_name] = array(
  1292. 'field_name' => $field_name,
  1293. 'entity_type' => $entity_type,
  1294. 'bundle' => $bundle->name,
  1295. 'label' => ucwords(preg_replace('/_/', ' ', $term->name)),
  1296. 'description' => $term->definition,
  1297. 'required' => FALSE,
  1298. 'settings' => array(
  1299. 'auto_attach' => TRUE,
  1300. 'term_vocabulary' => $term->dbxref_id->db_id->name,
  1301. 'term_accession' => $term->dbxref_id->accession,
  1302. 'term_name' => $term->name,
  1303. 'base_table' => $table_name,
  1304. 'chado_table' => $prop_table,
  1305. 'chado_column' => $pkey,
  1306. ),
  1307. 'widget' => array(
  1308. 'type' => 'chado_linker__prop_widget',
  1309. 'settings' => array(
  1310. 'display_label' => 1,
  1311. ),
  1312. ),
  1313. 'display' => array(
  1314. 'default' => array(
  1315. 'label' => 'inline',
  1316. 'type' => 'chado_linker__prop_formatter',
  1317. 'settings' => array(),
  1318. ),
  1319. ),
  1320. );
  1321. }
  1322. }
  1323. // PUBLICATIONS
  1324. $pub_table = $table_name . '_pub';
  1325. if (chado_table_exists($pub_table)) {
  1326. $field_name = 'schema__publication';
  1327. $schema = chado_get_schema($pub_table);
  1328. $pkey = $schema['primary key'][0];
  1329. $info[$field_name] = array(
  1330. 'field_name' => $field_name,
  1331. 'entity_type' => $entity_type,
  1332. 'bundle' => $bundle->name,
  1333. 'label' => 'Publication',
  1334. 'description' => 'This record has been referenced or is sourced from these publications.',
  1335. 'required' => FALSE,
  1336. 'settings' => array(
  1337. 'auto_attach' => FALSE,
  1338. 'chado_table' => $pub_table,
  1339. 'chado_column' => $pkey,
  1340. 'base_table' => $table_name,
  1341. ),
  1342. 'widget' => array(
  1343. 'type' => 'schema__publication_widget',
  1344. 'settings' => array(
  1345. 'display_label' => 1,
  1346. ),
  1347. ),
  1348. 'display' => array(
  1349. 'default' => array(
  1350. 'label' => 'above',
  1351. 'type' => 'schema__publication_formatter',
  1352. 'settings' => array(),
  1353. ),
  1354. ),
  1355. );
  1356. }
  1357. // PUBLICATIONS (in reverse)
  1358. // We want to be able to show all of the content that a publication links
  1359. // to. The sio__references field does that.
  1360. if ($table_name == 'pub') {
  1361. $field_name = 'sio__references';
  1362. $schema = chado_get_schema($table_name);
  1363. $pkey = $schema['primary key'][0];
  1364. $info[$field_name] = array(
  1365. 'field_name' => $field_name,
  1366. 'entity_type' => $entity_type,
  1367. 'bundle' => $bundle->name,
  1368. 'label' => 'References',
  1369. 'description' => 'Records that are referred to by the publication.',
  1370. 'required' => FALSE,
  1371. 'settings' => array(
  1372. 'auto_attach' => FALSE,
  1373. 'chado_table' => $table_name,
  1374. 'chado_column' => $pkey,
  1375. 'base_table' => $table_name,
  1376. ),
  1377. 'widget' => array(
  1378. 'type' => 'sio__references_widget',
  1379. 'settings' => array(
  1380. 'display_label' => 1,
  1381. ),
  1382. ),
  1383. 'display' => array(
  1384. 'default' => array(
  1385. 'label' => 'above',
  1386. 'type' => 'sio__references_formatter',
  1387. 'settings' => array(),
  1388. ),
  1389. ),
  1390. );
  1391. }
  1392. // RELATIONSHIPS
  1393. // If the linker table does not exists then we don't want to add attach.
  1394. $rel_table = $table_name . '_relationship';
  1395. if (chado_table_exists($rel_table)) {
  1396. $field_name = 'sbo__relationship';
  1397. $schema = chado_get_schema($rel_table);
  1398. $pkey = $schema['primary key'][0];
  1399. $info[$field_name] = array(
  1400. 'field_name' => $field_name,
  1401. 'entity_type' => $entity_type,
  1402. 'bundle' => $bundle->name,
  1403. 'label' => 'Relationship',
  1404. 'description' => 'Other records with relationships to this record.',
  1405. 'required' => FALSE,
  1406. 'settings' => array(
  1407. 'auto_attach' => FALSE,
  1408. 'chado_table' => $rel_table,
  1409. 'chado_column' => $pkey,
  1410. 'base_table' => $table_name,
  1411. ),
  1412. 'widget' => array(
  1413. 'type' => 'sbo__relationship_widget',
  1414. 'settings' => array(
  1415. 'display_label' => 1,
  1416. ),
  1417. ),
  1418. 'display' => array(
  1419. 'default' => array(
  1420. 'label' => 'above',
  1421. 'type' => 'sbo__relationship_formatter',
  1422. 'settings' => array(),
  1423. ),
  1424. ),
  1425. );
  1426. }
  1427. // SYNONYMS
  1428. $syn_table = $table_name . '_synonym';
  1429. if (chado_table_exists($syn_table)) {
  1430. $field_name = 'schema__alternate_name';
  1431. $schema = chado_get_schema($syn_table);
  1432. $pkey = $schema['primary key'][0];
  1433. $info[$field_name] = array(
  1434. 'field_name' => $field_name,
  1435. 'entity_type' => $entity_type,
  1436. 'bundle' => $bundle->name,
  1437. 'label' => 'Synonyms',
  1438. 'description' => 'Alternate names, aliases or synonyms for this record.',
  1439. 'required' => FALSE,
  1440. 'settings' => array(
  1441. 'auto_attach' => FALSE,
  1442. 'chado_table' => $syn_table,
  1443. 'chado_column' => $pkey,
  1444. 'base_table' => $table_name,
  1445. ),
  1446. 'widget' => array(
  1447. 'type' => 'schema__alternate_name_widget',
  1448. 'settings' => array(
  1449. 'display_label' => 1,
  1450. ),
  1451. ),
  1452. 'display' => array(
  1453. 'default' => array(
  1454. 'label' => 'inline',
  1455. 'type' => 'schema__alternate_name_formatter',
  1456. 'settings' => array(),
  1457. ),
  1458. ),
  1459. );
  1460. }
  1461. }
  1462. /**
  1463. * Implements hook_bundle_create_user_field().
  1464. *
  1465. * A priviledged user has the ability to add new fields to the bundle. The
  1466. * chado_linker__prop and chado_linker__cvterm fields are allowed to be
  1467. * added dynamically by the user. But, Drupal doesn't know whot to deal with
  1468. * it, so this function is called for any field attached to a TripalEntity
  1469. * bundle type. Any fields whose TripalField::$module argument is set to
  1470. * 'tripal_chado' and that can be added dynamically will result in a call
  1471. * to this function.
  1472. */
  1473. function tripal_chado_bundle_create_user_field($new_field, $bundle) {
  1474. // Get the table this bundle is mapped to.
  1475. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  1476. $vocab = $term->vocab;
  1477. $params = array(
  1478. 'vocabulary' => $vocab->vocabulary,
  1479. 'accession' => $term->accession,
  1480. );
  1481. $mapped_table = chado_get_cvterm_mapping($params);
  1482. $chado_table = $mapped_table->chado_table;
  1483. $chado_type_table = $mapped_table->chado_table;
  1484. $chado_type_column = $mapped_table->chado_field;
  1485. // We allow site admins to add new chado_linker__prop fields to an entity.
  1486. // This function will allow us to properly add them. But at this point we
  1487. // don't know the controlled vocabulary term. We'll have to use the
  1488. // defaults and let the user set it using the interface.
  1489. if ($new_field['type'] == 'chado_linker__prop') {
  1490. $table_name = $chado_table . 'prop';
  1491. if (chado_table_exists($table_name)) {
  1492. $schema = chado_get_schema($table_name);
  1493. $pkey = $schema['primary key'][0];
  1494. $field_name = $new_field['field_name'];
  1495. $field_type = 'chado_linker__prop';
  1496. // First add the field.
  1497. field_create_field(array(
  1498. 'field_name' => $field_name,
  1499. 'type' => $field_type,
  1500. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1501. 'locked' => FALSE,
  1502. 'storage' => array(
  1503. 'type' => 'field_chado_storage',
  1504. ),
  1505. ));
  1506. // Now add the instance
  1507. field_create_instance(array(
  1508. 'field_name' => $field_name,
  1509. 'entity_type' => 'TripalEntity',
  1510. 'bundle' => $bundle->name,
  1511. 'label' => $new_field['label'],
  1512. 'description' => '',
  1513. 'required' => FALSE,
  1514. 'settings' => array(
  1515. 'auto_attach' => TRUE,
  1516. 'base_table' => $chado_table,
  1517. 'chado_table' => $table_name,
  1518. 'chado_column' => $pkey,
  1519. ),
  1520. 'widget' => array(
  1521. 'type' => 'chado_linker__prop_widget',
  1522. 'settings' => array(
  1523. 'display_label' => 1,
  1524. ),
  1525. ),
  1526. 'display' => array(
  1527. 'default' => array(
  1528. 'label' => 'inline',
  1529. 'type' => 'chado_linker__prop_formatter',
  1530. 'settings' => array(),
  1531. ),
  1532. ),
  1533. ));
  1534. }
  1535. else {
  1536. drupal_set_message('Cannot add a property field to this entity. Chado does not support properties for this data type.', 'error');
  1537. }
  1538. }
  1539. // We allow site admins to add new chado_linker__cvterm fields to an entity.
  1540. // This function will allow us to properly add them. But at this point we
  1541. // don't know the controlled vocabulary term. We'll have to use the
  1542. // defaults and let the user set it using the interface.
  1543. if ($new_field['type'] == 'chado_linker__cvterm') {
  1544. $table_name = $chado_table . '_cvterm';
  1545. if (chado_table_exists($table_name)) {
  1546. $schema = chado_get_schema($table_name);
  1547. $pkey = $schema['primary key'][0];
  1548. $field_name = $new_field['field_name'];
  1549. $field_type = 'chado_linker__cvterm';
  1550. // First add the field.
  1551. field_create_field(array(
  1552. 'field_name' => $field_name,
  1553. 'type' => $field_type,
  1554. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1555. 'locked' => FALSE,
  1556. 'storage' => array(
  1557. 'type' => 'field_chado_storage',
  1558. ),
  1559. 'settings' => array(
  1560. 'base_table' => $chado_table,
  1561. 'chado_table' => $table_name,
  1562. 'chado_column' => $pkey,
  1563. ),
  1564. ));
  1565. // Now add the instance
  1566. field_create_instance(array(
  1567. 'field_name' => $field_name,
  1568. 'entity_type' => 'TripalEntity',
  1569. 'bundle' => $bundle->name,
  1570. 'label' => $new_field['label'],
  1571. 'description' => '',
  1572. 'required' => FALSE,
  1573. 'settings' => array(
  1574. 'auto_attach' => TRUE,
  1575. ),
  1576. 'widget' => array(
  1577. 'type' => 'chado_linker__cvterm_widget',
  1578. 'settings' => array(
  1579. 'display_label' => 1,
  1580. ),
  1581. ),
  1582. 'display' => array(
  1583. 'default' => array(
  1584. 'label' => 'above',
  1585. 'type' => 'chado_linker__cvterm_formatter',
  1586. 'settings' => array(),
  1587. ),
  1588. ),
  1589. ));
  1590. }
  1591. else {
  1592. drupal_set_message('Cannot add a property field to this entity. Chado does not support annotations for this data type.', 'error');
  1593. }
  1594. }
  1595. }
  1596. /**
  1597. * Allows for altering of a field's instance setting form.
  1598. *
  1599. * This appears to be a Drupal hook but is actually a custom function created
  1600. * by this module. It is called by the tripal_form_alter() function of this
  1601. * module.
  1602. *
  1603. * Here we put additional form elements for any field, regardless if it is
  1604. * a tripalField or not.
  1605. *
  1606. * @param $form
  1607. * The form array. Alterations to the form can be made within this array.
  1608. * @param $form_state
  1609. * The form state array.
  1610. */
  1611. function tripal_chado_field_instance_settings_form_alter(&$form, $form_state) {
  1612. global $language;
  1613. $field = $form['#field'];
  1614. $instance = $form['#instance'];
  1615. $base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  1616. $chado_table = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  1617. $chado_column = array_key_exists('chado_column', $instance['settings']) ? $instance['settings']['chado_column'] : '';
  1618. if ($chado_table) {
  1619. // Construct a table for the vocabulary information.
  1620. $headers = array();
  1621. $rows = array();
  1622. $rows[] = array(
  1623. array(
  1624. 'data' => 'Base Table',
  1625. 'header' => TRUE,
  1626. 'width' => '20%',
  1627. ),
  1628. $base_table
  1629. );
  1630. $rows[] = array(
  1631. array(
  1632. 'data' => 'Record Table',
  1633. 'header' => TRUE,
  1634. 'width' => '20%',
  1635. ),
  1636. $chado_table
  1637. );
  1638. $rows[] = array(
  1639. array(
  1640. 'data' => 'ID Column',
  1641. 'header' => TRUE,
  1642. 'width' => '20%',
  1643. ),
  1644. $chado_column
  1645. );
  1646. $table = array(
  1647. 'header' => $headers,
  1648. 'rows' => $rows,
  1649. 'attributes' => array(
  1650. ),
  1651. 'sticky' => FALSE,
  1652. 'caption' => '',
  1653. 'colgroups' => array(),
  1654. 'empty' => '',
  1655. );
  1656. $form['chado_mapping'] = array(
  1657. '#type' => 'fieldset',
  1658. '#title' => 'Chado Mapping',
  1659. '#description' => t('This field maps to data in Chado to the following table:'),
  1660. );
  1661. $form['chado_mapping']['details'] = array(
  1662. '#type' => 'item',
  1663. '#markup' => theme_table($table),
  1664. );
  1665. }
  1666. }
  1667. /**
  1668. * Implements hook_form_FROM_ID_alter()
  1669. */
  1670. function tripal_chado_form_tripalbundle_form_alter(&$form, $form_state) {
  1671. global $language;
  1672. $bundle = $form['#bundle'];
  1673. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  1674. $term = reset($term);
  1675. $vocab = $term->vocab;
  1676. $params = array(
  1677. 'vocabulary' => $vocab->vocabulary,
  1678. 'accession' => $term->accession,
  1679. );
  1680. $mapped_table = chado_get_cvterm_mapping($params);
  1681. $chado_table = $mapped_table->chado_table;
  1682. $chado_column = $mapped_table->chado_field;
  1683. // Construct a table for the vocabulary information.
  1684. $headers = array();
  1685. $rows = array();
  1686. $rows[] = array(
  1687. array(
  1688. 'data' => 'Chado Table',
  1689. 'header' => TRUE,
  1690. 'width' => '20%',
  1691. ),
  1692. $chado_table
  1693. );
  1694. $rows[] = array(
  1695. array(
  1696. 'data' => 'Type Column',
  1697. 'header' => TRUE,
  1698. 'width' => '20%',
  1699. ),
  1700. $chado_column
  1701. );
  1702. $table = array(
  1703. 'header' => $headers,
  1704. 'rows' => $rows,
  1705. 'attributes' => array(
  1706. ),
  1707. 'sticky' => FALSE,
  1708. 'caption' => '',
  1709. 'colgroups' => array(),
  1710. 'empty' => '',
  1711. );
  1712. $form['chado_mapping'] = array(
  1713. '#type' => 'item',
  1714. '#title' => 'Chado Mapping',
  1715. '#markup' => theme_table($table),
  1716. '#description' => t('This content type maps to the table in Chado
  1717. listed above. Chado allows multiple data types to be housed
  1718. in a single table. Therefore, the column that is used to
  1719. differentiate between data types is also listed above.'),
  1720. '#weight' => 0,
  1721. );
  1722. }