tripal_chado.fields.inc 53 KB

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