tripal_chado.fields.inc 50 KB

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