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