tripal_chado.fields.inc 77 KB

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