gff_loader.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. <?php
  2. // TODO: The rank column on the feature_relationship table needs to be used to
  3. // make sure the ordering of CDS (exons) is correct.
  4. // The entries in the GFF file are not in order so the order of the relationships
  5. // is not put in correctly.
  6. /*************************************************************************
  7. *
  8. */
  9. function tripal_core_gff3_load_form (){
  10. $form['gff_file']= array(
  11. '#type' => 'textfield',
  12. '#title' => t('GFF3 File'),
  13. '#description' => t('Please enter the full system path for the GFF file, or a path within the Drupal
  14. installation (e.g. /sites/default/files/xyz.obo). The path must be accessible to the
  15. server on which this Drupal instance is running.'),
  16. '#required' => TRUE,
  17. '#weight' => 1
  18. );
  19. // get the list of organisms
  20. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  21. $previous_db = tripal_db_set_active('chado'); // use chado database
  22. $org_rset = db_query($sql);
  23. tripal_db_set_active($previous_db); // now use drupal database
  24. $organisms = array();
  25. $organisms[''] = '';
  26. while($organism = db_fetch_object($org_rset)){
  27. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  28. }
  29. $form['organism_id'] = array (
  30. '#title' => t('Organism'),
  31. '#type' => t('select'),
  32. '#description' => t("Choose the organism to which these sequences are associated "),
  33. '#required' => TRUE,
  34. '#options' => $organisms,
  35. );
  36. $form['import_options'] = array(
  37. '#type' => 'fieldset',
  38. '#title' => t('Import Options'),
  39. '#weight'=> 6,
  40. '#collapsed' => TRUE
  41. );
  42. $form['import_options']['add_only']= array(
  43. '#type' => 'checkbox',
  44. '#title' => t('Import only new features'),
  45. '#required' => FALSE,
  46. '#description' => t('The job will skip features in the GFF file that already
  47. exist in the database and import only new features.'),
  48. '#weight' => 2
  49. );
  50. $form['import_options']['update']= array(
  51. '#type' => 'checkbox',
  52. '#title' => t('Import all and update'),
  53. '#required' => FALSE,
  54. '#default_value' => 'checked',
  55. '#description' => t('Existing features will be updated and new features will be added. Attributes
  56. for a feature that are not present in the GFF but which are present in the
  57. database will not be altered.'),
  58. '#weight' => 3
  59. );
  60. $form['import_options']['refresh']= array(
  61. '#type' => 'checkbox',
  62. '#title' => t('Import all and replace'),
  63. '#required' => FALSE,
  64. '#description' => t('Existing features will be updated and feature properties not
  65. present in the GFF file will be removed.'),
  66. '#weight' => 4
  67. );
  68. $form['import_options']['remove']= array(
  69. '#type' => 'checkbox',
  70. '#title' => t('Delete features'),
  71. '#required' => FALSE,
  72. '#description' => t('Features present in the GFF file that exist in the database
  73. will be removed rather than imported'),
  74. '#weight' => 5
  75. );
  76. $form['analysis'] = array(
  77. '#type' => 'fieldset',
  78. '#title' => t('Analysis Used to Derive Features'),
  79. '#weight'=> 6,
  80. '#collapsed' => TRUE
  81. );
  82. $form['analysis']['desc'] = array(
  83. '#type' => 'markup',
  84. '#value' => t("Why specify an analysis for a data load? All data comes
  85. from some place, even if downloaded from Genbank. By specifying
  86. analysis details for all data uploads, it allows an end user to reproduce the
  87. data set. In some cases some of the fields may not apply. For
  88. data downloaded from Genbank, the 'program' field does not apply but is
  89. required. In this case, simply provide the name of the data source
  90. (e.g. NCBI Genbank) for the program and use the date accessed for the
  91. program version. For the description, be sure to include the search
  92. criteria used for selecting data from GenBank."),
  93. );
  94. // get the list of organisms
  95. $sql = "SELECT * FROM {analysis} ORDER BY name";
  96. $previous_db = tripal_db_set_active('chado'); // use chado database
  97. $org_rset = db_query($sql);
  98. tripal_db_set_active($previous_db); // now use drupal database
  99. $analyses = array();
  100. $analyses[''] = '';
  101. while($analysis = db_fetch_object($org_rset)){
  102. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  103. }
  104. $form['analysis']['analysis_id'] = array (
  105. '#title' => t('Analysis'),
  106. '#type' => t('select'),
  107. '#description' => t("Choose the analysis to which these features are associated "),
  108. '#required' => TRUE,
  109. '#options' => $analyses,
  110. );
  111. $form['button'] = array(
  112. '#type' => 'submit',
  113. '#value' => t('Import GFF3 file'),
  114. '#weight' => 10,
  115. );
  116. return $form;
  117. }
  118. /*************************************************************************
  119. *
  120. */
  121. function tripal_core_gff3_load_form_validate ($form, &$form_state){
  122. $gff_file = $form_state['values']['gff_file'];
  123. $organism_id = $form_state['values']['organism_id'];
  124. $add_only = $form_state['values']['add_only'];
  125. $update = $form_state['values']['update'];
  126. $refresh = $form_state['values']['refresh'];
  127. $remove = $form_state['values']['remove'];
  128. // check to see if the file is located local to Drupal
  129. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  130. if(!file_exists($dfile)){
  131. // if not local to Drupal, the file must be someplace else, just use
  132. // the full path provided
  133. $dfile = $gff_file;
  134. }
  135. if(!file_exists($dfile)){
  136. form_set_error('gff_file',t("Cannot find the file on the system. Check that the file exists or that the web server has permissions to read the file."));
  137. }
  138. if (($add_only and ($update or $refresh or $remove)) or
  139. ($update and ($add_only or $refresh or $remove)) or
  140. ($refresh and ($update or $add_only or $remove)) or
  141. ($remove and ($update or $refresh or $add_only))){
  142. form_set_error('add_only',t("Please select only one checkbox from the import options section"));
  143. }
  144. }
  145. /*************************************************************************
  146. *
  147. */
  148. function tripal_core_gff3_load_form_submit ($form, &$form_state){
  149. global $user;
  150. $gff_file = $form_state['values']['gff_file'];
  151. $organism_id = $form_state['values']['organism_id'];
  152. $add_only = $form_state['values']['add_only'];
  153. $update = $form_state['values']['update'];
  154. $refresh = $form_state['values']['refresh'];
  155. $remove = $form_state['values']['remove'];
  156. $analysis = $form_state['values']['analysis_id'];
  157. $args = array($gff_file,$organism_id,$analysis_id,$add_only,$update,$refresh,$remove);
  158. $type = '';
  159. if($add_only){
  160. $type = 'import only new features';
  161. }
  162. if($update){
  163. $type = 'import all and update';
  164. }
  165. if($refresh){
  166. $type = 'import all and replace';
  167. }
  168. if($remove){
  169. $type = 'delete features';
  170. }
  171. tripal_add_job("$type GFF3 file $gff_file",'tripal_core',
  172. 'tripal_core_load_gff3',$args,$user->uid);
  173. return '';
  174. }
  175. /*************************************************************************
  176. *
  177. */
  178. function tripal_core_load_gff3($gff_file, $organism_id,$analysis_id,$add_only =0,
  179. $update = 0, $refresh = 0, $remove = 0, $job = NULL)
  180. {
  181. // this array is used to cache all of the features in the GFF file and
  182. // used to lookup parent and target relationships
  183. $gff_features = array();
  184. // check to see if the file is located local to Drupal
  185. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  186. if(!file_exists($dfile)){
  187. // if not local to Drupal, the file must be someplace else, just use
  188. // the full path provided
  189. $dfile = $gff_file;
  190. }
  191. if(!file_exists($dfile)){
  192. print "ERROR: cannot find the file: $dfile\n";
  193. return 0;
  194. }
  195. $previous_db = tripal_db_set_active('chado');
  196. print "Opening $gff_file\n";
  197. $lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  198. $i = 0;
  199. // get the controlled vocaubulary that we'll be using. The
  200. // default is the 'sequence' ontology
  201. $sql = "SELECT * FROM cv WHERE name = '%s'";
  202. $cv = db_fetch_object(db_query($sql,'sequence'));
  203. if(!$cv){
  204. print "ERROR: cannot find the 'sequence' ontology\n";
  205. return '';
  206. }
  207. // get the organism for which this GFF3 file belongs
  208. $sql = "SELECT * FROM organism WHERE organism_id = %d";
  209. $organism = db_fetch_object(db_query($sql,$organism_id));
  210. $num_lines = sizeof($lines);
  211. $interval = intval($num_lines * 0.01);
  212. foreach ($lines as $line_num => $line) {
  213. $i++; // update the line count
  214. // update the job status every 1% features
  215. if($job and $i % $interval == 0){
  216. tripal_job_set_progress($job,intval(($i/$num_lines)*100));
  217. }
  218. // skip comments
  219. if(preg_match('/^#/',$line)){
  220. continue;
  221. }
  222. // skip empty lines
  223. if(preg_match('/^\s*$/',$line)){
  224. continue;
  225. }
  226. // TODO: remove URL encoding
  227. $cols = explode("\t",$line);
  228. if(sizeof($cols) != 9){
  229. print "ERROR: improper number of columns on line $i\n";
  230. return '';
  231. }
  232. // get the column values
  233. $landmark = $cols[0];
  234. $source = $cols[1];
  235. $type = $cols[2];
  236. $start = $cols[3];
  237. $end = $cols[4];
  238. $score = $cols[5];
  239. $strand = $cols[6];
  240. $phase = $cols[7];
  241. $attrs = explode(";",$cols[8]); // split by a semi-colon
  242. // ready the start and stop for chado
  243. $fmin = $start;
  244. $fmax = $end;
  245. if($end < $start){
  246. $fmin = $end;
  247. $fmax = $start;
  248. }
  249. // format the strand for chado
  250. if(strcmp($strand,'.')==0){
  251. $strand = 0;
  252. }
  253. elseif(strcmp($strand,'+')==0){
  254. $strand = 1;
  255. }
  256. elseif(strcmp($strand,'-')==0){
  257. $strand = -1;
  258. }
  259. if(strcmp($phase,'.')==0){
  260. $phase = '';
  261. }
  262. // get the type record
  263. $cvtermsql = "SELECT CVT.cvterm_id, CVT.cv_id, CVT.name, CVT.definition,
  264. CVT.dbxref_id, CVT.is_obsolete, CVT.is_relationshiptype
  265. FROM {cvterm} CVT
  266. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  267. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  268. WHERE CV.cv_id = %d and (CVT.name = '%s' or CVTS.synonym = '%s')";
  269. $cvterm = db_fetch_object(db_query($cvtermsql,$cv->cv_id,$type,$type));
  270. if(!$cvterm){
  271. print "ERROR: cannot find ontology term '$type' on line $i.\n";
  272. return '';
  273. }
  274. // break apart each of the attributes
  275. $tags = array();
  276. $attr_name = '';
  277. $attr_uniquename = '';
  278. $attr_residue_info = '';
  279. $attr_locgroup = 0;
  280. $attr_fmin_partial = 'f';
  281. $attr_fmax_partial = 'f';
  282. $attr_is_obsolete = 'f';
  283. $attr_is_analysis = 'f';
  284. $attr_others = '';
  285. $residues = '';
  286. $generate_name = 0;
  287. foreach($attrs as $attr){
  288. $attr = rtrim($attr);
  289. $attr = ltrim($attr);
  290. if(strcmp($attr,'')==0){
  291. continue;
  292. }
  293. if(!preg_match('/^[^\=]+\=[^\=]+$/',$attr)){
  294. print "ERROR: attribute is not correctly formatted on line $i: $attr\n";
  295. return '';
  296. }
  297. // break apart each tag
  298. $tag = explode("=",$attr); // split by equals sign
  299. // multiple instances of an attribute are separated by commas
  300. $tags[$tag[0]] = explode(",",$tag[1]); // split by comma
  301. if(strcmp($tag[0],'ID')==0){
  302. $attr_uniquename = $tag[1];
  303. }
  304. elseif(strcmp($tag[0],'Name')==0){
  305. $attr_name = $tag[1];
  306. }
  307. // get the list of other attributes other than those reserved ones.
  308. elseif(strcmp($tag[0],'Alias')!=0 and strcmp($tag[0],'Parent')!=0 and
  309. strcmp($tag[0],'Target')!=0 and strcmp($tag[0],'Gap')!=0 and
  310. strcmp($tag[0],'Derives_from')!=0 and strcmp($tag[0],'Note')!=0 and
  311. strcmp($tag[0],'Dbxref')!=0 and strcmp($tag[0],'Ontology_term')!=0 and
  312. strcmp($tag[0],'Is_circular')!=0){
  313. $attr_others[$tag[0]] = $tag[1];
  314. }
  315. }
  316. // if neither name nor uniquename are provided then generate one
  317. if(!$attr_uniquename and !$attr_name){
  318. $generate_name = 1;
  319. }
  320. // if a name is not specified then use the unique name
  321. if(strcmp($attr_name,'')==0){
  322. $attr_name = $attr_uniquename;
  323. }
  324. // if an ID attribute is not specified then use the attribute name and
  325. // hope for the best
  326. if(!$attr_uniquename){
  327. $attr_uniquename = $attr_name;
  328. }
  329. // make sure the landmark sequence exists in the database. We don't
  330. // know the type of the landmark so we'll hope that it's unique across
  331. // all types. If not we'll error out. This test is only necessary if
  332. // if the landmark and the uniquename are different. If they are the same
  333. // then this is the information for the landmark
  334. if(strcmp($landmark,$attr_uniquename)!=0){
  335. $feature_sql = "SELECT count(*) as num_landmarks
  336. FROM {feature}
  337. WHERE organism_id = %d and uniquename = '%s'";
  338. $count = db_fetch_object(db_query($feature_sql,$organism_id,$landmark));
  339. if(!$count or $count->num_landmarks == 0){
  340. print "ERROR: the landmark '$landmark' cannot be found for this organism. ".
  341. "Please add the landmark and then retry the import of this GFF3 ".
  342. "file.\n";
  343. return '';
  344. }
  345. if($count->num_landmarks > 1){
  346. print "ERROR: the landmark '$landmark' is not unique for this organism. ".
  347. "The features cannot be associated.\n";
  348. return '';
  349. }
  350. }
  351. // if the option is to remove or refresh then we want to remove
  352. // the feature from the database.
  353. if(!$generate_name and ($remove or $refresh)){
  354. print "Removing feature '$attr_uniquename'\n";
  355. $sql = "DELETE FROM {feature}
  356. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  357. $result = db_query($sql,$organism->organism_id,$attr_uniquename,$cvterm->cvterm_id);
  358. if(!$result){
  359. print "ERROR: cannot delete feature $attr_uniquename\n";
  360. }
  361. $feature = 0;
  362. }
  363. // add or update the feature and all properties
  364. if($update or $refresh or $add_only){
  365. // add/update the feature
  366. $feature = tripal_core_load_gff3_feature($organism,$analysis_id,$cvterm,
  367. $attr_uniquename,$attr_name,$residues,$attr_is_analysis,
  368. $attr_is_obsolete, $add_only,$generate_name);
  369. // store all of the features so far use later by parent and target
  370. // relationships
  371. $gff_features[$feature->uniquename]['type'] = $type;
  372. if($feature){
  373. if($generate_name){
  374. $attr_uniquename = $feature->uniquename;
  375. }
  376. // add/update the featureloc if the landmark and the ID are not the same
  377. // if they are the same then this entry in the GFF is probably a landmark identifier
  378. if(strcmp($landmark,$attr_uniquename)!=0){
  379. tripal_core_load_gff3_featureloc($feature,$organism,
  380. $landmark,$fmin,$fmax,$strand,$phase,$attr_fmin_partial,
  381. $attr_fmax_partial,$attr_residue_info,$attr_locgroup);
  382. }
  383. // add any aliases for this feature
  384. if(array_key_exists('Alias',$tags)){
  385. tripal_core_load_gff3_alias($feature,$tags['Alias']);
  386. }
  387. // add any aliases for this feature
  388. if(array_key_exists('Dbxref',$tags)){
  389. tripal_core_load_gff3_dbxref($feature,$tags['Dbxref']);
  390. }
  391. // add parent relationships
  392. if(array_key_exists('Parent',$tags)){
  393. tripal_core_load_gff3_parents($feature,$cvterm,$tags['Parent'],$gff_features,$organism_id);
  394. }
  395. }
  396. }
  397. }
  398. tripal_db_set_active($previous_db);
  399. return '';
  400. }
  401. /*************************************************************************
  402. *
  403. */
  404. function tripal_core_load_gff3_parents($feature,$cvterm,$parents,$gff_features,$organism_id){
  405. $uname = $feature->uniquename;
  406. $type = $cvterm->name;
  407. $rel_type = 'part_of';
  408. // create these SQL statements that will be used repeatedly below.
  409. $cvtermsql = "SELECT CVT.cvterm_id
  410. FROM {cvterm} CVT
  411. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  412. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  413. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  414. $feature_sql = "SELECT * FROM {feature}
  415. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  416. // iterate through the parents in the list
  417. foreach($parents as $parent){
  418. $parent_type = $gff_features[$parent]['type'];
  419. $parentcvterm = db_fetch_object(db_query($cvtermsql,'sequence',$parent_type,$parent_type));
  420. $relcvterm = db_fetch_object(db_query($cvtermsql,'relationship',$rel_type,$rel_type));
  421. $parent_feature = db_fetch_object(db_query($feature_sql,$organism_id,$parent,$parentcvterm->cvterm_id));
  422. if($parent_feature){
  423. // check to see if the relationship already exists
  424. $sql = "SELECT * FROM {feature_relationship} WHERE subject_id = %d and object_id = %d and type_id = %d";
  425. $rel = db_fetch_object(db_query($sql,$feature->feature_id,$parent_feature->feature_id,$relcvterm->cvterm_id));
  426. if($rel){
  427. print " Relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  428. } else {
  429. $sql = "INSERT INTO {feature_relationship} (subject_id,object_id,type_id)
  430. VALUES (%d,%d,%d)";
  431. $result = db_query($sql,$feature->feature_id,$parent_feature->feature_id,$relcvterm->cvterm_id);
  432. if(!$result){
  433. print "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  434. } else {
  435. print " Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  436. }
  437. }
  438. }
  439. else {
  440. print "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n";
  441. }
  442. }
  443. }
  444. /*************************************************************************
  445. *
  446. */
  447. function tripal_core_load_gff3_dbxref($feature,$dbxrefs){
  448. // iterate through each of the dbxrefs
  449. foreach($dbxrefs as $dbxref){
  450. // get the database name from the reference. If it doesn't exist then create one.
  451. $ref = explode(":",$dbxref);
  452. $dbname = $ref[0];
  453. $accession = $ref[1];
  454. // first look for the database name if it doesn't exist then create one.
  455. // first check for the fully qualified URI (e.g. DB:<dbname>. If that
  456. // can't be found then look for the name as is. If it still can't be found
  457. // the create the database
  458. $db = tripal_core_chado_select('db',array('db_id'),array('name' => "DB:$dbname"));
  459. if(sizeof($db) == 0){
  460. $db = tripal_core_chado_select('db',array('db_id'),array('name' => "$dbname"));
  461. }
  462. if(sizeof($db) == 0){
  463. $ret = tripal_core_chado_insert('db',array('name' => $dbname,
  464. 'description' => 'Added automatically by the GFF loader'));
  465. if($ret){
  466. print "Added new database: $dbname\n";
  467. $db = tripal_core_chado_select('db',array('db_id'),array('name' => "$dbname"));
  468. } else {
  469. print "ERROR: cannot find or add the database $dbname\n";
  470. return 0;
  471. }
  472. }
  473. $db = $db[0];
  474. // now check to see if the accession exists
  475. $dbxref = tripal_core_chado_select('dbxref',array('dbxref_id'),array(
  476. 'accession' => $accession,'db_id' => $db->db_id));
  477. // if the accession doesn't exist then we want to add it
  478. if(sizeof($dbxref) == 0){
  479. $ret = tripal_core_chado_insert('dbxref',array('db_id' => $db->db_id,
  480. 'accession' => $accession,'version' => ''));
  481. $dbxref = tripal_core_chado_select('dbxref',array('dbxref_id'),array(
  482. 'accession' => $accession,'db_id' => $db->db_id));
  483. }
  484. $dbxref = $dbxref[0];
  485. // check to see if tihs feature dbxref already exists
  486. $fdbx = tripal_core_chado_select('feature_dbxref',array('feature_dbxref_id'),
  487. array('dbxref_id' => $dbxref->dbxref_id,'feature_id' => $feature->feature_id));
  488. // now associate this feature with the database reference if it doesn't
  489. // already exist
  490. if(sizeof($fdbx)==0){
  491. $ret = tripal_core_chado_insert('feature_dbxref',array(
  492. 'feature_id' => $feature->feature_id,
  493. 'dbxref_id' => $dbxref->dbxref_id));
  494. if($ret){
  495. print "Adding dbxref $dbname:$accession\n";
  496. } else {
  497. print "ERROR: failed to insert dbxref: $dbname:$accession\n";
  498. return 0;
  499. }
  500. } else {
  501. print "Dbxref already exists, skipping $dbname:$accession\n";
  502. }
  503. }
  504. return 1;
  505. }
  506. /*************************************************************************
  507. *
  508. */
  509. function tripal_core_load_gff3_alias($feature,$aliases){
  510. // make sure we have a 'synonym_type' vocabulary
  511. $sql = "SELECT * FROM {cv} WHERE name='synonym_type'";
  512. $syncv = db_fetch_object(db_query($sql));
  513. if(!$syncv){
  514. $sql = "INSERT INTO {cv} (name,definition) VALUES ('synonym_type','')";
  515. if(!db_query($sql)){
  516. print("ERROR: Failed to add the synonyms type vocabulary");
  517. return 0;
  518. }
  519. $syncv = db_fetch_object(db_query($sql));
  520. }
  521. // get the 'exact' cvterm, which is the type of synonym we're adding
  522. $cvtsql = "
  523. SELECT * FROM {cvterm} CVT
  524. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  525. WHERE CVT.name = '%s' and CV.name = '%s'
  526. ";
  527. $syntype = db_fetch_object(db_query($cvtsql,'exact','synonym_type'));
  528. if(!$syntype){
  529. $term = array(
  530. 'name' => array('exact'),
  531. 'id' => array("internal:exact"),
  532. 'definition' => array(''),
  533. 'is_obsolete' => array(0),
  534. );
  535. $syntype = tripal_cv_obo_add_cv_term($term,$syncv,0,1);
  536. if(!$syntype){
  537. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$type");
  538. }
  539. }
  540. // iterate through all of the aliases and add each one
  541. foreach($aliases as $alias){
  542. print " Adding Alias $alias\n";
  543. // check to see if the alias already exists in the synonym table
  544. // if not, then add it
  545. $synsql = "SELECT * FROM {synonym}
  546. WHERE name = '%s' and type_id = %d";
  547. $synonym = db_fetch_object(db_query($synsql,$alias,$syntype->cvterm_id));
  548. if(!$synonym){
  549. $sql = "INSERT INTO {synonym}
  550. (name,type_id,synonym_sgml)
  551. VALUES ('%s',%d,'%s')";
  552. $result = db_query($sql,$alias,$syntype->cvterm_id,'');
  553. if(!$result){
  554. print "ERROR: cannot add alias $alias to synonym table\n";
  555. }
  556. }
  557. $synonym = db_fetch_object(db_query($synsql,$alias,$syntype->cvterm_id));
  558. // check to see if we have a NULL publication in the pub table. If not,
  559. // then add one.
  560. $pubsql = "SELECT * FROM {pub} WHERE uniquename = 'null'";
  561. $pub = db_fetch_object(db_query($pubsql));
  562. if(!$pub){
  563. $sql = "INSERT INTO pub (uniquename,type_id) VALUES ('%s',
  564. (SELECT cvterm_id
  565. FROM cvterm CVT
  566. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  567. INNER JOIN db DB on DB.db_id = DBX.db_id
  568. WHERE CVT.name = 'null' and DB.name = 'null')";
  569. $result = db_query($sql,'null');
  570. if(!$result){
  571. print "ERROR: cannot add null publication needed for setup of alias\n";
  572. return 0;
  573. }
  574. }
  575. $pub = db_fetch_object(db_query($pubsql));
  576. // check to see if the synonym exists in the feature_synonym table
  577. // if not, then add it.
  578. $synsql = "SELECT * FROM {feature_synonym}
  579. WHERE synonym_id = %d and feature_id = %d and pub_id = %d";
  580. $fsyn = db_fetch_object(db_query($synsql,$synonym->synonym_id,$feature->feature_id,$pub->pub_id));
  581. if(!$fsyn){
  582. $sql = "INSERT INTO {feature_synonym}
  583. (synonym_id,feature_id,pub_id)
  584. VALUES (%d,%d,%d)";
  585. $result = db_query($sql,$synonym->synonym_id,$feature->feature_id,$pub->pub_id);
  586. if(!$result){
  587. print "ERROR: cannot add alias $alias to feature synonym table\n";
  588. return 0;
  589. }
  590. } else {
  591. print "Synonym $alias already exists. Skipping\n";
  592. }
  593. }
  594. return 1;
  595. }
  596. /*************************************************************************
  597. *
  598. */
  599. function tripal_core_load_gff3_feature($organism,$analysis_id,$cvterm,$uniquename,$name,
  600. $residues,$is_analysis='f',$is_obsolete='f',$add_only,$generate_name) {
  601. if($generate_name){
  602. $uniquename = 'tripal_temp_loading_name' . rand(1,99999999);
  603. }
  604. // check to see if the feature already exists
  605. $feature_sql = "SELECT * FROM {feature}
  606. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  607. $feature = db_fetch_object(db_query($feature_sql,$organism->organism_id,$uniquename,$cvterm->cvterm_id));
  608. if(strcmp($is_obsolete,'f')==0){
  609. $is_obsolete = 'false';
  610. }
  611. if(strcmp($is_analysis,'f')==0){
  612. $is_analysis = 'false';
  613. }
  614. // insert the feature if it does not exist otherwise perform an update
  615. if(!$feature){
  616. print "Adding feature '$uniquename' ($cvterm->name)\n";
  617. $isql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,
  618. md5checksum, type_id,is_analysis,is_obsolete)
  619. VALUES(%d,'%s','%s','%s',%d, '%s', %d, %s, %s)";
  620. $result = db_query($isql,$organism->organism_id,$name,$uniquename,$residues,strlen($residues),
  621. md5($residues),$cvterm->cvterm_id,$is_analysis,$is_obsolete);
  622. if(!$result){
  623. print "ERROR: failed to insert feature '$uniquename' ($cvterm->name)\n";
  624. return 0;
  625. }
  626. }
  627. elseif(!$add_only) {
  628. print "Updating feature '$uniquename' ($cvterm->name)\n";
  629. $usql = "UPDATE {feature}
  630. SET name = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s',
  631. is_analysis = %s, is_obsolete = %s
  632. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  633. $result = db_query($usql,$name,$residues,strlen($residues),md5($residues),$is_analysis,$is_obsolete,
  634. $organism_id,$uniquename,$cvterm->cvterm_id);
  635. if(!$result){
  636. print "ERROR: failed to update feature '$uniquename' ($cvterm->name)\n";
  637. return 0;
  638. }
  639. }
  640. else {
  641. // the feature exists and we don't want to update it so return
  642. // a value of 0. This will stop all downstream property additions
  643. print "Skipping existing feature: '$uniquename' ($cvterm->name).\n";
  644. return 0;
  645. }
  646. // get the newly added feature
  647. $feature = db_fetch_object(db_query($feature_sql,$organism->organism_id,$uniquename,$cvterm->cvterm_id));
  648. // add the analysisfeature entry to the analysisfeature table if it doesn't already exist
  649. $af_values = array('analysis_id' => $analysis_id, 'feature_id' => $feature->feature_id);
  650. if(tripal_core_chado_select('analysisfeature','analysisfeature_id',$af,true)){
  651. if(!tripal_core_chado_insert('analysisfeature',$af_values)){
  652. print "ERROR: could not add analysisfeature record: $analysis_id, $feature->feature_id\n";
  653. return 0;
  654. }
  655. }
  656. // now that we've added the feature let's reset it's uniquename to be
  657. // the feature id
  658. if($generate_name){
  659. $usql = "UPDATE {feature}
  660. SET name = '%s', uniquename = '%s'
  661. WHERE feature_id = %d";
  662. print " Renaming feature '$uniquename' => ";
  663. $uniquename = "$feature->feature_id";
  664. print "$uniquename\n";
  665. $result = db_query($usql,$uniquename,$uniquename,$feature_id);
  666. if(!$result){
  667. print "ERROR: failed to update unnamed feature '$uniquename' with generic name\n";
  668. return 0;
  669. }
  670. $feature->name = $uniquename;
  671. $feature->uniquename = $uniquename;
  672. }
  673. return $feature;
  674. }
  675. /*************************************************************************
  676. *
  677. */
  678. function tripal_core_load_gff3_featureloc($feature,$organism,$landmark,$fmin,
  679. $fmax,$strand,$phase,$is_fmin_partial,$is_fmax_partial,$residue_info,$locgroup)
  680. {
  681. // get the source feature
  682. $sql = "SELECT * FROM {feature}
  683. WHERE organism_id = %d and uniquename = '%s'";
  684. $srcfeature = db_fetch_object(db_query($sql,$organism->organism_id,$landmark));
  685. if(!$srcfeature){
  686. print "ERROR: cannot find landmark feature $landmark. Cannot add the feature location record\n";
  687. return 0;
  688. }
  689. // TODO: create an attribute that recognizes the residue_info,locgroup, is_fmin_partial and is_fmax_partial, right now these are
  690. // hardcoded to be false and 0 below.
  691. // check to see if this featureloc already exists, but also keep track of the
  692. // last rank value
  693. $rank = -1;
  694. $exists = 0;
  695. $featureloc_sql = "SELECT FL.featureloc_id,FL.fmin,FL.fmax, FL.is_fmin_partial,
  696. FL.is_fmax_partial, FL.strand, FL.phase, FL.residue_info,
  697. FL.locgroup, F.uniquename as srcname
  698. FROM {featureloc} FL
  699. INNER JOIN {feature} F on F.feature_id = FL.srcfeature_id
  700. WHERE FL.feature_id = %d
  701. ORDER BY rank ASC";
  702. $recs = db_query($featureloc_sql,$feature->feature_id);
  703. while ($featureloc = db_fetch_object($recs)){
  704. if(strcmp($featureloc->srcname,$landmark)==0 and
  705. $featureloc->fmin == $fmin and strcmp($featureloc->is_fmin_partial,$is_fmin_partial)==0 and
  706. $featureloc->fmax == $fmax and strcmp($featureloc->is_fmax_partial,$is_fmax_partial)==0 and
  707. $featureloc->phase == $phase and $featureloc->strand == $strand and
  708. strcmp($featureloc->residue_info,$residue_info)==0 and
  709. $featureloc->locgroup == $locgroup){
  710. // this is the same featureloc, so do nothing... no need to update
  711. //TODO: need more checks here
  712. print " No change to featureloc\n";
  713. $exists = 1;
  714. }
  715. $rank = $featureloc->rank;
  716. }
  717. if(!$exists){
  718. $rank++;
  719. // this feature location is new so add it
  720. if(!$phase){
  721. $phase = 'NULL';
  722. }
  723. if(strcmp($is_fmin_partial,'f')==0){
  724. $is_fmin_partial = 'false';
  725. }
  726. elseif(strcmp($is_fmin_partial,'t')==0){
  727. $is_fmin_partial = 'true';
  728. }
  729. if(strcmp($is_fmax_partial,'f')==0){
  730. $is_fmax_partial = 'false';
  731. }
  732. elseif(strcmp($is_fmax_partial,'t')==0){
  733. $is_fmax_partial = 'true';
  734. }
  735. print " Adding featureloc $srcfeature->uniquename fmin: $fmin, fmax: $fmax, strand: $strand, phase: $phase, rank: $rank\n";
  736. $fl_isql = "INSERT INTO {featureloc}
  737. (feature_id, srcfeature_id, fmin, is_fmin_partial, fmax, is_fmax_partial,
  738. strand, phase, residue_info, locgroup, rank)
  739. VALUES (%d,%d,%d,%s,%d,%s,%d,%s,'%s',%d,%d)";
  740. $result = db_query($fl_isql,$feature->feature_id,$srcfeature->feature_id,$fmin,$is_fmin_partial,$fmax,$is_fmax_partial,
  741. $strand,$phase,$residue_info,$locgroup,$rank);
  742. if(!$result){
  743. print "ERROR: failed to insert featureloc\n";
  744. exit;
  745. return 0;
  746. }
  747. }
  748. return 1;
  749. }
  750. /*************************************************************************
  751. *
  752. */