tripal_chado.fields.inc 51 KB

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