tripal_chado.fields.inc 72 KB

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