tripal_chado.setup.inc 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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. $args = array(
  348. 'vocabulary' => 'OBI',
  349. 'accession' => '0100026',
  350. 'term_name' => 'organism',
  351. 'storage_args' => array(
  352. 'data_table' => 'organism',
  353. )
  354. );
  355. if (!tripal_create_bundle($args, $error)) {
  356. throw new Exception($error['!message']);
  357. }
  358. // Create the 'Analysis' entity type. This uses the local:analysis term.
  359. $error = '';
  360. $args = array(
  361. 'vocabulary' => 'local',
  362. 'accession' => 'analysis',
  363. 'term_name' => 'analysis',
  364. 'storage_args' => array(
  365. 'data_table' => 'analysis',
  366. )
  367. );
  368. if (!tripal_create_bundle($args, $error)) {
  369. throw new Exception($error['!message']);
  370. }
  371. // Create the 'Project' entity type. This uses the local:project term.
  372. $error = '';
  373. $args = array(
  374. 'vocabulary' => 'local',
  375. 'accession' => 'project',
  376. 'term_name' => 'project',
  377. 'storage_args' => array(
  378. 'data_table' => 'project',
  379. )
  380. );
  381. if (!tripal_create_bundle($args, $error)) {
  382. throw new Exception($error['!message']);
  383. }
  384. // Create the 'Genetic Map' entity type. This uses the local:project term.
  385. $error = '';
  386. $args = array(
  387. 'vocabulary' => 'data',
  388. 'accession' => '1274',
  389. 'term_name' => 'Map',
  390. 'storage_args' => array(
  391. 'data_table' => 'featuremap',
  392. )
  393. );
  394. if (!tripal_create_bundle($args, $error)) {
  395. throw new Exception($error['!message']);
  396. }
  397. // Create the 'Publication' entity type.
  398. $error = '';
  399. $args = array(
  400. 'vocabulary' => 'TPUB',
  401. 'accession' => '0000002',
  402. 'term_name' => 'Publication',
  403. 'storage_args' => array(
  404. 'data_table' => 'pub',
  405. )
  406. );
  407. if (!tripal_create_bundle($args, $error)) {
  408. throw new Exception($error['!message']);
  409. }
  410. // Initialize the population of the chado_cvterm_mapping table.
  411. tripal_chado_map_cvterms();
  412. // Set a variable to indicate the site is prepared.
  413. variable_set('tripal_chado_is_prepared', TRUE);
  414. }
  415. catch (Exception $e) {
  416. throw new Exception($e);
  417. }
  418. }
  419. /**
  420. * Creates a materialized view that stores the type & number of stocks per organism
  421. *
  422. * @ingroup tripal_stock
  423. */
  424. function tripal_stock_add_organism_count_mview() {
  425. $view_name = 'organism_stock_count';
  426. $comment = 'Stores the type and number of stocks per organism';
  427. $schema = array(
  428. 'description' => $comment,
  429. 'table' => $view_name,
  430. 'fields' => array(
  431. 'organism_id' => array(
  432. 'size' => 'big',
  433. 'type' => 'int',
  434. 'not null' => TRUE,
  435. ),
  436. 'genus' => array(
  437. 'type' => 'varchar',
  438. 'length' => '255',
  439. 'not null' => TRUE,
  440. ),
  441. 'species' => array(
  442. 'type' => 'varchar',
  443. 'length' => '255',
  444. 'not null' => TRUE,
  445. ),
  446. 'common_name' => array(
  447. 'type' => 'varchar',
  448. 'length' => '255',
  449. 'not null' => FALSE,
  450. ),
  451. 'num_stocks' => array(
  452. 'type' => 'int',
  453. 'not null' => TRUE,
  454. ),
  455. 'cvterm_id' => array(
  456. 'size' => 'big',
  457. 'type' => 'int',
  458. 'not null' => TRUE,
  459. ),
  460. 'stock_type' => array(
  461. 'type' => 'varchar',
  462. 'length' => '255',
  463. 'not null' => TRUE,
  464. ),
  465. ),
  466. 'indexes' => array(
  467. 'organism_stock_count_idx1' => array('organism_id'),
  468. 'organism_stock_count_idx2' => array('cvterm_id'),
  469. 'organism_stock_count_idx3' => array('stock_type'),
  470. ),
  471. );
  472. $sql = "
  473. SELECT
  474. O.organism_id, O.genus, O.species, O.common_name,
  475. count(S.stock_id) as num_stocks,
  476. CVT.cvterm_id, CVT.name as stock_type
  477. FROM organism O
  478. INNER JOIN stock S ON O.Organism_id = S.organism_id
  479. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  480. GROUP BY
  481. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  482. ";
  483. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  484. }
  485. /**
  486. * Add cvs related to publications
  487. *
  488. * @ingroup tripal_pub
  489. */
  490. function tripal_stock_add_cvs() {
  491. // Add cv for relationship types
  492. tripal_insert_cv(
  493. 'stock_relationship',
  494. 'Contains types of relationships between stocks.'
  495. );
  496. tripal_insert_cv(
  497. 'stock_property',
  498. 'Contains properties for stocks.'
  499. );
  500. tripal_insert_cv(
  501. 'stock_type',
  502. 'Contains a list of types for stocks.'
  503. );
  504. }
  505. /**
  506. * Add custom table related to publications
  507. * - pubauthor_contact
  508. *
  509. * @ingroup tripal_pub
  510. */
  511. function tripal_pub_add_custom_tables() {
  512. $schema = array (
  513. 'table' => 'pubauthor_contact',
  514. 'fields' => array (
  515. 'pubauthor_contact_id' => array (
  516. 'type' => 'serial',
  517. 'not null' => true,
  518. ),
  519. 'contact_id' => array (
  520. 'type' => 'int',
  521. 'not null' => true,
  522. ),
  523. 'pubauthor_id' => array (
  524. 'type' => 'int',
  525. 'not null' => true,
  526. ),
  527. ),
  528. 'primary key' => array (
  529. 0 => 'pubauthor_contact_id',
  530. ),
  531. 'unique keys' => array (
  532. 'pubauthor_contact_c1' => array (
  533. 0 => 'contact_id',
  534. 1 => 'pubauthor_id',
  535. ),
  536. ),
  537. 'foreign keys' => array (
  538. 'contact' => array (
  539. 'table' => 'contact',
  540. 'columns' => array (
  541. 'contact_id' => 'contact_id',
  542. ),
  543. ),
  544. 'pubauthor' => array (
  545. 'table' => 'pubauthor',
  546. 'columns' => array (
  547. 'pubauthor_id' => 'pubauthor_id',
  548. ),
  549. ),
  550. ),
  551. );
  552. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  553. }
  554. /**
  555. * Adds dbs related to publications
  556. *
  557. * @ingroup tripal_pub
  558. */
  559. function tripal_pub_add_dbs() {
  560. // make sure we have our supported databases
  561. tripal_insert_db(
  562. array(
  563. 'name' => 'PMID',
  564. 'description' => 'PubMed',
  565. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  566. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  567. ),
  568. array('update_existing' => TRUE)
  569. );
  570. tripal_insert_db(
  571. array(
  572. 'name' => 'AGL',
  573. 'description' => 'USDA National Agricultural Library',
  574. 'url' => 'http://agricola.nal.usda.gov/'
  575. ),
  576. array('update_existing' => TRUE)
  577. );
  578. }
  579. /**
  580. * Add cvs related to publications
  581. *
  582. * @ingroup tripal_pub
  583. */
  584. function tripal_pub_add_cvs() {
  585. // Add the cv for pub properties
  586. tripal_insert_cv(
  587. 'tripal_pub',
  588. '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.'
  589. );
  590. // Add the cv for pub types
  591. tripal_insert_cv(
  592. 'pub_type',
  593. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  594. );
  595. // Add the cv for pub properties
  596. tripal_insert_cv(
  597. 'pub_property',
  598. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  599. );
  600. // Add cv for relationship types
  601. tripal_insert_cv(
  602. 'pub_relationship',
  603. 'Contains types of relationships between publications.'
  604. );
  605. }
  606. /**
  607. * Add cvs pertaining to projects
  608. *
  609. * @ingroup tripal_project
  610. */
  611. function tripal_project_add_cvs() {
  612. // Add the cv for project properties
  613. tripal_insert_cv(
  614. 'project_property',
  615. 'Contains properties for projects'
  616. );
  617. // Add cv for relationship types
  618. tripal_insert_cv(
  619. 'project_relationship',
  620. 'Contains Types of relationships between projects.'
  621. );
  622. }
  623. /**
  624. * Add cvterms pertaining to projects
  625. *
  626. * @ingroup tripal_project
  627. */
  628. function tripal_project_add_cvterms() {
  629. // Insert cvterm 'Project Description' into cvterm table of chado
  630. // database. This CV term is used to keep track of the project
  631. // description in the projectprop table.
  632. tripal_insert_cvterm(
  633. array(
  634. 'name' => 'Project Description',
  635. 'definition' => 'Description of a project',
  636. 'cv_name' => 'project_property',
  637. 'is_relationship' => 0,
  638. 'db_name' => 'local'
  639. ),
  640. array('update_existing' => TRUE)
  641. );
  642. }
  643. /**
  644. * Add cvterms related to natural diversity
  645. *
  646. * @ingroup tripal_natural_diversity
  647. */
  648. function tripal_natural_diversity_add_cvterms(){
  649. // add cvterms for the nd_experiment_types
  650. tripal_insert_cvterm(
  651. array(
  652. 'name' => 'Genotyping',
  653. 'definition' => 'An experiment where genotypes of individuals are identified.',
  654. 'cv_name' => 'nd_experiment_types',
  655. 'is_relationship' => 0,
  656. 'db_name' => 'local'
  657. ),
  658. array('update_existing' => TRUE)
  659. );
  660. tripal_insert_cvterm(
  661. array(
  662. 'name' => 'Phenotyping',
  663. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  664. 'cv_name' => 'nd_experiment_types',
  665. 'is_relationship' => 0,
  666. 'db_name' => 'local'
  667. ),
  668. array('update_existing' => TRUE)
  669. );
  670. tripal_insert_cvterm(
  671. array(
  672. 'name' => 'Location',
  673. 'definition' => 'The name of the location.',
  674. 'cv_name' => 'nd_geolocation_property',
  675. 'is_relationship' => 0,
  676. 'db_name' => 'local'
  677. ),
  678. array('update_existing' => TRUE)
  679. );
  680. }
  681. /**
  682. * Adds a materialized view keeping track of the type of features associated with each library
  683. *
  684. * @ingroup tripal_library
  685. */
  686. function tripal_library_add_mview_library_feature_count(){
  687. $view_name = 'library_feature_count';
  688. $comment = 'Provides count of feature by type that are associated with all libraries';
  689. $schema = array(
  690. 'table' => $view_name,
  691. 'description' => $comment,
  692. 'fields' => array(
  693. 'library_id' => array(
  694. 'size' => 'big',
  695. 'type' => 'int',
  696. 'not null' => TRUE,
  697. ),
  698. 'name' => array(
  699. 'type' => 'varchar',
  700. 'length' => 255,
  701. 'not null' => TRUE,
  702. ),
  703. 'num_features' => array(
  704. 'type' => 'int',
  705. 'not null' => TRUE,
  706. ),
  707. 'feature_type' => array(
  708. 'type' => 'varchar',
  709. 'length' => 255,
  710. 'not null' => TRUE,
  711. ),
  712. ),
  713. 'indexes' => array(
  714. 'library_feature_count_idx1' => array('library_id'),
  715. ),
  716. );
  717. $sql = "
  718. SELECT
  719. L.library_id, L.name,
  720. count(F.feature_id) as num_features,
  721. CVT.name as feature_type
  722. FROM library L
  723. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  724. INNER JOIN feature F ON LF.feature_id = F.feature_id
  725. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  726. GROUP BY L.library_id, L.name, CVT.name
  727. ";
  728. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  729. }
  730. /**
  731. * Adds cvterms needed for the library module
  732. *
  733. * @ingroup tripal_library
  734. */
  735. function tripal_library_add_cvterms() {
  736. // Insert cvterm 'library_description' into cvterm table of chado
  737. // database. This CV term is used to keep track of the library
  738. // description in the libraryprop table.
  739. tripal_insert_cvterm(
  740. array(
  741. 'id' => 'local:library_description',
  742. 'name' => 'Library Description',
  743. 'definition' => 'Description of a library',
  744. 'cv_name' => 'library_property',
  745. 'is_relationship' => 0,
  746. 'db_name' => 'local'
  747. ),
  748. array('update_existing' => TRUE)
  749. );
  750. // add cvterms for the map unit types
  751. tripal_insert_cvterm(
  752. array(
  753. 'id' => 'local:cdna_library',
  754. 'name' => 'cdna_library',
  755. 'definition' => 'cDNA library',
  756. 'cv_name' => 'library_type',
  757. 'is_relationship' => 0,
  758. 'db_name' => 'local'
  759. ),
  760. array('update_existing' => TRUE)
  761. );
  762. tripal_insert_cvterm(
  763. array(
  764. 'id' => 'local:bac_library',
  765. 'name' => 'bac_library',
  766. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  767. 'cv_name' => 'library_type',
  768. 'is_relationship' => 0,
  769. 'db_name' => 'local'
  770. ),
  771. array('update_existing' => TRUE)
  772. );
  773. tripal_insert_cvterm(
  774. array(
  775. 'id' => 'local:fosmid_library',
  776. 'name' => 'fosmid_library',
  777. 'definition' => 'Fosmid library',
  778. 'cv_name' => 'library_type',
  779. 'is_relationship' => 0,
  780. 'db_name' => 'local'
  781. ),
  782. array('update_existing' => TRUE)
  783. );
  784. tripal_insert_cvterm(
  785. array(
  786. 'id' => 'local:cosmid_library',
  787. 'name' => 'cosmid_library',
  788. 'definition' => 'Cosmid library',
  789. 'cv_name' => 'library_type',
  790. 'is_relationship' => 0,
  791. 'db_name' => 'local'
  792. ),
  793. array('update_existing' => TRUE)
  794. );
  795. tripal_insert_cvterm(
  796. array(
  797. 'id' => 'local:yac_library',
  798. 'name' => 'yac_library',
  799. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  800. 'cv_name' => 'library_type',
  801. 'is_relationship' => 0,
  802. 'db_name' => 'local'
  803. ),
  804. array('update_existing' => TRUE)
  805. );
  806. tripal_insert_cvterm(
  807. array(
  808. 'id' => 'local:genomic_library',
  809. 'name' => 'genomic_library',
  810. 'definition' => 'Genomic Library',
  811. 'cv_name' => 'library_type',
  812. 'is_relationship' => 0,
  813. 'db_name' => 'local'
  814. ),
  815. array('update_existing' => TRUE)
  816. );
  817. }
  818. /**
  819. * Adds new CV's used by this module
  820. *
  821. * @ingroup tripal_library
  822. */
  823. function tripal_library_add_cvs(){
  824. tripal_insert_cv(
  825. 'library_property',
  826. 'Contains properties for libraries.'
  827. );
  828. tripal_insert_cv(
  829. 'library_type',
  830. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  831. );
  832. }
  833. /**
  834. * Add custom tables needed by the feature map module
  835. * - featuremapprop
  836. * - featuremap_dbxref
  837. * - featureposprop
  838. *
  839. * @ingroup tripal_featuremap
  840. */
  841. function tripal_featuremap_add_custom_tables(){
  842. // add the featuremaprop table to Chado
  843. $schema = array (
  844. 'table' => 'featuremapprop',
  845. 'fields' => array (
  846. 'featuremapprop_id' => array (
  847. 'type' => 'serial',
  848. 'not null' => true,
  849. ),
  850. 'featuremap_id' => array (
  851. 'type' => 'int',
  852. 'not null' => true,
  853. ),
  854. 'type_id' => array (
  855. 'type' => 'int',
  856. 'not null' => true,
  857. ),
  858. 'value' => array (
  859. 'type' => 'text',
  860. 'not null' => false,
  861. ),
  862. 'rank' => array (
  863. 'type' => 'int',
  864. 'not null' => true,
  865. 'default' => 0,
  866. ),
  867. ),
  868. 'primary key' => array (
  869. 0 => 'featuremapprop_id',
  870. ),
  871. 'unique keys' => array (
  872. 'featuremapprop_c1' => array (
  873. 0 => 'featuremap_id',
  874. 1 => 'type_id',
  875. 2 => 'rank',
  876. ),
  877. ),
  878. 'indexes' => array (
  879. 'featuremapprop_idx1' => array (
  880. 0 => 'featuremap_id',
  881. ),
  882. 'featuremapprop_idx2' => array (
  883. 0 => 'type_id',
  884. ),
  885. ),
  886. 'foreign keys' => array (
  887. 'cvterm' => array (
  888. 'table' => 'cvterm',
  889. 'columns' => array (
  890. 'type_id' => 'cvterm_id',
  891. ),
  892. ),
  893. 'featuremap' => array (
  894. 'table' => 'featuremap',
  895. 'columns' => array (
  896. 'featuremap_id' => 'featuremap_id',
  897. ),
  898. ),
  899. ),
  900. );
  901. chado_create_custom_table('featuremapprop', $schema, TRUE);
  902. // add the featuremap_dbxref table to Chado
  903. $schema = array (
  904. 'table' => 'featuremap_dbxref',
  905. 'fields' => array (
  906. 'featuremap_dbxref_id' => array (
  907. 'type' => 'serial',
  908. 'not null' => true,
  909. ),
  910. 'featuremap_id' => array (
  911. 'type' => 'int',
  912. 'not null' => true,
  913. ),
  914. 'dbxref_id' => array (
  915. 'type' => 'int',
  916. 'not null' => true,
  917. ),
  918. ),
  919. 'primary key' => array (
  920. 0 => 'featuremap_dbxref_id',
  921. ),
  922. 'unique keys' => array (
  923. 'featuremap_dbxref_c1' => array (
  924. 0 => 'featuremap_id',
  925. 1 => 'dbxref_id',
  926. ),
  927. ),
  928. 'indexes' => array (
  929. 'featuremap_dbxref_idx1' => array (
  930. 0 => 'featuremap_dbxref_id',
  931. ),
  932. 'featuremap_dbxref_idx2' => array (
  933. 0 => 'dbxref_id',
  934. ),
  935. ),
  936. 'foreign keys' => array (
  937. 'dbxref' => array (
  938. 'table' => 'dbxref',
  939. 'columns' => array (
  940. 'dbxref_id' => 'dbxref_id',
  941. ),
  942. ),
  943. 'featuremap' => array (
  944. 'table' => 'featuremap',
  945. 'columns' => array (
  946. 'featuremap_id' => 'featuremap_id',
  947. ),
  948. ),
  949. ),
  950. 'referring_tables' => NULL,
  951. );
  952. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  953. $schema = array (
  954. 'table' => 'featureposprop',
  955. 'fields' => array (
  956. 'featureposprop_id' => array (
  957. 'type' => 'serial',
  958. 'not null' => true,
  959. ),
  960. 'featurepos_id' => array (
  961. 'type' => 'int',
  962. 'not null' => true,
  963. ),
  964. 'type_id' => array (
  965. 'type' => 'int',
  966. 'not null' => true,
  967. ),
  968. 'value' => array (
  969. 'type' => 'text',
  970. 'not null' => false,
  971. ),
  972. 'rank' => array (
  973. 'type' => 'int',
  974. 'not null' => true,
  975. 'default' => 0,
  976. ),
  977. ),
  978. 'primary key' => array (
  979. 0 => 'featureposprop_id',
  980. ),
  981. 'unique keys' => array (
  982. 'featureposprop_id' => array (
  983. 0 => 'featurepos_id',
  984. 1 => 'type_id',
  985. 2 => 'rank',
  986. ),
  987. ),
  988. 'indexes' => array (
  989. 'featureposprop_c1' => array (
  990. 0 => 'featurepos_id',
  991. ),
  992. 'featureposprop_idx2' => array (
  993. 0 => 'type_id',
  994. ),
  995. ),
  996. 'foreign keys' => array (
  997. 'cvterm' => array (
  998. 'table' => 'cvterm',
  999. 'columns' => array (
  1000. 'type_id' => 'cvterm_id',
  1001. ),
  1002. ),
  1003. 'featurepos' => array (
  1004. 'table' => 'featurepos',
  1005. 'columns' => array (
  1006. 'featurepos_id' => 'featurepos_id',
  1007. ),
  1008. ),
  1009. ),
  1010. );
  1011. chado_create_custom_table('featureposprop', $schema, TRUE);
  1012. }
  1013. /**
  1014. * Add cv terms needed by the featuremap module
  1015. *
  1016. * @ingroup tripal_featuremap
  1017. */
  1018. function tripal_featuremap_add_cvterms() {
  1019. // add cvterms for the map unit types
  1020. tripal_insert_cvterm(
  1021. array(
  1022. 'name' => 'cM',
  1023. 'definition' => 'Centimorgan units',
  1024. 'cv_name' => 'featuremap_units',
  1025. 'is_relationship' => 0,
  1026. 'db_name' => 'local'
  1027. ),
  1028. array('update_existing' => TRUE)
  1029. );
  1030. tripal_insert_cvterm(
  1031. array(
  1032. 'name' => 'bp',
  1033. 'definition' => 'Base pairs units',
  1034. 'cv_name' => 'featuremap_units',
  1035. 'is_relationship' => 0,
  1036. 'db_name' => 'local'
  1037. ),
  1038. array('update_existing' => TRUE)
  1039. );
  1040. tripal_insert_cvterm(
  1041. array(
  1042. 'name' => 'bin_unit',
  1043. 'definition' => 'The bin unit',
  1044. 'cv_name' => 'featuremap_units',
  1045. 'is_relationship' => 0,
  1046. 'db_name' => 'local'
  1047. ),
  1048. array('update_existing' => TRUE)
  1049. );
  1050. tripal_insert_cvterm(
  1051. array(
  1052. 'name' => 'marker_order',
  1053. 'definition' => 'Units simply to define marker order.',
  1054. 'cv_name' => 'featuremap_units',
  1055. 'is_relationship' => 0,
  1056. 'db_name' => 'local'
  1057. ),
  1058. array('update_existing' => TRUE)
  1059. );
  1060. tripal_insert_cvterm(
  1061. array(
  1062. 'name' => 'undefined',
  1063. 'definition' => 'A catch-all for an undefined unit type',
  1064. 'cv_name' => 'featuremap_units',
  1065. 'is_relationship' => 0,
  1066. 'db_name' => 'local'
  1067. ),
  1068. array('update_existing' => TRUE)
  1069. );
  1070. // featurepos properties
  1071. tripal_insert_cvterm(
  1072. array(
  1073. 'name' => 'start',
  1074. 'definition' => 'The start coordinate for a map feature.',
  1075. 'cv_name' => 'featurepos_property',
  1076. 'is_relationship' => 0,
  1077. 'db_name' => 'local'
  1078. ),
  1079. array('update_existing' => TRUE)
  1080. );
  1081. tripal_insert_cvterm(
  1082. array(
  1083. 'name' => 'stop',
  1084. 'definition' => 'The end coordinate for a map feature',
  1085. 'cv_name' => 'featurepos_property',
  1086. 'is_relationship' => 0,
  1087. 'db_name' => 'local'
  1088. ),
  1089. array('update_existing' => TRUE)
  1090. );
  1091. // add cvterms for map properties
  1092. tripal_insert_cvterm(
  1093. array(
  1094. 'name' => 'Map Dbxref',
  1095. 'definition' => 'A unique identifer for the map in a remote database. The '
  1096. . 'format is a database abbreviation and a unique accession separated '
  1097. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1098. 'cv_name' => 'featuremap_property',
  1099. 'is_relationship' => 0,
  1100. 'db_name' => 'local'
  1101. ),
  1102. array('update_existing' => TRUE)
  1103. );
  1104. tripal_insert_cvterm(
  1105. array(
  1106. 'name' => 'Map Type',
  1107. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1108. 'cv_name' => 'featuremap_property',
  1109. 'is_relationship' => 0,
  1110. 'db_name' => 'local'
  1111. ),
  1112. array('update_existing' => TRUE)
  1113. );
  1114. tripal_insert_cvterm(
  1115. array(
  1116. 'name' => 'Genome Group',
  1117. 'definition' => '',
  1118. 'cv_name' => 'featuremap_property',
  1119. 'is_relationship' => 0,
  1120. 'db_name' => 'local'
  1121. ),
  1122. array('update_existing' => TRUE)
  1123. );
  1124. tripal_insert_cvterm(
  1125. array(
  1126. 'name' => 'URL',
  1127. 'definition' => 'A univeral resource locator (URL) reference where the '
  1128. . 'publication can be found. For maps found online, this would be '
  1129. . 'the web address for the map.',
  1130. 'cv_name' => 'featuremap_property',
  1131. 'is_relationship' => 0,
  1132. 'db_name' => 'local'
  1133. ),
  1134. array('update_existing' => TRUE)
  1135. );
  1136. tripal_insert_cvterm(
  1137. array(
  1138. 'name' => 'Population Type',
  1139. 'definition' => 'A brief description of the population type used to generate '
  1140. . 'the map (e.g. RIL, F2, BC1, etc).',
  1141. 'cv_name' => 'featuremap_property',
  1142. 'is_relationship' => 0,
  1143. 'db_name' => 'local'
  1144. ),
  1145. array('update_existing' => TRUE)
  1146. );
  1147. tripal_insert_cvterm(
  1148. array(
  1149. 'name' => 'Population Size',
  1150. 'definition' => 'The size of the population used to construct the map.',
  1151. 'cv_name' => 'featuremap_property',
  1152. 'is_relationship' => 0,
  1153. 'db_name' => 'local'
  1154. ),
  1155. array('update_existing' => TRUE)
  1156. );
  1157. tripal_insert_cvterm(
  1158. array(
  1159. 'name' => 'Methods',
  1160. 'definition' => 'A brief description of the methods used to construct the map.',
  1161. 'cv_name' => 'featuremap_property',
  1162. 'is_relationship' => 0,
  1163. 'db_name' => 'local'
  1164. ),
  1165. array('update_existing' => TRUE)
  1166. );
  1167. tripal_insert_cvterm(
  1168. array(
  1169. 'name' => 'Software',
  1170. 'definition' => 'The software used to construct the map.',
  1171. 'cv_name' => 'featuremap_property',
  1172. 'is_relationship' => 0,
  1173. 'db_name' => 'local'
  1174. ),
  1175. array('update_existing' => TRUE)
  1176. );
  1177. }
  1178. /**
  1179. * Add cvs needed by the featuremap module
  1180. *
  1181. * @ingroup tripal_featuremap
  1182. */
  1183. function tripal_featuremap_add_cvs() {
  1184. tripal_insert_cv(
  1185. 'featuremap_units',
  1186. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1187. );
  1188. tripal_insert_cv(
  1189. 'featurepos_property',
  1190. 'Contains terms map properties.'
  1191. );
  1192. tripal_insert_cv(
  1193. 'featuremap_property',
  1194. 'Contains positional types for the feature positions'
  1195. );
  1196. }
  1197. /**
  1198. * Add cvs related to features
  1199. *
  1200. * @ingroup tripal_pub
  1201. */
  1202. function tripal_feature_add_cvs() {
  1203. // Add cv for relationship types
  1204. tripal_insert_cv(
  1205. 'feature_relationship',
  1206. 'Contains types of relationships between features.'
  1207. );
  1208. // The feature_property CV may already exists. It comes with Chado, but
  1209. // we need to add it just in case it doesn't get added before the feature
  1210. // module is installed. But as of Tripal v3.0 the Chado version of this
  1211. // vocabulary is no longer loaded by default.
  1212. tripal_insert_cv(
  1213. 'feature_property',
  1214. 'Stores properties about features'
  1215. );
  1216. // the feature type vocabulary should be the sequence ontology, and even though
  1217. // this ontology should get loaded we will create it here just so that we can
  1218. // set the default vocabulary for the feature.type_id field
  1219. tripal_insert_cv(
  1220. 'sequence',
  1221. 'The Sequence Ontology'
  1222. );
  1223. }
  1224. /**
  1225. *
  1226. */
  1227. function tripal_feature_add_tripal_gff_temp_table() {
  1228. $schema = array(
  1229. 'table' => 'tripal_gff_temp',
  1230. 'fields' => array(
  1231. 'feature_id' => array(
  1232. 'type' => 'int',
  1233. 'not null' => TRUE,
  1234. ),
  1235. 'organism_id' => array(
  1236. 'type' => 'int',
  1237. 'not null' => TRUE,
  1238. ),
  1239. 'uniquename' => array(
  1240. 'type' => 'text',
  1241. 'not null' => TRUE,
  1242. ),
  1243. 'type_name' => array(
  1244. 'type' => 'varchar',
  1245. 'length' => '1024',
  1246. 'not null' => TRUE,
  1247. ),
  1248. ),
  1249. 'indexes' => array(
  1250. 'tripal_gff_temp_idx0' => array('feature_id'),
  1251. 'tripal_gff_temp_idx0' => array('organism_id'),
  1252. 'tripal_gff_temp_idx1' => array('uniquename'),
  1253. ),
  1254. 'unique keys' => array(
  1255. 'tripal_gff_temp_uq0' => array('feature_id'),
  1256. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1257. ),
  1258. );
  1259. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1260. }
  1261. /**
  1262. *
  1263. */
  1264. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1265. $schema = array(
  1266. 'table' => 'tripal_gffcds_temp',
  1267. 'fields' => array(
  1268. 'feature_id' => array(
  1269. 'type' => 'int',
  1270. 'not null' => TRUE,
  1271. ),
  1272. 'parent_id' => array(
  1273. 'type' => 'int',
  1274. 'not null' => TRUE,
  1275. ),
  1276. 'phase' => array(
  1277. 'type' => 'int',
  1278. 'not null' => TRUE,
  1279. ),
  1280. 'strand' => array(
  1281. 'type' => 'int',
  1282. 'not null' => TRUE,
  1283. ),
  1284. 'fmin' => array(
  1285. 'type' => 'int',
  1286. 'not null' => TRUE,
  1287. ),
  1288. 'fmax' => array(
  1289. 'type' => 'int',
  1290. 'not null' => TRUE,
  1291. ),
  1292. ),
  1293. 'indexes' => array(
  1294. 'tripal_gff_temp_idx0' => array('feature_id'),
  1295. 'tripal_gff_temp_idx0' => array('parent_id'),
  1296. ),
  1297. );
  1298. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1299. }
  1300. /**
  1301. *
  1302. */
  1303. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1304. $schema = array(
  1305. 'table' => 'tripal_gffprotein_temp',
  1306. 'fields' => array(
  1307. 'feature_id' => array(
  1308. 'type' => 'int',
  1309. 'not null' => TRUE,
  1310. ),
  1311. 'parent_id' => array(
  1312. 'type' => 'int',
  1313. 'not null' => TRUE,
  1314. ),
  1315. 'fmin' => array(
  1316. 'type' => 'int',
  1317. 'not null' => TRUE,
  1318. ),
  1319. 'fmax' => array(
  1320. 'type' => 'int',
  1321. 'not null' => TRUE,
  1322. ),
  1323. ),
  1324. 'indexes' => array(
  1325. 'tripal_gff_temp_idx0' => array('feature_id'),
  1326. 'tripal_gff_temp_idx0' => array('parent_id'),
  1327. ),
  1328. 'unique keys' => array(
  1329. 'tripal_gff_temp_uq0' => array('feature_id'),
  1330. ),
  1331. );
  1332. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1333. }
  1334. /**
  1335. * Creates a materialized view that stores the type & number of features per organism
  1336. *
  1337. * @ingroup tripal_feature
  1338. */
  1339. function tripal_feature_add_organism_count_mview() {
  1340. $view_name = 'organism_feature_count';
  1341. $comment = 'Stores the type and number of features per organism';
  1342. $schema = array(
  1343. 'description' => $comment,
  1344. 'table' => $view_name,
  1345. 'fields' => array(
  1346. 'organism_id' => array(
  1347. 'size' => 'big',
  1348. 'type' => 'int',
  1349. 'not null' => TRUE,
  1350. ),
  1351. 'genus' => array(
  1352. 'type' => 'varchar',
  1353. 'length' => '255',
  1354. 'not null' => TRUE,
  1355. ),
  1356. 'species' => array(
  1357. 'type' => 'varchar',
  1358. 'length' => '255',
  1359. 'not null' => TRUE,
  1360. ),
  1361. 'common_name' => array(
  1362. 'type' => 'varchar',
  1363. 'length' => '255',
  1364. 'not null' => FALSE,
  1365. ),
  1366. 'num_features' => array(
  1367. 'type' => 'int',
  1368. 'not null' => TRUE,
  1369. ),
  1370. 'cvterm_id' => array(
  1371. 'size' => 'big',
  1372. 'type' => 'int',
  1373. 'not null' => TRUE,
  1374. ),
  1375. 'feature_type' => array(
  1376. 'type' => 'varchar',
  1377. 'length' => '255',
  1378. 'not null' => TRUE,
  1379. ),
  1380. ),
  1381. 'indexes' => array(
  1382. 'organism_feature_count_idx1' => array('organism_id'),
  1383. 'organism_feature_count_idx2' => array('cvterm_id'),
  1384. 'organism_feature_count_idx3' => array('feature_type'),
  1385. ),
  1386. );
  1387. $sql = "
  1388. SELECT
  1389. O.organism_id, O.genus, O.species, O.common_name,
  1390. count(F.feature_id) as num_features,
  1391. CVT.cvterm_id, CVT.name as feature_type
  1392. FROM organism O
  1393. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1394. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1395. GROUP BY
  1396. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1397. ";
  1398. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1399. }
  1400. /**
  1401. * Adds any cvs needed by this module.
  1402. *
  1403. * @ingroup tripal_contact
  1404. */
  1405. function tripal_contact_add_cvs() {
  1406. // Add the cv for contact properties. This is a default vocabulary in the event
  1407. // that a user does not want to use the tripal_contact vocabulary
  1408. tripal_insert_cv(
  1409. 'contact_property',
  1410. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1411. );
  1412. // add the cv for the contact type. This is a default vocabulary in the event
  1413. // that a user does not want to use the tripal_contact vocabulary
  1414. tripal_insert_cv(
  1415. 'contact_type',
  1416. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1417. );
  1418. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1419. tripal_insert_cv(
  1420. 'tripal_contact',
  1421. '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.'
  1422. );
  1423. // add the cv for contact relationships
  1424. tripal_insert_cv(
  1425. 'contact_relationship',
  1426. 'Contains types of relationships between contacts.'
  1427. );
  1428. }
  1429. /**
  1430. * Add any custom tables needed by this module.
  1431. * - Contactprop: keep track of properties of contact
  1432. *
  1433. * @ingroup tripal_contact
  1434. */
  1435. function tripal_contact_add_custom_tables(){
  1436. $schema = array (
  1437. 'table' => 'contactprop',
  1438. 'fields' => array (
  1439. 'contactprop_id' => array (
  1440. 'type' => 'serial',
  1441. 'not null' => true,
  1442. ),
  1443. 'contact_id' => array (
  1444. 'type' => 'int',
  1445. 'not null' => true,
  1446. ),
  1447. 'type_id' => array (
  1448. 'type' => 'int',
  1449. 'not null' => true,
  1450. ),
  1451. 'value' => array (
  1452. 'type' => 'text',
  1453. 'not null' => false,
  1454. ),
  1455. 'rank' => array (
  1456. 'type' => 'int',
  1457. 'not null' => true,
  1458. 'default' => 0,
  1459. ),
  1460. ),
  1461. 'primary key' => array (
  1462. 0 => 'contactprop_id',
  1463. ),
  1464. 'unique keys' => array (
  1465. 'contactprop_c1' => array (
  1466. 0 => 'contact_id',
  1467. 1 => 'type_id',
  1468. 2 => 'rank',
  1469. ),
  1470. ),
  1471. 'indexes' => array (
  1472. 'contactprop_idx1' => array (
  1473. 0 => 'contact_id',
  1474. ),
  1475. 'contactprop_idx2' => array (
  1476. 0 => 'type_id',
  1477. ),
  1478. ),
  1479. 'foreign keys' => array (
  1480. 'cvterm' => array (
  1481. 'table' => 'cvterm',
  1482. 'columns' => array (
  1483. 'type_id' => 'cvterm_id',
  1484. ),
  1485. ),
  1486. 'contact' => array (
  1487. 'table' => 'contact',
  1488. 'columns' => array (
  1489. 'contact_id' => 'contact_id',
  1490. ),
  1491. ),
  1492. ),
  1493. );
  1494. chado_create_custom_table('contactprop', $schema, TRUE);
  1495. }
  1496. /**
  1497. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1498. * analysisfeature links.
  1499. *
  1500. * @ingroup tripal_analysis
  1501. */
  1502. function tripal_analysis_create_analysisfeatureprop() {
  1503. // Create analysisfeatureprop table in chado. This is needed for Chado
  1504. // version 1.11, the table exists in Chado 1.2.
  1505. if (!db_table_exists('chado.analysisfeatureprop')) {
  1506. $sql = "
  1507. CREATE TABLE {analysisfeatureprop} (
  1508. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1509. analysisfeature_id INTEGER NOT NULL,
  1510. type_id INTEGER NOT NULL,
  1511. value TEXT,
  1512. rank INTEGER NOT NULL,
  1513. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1514. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1515. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1516. )
  1517. ";
  1518. chado_query($sql);
  1519. }
  1520. }
  1521. /**
  1522. * Creates a view showing the link between an organism & it's analysis through associated features.
  1523. *
  1524. * @ingroup tripal_analysis
  1525. */
  1526. function tripal_analysis_add_mview_analysis_organism() {
  1527. $view_name = 'analysis_organism';
  1528. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1529. // this is the SQL used to identify the organism to which an analsysis
  1530. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1531. // joins
  1532. $sql = "
  1533. SELECT DISTINCT A.analysis_id, O.organism_id
  1534. FROM analysis A
  1535. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1536. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1537. INNER JOIN organism O ON O.organism_id = F.organism_id
  1538. ";
  1539. // the schema array for describing this view
  1540. $schema = array(
  1541. 'table' => $view_name,
  1542. 'description' => $comment,
  1543. 'fields' => array(
  1544. 'analysis_id' => array(
  1545. 'size' => 'big',
  1546. 'type' => 'int',
  1547. 'not null' => TRUE,
  1548. ),
  1549. 'organism_id' => array(
  1550. 'size' => 'big',
  1551. 'type' => 'int',
  1552. 'not null' => TRUE,
  1553. ),
  1554. ),
  1555. 'indexes' => array(
  1556. 'networkmod_qtl_indx0' => array('analysis_id'),
  1557. 'networkmod_qtl_indx1' => array('organism_id'),
  1558. ),
  1559. 'foreign keys' => array(
  1560. 'analysis' => array(
  1561. 'table' => 'analysis',
  1562. 'columns' => array(
  1563. 'analysis_id' => 'analysis_id',
  1564. ),
  1565. ),
  1566. 'organism' => array(
  1567. 'table' => 'organism',
  1568. 'columns' => array(
  1569. 'organism_id' => 'organism_id',
  1570. ),
  1571. ),
  1572. ),
  1573. );
  1574. // add the view
  1575. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1576. }
  1577. /**
  1578. * Adds controlled vocabulary terms needed by this module.
  1579. *
  1580. * @ingroup tripal_analysis
  1581. */
  1582. function tripal_analysis_add_cvterms() {
  1583. tripal_insert_cv(
  1584. 'tripal_analysis',
  1585. 'Terms used for managing analyses in Tripal'
  1586. );
  1587. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1588. // get rid of it in case it is used, so just keep it in the Tripal CV
  1589. tripal_insert_cvterm(
  1590. array(
  1591. 'name' => 'analysis_date',
  1592. 'definition' => 'The date that an analysis was performed.',
  1593. 'cv_name' => 'tripal_analysis',
  1594. 'is_relationship' => 0,
  1595. 'db_name' => 'local'
  1596. ),
  1597. array('update_existing' => TRUE)
  1598. );
  1599. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1600. // get rid of it in case it is used, so just keep it in the Tripal CV
  1601. tripal_insert_cvterm(
  1602. array(
  1603. 'name' => 'analysis_short_name',
  1604. 'definition' => 'A computer legible (no spaces or special characters) '
  1605. . 'abbreviation for the analysis.',
  1606. 'cv_name' => 'tripal_analysis',
  1607. 'is_relationship' => 0,
  1608. 'db_name' => 'local'
  1609. ),
  1610. array('update_existing' => TRUE)
  1611. );
  1612. // the 'analysis_property' vocabulary is for user definable properties wo we
  1613. // will add an 'Analysis Type' to this vocubulary
  1614. tripal_insert_cvterm(
  1615. array(
  1616. 'name' => 'Analysis Type',
  1617. 'definition' => 'The type of analysis that was performed.',
  1618. 'cv_name' => 'analysis_property',
  1619. 'is_relationship' => 0,
  1620. 'db_name' => 'local'
  1621. ),
  1622. array('update_existing' => TRUE)
  1623. );
  1624. }
  1625. /**
  1626. * Add's defaults to the tripal_cv_obo table
  1627. *
  1628. * @ingroup tripal_cv
  1629. */
  1630. function tripal_cv_add_obo_defaults() {
  1631. // Insert commonly used ontologies into the tables.
  1632. $ontologies = array(
  1633. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  1634. array('Relationship Ontology (legacy)', '{tripal_chado}/files/legacy_ro.obo'),
  1635. array('Sequence Ontology', 'http://purl.obolibrary.org/obo/so.obo'),
  1636. array('Gene Ontology', 'http://purl.obolibrary.org/obo/go.obo'),
  1637. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  1638. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  1639. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  1640. );
  1641. foreach ($ontologies as $o) {
  1642. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  1643. }
  1644. }
  1645. /**
  1646. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1647. *
  1648. * @ingroup tripal_cv
  1649. */
  1650. function tripal_cv_add_cv_root_mview() {
  1651. $mv_name = 'cv_root_mview';
  1652. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1653. $schema = array(
  1654. 'table' => $mv_name,
  1655. 'description' => $comment,
  1656. 'fields' => array(
  1657. 'name' => array(
  1658. 'type' => 'varchar',
  1659. 'length' => 255,
  1660. 'not null' => TRUE,
  1661. ),
  1662. 'cvterm_id' => array(
  1663. 'size' => 'big',
  1664. 'type' => 'int',
  1665. 'not null' => TRUE,
  1666. ),
  1667. 'cv_id' => array(
  1668. 'size' => 'big',
  1669. 'type' => 'int',
  1670. 'not null' => TRUE,
  1671. ),
  1672. 'cv_name' => array(
  1673. 'type' => 'varchar',
  1674. 'length' => 255,
  1675. 'not null' => TRUE,
  1676. ),
  1677. ),
  1678. 'indexes' => array(
  1679. 'cv_root_mview_indx1' => array('cvterm_id'),
  1680. 'cv_root_mview_indx2' => array('cv_id'),
  1681. ),
  1682. );
  1683. $sql = "
  1684. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1685. FROM cvterm_relationship CVTR
  1686. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1687. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1688. WHERE CVTR.object_id not in
  1689. (SELECT subject_id FROM cvterm_relationship)
  1690. ";
  1691. // Create the MView
  1692. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1693. }
  1694. /**
  1695. * Creates a temporary table to store obo details while loading an obo file
  1696. *
  1697. * @ingroup tripal_cv
  1698. */
  1699. function tripal_cv_create_tripal_obo_temp() {
  1700. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1701. // we create it here using plain SQL because we want it to be in the chado schema but we
  1702. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1703. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1704. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1705. if (!db_table_exists('chado.tripal_obo_temp')) {
  1706. $sql = "
  1707. CREATE TABLE {tripal_obo_temp} (
  1708. id character varying(255) NOT NULL,
  1709. stanza text NOT NULL,
  1710. type character varying(50) NOT NULL,
  1711. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1712. );
  1713. ";
  1714. chado_query($sql);
  1715. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1716. chado_query($sql);
  1717. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1718. chado_query($sql);
  1719. }
  1720. }
  1721. /**
  1722. * Adds generic CVterms.
  1723. */
  1724. function tripal_insert_misc_cvterms() {
  1725. tripal_insert_cvterm(
  1726. array(
  1727. 'name' => 'exact',
  1728. 'id' => "local:exact",
  1729. 'definition' => 'An exact equivalent; interchangeable with the term name.',
  1730. 'is_obsolete' => 0,
  1731. 'cv_name' => 'synonym_type',
  1732. 'is_relationship' => FALSE
  1733. ),
  1734. array('update_existing' => TRUE)
  1735. );
  1736. tripal_insert_cvterm(
  1737. array(
  1738. 'name' => 'broad',
  1739. 'id' => "local:broad",
  1740. 'definition' => 'The synonym is broader than the name.',
  1741. 'is_obsolete' => 0,
  1742. 'cv_name' => 'synonym_type',
  1743. 'is_relationship' => FALSE
  1744. ),
  1745. array('update_existing' => TRUE)
  1746. );
  1747. tripal_insert_cvterm(
  1748. array(
  1749. 'name' => 'narrow',
  1750. 'id' => "local:narrow",
  1751. 'definition' => 'The synonym is narrower or more precise than the name.',
  1752. 'is_obsolete' => 0,
  1753. 'cv_name' => 'synonym_type',
  1754. 'is_relationship' => FALSE
  1755. ),
  1756. array('update_existing' => TRUE)
  1757. );
  1758. tripal_insert_cvterm(
  1759. array(
  1760. 'name' => 'related',
  1761. 'id' => "local:related",
  1762. 'definition' => 'The synonym is related in some way.',
  1763. 'is_obsolete' => 0,
  1764. 'cv_name' => 'synonym_type',
  1765. 'is_relationship' => FALSE
  1766. ),
  1767. array('update_existing' => TRUE)
  1768. );
  1769. }