gff_loader.php 31 KB

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