tripal_chado.fields.inc 63 KB

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