tripal_chado.fields.inc 53 KB

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