tripal_chado.fields.inc 57 KB

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