gff_loader.php 31 KB

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