fasta_loader.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup fasta_loader FASTA Feature Loader
  8. * @{
  9. * Provides fasta loading functionality. Creates features based on their specification in a fasta file.
  10. * @}
  11. * @ingroup tripal_feature
  12. */
  13. /**
  14. *
  15. *
  16. * @ingroup fasta_loader
  17. */
  18. function tripal_feature_fasta_load_form( ) {
  19. $form['fasta_file']= array(
  20. '#type' => 'textfield',
  21. '#title' => t('FASTA File'),
  22. '#description' => t('Please enter the full system path for the FASTA 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. );
  27. // get the list of organisms
  28. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  29. $previous_db = tripal_db_set_active('chado'); // use chado database
  30. $org_rset = db_query($sql);
  31. tripal_db_set_active($previous_db); // now use drupal database
  32. $organisms = array();
  33. $organisms[''] = '';
  34. while ($organism = db_fetch_object($org_rset)) {
  35. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  36. }
  37. $form['organism_id'] = array(
  38. '#title' => t('Organism'),
  39. '#type' => t('select'),
  40. '#description' => t("Choose the organism to which these sequences are associated"),
  41. '#required' => TRUE,
  42. '#options' => $organisms,
  43. );
  44. $form['seqtype']= array(
  45. '#type' => 'textfield',
  46. '#title' => t('Sequence Type'),
  47. '#required' => TRUE,
  48. '#description' => t('Please enter the Sequence Ontology term that describes the sequences in the FASTA file.'),
  49. );
  50. // get the list of organisms
  51. $sql = "SELECT L.library_id, L.name, CVT.name as type
  52. FROM {library} L
  53. INNER JOIN {cvterm} CVT ON L.type_id = CVT.cvterm_id
  54. ORDER BY name";
  55. $previous_db = tripal_db_set_active('chado'); // use chado database
  56. $lib_rset = db_query($sql);
  57. tripal_db_set_active($previous_db); // now use drupal database
  58. $libraries = array();
  59. $libraries[''] = '';
  60. while ($library = db_fetch_object($lib_rset)) {
  61. $libraries[$library->library_id] = "$library->name ($library->type)";
  62. }
  63. // $form['library_id'] = array (
  64. // '#title' => t('Library'),
  65. // '#type' => t('select'),
  66. // '#description' => t("Choose the library to which these sequences are associated "),
  67. // '#required' => FALSE,
  68. // '#options' => $libraries,
  69. // '#weight' => 5,
  70. // );
  71. $form['method']= array(
  72. '#type' => 'radios',
  73. '#title' => 'Method',
  74. '#required' => TRUE,
  75. '#options' => array(
  76. t('Insert only'),
  77. t('Update only'),
  78. t('Insert and update'),
  79. ),
  80. '#description' => t('Select how features in the FASTA file are handled.
  81. Select "Insert only" to insert the new features. If a feature already
  82. exists with the same name or unique name and type then it is skipped.
  83. Select "Update only" to only update featues that already exist in the
  84. database. Select "Insert and Update" to insert features that do
  85. not exist and upate those that do.'),
  86. '#default_value' => 2,
  87. );
  88. $form['match_type']= array(
  89. '#type' => 'radios',
  90. '#title' => 'Name Match Type',
  91. '#required' => TRUE,
  92. '#options' => array(
  93. t('Name'),
  94. t('Unique name'),
  95. ),
  96. '#description' => t('Feature data is stored in Chado with both a human-readable
  97. name and a unique name. If the features in your FASTA file are identified using
  98. a human-readable name then select the "Name" button. If your features are
  99. identified using the unique name then select the "Unique name" button. If you
  100. loaded your features first using the GFF loader then the unique name of each
  101. features were indicated by the "ID=" attribute and the name by the "Name=" attribute.
  102. By default, the FASTA loader will use the first word (character string
  103. before the first space) as the name for your feature. If
  104. this does not uniquely identify your feature consider specifying a regular expression in the advanced section below.
  105. Additionally, you may import both a name and a unique name for each sequence using the advanced options.
  106. When updating a sequence, the value selected here will be used to identify the sequence in the
  107. database in combination with any regular expression provided below.'),
  108. '#default_value' => 1,
  109. );
  110. $form['analysis'] = array(
  111. '#type' => 'fieldset',
  112. '#title' => t('Analysis Used to Derive Features'),
  113. '#collapsed' => TRUE
  114. );
  115. $form['analysis']['desc'] = array(
  116. '#type' => 'markup',
  117. '#value' => t("Why specify an analysis for a data load? All data comes
  118. from some place, even if downloaded from Genbank. By specifying
  119. analysis details for all data uploads, it allows an end user to reproduce the
  120. data set, but at least indicates the source of the data."),
  121. );
  122. // get the list of organisms
  123. $sql = "SELECT * FROM {analysis} ORDER BY name";
  124. $previous_db = tripal_db_set_active('chado'); // use chado database
  125. $org_rset = db_query($sql);
  126. tripal_db_set_active($previous_db); // now use drupal database
  127. $analyses = array();
  128. $analyses[''] = '';
  129. while ($analysis = db_fetch_object($org_rset)) {
  130. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  131. }
  132. $form['analysis']['analysis_id'] = array(
  133. '#title' => t('Analysis'),
  134. '#type' => t('select'),
  135. '#description' => t("Choose the analysis to which these features are associated"),
  136. '#required' => TRUE,
  137. '#options' => $analyses,
  138. );
  139. // Advanced Options
  140. $form['advanced'] = array(
  141. '#type' => 'fieldset',
  142. '#title' => t('Advanced Options'),
  143. '#collapsed' => TRUE
  144. );
  145. $form['advanced']['re_help']= array(
  146. '#type' => 'item',
  147. '#value' => t('A regular expression is an advanced method for extracting information from a string of text.
  148. Your FASTA file may contain both a human-readable name and a unique name for each sequence.
  149. If you want to import
  150. both the name and unique name for all sequences, then you must provide regular expressions
  151. so that the loader knows how to separate them.
  152. Otherwise the name and uniquename will be the same.
  153. By default, this loader will use the first word in the definition
  154. lines of the FASTA file
  155. as the name or unique name of the feature.'),
  156. );
  157. $form['advanced']['re_name']= array(
  158. '#type' => 'textfield',
  159. '#title' => t('Regular expression for the name'),
  160. '#required' => FALSE,
  161. '#description' => t('Enter the regular expression that will extract the
  162. feature name from the FASTA definition line. For example, for a
  163. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  164. the regular expression for the name would be, "^(.*?)\|.*$".'),
  165. );
  166. $form['advanced']['re_uname']= array(
  167. '#type' => 'textfield',
  168. '#title' => t('Regular expression for the unique name'),
  169. '#required' => FALSE,
  170. '#description' => t('Enter the regular expression that will extract the
  171. feature name from the FASTA definition line. For example, for a
  172. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  173. the regular expression for the unique name would be "^.*?\|(.*)$").'),
  174. );
  175. // Advanced database cross-reference optoins
  176. $form['advanced']['db'] = array(
  177. '#type' => 'fieldset',
  178. '#title' => t('External Database Reference'),
  179. '#weight' => 6,
  180. '#collapsed' => TRUE
  181. );
  182. $form['advanced']['db']['re_accession']= array(
  183. '#type' => 'textfield',
  184. '#title' => t('Regular expression for the accession'),
  185. '#required' => FALSE,
  186. '#description' => t('Enter the regular expression that will extract the accession for the external database for each feature from the FASTA definition line.'),
  187. '#weight' => 2
  188. );
  189. // get the list of databases
  190. $sql = "SELECT * FROM {db} ORDER BY name";
  191. $previous_db = tripal_db_set_active('chado'); // use chado database
  192. $db_rset = db_query($sql);
  193. tripal_db_set_active($previous_db); // now use drupal database
  194. $dbs = array();
  195. $dbs[''] = '';
  196. while ($db = db_fetch_object($db_rset)) {
  197. $dbs[$db->db_id] = "$db->name";
  198. }
  199. $form['advanced']['db']['db_id'] = array(
  200. '#title' => t('External Database'),
  201. '#type' => t('select'),
  202. '#description' => t("Plese choose an external database for which these sequences have a cross reference."),
  203. '#required' => FALSE,
  204. '#options' => $dbs,
  205. '#weight' => 1,
  206. );
  207. $form['advanced']['relationship'] = array(
  208. '#type' => 'fieldset',
  209. '#title' => t('Relationships'),
  210. '#weight' => 6,
  211. '#collapsed' => TRUE
  212. );
  213. $rels = array();
  214. $rels[''] = '';
  215. $rels['part_of'] = 'part of';
  216. $rels['derives_from'] = 'produced by';
  217. // Advanced references options
  218. $form['advanced']['relationship']['rel_type']= array(
  219. '#title' => t('Relationship Type'),
  220. '#type' => t('select'),
  221. '#description' => t("Use this option to create associations, or relationships between the
  222. features of this FASTA file and existing features in the database. For
  223. example, to associate a FASTA file of peptides to existing genes or transcript sequence,
  224. select the type 'produced by'. For a CDS sequences select the type 'part of'"),
  225. '#required' => FALSE,
  226. '#options' => $rels,
  227. '#weight' => 5,
  228. );
  229. $form['advanced']['relationship']['re_subject']= array(
  230. '#type' => 'textfield',
  231. '#title' => t('Regular expression for the parent'),
  232. '#required' => FALSE,
  233. '#description' => t('Enter the regular expression that will extract the unique
  234. name needed to identify the existing sequence for which the
  235. relationship type selected above will apply.'),
  236. '#weight' => 6
  237. );
  238. $form['advanced']['relationship']['parent_type']= array(
  239. '#type' => 'textfield',
  240. '#title' => t('Parent Type'),
  241. '#required' => FALSE,
  242. '#description' => t('Please enter the Sequence Ontology term for the parent. For example
  243. if the FASTA file being loaded is a set of proteins that are
  244. products of genes, then use the SO term \'gene\' or \'transcript\' or equivalent. However,
  245. this type must match the type for already loaded features.'),
  246. '#weight' => 7
  247. );
  248. $form['button'] = array(
  249. '#type' => 'submit',
  250. '#value' => t('Import FASTA file'),
  251. '#weight' => 10,
  252. );
  253. return $form;
  254. }
  255. /**
  256. *
  257. *
  258. * @ingroup fasta_loader
  259. */
  260. function tripal_feature_fasta_load_form_validate($form, &$form_state) {
  261. $fasta_file = trim($form_state['values']['fasta_file']);
  262. $organism_id = $form_state['values']['organism_id'];
  263. $type = trim($form_state['values']['seqtype']);
  264. $method = trim($form_state['values']['method']);
  265. $match_type = trim($form_state['values']['match_type']);
  266. $library_id = $form_state['values']['library_id'];
  267. $re_name = trim($form_state['values']['re_name']);
  268. $re_uname = trim($form_state['values']['re_uname']);
  269. $re_accession = trim($form_state['values']['re_accession']);
  270. $db_id = $form_state['values']['db_id'];
  271. $rel_type = $form_state['values']['rel_type'];
  272. $re_subject = trim($form_state['values']['re_subject']);
  273. $parent_type = trim($form_state['values']['parent_type']);
  274. if ($method == 0) {
  275. $method = 'Insert only';
  276. }
  277. if ($method == 1) {
  278. $method = 'Update only';
  279. }
  280. if ($method == 2) {
  281. $method = 'Insert and update';
  282. }
  283. if ($match_type == 0) {
  284. $match_type = 'Name';
  285. }
  286. if ($match_type == 1) {
  287. $match_type = 'Unique name';
  288. }
  289. if ($re_name and !$re_uname and strcmp($match_type, 'Unique name')==0) {
  290. form_set_error('re_uname', t("You must provide a regular expression to identify the sequence unique name"));
  291. }
  292. if (!$re_name and $re_uname and strcmp($match_type, 'Name')==0) {
  293. form_set_error('re_name', t("You must provide a regular expression to identify the sequence name"));
  294. }
  295. // check to see if the file is located local to Drupal
  296. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $fasta_file;
  297. if (!file_exists($dfile)) {
  298. // if not local to Drupal, the file must be someplace else, just use
  299. // the full path provided
  300. $dfile = $fasta_file;
  301. }
  302. if (!file_exists($dfile)) {
  303. form_set_error('fasta_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."));
  304. }
  305. // make sure if a relationship is specified that all fields are provided.
  306. if (($rel_type or $parent_type) and !$re_subject) {
  307. form_set_error('re_subject', t("Please provide a regular expression for the parent"));
  308. }
  309. if (($rel_type or $re_subject) and !$parent_type) {
  310. form_set_error('parent_type', t("Please provide a SO term for the parent"));
  311. }
  312. if (($parent_type or $re_subject) and !$rel_type) {
  313. form_set_error('rel_type', t("Please select a relationship type"));
  314. }
  315. // make sure if a database is specified that all fields are provided
  316. if ($db_id and !$re_accession) {
  317. form_set_error('re_accession', t("Please provide a regular expression for the accession"));
  318. }
  319. if ($re_accession and !$db_id) {
  320. form_set_error('db_id', t("Please select a database"));
  321. }
  322. // check to make sure the types exists
  323. $cvtermsql = "SELECT CVT.cvterm_id
  324. FROM {cvterm} CVT
  325. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  326. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  327. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  328. $cvterm = db_fetch_object(db_query($cvtermsql, 'sequence', $type, $type));
  329. if (!$cvterm) {
  330. form_set_error('type', t("The Sequence Ontology (SO) term selected for the sequence type is not available in the database. Please check spelling or select another."));
  331. }
  332. if ($rel_type) {
  333. $cvterm = db_fetch_object(db_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  334. if (!$cvterm) {
  335. form_set_error('parent_type', t("The Sequence Ontology (SO) term selected for the parent relationship is not available in the database. Please check spelling or select another."));
  336. }
  337. }
  338. // check to make sure the 'relationship' and 'sequence' ontologies are loaded
  339. $form_state['storage']['dfile'] = $dfile;
  340. }
  341. /**
  342. *
  343. *
  344. * @ingroup fasta_loader
  345. */
  346. function tripal_feature_fasta_load_form_submit($form, &$form_state) {
  347. global $user;
  348. $dfile = $form_state['storage']['dfile'];
  349. $organism_id = $form_state['values']['organism_id'];
  350. $type = trim($form_state['values']['seqtype']);
  351. $method = trim($form_state['values']['method']);
  352. $match_type = trim($form_state['values']['match_type']);
  353. $library_id = $form_state['values']['library_id'];
  354. $re_name = trim($form_state['values']['re_name']);
  355. $re_uname = trim($form_state['values']['re_uname']);
  356. $re_accession = trim($form_state['values']['re_accession']);
  357. $db_id = $form_state['values']['db_id'];
  358. $rel_type = $form_state['values']['rel_type'];
  359. $re_subject = trim($form_state['values']['re_subject']);
  360. $parent_type = trim($form_state['values']['parent_type']);
  361. $analysis_id = $form_state['values']['analysis_id'];
  362. if ($method == 0) {
  363. $method = 'Insert only';
  364. }
  365. if ($method == 1) {
  366. $method = 'Update only';
  367. }
  368. if ($method == 2) {
  369. $method = 'Insert and update';
  370. }
  371. if ($match_type == 0) {
  372. $match_type = 'Name';
  373. }
  374. if ($match_type == 1) {
  375. $match_type = 'Unique name';
  376. }
  377. $args = array($dfile, $organism_id, $type, $library_id, $re_name, $re_uname,
  378. $re_accession, $db_id, $rel_type, $re_subject, $parent_type, $method,
  379. $user->uid, $analysis_id, $match_type);
  380. tripal_add_job("Import FASTA file: $dfile", 'tripal_feature',
  381. 'tripal_feature_load_fasta', $args, $user->uid);
  382. }
  383. /**
  384. *
  385. *
  386. * @ingroup fasta_loader
  387. */
  388. function tripal_feature_load_fasta($dfile, $organism_id, $type,
  389. $library_id, $re_name, $re_uname, $re_accession, $db_id, $rel_type,
  390. $re_subject, $parent_type, $method, $uid, $analysis_id,
  391. $match_type, $job = NULL) {
  392. print "Opening FASTA file $dfile\n";
  393. $lines = file($dfile, FILE_SKIP_EMPTY_LINES);
  394. $i = 0;
  395. $name = '';
  396. $uname = '';
  397. $residues = '';
  398. $num_lines = sizeof($lines);
  399. $interval = intval($num_lines * 0.01);
  400. if ($interval == 0) {
  401. $interval = 1;
  402. }
  403. foreach ($lines as $line_num => $line) {
  404. $i++; // update the line count
  405. // update the job status every 1% features
  406. if ($job and $i % $interval == 0) {
  407. tripal_job_set_progress($job, intval(($i/$num_lines)*100));
  408. }
  409. // if we encounter a definition line then get the name, uniquename,
  410. // accession and relationship subject from the definition line
  411. if (preg_match('/^>/', $line)) {
  412. // if we have a feature name then we are starting a new sequence
  413. // so let's handle the previous one before moving on
  414. if ($name or $uname) {
  415. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  416. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $type,
  417. $source, $residues, $method, $re_name, $match_type);
  418. $residues = '';
  419. $name = '';
  420. $uname = '';
  421. }
  422. $line = preg_replace("/^>/", '', $line);
  423. // get the feature name
  424. if ($re_name) {
  425. if (!preg_match("/$re_name/", $line, $matches)) {
  426. print "WARNING: Regular expression for the feature name finds nothing\n";
  427. }
  428. $name = trim($matches[1]);
  429. }
  430. else {
  431. // if the match_type is name and no regular expression was provided
  432. // then use the first word as the name, otherwise we don't set the name
  433. if (strcmp($match_type, 'Name')==0) {
  434. preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches);
  435. $name = trim($matches[1]);
  436. }
  437. }
  438. // get the feature unique name
  439. if ($re_uname) {
  440. if (!preg_match("/$re_uname/", $line, $matches)) {
  441. print "WARNING: Regular expression for the feature unique name finds nothing\n";
  442. }
  443. $uname = trim($matches[1]);
  444. }
  445. else {
  446. // if the match_type is name and no regular expression was provided
  447. // then use the first word as the name, otherwise, we don't set the unqiuename
  448. if (strcmp($match_type, 'Unique name')==0) {
  449. preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches);
  450. $uname = trim($matches[1]);
  451. }
  452. }
  453. // get the accession
  454. preg_match("/$re_accession/", $line, $matches);
  455. $accession = trim($matches[1]);
  456. // get the relationship subject
  457. preg_match("/$re_subject/", $line, $matches);
  458. $subject = trim($matches[1]);
  459. }
  460. else {
  461. $residues .= trim($line);
  462. }
  463. }
  464. // now load the last sequence in the file
  465. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  466. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $type,
  467. $source, $residues, $method, $re_name, $match_type);
  468. return '';
  469. }
  470. /**
  471. *
  472. *
  473. * @ingroup fasta_loader
  474. */
  475. function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $accession,
  476. $parent, $rel_type, $parent_type, $analysis_id, $organism_id, $type,
  477. $source, $residues, $method, $re_name, $match_type) {
  478. $previous_db = tripal_db_set_active('chado');
  479. // first get the type for this sequence
  480. $cvtermsql = "SELECT CVT.cvterm_id
  481. FROM {cvterm} CVT
  482. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  483. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  484. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  485. $cvterm = db_fetch_object(db_query($cvtermsql, 'sequence', $type, $type));
  486. if (!$cvterm) {
  487. print "ERROR: cannot find the term type: '$type'\n";
  488. return 0;
  489. }
  490. // check to see if this feature already exists
  491. if (strcmp($match_type, 'Name')==0) {
  492. $cnt_sql = "SELECT count(*) as cnt FROM {feature}
  493. WHERE organism_id = %d and name = '%s' and type_id = %d";
  494. $cnt = db_fetch_object(db_query($cnt_sql, $organism_id, $name, $cvterm->cvterm_id));
  495. if ($cnt->cnt > 1) {
  496. print "ERROR: multiple features exist with the name '$name' of type '$type' for the organism. skipping\n";
  497. return 0;
  498. }
  499. else {
  500. $feature_sql = "SELECT * FROM {feature}
  501. WHERE organism_id = %d and name = '%s' and type_id = %d";
  502. $feature = db_fetch_object(db_query($feature_sql, $organism_id, $name, $cvterm->cvterm_id));
  503. }
  504. }
  505. if (strcmp($match_type, 'Unique name')==0) {
  506. $feature_sql = "SELECT * FROM {feature}
  507. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  508. $feature = db_fetch_object(db_query($feature_sql, $organism_id, $uname, $cvterm->cvterm_id));
  509. }
  510. if (!$feature and (strcmp($method, 'Insert only')==0 or strcmp($method, 'Insert and update')==0)) {
  511. // if we have a unique name but not a name then set them to be teh same
  512. // and vice versa
  513. if (!$uname) {
  514. $uname = $name;
  515. }
  516. elseif (!$name) {
  517. $name = $uname;
  518. }
  519. // now insert the feature
  520. $sql = "INSERT INTO {feature}
  521. (organism_id, name, uniquename, residues, seqlen,
  522. md5checksum,type_id,is_analysis,is_obsolete)
  523. VALUES(%d,'%s','%s','%s',%d, '%s', %d, %s, %s)";
  524. $result = db_query($sql, $organism_id, $name, $uname, $residues, drupal_strlen($residues),
  525. md5($residues), $cvterm->cvterm_id, 'false', 'false');
  526. if (!$result) {
  527. print "ERROR: failed to insert feature '$name ($uname)'\n";
  528. return 0;
  529. }
  530. else {
  531. print "Inserted feature $name ($uname)\n";
  532. }
  533. $feature = db_fetch_object(db_query($feature_sql, $organism_id, $uname, $cvterm->cvterm_id));
  534. }
  535. if (!$feature and (strcmp($method, 'Update only')==0 or drupal_strcmp($method, 'Insert and update')==0)) {
  536. print "WARNING: failed to find feature '$name' ('$uname') while matching on " . drupal_strtolower($match_type) . ". Skipping\n";
  537. return 0;
  538. }
  539. if ($feature and (strcmp($method, 'Update only')==0 or strcmp($method, 'Insert and update')==0)) {
  540. if (strcmp($method, 'Update only')==0 or strcmp($method, 'Insert and update')==0) {
  541. if (strcmp($match_type, 'Name')==0) {
  542. // if we're matching on the name but do not have a new unique name then we
  543. // don't want to update the uniquename. If we do have a uniquename then we
  544. // should update it. We only get a uniquename if there was a regular expression
  545. // provided for pulling it out
  546. if ($uname) {
  547. $sql = "UPDATE {feature}
  548. SET uniquename = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s'
  549. WHERE organism_id = %d and name = '%s' and type_id = %d";
  550. $result = db_query($sql, $uname, $residues, drupal_strlen($residues), md5($residues), $organism_id, $name, $cvterm->cvterm_id);
  551. }
  552. else {
  553. $sql = "UPDATE {feature}
  554. SET residues = '%s', seqlen = '%s', md5checksum = '%s'
  555. WHERE organism_id = %d and name = '%s' and type_id = %d";
  556. $result = db_query($sql, $residues, durpal_strlen($residues), md5($residues), $organism_id, $name, $cvterm->cvterm_id);
  557. }
  558. }
  559. else {
  560. // if we're matching on the unique name but do not have a new name then we
  561. // don't want to update the name. If we do have a name then we
  562. // should update it. We only get a name if there was a regular expression
  563. // provided for pulling it out
  564. if ($name) {
  565. $sql = "UPDATE {feature}
  566. SET name = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s'
  567. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  568. $result = db_query($sql, $name, $residues, drupal_strlen($residues), md5($residues), $organism_id, $uname, $cvterm->cvterm_id);
  569. }
  570. else {
  571. $sql = "UPDATE {feature}
  572. SET residues = '%s', seqlen = '%s', md5checksum = '%s'
  573. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  574. $result = db_query($sql, $residues, drupal_strlen($residues), md5($residues), $organism_id, $uname, $cvterm->cvterm_id);
  575. }
  576. }
  577. if (!$result) {
  578. print "ERROR: failed to update feature '$name ($uname)'\n";
  579. return 0;
  580. }
  581. else {
  582. print "Updated feature $name ($uname)\n";
  583. }
  584. }
  585. else {
  586. print "WARNING: feature already exists: '$name' ('$uname'). Skipping\n";
  587. }
  588. }
  589. // now get the feature
  590. $feature = db_fetch_object(db_query($feature_sql, $organism_id, $uname, $cvterm->cvterm_id));
  591. if (!$feature) {
  592. print "Something bad has happened: $organism_id, $uname, $cvterm->cvterm_id\n";
  593. return 0;
  594. }
  595. // add in the analysis link
  596. if ($analysis_id) {
  597. // @coder-ignore: non-drupal table thus table prefixing doesn't apply
  598. $analysis_link_sql = 'SELECT * FROM analysisfeature WHERE analysis_id=%d AND feature_id=%d';
  599. $analysis_link = db_fetch_object(db_query($analysis_link_sql, $analysis_id, $feature->feature_id));
  600. if (!$analysis_link) {
  601. // @coder-ignore: non-drupal table thus table prefixing doesn't apply
  602. $sql = "INSERT INTO analysisfeature (analysis_id, feature_id) VALUES (%d, %d)";
  603. $result = db_query($sql, $analysis_id, $feature->feature_id);
  604. if (!$result) {
  605. print "WARNING: could not add link between analysis: " . $analysis_id . " and feature: " . $feature->uniquename . "\n";
  606. }
  607. $analysis_link = db_fetch_object(db_query($analysis_link_sql, $analysis_id, $feature->feature_id));
  608. }
  609. }
  610. // now add the database cross reference
  611. if ($db_id) {
  612. // check to see if this accession reference exists, if not add it
  613. // @coder-ignore: non-drupal table thus table prefixing doesn't apply
  614. $dbxrefsql = "SELECT * FROM dbxref WHERE db_id = %d and accession = '%s'";
  615. $dbxref = db_fetch_object(db_query($dbxrefsql, $db_id, $accession));
  616. if (!$dbxref) {
  617. // @coder-ignore: non-drupal table thus table prefixing doesn't apply
  618. $sql = "INSERT INTO dbxref (db_id,accession) VALUES (%d, '%s')";
  619. $result = db_query($sql, $db_id, $accession);
  620. if (!$result) {
  621. print "WARNING: could not add external database acession: '$name accession: $accession'\n";
  622. }
  623. $dbxref = db_fetch_object(db_query($dbxrefsql, $db_id, $accession));
  624. }
  625. // check to see if the feature dbxref record exists if not, then add it
  626. $fdbxrefsql = "SELECT * FROM {feature_dbxref} WHERE feature_id = %d and dbxref_id = %d";
  627. $fdbxref = db_fetch_object(db_query($fdbxrefsql, $feature->feature_id, $dbxref->dbxref_id));
  628. if (!$fdbxref) {
  629. $sql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) VALUES (%d, %d)";
  630. $result = db_query($sql, $feature->feature_id, $dbxref->dbxref_id);
  631. if (!$result) {
  632. print "WARNING: could not associate database cross reference with feature: '$name accession: $accession'\n";
  633. }
  634. else {
  635. print "Added database crossreference $name ($uname) -> $accession\n";
  636. }
  637. }
  638. }
  639. // now add in the relationship if one exists. First, get the parent type for the relationship
  640. // then get the parent feature
  641. if ($rel_type) {
  642. $parentcvterm = db_fetch_object(db_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  643. $relcvterm = db_fetch_object(db_query($cvtermsql, 'relationship', $rel_type, $rel_type));
  644. $parent_feature = db_fetch_object(db_query($feature_sql, $organism_id, $parent, $parentcvterm->cvterm_id));
  645. if ($parent_feature) {
  646. // check to see if the relationship already exists
  647. $sql = "SELECT * FROM {feature_relationship} WHERE subject_id = %d and object_id = %d and type_id = %d";
  648. $rel = db_fetch_object(db_query($sql, $feature->feature_id, $parent_feature->feature_id, $relcvterm->cvterm_id));
  649. if ($rel) {
  650. print "WARNING: relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  651. }
  652. else {
  653. $sql = "INSERT INTO {feature_relationship} (subject_id,object_id,type_id)
  654. VALUES (%d,%d,%d)";
  655. $result = db_query($sql, $feature->feature_id, $parent_feature->feature_id, $relcvterm->cvterm_id);
  656. if (!$result) {
  657. print "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  658. }
  659. else {
  660. print "Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  661. }
  662. }
  663. }
  664. else {
  665. print "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n";
  666. }
  667. }
  668. tripal_db_set_active($previous_db);
  669. }