tripal_chado.fields.inc 57 KB

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