tripal_chado.fields.inc 69 KB

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