tripal_chado.fields.inc 53 KB

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