tripal_chado.fields.inc 54 KB

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