tripal_chado.fields.inc 55 KB

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