tripal_chado.setup.inc 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Prepares Chado for Tripal use
  8. */
  9. function tripal_chado_prepare_form($form, $form_state) {
  10. $form = array();
  11. if (variable_get('tripal_chado_is_prepared') == TRUE) {
  12. drupal_set_message('Your site is prepared.');
  13. }
  14. $form['instructions'] = array(
  15. '#type' => 'item',
  16. '#title' => 'Prepare Drupal for Chado.',
  17. '#description' => t("Before a Drupal site can use Chado (via Tripal), both
  18. Chado and Drupal must be prepared a bit more. Tripal will add some new
  19. materialized views, custom tables and controlled vocabularies to Chado.
  20. It will also add some management tables to Drupal. You only are
  21. required to prepare your Drupal site if this is a brand-new Drupal
  22. installation or if Chado was installed outside of Tripal. If you
  23. installed Chado using Tripal then you do not need to run this step.
  24. If you are upgrading from a previous version of Tripal, you do not
  25. need to prepare your site, and you can click the 'Skip' button."),
  26. );
  27. $form['prepare-button'] = array(
  28. '#type' => 'submit',
  29. '#value' => t('Prepare this site'),
  30. '#name' => 'prepare-chado',
  31. );
  32. $form['skip-button'] = array(
  33. '#type' => 'submit',
  34. '#value' => t('Skip'),
  35. '#name' => 'prepare-skip',
  36. );
  37. return $form;
  38. }
  39. /**
  40. * Submit function for the tripal_chado_prepare_form().
  41. *
  42. * @param $form
  43. * @param $form_state
  44. */
  45. function tripal_chado_prepare_form_submit($form, $form_state) {
  46. if ($form_state['clicked_button']['#name'] == "prepare-chado") {
  47. global $user;
  48. $args = array();
  49. $includes = array(
  50. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup'),
  51. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader'),
  52. );
  53. tripal_add_job('Prepare Chado', 'tripal_chado',
  54. 'tripal_chado_prepare_chado', $args,
  55. $user->uid, 10, $includes);
  56. }
  57. if ($form_state['clicked_button']['#name'] == "prepare-skip") {
  58. variable_set('tripal_chado_is_prepared', TRUE);
  59. }
  60. }
  61. /**
  62. * Prepares Chado for use by Tripal.
  63. */
  64. function tripal_chado_prepare_chado() {
  65. try {
  66. // we want to force the version of Chado to be set properly
  67. $real_version = chado_get_version(TRUE);
  68. // get the effective version. Pass true as second argument
  69. // to warn the user if the current version is not compatible
  70. $version = chado_get_version(FALSE, FALSE);
  71. ///////////////////////////////////////////////////////////////////////////
  72. // Chado DB Module
  73. ///////////////////////////////////////////////////////////////////////////
  74. tripal_insert_db(array(
  75. 'name' => 'null',
  76. 'description' => 'No online database.'
  77. ));
  78. tripal_insert_db(array(
  79. 'name' => 'local',
  80. 'description' => variable_get('site_name', 'This site.'),
  81. ));
  82. ///////////////////////////////////////////////////////////////////////////
  83. // Chado CV Module
  84. ///////////////////////////////////////////////////////////////////////////
  85. // Add the cv_root_mview.
  86. tripal_cv_add_cv_root_mview();
  87. // Add defaults to the tables that correlate OBO files/references with
  88. // a chado CV.
  89. tripal_cv_add_obo_defaults();
  90. // Create the temp table we will use for loading OBO files.
  91. tripal_cv_create_tripal_obo_temp();
  92. // Add the synonym_type vocabulary.
  93. tripal_insert_cv(
  94. 'synonym_type',
  95. 'A vocabulary used for synonym types (e.g. exact, broad, narrow, related).'
  96. );
  97. // Add the Chado ontology CV.
  98. //$obo_path = '{tripal_chado}/files/cv_property.obo';
  99. //$obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  100. //tripal_chado_load_obo_v1_2_id($obo_id);
  101. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  102. ///////////////////////////////////////////////////////////////////////////
  103. // Chado Misc
  104. ///////////////////////////////////////////////////////////////////////////
  105. tripal_insert_cv(
  106. 'synonym_type',
  107. 'A vocabulary for storing synonym types.'
  108. );
  109. // set the default vocabularies
  110. //tripal_set_default_cv('synonym', 'type_id', 'synonym_type');
  111. // Add cvterms.
  112. tripal_insert_misc_cvterms();
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Chado Organism Module
  115. /////////////////////////////////////////////////////////////////////////////
  116. tripal_insert_cv(
  117. 'organism_property',
  118. 'Contains properties for organisms'
  119. );
  120. // Set the default vocabularies.
  121. //tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
  122. // Add taxonomic terms.
  123. $obo_id = tripal_insert_obo('Taxonomic Rank', 'http://purl.obolibrary.org/obo/taxrank.obo');
  124. $cv = chado_select_record('cv', array('name'), array('name' => 'taxonomic_rank'));
  125. if (count($cv) == 0 and $obo_id) {
  126. tripal_chado_load_obo_v1_2_id($obo_id);
  127. }
  128. /////////////////////////////////////////////////////////////////////////////
  129. // Chado CompAnalysis Module
  130. /////////////////////////////////////////////////////////////////////////////
  131. // we may need the analysisfeatureprop table if it doesn't already exist
  132. tripal_analysis_create_analysisfeatureprop();
  133. // add cvterms
  134. tripal_analysis_add_cvterms();
  135. // add materialized views
  136. tripal_analysis_add_mview_analysis_organism();
  137. // set the default vocabularies
  138. //tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  139. /////////////////////////////////////////////////////////////////////////////
  140. // Chado Contact Module
  141. /////////////////////////////////////////////////////////////////////////////
  142. // Add the contactprop table to Chado.
  143. tripal_contact_add_custom_tables();
  144. tripal_insert_db(array(
  145. 'name' => 'TContact',
  146. 'description' => 'The Tripal Contact controlled vocabulary.',
  147. ));
  148. tripal_insert_db(array(
  149. 'name' => 'TPUB',
  150. 'description' => 'The Tripal Publication controlled vocabulary.',
  151. ));
  152. // Add loading of the the tripal contact ontology to the job queue.
  153. $obo_path = '{tripal_chado}/files/tcontact.obo';
  154. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  155. $cv = chado_select_record('cv', array('name'), array('name' => 'tripal_contact'));
  156. if (count($cv) == 0 and $obo) {
  157. tripal_chado_load_obo_v1_2_id($obo_id);
  158. }
  159. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  160. // Add cvterms for relationship types.
  161. tripal_contact_add_cvs();
  162. // Set the default vocabularies.
  163. //('contact', 'type_id', 'tripal_contact');
  164. //tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  165. //tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  166. /////////////////////////////////////////////////////////////////////////////
  167. // Chado Feature Module
  168. /////////////////////////////////////////////////////////////////////////////
  169. // Note: the feature_property OBO that came with Chado v1.2 should not
  170. // be automatically installed. Some of the terms are duplicates of
  171. // others in better maintained vocabularies. New Tripal sites should
  172. // use those.
  173. // $obo_path = '{tripal_feature}/files/feature_property.obo';
  174. // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  175. // tripal_chado_load_obo_v1_2_id($obo_id);
  176. //// tripal_submit_obo_job(array('obo_id' => $obo_id));
  177. // Add the vocabularies used by the feature module.
  178. tripal_feature_add_cvs();
  179. // Add the materialized view.
  180. tripal_feature_add_organism_count_mview();
  181. // Add the custom tables.
  182. tripal_feature_add_tripal_gff_temp_table();
  183. tripal_feature_add_tripal_gffcds_temp_table();
  184. tripal_feature_add_tripal_gffprotein_temp_table();
  185. // Load the sequence ontology if it isn't already loaded
  186. $cv = chado_select_record('cv', array('name'), array('name' => 'sequence'));
  187. $obo = tripal_get_obo(array('obo_name' => 'Sequence Ontology'));
  188. if (count($cv) == 0 and $obo) {
  189. print "Installing the Sequence Ontology... This may take a while...\n";
  190. tripal_chado_load_obo_v1_2_id($obo->obo_id);
  191. }
  192. // Set the default vocabularies.
  193. //tripal_set_default_cv('feature', 'type_id', 'sequence');
  194. //tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  195. //tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  196. /////////////////////////////////////////////////////////////////////////////
  197. // Chado Map Module
  198. /////////////////////////////////////////////////////////////////////////////
  199. // add the featuremapprop table to Chado
  200. tripal_featuremap_add_custom_tables();
  201. // Add cvterms
  202. tripal_featuremap_add_cvs();
  203. tripal_featuremap_add_cvterms();
  204. // set the default vocabularies
  205. //tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
  206. //tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
  207. //tripal_set_default_cv('featuremap', 'unittype_id', 'featuremap_units');
  208. /////////////////////////////////////////////////////////////////////////////
  209. // Chado Library Module
  210. /////////////////////////////////////////////////////////////////////////////
  211. // add the materialized view
  212. tripal_library_add_mview_library_feature_count();
  213. // add cvterms
  214. tripal_library_add_cvs();
  215. tripal_library_add_cvterms();
  216. // set the default vocabularies
  217. //tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  218. //tripal_set_default_cv('library', 'type_id', 'library_type');
  219. /////////////////////////////////////////////////////////////////////////////
  220. // Chado NatDiv Module
  221. /////////////////////////////////////////////////////////////////////////////
  222. // add cvterms
  223. tripal_natural_diversity_add_cvterms();
  224. /////////////////////////////////////////////////////////////////////////////
  225. // Chado Project Module
  226. /////////////////////////////////////////////////////////////////////////////
  227. tripal_project_add_cvs();
  228. tripal_project_add_cvterms();
  229. // set the default vocabularies
  230. //tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  231. ///tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  232. /////////////////////////////////////////////////////////////////////////////
  233. // Chado Pub Module
  234. /////////////////////////////////////////////////////////////////////////////
  235. global $base_path;
  236. // add loading of the the tripal pub ontology to the job queue
  237. $obo_path = '{tripal_chado}/files/tpub.obo';
  238. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  239. $cv = chado_select_record('cv', array('name'), array('name' => 'tripal_pub'));
  240. if (count($cv) == 0 and $obo) {
  241. tripal_chado_load_obo_v1_2_id($obo_id);
  242. }
  243. tripal_pub_add_cvs();
  244. tripal_pub_add_dbs();
  245. // add the custom tables
  246. tripal_pub_add_custom_tables();
  247. // set the default vocabularies
  248. //tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  249. //tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  250. //tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  251. // Add the supported loaders
  252. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  253. /////////////////////////////////////////////////////////////////////////////
  254. // Chado Stock Module
  255. /////////////////////////////////////////////////////////////////////////////
  256. // add some controlled vocabularies
  257. tripal_stock_add_cvs();
  258. // set the default vocabularies
  259. //tripal_set_default_cv('stock', 'type_id', 'stock_type');
  260. //tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  261. //tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  262. // add the materialized view
  263. tripal_stock_add_organism_count_mview();
  264. /////////////////////////////////////////////////////////////////////////////
  265. // Entity Bundles
  266. /////////////////////////////////////////////////////////////////////////////
  267. // First, populate the semantic web associations for Chado tables/fields.
  268. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.semweb');
  269. tripal_chado_populate_chado_semweb_table();
  270. // Unfortunately, some Chado base tables do not have a type_id, so we must
  271. // take special action for those tables. These include: organism and
  272. // analysis. Until we can find an appropriate controlled vocabulary
  273. // that is well supported by the community with types for these tables we
  274. // will have to use in-house terms.
  275. // Add a term to be used for an inherent 'type_id' for the organism table.
  276. tripal_insert_cvterm(array(
  277. 'id' => 'local:analysis',
  278. 'name' => 'analysis',
  279. 'definition' => 'A process as a method of studying the nature of something ' .
  280. 'or of determining its essential features and their relations. ' .
  281. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  282. 'Dictionaries Ltd).',
  283. 'cv_name' => 'local',
  284. ));
  285. // TODO: change this to foaf:Project
  286. tripal_insert_cvterm(array(
  287. 'id' => 'local:project',
  288. 'name' => 'project',
  289. 'definition' => 'A plan or proposal for accomplishing something. ' .
  290. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  291. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  292. 'cv_name' => 'local',
  293. ));
  294. tripal_insert_cvterm(array(
  295. 'id' => 'local:contact',
  296. 'name' => 'contact',
  297. 'definition' => 'An entity (e.g. individual or organization) through ' .
  298. 'whom a person can gain access to information, favors, ' .
  299. 'influential people, and the like.',
  300. 'cv_name' => 'local',
  301. ));
  302. tripal_insert_cvterm(array(
  303. 'id' => 'local:relationship',
  304. 'name' => 'relationship',
  305. 'definition' => 'The way in which two things are connected.',
  306. 'cv_name' => 'local',
  307. ));
  308. tripal_insert_cvterm(array(
  309. 'id' => 'local:biomaterial',
  310. 'name' => 'biomaterial',
  311. 'definition' => 'A biomaterial represents the MAGE concept of BioSource, BioSample, ' .
  312. 'and LabeledExtract. It is essentially some biological material (tissue, cells, serum) that ' .
  313. 'may have been processed. Processed biomaterials should be traceable back to raw ' .
  314. 'biomaterials via the biomaterialrelationship table.',
  315. 'cv_name' => 'local',
  316. ));
  317. // For the TripalBundle entities we will want to associate the cvterm_id,
  318. // and the chado table and field that it maps to. We will use a few
  319. // variables to do this:
  320. tripal_insert_variable(
  321. 'chado_cv_id',
  322. 'The ID of the controlled vocabulary in the chado.cv table..'
  323. );
  324. tripal_insert_variable(
  325. 'chado_cvterm_id',
  326. 'The ID of the controlled vocabulary term in the chado.cvterm table.'
  327. );
  328. tripal_insert_variable(
  329. 'chado_table',
  330. 'The name of the table to which a TripalBundle maps.'
  331. );
  332. tripal_insert_variable(
  333. 'chado_type_table',
  334. 'The table that houses the foreign key the cvterm table.'
  335. );
  336. tripal_insert_variable(
  337. 'chado_type_column',
  338. 'The name of the column within the type table that houses the cvterm.'
  339. );
  340. // We want to provide a set of commonly used entity types by default. This
  341. // way when a user first installs Tripal there are some commonly used
  342. // formats.
  343. module_load_include('inc', 'tripal', 'api/tripal.api');
  344. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  345. // Create the 'Organism' entity type. This uses the obi:organism term.
  346. $error = '';
  347. if (!tripal_create_bundle('OBI', '0100026', 'organism', $error)) {
  348. throw new Exception($error['!message']);
  349. }
  350. // Create the 'Analysis' entity type. This uses the local:analysis term.
  351. $error = '';
  352. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  353. throw new Exception($error['!message']);
  354. }
  355. // Create the 'Project' entity type. This uses the local:project term.
  356. $error = '';
  357. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  358. throw new Exception($error['!message']);
  359. }
  360. // Initialize the population of the tripal_cvterm_mapping table.
  361. tripal_chado_map_cvterms();
  362. // Set a variable to indicate the site is prepared.
  363. variable_set('tripal_chado_is_prepared', TRUE);
  364. }
  365. catch (Exception $e) {
  366. throw new Exception($e);
  367. }
  368. }
  369. /**
  370. * Creates a materialized view that stores the type & number of stocks per organism
  371. *
  372. * @ingroup tripal_stock
  373. */
  374. function tripal_stock_add_organism_count_mview() {
  375. $view_name = 'organism_stock_count';
  376. $comment = 'Stores the type and number of stocks per organism';
  377. $schema = array(
  378. 'description' => $comment,
  379. 'table' => $view_name,
  380. 'fields' => array(
  381. 'organism_id' => array(
  382. 'size' => 'big',
  383. 'type' => 'int',
  384. 'not null' => TRUE,
  385. ),
  386. 'genus' => array(
  387. 'type' => 'varchar',
  388. 'length' => '255',
  389. 'not null' => TRUE,
  390. ),
  391. 'species' => array(
  392. 'type' => 'varchar',
  393. 'length' => '255',
  394. 'not null' => TRUE,
  395. ),
  396. 'common_name' => array(
  397. 'type' => 'varchar',
  398. 'length' => '255',
  399. 'not null' => FALSE,
  400. ),
  401. 'num_stocks' => array(
  402. 'type' => 'int',
  403. 'not null' => TRUE,
  404. ),
  405. 'cvterm_id' => array(
  406. 'size' => 'big',
  407. 'type' => 'int',
  408. 'not null' => TRUE,
  409. ),
  410. 'stock_type' => array(
  411. 'type' => 'varchar',
  412. 'length' => '255',
  413. 'not null' => TRUE,
  414. ),
  415. ),
  416. 'indexes' => array(
  417. 'organism_stock_count_idx1' => array('organism_id'),
  418. 'organism_stock_count_idx2' => array('cvterm_id'),
  419. 'organism_stock_count_idx3' => array('stock_type'),
  420. ),
  421. );
  422. $sql = "
  423. SELECT
  424. O.organism_id, O.genus, O.species, O.common_name,
  425. count(S.stock_id) as num_stocks,
  426. CVT.cvterm_id, CVT.name as stock_type
  427. FROM organism O
  428. INNER JOIN stock S ON O.Organism_id = S.organism_id
  429. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  430. GROUP BY
  431. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  432. ";
  433. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  434. }
  435. /**
  436. * Add cvs related to publications
  437. *
  438. * @ingroup tripal_pub
  439. */
  440. function tripal_stock_add_cvs() {
  441. // Add cv for relationship types
  442. tripal_insert_cv(
  443. 'stock_relationship',
  444. 'Contains types of relationships between stocks.'
  445. );
  446. tripal_insert_cv(
  447. 'stock_property',
  448. 'Contains properties for stocks.'
  449. );
  450. tripal_insert_cv(
  451. 'stock_type',
  452. 'Contains a list of types for stocks.'
  453. );
  454. }
  455. /**
  456. * Add custom table related to publications
  457. * - pubauthor_contact
  458. *
  459. * @ingroup tripal_pub
  460. */
  461. function tripal_pub_add_custom_tables() {
  462. $schema = array (
  463. 'table' => 'pubauthor_contact',
  464. 'fields' => array (
  465. 'pubauthor_contact_id' => array (
  466. 'type' => 'serial',
  467. 'not null' => true,
  468. ),
  469. 'contact_id' => array (
  470. 'type' => 'int',
  471. 'not null' => true,
  472. ),
  473. 'pubauthor_id' => array (
  474. 'type' => 'int',
  475. 'not null' => true,
  476. ),
  477. ),
  478. 'primary key' => array (
  479. 0 => 'pubauthor_contact_id',
  480. ),
  481. 'unique keys' => array (
  482. 'pubauthor_contact_c1' => array (
  483. 0 => 'contact_id',
  484. 1 => 'pubauthor_id',
  485. ),
  486. ),
  487. 'foreign keys' => array (
  488. 'contact' => array (
  489. 'table' => 'contact',
  490. 'columns' => array (
  491. 'contact_id' => 'contact_id',
  492. ),
  493. ),
  494. 'pubauthor' => array (
  495. 'table' => 'pubauthor',
  496. 'columns' => array (
  497. 'pubauthor_id' => 'pubauthor_id',
  498. ),
  499. ),
  500. ),
  501. );
  502. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  503. }
  504. /**
  505. * Adds dbs related to publications
  506. *
  507. * @ingroup tripal_pub
  508. */
  509. function tripal_pub_add_dbs() {
  510. // make sure we have our supported databases
  511. tripal_insert_db(
  512. array(
  513. 'name' => 'PMID',
  514. 'description' => 'PubMed',
  515. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  516. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  517. ),
  518. array('update_existing' => TRUE)
  519. );
  520. tripal_insert_db(
  521. array(
  522. 'name' => 'AGL',
  523. 'description' => 'USDA National Agricultural Library',
  524. 'url' => 'http://agricola.nal.usda.gov/'
  525. ),
  526. array('update_existing' => TRUE)
  527. );
  528. }
  529. /**
  530. * Add cvs related to publications
  531. *
  532. * @ingroup tripal_pub
  533. */
  534. function tripal_pub_add_cvs() {
  535. // Add the cv for pub properties
  536. tripal_insert_cv(
  537. 'tripal_pub',
  538. 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  539. );
  540. // Add the cv for pub types
  541. tripal_insert_cv(
  542. 'pub_type',
  543. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  544. );
  545. // Add the cv for pub properties
  546. tripal_insert_cv(
  547. 'pub_property',
  548. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  549. );
  550. // Add cv for relationship types
  551. tripal_insert_cv(
  552. 'pub_relationship',
  553. 'Contains types of relationships between publications.'
  554. );
  555. }
  556. /**
  557. * Add cvs pertaining to projects
  558. *
  559. * @ingroup tripal_project
  560. */
  561. function tripal_project_add_cvs() {
  562. // Add the cv for project properties
  563. tripal_insert_cv(
  564. 'project_property',
  565. 'Contains properties for projects'
  566. );
  567. // Add cv for relationship types
  568. tripal_insert_cv(
  569. 'project_relationship',
  570. 'Contains Types of relationships between projects.'
  571. );
  572. }
  573. /**
  574. * Add cvterms pertaining to projects
  575. *
  576. * @ingroup tripal_project
  577. */
  578. function tripal_project_add_cvterms() {
  579. // Insert cvterm 'Project Description' into cvterm table of chado
  580. // database. This CV term is used to keep track of the project
  581. // description in the projectprop table.
  582. tripal_insert_cvterm(
  583. array(
  584. 'name' => 'Project Description',
  585. 'definition' => 'Description of a project',
  586. 'cv_name' => 'project_property',
  587. 'is_relationship' => 0,
  588. 'db_name' => 'local'
  589. ),
  590. array('update_existing' => TRUE)
  591. );
  592. }
  593. /**
  594. * Add cvterms related to natural diversity
  595. *
  596. * @ingroup tripal_natural_diversity
  597. */
  598. function tripal_natural_diversity_add_cvterms(){
  599. // add cvterms for the nd_experiment_types
  600. tripal_insert_cvterm(
  601. array(
  602. 'name' => 'Genotyping',
  603. 'definition' => 'An experiment where genotypes of individuals are identified.',
  604. 'cv_name' => 'nd_experiment_types',
  605. 'is_relationship' => 0,
  606. 'db_name' => 'local'
  607. ),
  608. array('update_existing' => TRUE)
  609. );
  610. tripal_insert_cvterm(
  611. array(
  612. 'name' => 'Phenotyping',
  613. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  614. 'cv_name' => 'nd_experiment_types',
  615. 'is_relationship' => 0,
  616. 'db_name' => 'local'
  617. ),
  618. array('update_existing' => TRUE)
  619. );
  620. tripal_insert_cvterm(
  621. array(
  622. 'name' => 'Location',
  623. 'definition' => 'The name of the location.',
  624. 'cv_name' => 'nd_geolocation_property',
  625. 'is_relationship' => 0,
  626. 'db_name' => 'local'
  627. ),
  628. array('update_existing' => TRUE)
  629. );
  630. }
  631. /**
  632. * Adds a materialized view keeping track of the type of features associated with each library
  633. *
  634. * @ingroup tripal_library
  635. */
  636. function tripal_library_add_mview_library_feature_count(){
  637. $view_name = 'library_feature_count';
  638. $comment = 'Provides count of feature by type that are associated with all libraries';
  639. $schema = array(
  640. 'table' => $view_name,
  641. 'description' => $comment,
  642. 'fields' => array(
  643. 'library_id' => array(
  644. 'size' => 'big',
  645. 'type' => 'int',
  646. 'not null' => TRUE,
  647. ),
  648. 'name' => array(
  649. 'type' => 'varchar',
  650. 'length' => 255,
  651. 'not null' => TRUE,
  652. ),
  653. 'num_features' => array(
  654. 'type' => 'int',
  655. 'not null' => TRUE,
  656. ),
  657. 'feature_type' => array(
  658. 'type' => 'varchar',
  659. 'length' => 255,
  660. 'not null' => TRUE,
  661. ),
  662. ),
  663. 'indexes' => array(
  664. 'library_feature_count_idx1' => array('library_id'),
  665. ),
  666. );
  667. $sql = "
  668. SELECT
  669. L.library_id, L.name,
  670. count(F.feature_id) as num_features,
  671. CVT.name as feature_type
  672. FROM library L
  673. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  674. INNER JOIN feature F ON LF.feature_id = F.feature_id
  675. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  676. GROUP BY L.library_id, L.name, CVT.name
  677. ";
  678. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  679. }
  680. /**
  681. * Adds cvterms needed for the library module
  682. *
  683. * @ingroup tripal_library
  684. */
  685. function tripal_library_add_cvterms() {
  686. // Insert cvterm 'library_description' into cvterm table of chado
  687. // database. This CV term is used to keep track of the library
  688. // description in the libraryprop table.
  689. tripal_insert_cvterm(
  690. array(
  691. 'id' => 'local:library_description',
  692. 'name' => 'Library Description',
  693. 'definition' => 'Description of a library',
  694. 'cv_name' => 'library_property',
  695. 'is_relationship' => 0,
  696. 'db_name' => 'local'
  697. ),
  698. array('update_existing' => TRUE)
  699. );
  700. // add cvterms for the map unit types
  701. tripal_insert_cvterm(
  702. array(
  703. 'id' => 'local:cdna_library',
  704. 'name' => 'cdna_library',
  705. 'definition' => 'cDNA library',
  706. 'cv_name' => 'library_type',
  707. 'is_relationship' => 0,
  708. 'db_name' => 'local'
  709. ),
  710. array('update_existing' => TRUE)
  711. );
  712. tripal_insert_cvterm(
  713. array(
  714. 'id' => 'local:bac_library',
  715. 'name' => 'bac_library',
  716. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  717. 'cv_name' => 'library_type',
  718. 'is_relationship' => 0,
  719. 'db_name' => 'local'
  720. ),
  721. array('update_existing' => TRUE)
  722. );
  723. tripal_insert_cvterm(
  724. array(
  725. 'id' => 'local:fosmid_library',
  726. 'name' => 'fosmid_library',
  727. 'definition' => 'Fosmid library',
  728. 'cv_name' => 'library_type',
  729. 'is_relationship' => 0,
  730. 'db_name' => 'local'
  731. ),
  732. array('update_existing' => TRUE)
  733. );
  734. tripal_insert_cvterm(
  735. array(
  736. 'id' => 'local:cosmid_library',
  737. 'name' => 'cosmid_library',
  738. 'definition' => 'Cosmid library',
  739. 'cv_name' => 'library_type',
  740. 'is_relationship' => 0,
  741. 'db_name' => 'local'
  742. ),
  743. array('update_existing' => TRUE)
  744. );
  745. tripal_insert_cvterm(
  746. array(
  747. 'id' => 'local:yac_library',
  748. 'name' => 'yac_library',
  749. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  750. 'cv_name' => 'library_type',
  751. 'is_relationship' => 0,
  752. 'db_name' => 'local'
  753. ),
  754. array('update_existing' => TRUE)
  755. );
  756. tripal_insert_cvterm(
  757. array(
  758. 'id' => 'local:genomic_library',
  759. 'name' => 'genomic_library',
  760. 'definition' => 'Genomic Library',
  761. 'cv_name' => 'library_type',
  762. 'is_relationship' => 0,
  763. 'db_name' => 'local'
  764. ),
  765. array('update_existing' => TRUE)
  766. );
  767. }
  768. /**
  769. * Adds new CV's used by this module
  770. *
  771. * @ingroup tripal_library
  772. */
  773. function tripal_library_add_cvs(){
  774. tripal_insert_cv(
  775. 'library_property',
  776. 'Contains properties for libraries.'
  777. );
  778. tripal_insert_cv(
  779. 'library_type',
  780. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  781. );
  782. }
  783. /**
  784. * Add custom tables needed by the feature map module
  785. * - featuremapprop
  786. * - featuremap_dbxref
  787. * - featureposprop
  788. *
  789. * @ingroup tripal_featuremap
  790. */
  791. function tripal_featuremap_add_custom_tables(){
  792. // add the featuremaprop table to Chado
  793. $schema = array (
  794. 'table' => 'featuremapprop',
  795. 'fields' => array (
  796. 'featuremapprop_id' => array (
  797. 'type' => 'serial',
  798. 'not null' => true,
  799. ),
  800. 'featuremap_id' => array (
  801. 'type' => 'int',
  802. 'not null' => true,
  803. ),
  804. 'type_id' => array (
  805. 'type' => 'int',
  806. 'not null' => true,
  807. ),
  808. 'value' => array (
  809. 'type' => 'text',
  810. 'not null' => false,
  811. ),
  812. 'rank' => array (
  813. 'type' => 'int',
  814. 'not null' => true,
  815. 'default' => 0,
  816. ),
  817. ),
  818. 'primary key' => array (
  819. 0 => 'featuremapprop_id',
  820. ),
  821. 'unique keys' => array (
  822. 'featuremapprop_c1' => array (
  823. 0 => 'featuremap_id',
  824. 1 => 'type_id',
  825. 2 => 'rank',
  826. ),
  827. ),
  828. 'indexes' => array (
  829. 'featuremapprop_idx1' => array (
  830. 0 => 'featuremap_id',
  831. ),
  832. 'featuremapprop_idx2' => array (
  833. 0 => 'type_id',
  834. ),
  835. ),
  836. 'foreign keys' => array (
  837. 'cvterm' => array (
  838. 'table' => 'cvterm',
  839. 'columns' => array (
  840. 'type_id' => 'cvterm_id',
  841. ),
  842. ),
  843. 'featuremap' => array (
  844. 'table' => 'featuremap',
  845. 'columns' => array (
  846. 'featuremap_id' => 'featuremap_id',
  847. ),
  848. ),
  849. ),
  850. );
  851. chado_create_custom_table('featuremapprop', $schema, TRUE);
  852. // add the featuremap_dbxref table to Chado
  853. $schema = array (
  854. 'table' => 'featuremap_dbxref',
  855. 'fields' => array (
  856. 'featuremap_dbxref_id' => array (
  857. 'type' => 'serial',
  858. 'not null' => true,
  859. ),
  860. 'featuremap_id' => array (
  861. 'type' => 'int',
  862. 'not null' => true,
  863. ),
  864. 'dbxref_id' => array (
  865. 'type' => 'int',
  866. 'not null' => true,
  867. ),
  868. ),
  869. 'primary key' => array (
  870. 0 => 'featuremap_dbxref_id',
  871. ),
  872. 'unique keys' => array (
  873. 'featuremap_dbxref_c1' => array (
  874. 0 => 'featuremap_id',
  875. 1 => 'dbxref_id',
  876. ),
  877. ),
  878. 'indexes' => array (
  879. 'featuremap_dbxref_idx1' => array (
  880. 0 => 'featuremap_dbxref_id',
  881. ),
  882. 'featuremap_dbxref_idx2' => array (
  883. 0 => 'dbxref_id',
  884. ),
  885. ),
  886. 'foreign keys' => array (
  887. 'dbxref' => array (
  888. 'table' => 'dbxref',
  889. 'columns' => array (
  890. 'dbxref_id' => 'dbxref_id',
  891. ),
  892. ),
  893. 'featuremap' => array (
  894. 'table' => 'featuremap',
  895. 'columns' => array (
  896. 'featuremap_id' => 'featuremap_id',
  897. ),
  898. ),
  899. ),
  900. 'referring_tables' => NULL,
  901. );
  902. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  903. $schema = array (
  904. 'table' => 'featureposprop',
  905. 'fields' => array (
  906. 'featureposprop_id' => array (
  907. 'type' => 'serial',
  908. 'not null' => true,
  909. ),
  910. 'featurepos_id' => array (
  911. 'type' => 'int',
  912. 'not null' => true,
  913. ),
  914. 'type_id' => array (
  915. 'type' => 'int',
  916. 'not null' => true,
  917. ),
  918. 'value' => array (
  919. 'type' => 'text',
  920. 'not null' => false,
  921. ),
  922. 'rank' => array (
  923. 'type' => 'int',
  924. 'not null' => true,
  925. 'default' => 0,
  926. ),
  927. ),
  928. 'primary key' => array (
  929. 0 => 'featureposprop_id',
  930. ),
  931. 'unique keys' => array (
  932. 'featureposprop_id' => array (
  933. 0 => 'featurepos_id',
  934. 1 => 'type_id',
  935. 2 => 'rank',
  936. ),
  937. ),
  938. 'indexes' => array (
  939. 'featureposprop_c1' => array (
  940. 0 => 'featurepos_id',
  941. ),
  942. 'featureposprop_idx2' => array (
  943. 0 => 'type_id',
  944. ),
  945. ),
  946. 'foreign keys' => array (
  947. 'cvterm' => array (
  948. 'table' => 'cvterm',
  949. 'columns' => array (
  950. 'type_id' => 'cvterm_id',
  951. ),
  952. ),
  953. 'featurepos' => array (
  954. 'table' => 'featurepos',
  955. 'columns' => array (
  956. 'featurepos_id' => 'featurepos_id',
  957. ),
  958. ),
  959. ),
  960. );
  961. chado_create_custom_table('featureposprop', $schema, TRUE);
  962. }
  963. /**
  964. * Add cv terms needed by the featuremap module
  965. *
  966. * @ingroup tripal_featuremap
  967. */
  968. function tripal_featuremap_add_cvterms() {
  969. // add cvterms for the map unit types
  970. tripal_insert_cvterm(
  971. array(
  972. 'name' => 'cM',
  973. 'definition' => 'Centimorgan units',
  974. 'cv_name' => 'featuremap_units',
  975. 'is_relationship' => 0,
  976. 'db_name' => 'local'
  977. ),
  978. array('update_existing' => TRUE)
  979. );
  980. tripal_insert_cvterm(
  981. array(
  982. 'name' => 'bp',
  983. 'definition' => 'Base pairs units',
  984. 'cv_name' => 'featuremap_units',
  985. 'is_relationship' => 0,
  986. 'db_name' => 'local'
  987. ),
  988. array('update_existing' => TRUE)
  989. );
  990. tripal_insert_cvterm(
  991. array(
  992. 'name' => 'bin_unit',
  993. 'definition' => 'The bin unit',
  994. 'cv_name' => 'featuremap_units',
  995. 'is_relationship' => 0,
  996. 'db_name' => 'local'
  997. ),
  998. array('update_existing' => TRUE)
  999. );
  1000. tripal_insert_cvterm(
  1001. array(
  1002. 'name' => 'marker_order',
  1003. 'definition' => 'Units simply to define marker order.',
  1004. 'cv_name' => 'featuremap_units',
  1005. 'is_relationship' => 0,
  1006. 'db_name' => 'local'
  1007. ),
  1008. array('update_existing' => TRUE)
  1009. );
  1010. tripal_insert_cvterm(
  1011. array(
  1012. 'name' => 'undefined',
  1013. 'definition' => 'A catch-all for an undefined unit type',
  1014. 'cv_name' => 'featuremap_units',
  1015. 'is_relationship' => 0,
  1016. 'db_name' => 'local'
  1017. ),
  1018. array('update_existing' => TRUE)
  1019. );
  1020. // featurepos properties
  1021. tripal_insert_cvterm(
  1022. array(
  1023. 'name' => 'start',
  1024. 'definition' => 'The start coordinate for a map feature.',
  1025. 'cv_name' => 'featurepos_property',
  1026. 'is_relationship' => 0,
  1027. 'db_name' => 'local'
  1028. ),
  1029. array('update_existing' => TRUE)
  1030. );
  1031. tripal_insert_cvterm(
  1032. array(
  1033. 'name' => 'stop',
  1034. 'definition' => 'The end coordinate for a map feature',
  1035. 'cv_name' => 'featurepos_property',
  1036. 'is_relationship' => 0,
  1037. 'db_name' => 'local'
  1038. ),
  1039. array('update_existing' => TRUE)
  1040. );
  1041. // add cvterms for map properties
  1042. tripal_insert_cvterm(
  1043. array(
  1044. 'name' => 'Map Dbxref',
  1045. 'definition' => 'A unique identifer for the map in a remote database. The '
  1046. . 'format is a database abbreviation and a unique accession separated '
  1047. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1048. 'cv_name' => 'featuremap_property',
  1049. 'is_relationship' => 0,
  1050. 'db_name' => 'local'
  1051. ),
  1052. array('update_existing' => TRUE)
  1053. );
  1054. tripal_insert_cvterm(
  1055. array(
  1056. 'name' => 'Map Type',
  1057. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1058. 'cv_name' => 'featuremap_property',
  1059. 'is_relationship' => 0,
  1060. 'db_name' => 'local'
  1061. ),
  1062. array('update_existing' => TRUE)
  1063. );
  1064. tripal_insert_cvterm(
  1065. array(
  1066. 'name' => 'Genome Group',
  1067. 'definition' => '',
  1068. 'cv_name' => 'featuremap_property',
  1069. 'is_relationship' => 0,
  1070. 'db_name' => 'local'
  1071. ),
  1072. array('update_existing' => TRUE)
  1073. );
  1074. tripal_insert_cvterm(
  1075. array(
  1076. 'name' => 'URL',
  1077. 'definition' => 'A univeral resource locator (URL) reference where the '
  1078. . 'publication can be found. For maps found online, this would be '
  1079. . 'the web address for the map.',
  1080. 'cv_name' => 'featuremap_property',
  1081. 'is_relationship' => 0,
  1082. 'db_name' => 'local'
  1083. ),
  1084. array('update_existing' => TRUE)
  1085. );
  1086. tripal_insert_cvterm(
  1087. array(
  1088. 'name' => 'Population Type',
  1089. 'definition' => 'A brief description of the population type used to generate '
  1090. . 'the map (e.g. RIL, F2, BC1, etc).',
  1091. 'cv_name' => 'featuremap_property',
  1092. 'is_relationship' => 0,
  1093. 'db_name' => 'local'
  1094. ),
  1095. array('update_existing' => TRUE)
  1096. );
  1097. tripal_insert_cvterm(
  1098. array(
  1099. 'name' => 'Population Size',
  1100. 'definition' => 'The size of the population used to construct the map.',
  1101. 'cv_name' => 'featuremap_property',
  1102. 'is_relationship' => 0,
  1103. 'db_name' => 'local'
  1104. ),
  1105. array('update_existing' => TRUE)
  1106. );
  1107. tripal_insert_cvterm(
  1108. array(
  1109. 'name' => 'Methods',
  1110. 'definition' => 'A brief description of the methods used to construct the map.',
  1111. 'cv_name' => 'featuremap_property',
  1112. 'is_relationship' => 0,
  1113. 'db_name' => 'local'
  1114. ),
  1115. array('update_existing' => TRUE)
  1116. );
  1117. tripal_insert_cvterm(
  1118. array(
  1119. 'name' => 'Software',
  1120. 'definition' => 'The software used to construct the map.',
  1121. 'cv_name' => 'featuremap_property',
  1122. 'is_relationship' => 0,
  1123. 'db_name' => 'local'
  1124. ),
  1125. array('update_existing' => TRUE)
  1126. );
  1127. }
  1128. /**
  1129. * Add cvs needed by the featuremap module
  1130. *
  1131. * @ingroup tripal_featuremap
  1132. */
  1133. function tripal_featuremap_add_cvs() {
  1134. tripal_insert_cv(
  1135. 'featuremap_units',
  1136. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1137. );
  1138. tripal_insert_cv(
  1139. 'featurepos_property',
  1140. 'Contains terms map properties.'
  1141. );
  1142. tripal_insert_cv(
  1143. 'featuremap_property',
  1144. 'Contains positional types for the feature positions'
  1145. );
  1146. }
  1147. /**
  1148. * Add cvs related to features
  1149. *
  1150. * @ingroup tripal_pub
  1151. */
  1152. function tripal_feature_add_cvs() {
  1153. // Add cv for relationship types
  1154. tripal_insert_cv(
  1155. 'feature_relationship',
  1156. 'Contains types of relationships between features.'
  1157. );
  1158. // The feature_property CV may already exists. It comes with Chado, but
  1159. // we need to add it just in case it doesn't get added before the feature
  1160. // module is installed. But as of Tripal v3.0 the Chado version of this
  1161. // vocabulary is no longer loaded by default.
  1162. tripal_insert_cv(
  1163. 'feature_property',
  1164. 'Stores properties about features'
  1165. );
  1166. // the feature type vocabulary should be the sequence ontology, and even though
  1167. // this ontology should get loaded we will create it here just so that we can
  1168. // set the default vocabulary for the feature.type_id field
  1169. tripal_insert_cv(
  1170. 'sequence',
  1171. 'The Sequence Ontology'
  1172. );
  1173. }
  1174. /**
  1175. *
  1176. */
  1177. function tripal_feature_add_tripal_gff_temp_table() {
  1178. $schema = array(
  1179. 'table' => 'tripal_gff_temp',
  1180. 'fields' => array(
  1181. 'feature_id' => array(
  1182. 'type' => 'int',
  1183. 'not null' => TRUE,
  1184. ),
  1185. 'organism_id' => array(
  1186. 'type' => 'int',
  1187. 'not null' => TRUE,
  1188. ),
  1189. 'uniquename' => array(
  1190. 'type' => 'text',
  1191. 'not null' => TRUE,
  1192. ),
  1193. 'type_name' => array(
  1194. 'type' => 'varchar',
  1195. 'length' => '1024',
  1196. 'not null' => TRUE,
  1197. ),
  1198. ),
  1199. 'indexes' => array(
  1200. 'tripal_gff_temp_idx0' => array('feature_id'),
  1201. 'tripal_gff_temp_idx0' => array('organism_id'),
  1202. 'tripal_gff_temp_idx1' => array('uniquename'),
  1203. ),
  1204. 'unique keys' => array(
  1205. 'tripal_gff_temp_uq0' => array('feature_id'),
  1206. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1207. ),
  1208. );
  1209. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1210. }
  1211. /**
  1212. *
  1213. */
  1214. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1215. $schema = array(
  1216. 'table' => 'tripal_gffcds_temp',
  1217. 'fields' => array(
  1218. 'feature_id' => array(
  1219. 'type' => 'int',
  1220. 'not null' => TRUE,
  1221. ),
  1222. 'parent_id' => array(
  1223. 'type' => 'int',
  1224. 'not null' => TRUE,
  1225. ),
  1226. 'phase' => array(
  1227. 'type' => 'int',
  1228. 'not null' => TRUE,
  1229. ),
  1230. 'strand' => array(
  1231. 'type' => 'int',
  1232. 'not null' => TRUE,
  1233. ),
  1234. 'fmin' => array(
  1235. 'type' => 'int',
  1236. 'not null' => TRUE,
  1237. ),
  1238. 'fmax' => array(
  1239. 'type' => 'int',
  1240. 'not null' => TRUE,
  1241. ),
  1242. ),
  1243. 'indexes' => array(
  1244. 'tripal_gff_temp_idx0' => array('feature_id'),
  1245. 'tripal_gff_temp_idx0' => array('parent_id'),
  1246. ),
  1247. );
  1248. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1249. }
  1250. /**
  1251. *
  1252. */
  1253. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1254. $schema = array(
  1255. 'table' => 'tripal_gffprotein_temp',
  1256. 'fields' => array(
  1257. 'feature_id' => array(
  1258. 'type' => 'int',
  1259. 'not null' => TRUE,
  1260. ),
  1261. 'parent_id' => array(
  1262. 'type' => 'int',
  1263. 'not null' => TRUE,
  1264. ),
  1265. 'fmin' => array(
  1266. 'type' => 'int',
  1267. 'not null' => TRUE,
  1268. ),
  1269. 'fmax' => array(
  1270. 'type' => 'int',
  1271. 'not null' => TRUE,
  1272. ),
  1273. ),
  1274. 'indexes' => array(
  1275. 'tripal_gff_temp_idx0' => array('feature_id'),
  1276. 'tripal_gff_temp_idx0' => array('parent_id'),
  1277. ),
  1278. 'unique keys' => array(
  1279. 'tripal_gff_temp_uq0' => array('feature_id'),
  1280. ),
  1281. );
  1282. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1283. }
  1284. /**
  1285. * Creates a materialized view that stores the type & number of features per organism
  1286. *
  1287. * @ingroup tripal_feature
  1288. */
  1289. function tripal_feature_add_organism_count_mview() {
  1290. $view_name = 'organism_feature_count';
  1291. $comment = 'Stores the type and number of features per organism';
  1292. $schema = array(
  1293. 'description' => $comment,
  1294. 'table' => $view_name,
  1295. 'fields' => array(
  1296. 'organism_id' => array(
  1297. 'size' => 'big',
  1298. 'type' => 'int',
  1299. 'not null' => TRUE,
  1300. ),
  1301. 'genus' => array(
  1302. 'type' => 'varchar',
  1303. 'length' => '255',
  1304. 'not null' => TRUE,
  1305. ),
  1306. 'species' => array(
  1307. 'type' => 'varchar',
  1308. 'length' => '255',
  1309. 'not null' => TRUE,
  1310. ),
  1311. 'common_name' => array(
  1312. 'type' => 'varchar',
  1313. 'length' => '255',
  1314. 'not null' => FALSE,
  1315. ),
  1316. 'num_features' => array(
  1317. 'type' => 'int',
  1318. 'not null' => TRUE,
  1319. ),
  1320. 'cvterm_id' => array(
  1321. 'size' => 'big',
  1322. 'type' => 'int',
  1323. 'not null' => TRUE,
  1324. ),
  1325. 'feature_type' => array(
  1326. 'type' => 'varchar',
  1327. 'length' => '255',
  1328. 'not null' => TRUE,
  1329. ),
  1330. ),
  1331. 'indexes' => array(
  1332. 'organism_feature_count_idx1' => array('organism_id'),
  1333. 'organism_feature_count_idx2' => array('cvterm_id'),
  1334. 'organism_feature_count_idx3' => array('feature_type'),
  1335. ),
  1336. );
  1337. $sql = "
  1338. SELECT
  1339. O.organism_id, O.genus, O.species, O.common_name,
  1340. count(F.feature_id) as num_features,
  1341. CVT.cvterm_id, CVT.name as feature_type
  1342. FROM organism O
  1343. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1344. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1345. GROUP BY
  1346. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1347. ";
  1348. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1349. }
  1350. /**
  1351. * Adds any cvs needed by this module.
  1352. *
  1353. * @ingroup tripal_contact
  1354. */
  1355. function tripal_contact_add_cvs() {
  1356. // Add the cv for contact properties. This is a default vocabulary in the event
  1357. // that a user does not want to use the tripal_contact vocabulary
  1358. tripal_insert_cv(
  1359. 'contact_property',
  1360. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1361. );
  1362. // add the cv for the contact type. This is a default vocabulary in the event
  1363. // that a user does not want to use the tripal_contact vocabulary
  1364. tripal_insert_cv(
  1365. 'contact_type',
  1366. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1367. );
  1368. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1369. tripal_insert_cv(
  1370. 'tripal_contact',
  1371. 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
  1372. );
  1373. // add the cv for contact relationships
  1374. tripal_insert_cv(
  1375. 'contact_relationship',
  1376. 'Contains types of relationships between contacts.'
  1377. );
  1378. }
  1379. /**
  1380. * Add any custom tables needed by this module.
  1381. * - Contactprop: keep track of properties of contact
  1382. *
  1383. * @ingroup tripal_contact
  1384. */
  1385. function tripal_contact_add_custom_tables(){
  1386. $schema = array (
  1387. 'table' => 'contactprop',
  1388. 'fields' => array (
  1389. 'contactprop_id' => array (
  1390. 'type' => 'serial',
  1391. 'not null' => true,
  1392. ),
  1393. 'contact_id' => array (
  1394. 'type' => 'int',
  1395. 'not null' => true,
  1396. ),
  1397. 'type_id' => array (
  1398. 'type' => 'int',
  1399. 'not null' => true,
  1400. ),
  1401. 'value' => array (
  1402. 'type' => 'text',
  1403. 'not null' => false,
  1404. ),
  1405. 'rank' => array (
  1406. 'type' => 'int',
  1407. 'not null' => true,
  1408. 'default' => 0,
  1409. ),
  1410. ),
  1411. 'primary key' => array (
  1412. 0 => 'contactprop_id',
  1413. ),
  1414. 'unique keys' => array (
  1415. 'contactprop_c1' => array (
  1416. 0 => 'contact_id',
  1417. 1 => 'type_id',
  1418. 2 => 'rank',
  1419. ),
  1420. ),
  1421. 'indexes' => array (
  1422. 'contactprop_idx1' => array (
  1423. 0 => 'contact_id',
  1424. ),
  1425. 'contactprop_idx2' => array (
  1426. 0 => 'type_id',
  1427. ),
  1428. ),
  1429. 'foreign keys' => array (
  1430. 'cvterm' => array (
  1431. 'table' => 'cvterm',
  1432. 'columns' => array (
  1433. 'type_id' => 'cvterm_id',
  1434. ),
  1435. ),
  1436. 'contact' => array (
  1437. 'table' => 'contact',
  1438. 'columns' => array (
  1439. 'contact_id' => 'contact_id',
  1440. ),
  1441. ),
  1442. ),
  1443. );
  1444. chado_create_custom_table('contactprop', $schema, TRUE);
  1445. }
  1446. /**
  1447. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1448. * analysisfeature links.
  1449. *
  1450. * @ingroup tripal_analysis
  1451. */
  1452. function tripal_analysis_create_analysisfeatureprop() {
  1453. // Create analysisfeatureprop table in chado. This is needed for Chado
  1454. // version 1.11, the table exists in Chado 1.2.
  1455. if (!db_table_exists('chado.analysisfeatureprop')) {
  1456. $sql = "
  1457. CREATE TABLE {analysisfeatureprop} (
  1458. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1459. analysisfeature_id INTEGER NOT NULL,
  1460. type_id INTEGER NOT NULL,
  1461. value TEXT,
  1462. rank INTEGER NOT NULL,
  1463. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1464. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1465. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1466. )
  1467. ";
  1468. chado_query($sql);
  1469. }
  1470. }
  1471. /**
  1472. * Creates a view showing the link between an organism & it's analysis through associated features.
  1473. *
  1474. * @ingroup tripal_analysis
  1475. */
  1476. function tripal_analysis_add_mview_analysis_organism() {
  1477. $view_name = 'analysis_organism';
  1478. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1479. // this is the SQL used to identify the organism to which an analsysis
  1480. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1481. // joins
  1482. $sql = "
  1483. SELECT DISTINCT A.analysis_id, O.organism_id
  1484. FROM analysis A
  1485. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1486. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1487. INNER JOIN organism O ON O.organism_id = F.organism_id
  1488. ";
  1489. // the schema array for describing this view
  1490. $schema = array(
  1491. 'table' => $view_name,
  1492. 'description' => $comment,
  1493. 'fields' => array(
  1494. 'analysis_id' => array(
  1495. 'size' => 'big',
  1496. 'type' => 'int',
  1497. 'not null' => TRUE,
  1498. ),
  1499. 'organism_id' => array(
  1500. 'size' => 'big',
  1501. 'type' => 'int',
  1502. 'not null' => TRUE,
  1503. ),
  1504. ),
  1505. 'indexes' => array(
  1506. 'networkmod_qtl_indx0' => array('analysis_id'),
  1507. 'networkmod_qtl_indx1' => array('organism_id'),
  1508. ),
  1509. 'foreign keys' => array(
  1510. 'analysis' => array(
  1511. 'table' => 'analysis',
  1512. 'columns' => array(
  1513. 'analysis_id' => 'analysis_id',
  1514. ),
  1515. ),
  1516. 'organism' => array(
  1517. 'table' => 'organism',
  1518. 'columns' => array(
  1519. 'organism_id' => 'organism_id',
  1520. ),
  1521. ),
  1522. ),
  1523. );
  1524. // add the view
  1525. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1526. }
  1527. /**
  1528. * Adds controlled vocabulary terms needed by this module.
  1529. *
  1530. * @ingroup tripal_analysis
  1531. */
  1532. function tripal_analysis_add_cvterms() {
  1533. tripal_insert_cv(
  1534. 'tripal_analysis',
  1535. 'Terms used for managing analyses in Tripal'
  1536. );
  1537. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1538. // get rid of it in case it is used, so just keep it in the Tripal CV
  1539. tripal_insert_cvterm(
  1540. array(
  1541. 'name' => 'analysis_date',
  1542. 'definition' => 'The date that an analysis was performed.',
  1543. 'cv_name' => 'tripal_analysis',
  1544. 'is_relationship' => 0,
  1545. 'db_name' => 'local'
  1546. ),
  1547. array('update_existing' => TRUE)
  1548. );
  1549. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1550. // get rid of it in case it is used, so just keep it in the Tripal CV
  1551. tripal_insert_cvterm(
  1552. array(
  1553. 'name' => 'analysis_short_name',
  1554. 'definition' => 'A computer legible (no spaces or special characters) '
  1555. . 'abbreviation for the analysis.',
  1556. 'cv_name' => 'tripal_analysis',
  1557. 'is_relationship' => 0,
  1558. 'db_name' => 'local'
  1559. ),
  1560. array('update_existing' => TRUE)
  1561. );
  1562. // the 'analysis_property' vocabulary is for user definable properties wo we
  1563. // will add an 'Analysis Type' to this vocubulary
  1564. tripal_insert_cvterm(
  1565. array(
  1566. 'name' => 'Analysis Type',
  1567. 'definition' => 'The type of analysis that was performed.',
  1568. 'cv_name' => 'analysis_property',
  1569. 'is_relationship' => 0,
  1570. 'db_name' => 'local'
  1571. ),
  1572. array('update_existing' => TRUE)
  1573. );
  1574. }
  1575. /**
  1576. * Add's defaults to the tripal_cv_obo table
  1577. *
  1578. * @ingroup tripal_cv
  1579. */
  1580. function tripal_cv_add_obo_defaults() {
  1581. // Insert commonly used ontologies into the tables.
  1582. $ontologies = array(
  1583. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  1584. array('Relationship Ontology (legacy)', '{tripal_chado}/files/legacy_ro.obo'),
  1585. array('Sequence Ontology', 'http://purl.obolibrary.org/obo/so.obo'),
  1586. array('Gene Ontology', 'http://purl.obolibrary.org/obo/go.obo'),
  1587. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  1588. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  1589. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  1590. );
  1591. foreach ($ontologies as $o) {
  1592. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  1593. }
  1594. }
  1595. /**
  1596. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1597. *
  1598. * @ingroup tripal_cv
  1599. */
  1600. function tripal_cv_add_cv_root_mview() {
  1601. $mv_name = 'cv_root_mview';
  1602. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1603. $schema = array(
  1604. 'table' => $mv_name,
  1605. 'description' => $comment,
  1606. 'fields' => array(
  1607. 'name' => array(
  1608. 'type' => 'varchar',
  1609. 'length' => 255,
  1610. 'not null' => TRUE,
  1611. ),
  1612. 'cvterm_id' => array(
  1613. 'size' => 'big',
  1614. 'type' => 'int',
  1615. 'not null' => TRUE,
  1616. ),
  1617. 'cv_id' => array(
  1618. 'size' => 'big',
  1619. 'type' => 'int',
  1620. 'not null' => TRUE,
  1621. ),
  1622. 'cv_name' => array(
  1623. 'type' => 'varchar',
  1624. 'length' => 255,
  1625. 'not null' => TRUE,
  1626. ),
  1627. ),
  1628. 'indexes' => array(
  1629. 'cv_root_mview_indx1' => array('cvterm_id'),
  1630. 'cv_root_mview_indx2' => array('cv_id'),
  1631. ),
  1632. );
  1633. $sql = "
  1634. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1635. FROM cvterm_relationship CVTR
  1636. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1637. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1638. WHERE CVTR.object_id not in
  1639. (SELECT subject_id FROM cvterm_relationship)
  1640. ";
  1641. // Create the MView
  1642. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1643. }
  1644. /**
  1645. * Creates a temporary table to store obo details while loading an obo file
  1646. *
  1647. * @ingroup tripal_cv
  1648. */
  1649. function tripal_cv_create_tripal_obo_temp() {
  1650. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1651. // we create it here using plain SQL because we want it to be in the chado schema but we
  1652. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1653. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1654. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1655. if (!db_table_exists('chado.tripal_obo_temp')) {
  1656. $sql = "
  1657. CREATE TABLE {tripal_obo_temp} (
  1658. id character varying(255) NOT NULL,
  1659. stanza text NOT NULL,
  1660. type character varying(50) NOT NULL,
  1661. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1662. );
  1663. ";
  1664. chado_query($sql);
  1665. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1666. chado_query($sql);
  1667. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1668. chado_query($sql);
  1669. }
  1670. }
  1671. /**
  1672. * Adds generic CVterms.
  1673. */
  1674. function tripal_insert_misc_cvterms() {
  1675. tripal_insert_cvterm(
  1676. array(
  1677. 'name' => 'exact',
  1678. 'id' => "local:exact",
  1679. 'definition' => 'An exact equivalent; interchangeable with the term name.',
  1680. 'is_obsolete' => 0,
  1681. 'cv_name' => 'synonym_type',
  1682. 'is_relationship' => FALSE
  1683. ),
  1684. array('update_existing' => TRUE)
  1685. );
  1686. tripal_insert_cvterm(
  1687. array(
  1688. 'name' => 'broad',
  1689. 'id' => "local:broad",
  1690. 'definition' => 'The synonym is broader than the name.',
  1691. 'is_obsolete' => 0,
  1692. 'cv_name' => 'synonym_type',
  1693. 'is_relationship' => FALSE
  1694. ),
  1695. array('update_existing' => TRUE)
  1696. );
  1697. tripal_insert_cvterm(
  1698. array(
  1699. 'name' => 'narrow',
  1700. 'id' => "local:narrow",
  1701. 'definition' => 'The synonym is narrower or more precise than the name.',
  1702. 'is_obsolete' => 0,
  1703. 'cv_name' => 'synonym_type',
  1704. 'is_relationship' => FALSE
  1705. ),
  1706. array('update_existing' => TRUE)
  1707. );
  1708. tripal_insert_cvterm(
  1709. array(
  1710. 'name' => 'related',
  1711. 'id' => "local:related",
  1712. 'definition' => 'The synonym is related in some way.',
  1713. 'is_obsolete' => 0,
  1714. 'cv_name' => 'synonym_type',
  1715. 'is_relationship' => FALSE
  1716. ),
  1717. array('update_existing' => TRUE)
  1718. );
  1719. }