fasta_loader.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. $org_rset = chado_query($sql);
  30. $organisms = array();
  31. $organisms[''] = '';
  32. while ($organism = db_fetch_object($org_rset)) {
  33. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  34. }
  35. $form['organism_id'] = array(
  36. '#title' => t('Organism'),
  37. '#type' => t('select'),
  38. '#description' => t("Choose the organism to which these sequences are associated"),
  39. '#required' => TRUE,
  40. '#options' => $organisms,
  41. );
  42. $form['seqtype']= array(
  43. '#type' => 'textfield',
  44. '#title' => t('Sequence Type'),
  45. '#required' => TRUE,
  46. '#description' => t('Please enter the Sequence Ontology term that describes the sequences in the FASTA file.'),
  47. );
  48. // get the list of organisms
  49. $sql = "SELECT L.library_id, L.name, CVT.name as type
  50. FROM {library} L
  51. INNER JOIN {cvterm} CVT ON L.type_id = CVT.cvterm_id
  52. ORDER BY name";
  53. $lib_rset = chado_query($sql);
  54. $libraries = array();
  55. $libraries[''] = '';
  56. while ($library = db_fetch_object($lib_rset)) {
  57. $libraries[$library->library_id] = "$library->name ($library->type)";
  58. }
  59. // $form['library_id'] = array (
  60. // '#title' => t('Library'),
  61. // '#type' => t('select'),
  62. // '#description' => t("Choose the library to which these sequences are associated "),
  63. // '#required' => FALSE,
  64. // '#options' => $libraries,
  65. // '#weight' => 5,
  66. // );
  67. $form['method']= array(
  68. '#type' => 'radios',
  69. '#title' => 'Method',
  70. '#required' => TRUE,
  71. '#options' => array(
  72. t('Insert only'),
  73. t('Update only'),
  74. t('Insert and update'),
  75. ),
  76. '#description' => t('Select how features in the FASTA file are handled.
  77. Select "Insert only" to insert the new features. If a feature already
  78. exists with the same name or unique name and type then it is skipped.
  79. Select "Update only" to only update featues that already exist in the
  80. database. Select "Insert and Update" to insert features that do
  81. not exist and upate those that do.'),
  82. '#default_value' => 2,
  83. );
  84. $form['match_type']= array(
  85. '#type' => 'radios',
  86. '#title' => 'Name Match Type',
  87. '#required' => TRUE,
  88. '#options' => array(
  89. t('Name'),
  90. t('Unique name'),
  91. ),
  92. '#description' => t('Feature data is stored in Chado with both a human-readable
  93. name and a unique name. If the features in your FASTA file are identified using
  94. a human-readable name then select the "Name" button. If your features are
  95. identified using the unique name then select the "Unique name" button. If you
  96. loaded your features first using the GFF loader then the unique name of each
  97. features were indicated by the "ID=" attribute and the name by the "Name=" attribute.
  98. By default, the FASTA loader will use the first word (character string
  99. before the first space) as the name for your feature. If
  100. this does not uniquely identify your feature consider specifying a regular expression in the advanced section below.
  101. Additionally, you may import both a name and a unique name for each sequence using the advanced options.
  102. When updating a sequence, the value selected here will be used to identify the sequence in the
  103. database in combination with any regular expression provided below.'),
  104. '#default_value' => 1,
  105. );
  106. $form['analysis'] = array(
  107. '#type' => 'fieldset',
  108. '#title' => t('Analysis Used to Derive Features'),
  109. '#collapsed' => TRUE
  110. );
  111. $form['analysis']['desc'] = array(
  112. '#type' => 'markup',
  113. '#value' => t("Why specify an analysis for a data load? All data comes
  114. from some place, even if downloaded from Genbank. By specifying
  115. analysis details for all data uploads, it allows an end user to reproduce the
  116. data set, but at least indicates the source of the data."),
  117. );
  118. // get the list of organisms
  119. $sql = "SELECT * FROM {analysis} ORDER BY name";
  120. $org_rset = chado_query($sql);
  121. $analyses = array();
  122. $analyses[''] = '';
  123. while ($analysis = db_fetch_object($org_rset)) {
  124. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  125. }
  126. $form['analysis']['analysis_id'] = array(
  127. '#title' => t('Analysis'),
  128. '#type' => t('select'),
  129. '#description' => t("Choose the analysis to which these features are associated"),
  130. '#required' => TRUE,
  131. '#options' => $analyses,
  132. );
  133. // Advanced Options
  134. $form['advanced'] = array(
  135. '#type' => 'fieldset',
  136. '#title' => t('Advanced Options'),
  137. '#collapsible' => TRUE,
  138. '#collapsed' => TRUE
  139. );
  140. $form['advanced']['re_help']= array(
  141. '#type' => 'item',
  142. '#value' => t('A regular expression is an advanced method for extracting information from a string of text.
  143. Your FASTA file may contain both a human-readable name and a unique name for each sequence.
  144. If you want to import
  145. both the name and unique name for all sequences, then you must provide regular expressions
  146. so that the loader knows how to separate them.
  147. Otherwise the name and uniquename will be the same.
  148. By default, this loader will use the first word in the definition
  149. lines of the FASTA file
  150. as the name or unique name of the feature.'),
  151. );
  152. $form['advanced']['re_name']= array(
  153. '#type' => 'textfield',
  154. '#title' => t('Regular expression for the name'),
  155. '#required' => FALSE,
  156. '#description' => t('Enter the regular expression that will extract the
  157. feature name from the FASTA definition line. For example, for a
  158. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  159. the regular expression for the name would be, "^(.*?)\|.*$".'),
  160. );
  161. $form['advanced']['re_uname']= array(
  162. '#type' => 'textfield',
  163. '#title' => t('Regular expression for the unique name'),
  164. '#required' => FALSE,
  165. '#description' => t('Enter the regular expression that will extract the
  166. feature name from the FASTA definition line. For example, for a
  167. defintion line with a name and unique name separated by a bar \'|\' (>seqname|uniquename),
  168. the regular expression for the unique name would be "^.*?\|(.*)$").'),
  169. );
  170. // Advanced database cross-reference optoins
  171. $form['advanced']['db'] = array(
  172. '#type' => 'fieldset',
  173. '#title' => t('External Database Reference'),
  174. '#weight' => 6,
  175. '#collapsed' => TRUE
  176. );
  177. $form['advanced']['db']['re_accession']= array(
  178. '#type' => 'textfield',
  179. '#title' => t('Regular expression for the accession'),
  180. '#required' => FALSE,
  181. '#description' => t('Enter the regular expression that will extract the accession for the external database for each feature from the FASTA definition line.'),
  182. '#weight' => 2
  183. );
  184. // get the list of databases
  185. $sql = "SELECT * FROM {db} ORDER BY name";
  186. $db_rset = chado_query($sql);
  187. $dbs = array();
  188. $dbs[''] = '';
  189. while ($db = db_fetch_object($db_rset)) {
  190. $dbs[$db->db_id] = "$db->name";
  191. }
  192. $form['advanced']['db']['db_id'] = array(
  193. '#title' => t('External Database'),
  194. '#type' => t('select'),
  195. '#description' => t("Plese choose an external database for which these sequences have a cross reference."),
  196. '#required' => FALSE,
  197. '#options' => $dbs,
  198. '#weight' => 1,
  199. );
  200. $form['advanced']['relationship'] = array(
  201. '#type' => 'fieldset',
  202. '#title' => t('Relationships'),
  203. '#weight' => 6,
  204. '#collapsed' => TRUE
  205. );
  206. $rels = array();
  207. $rels[''] = '';
  208. $rels['part_of'] = 'part of';
  209. $rels['derives_from'] = 'produced by';
  210. // Advanced references options
  211. $form['advanced']['relationship']['rel_type']= array(
  212. '#title' => t('Relationship Type'),
  213. '#type' => t('select'),
  214. '#description' => t("Use this option to create associations, or relationships between the
  215. features of this FASTA file and existing features in the database. For
  216. example, to associate a FASTA file of peptides to existing genes or transcript sequence,
  217. select the type 'produced by'. For a CDS sequences select the type 'part of'"),
  218. '#required' => FALSE,
  219. '#options' => $rels,
  220. '#weight' => 5,
  221. );
  222. $form['advanced']['relationship']['re_subject']= array(
  223. '#type' => 'textfield',
  224. '#title' => t('Regular expression for the parent'),
  225. '#required' => FALSE,
  226. '#description' => t('Enter the regular expression that will extract the unique
  227. name needed to identify the existing sequence for which the
  228. relationship type selected above will apply.'),
  229. '#weight' => 6
  230. );
  231. $form['advanced']['relationship']['parent_type']= array(
  232. '#type' => 'textfield',
  233. '#title' => t('Parent Type'),
  234. '#required' => FALSE,
  235. '#description' => t('Please enter the Sequence Ontology term for the parent. For example
  236. if the FASTA file being loaded is a set of proteins that are
  237. products of genes, then use the SO term \'gene\' or \'transcript\' or equivalent. However,
  238. this type must match the type for already loaded features.'),
  239. '#weight' => 7
  240. );
  241. $form['button'] = array(
  242. '#type' => 'submit',
  243. '#value' => t('Import FASTA file'),
  244. '#weight' => 10,
  245. );
  246. return $form;
  247. }
  248. /**
  249. *
  250. *
  251. * @ingroup fasta_loader
  252. */
  253. function tripal_feature_fasta_load_form_validate($form, &$form_state) {
  254. $fasta_file = trim($form_state['values']['fasta_file']);
  255. $organism_id = $form_state['values']['organism_id'];
  256. $type = trim($form_state['values']['seqtype']);
  257. $method = trim($form_state['values']['method']);
  258. $match_type = trim($form_state['values']['match_type']);
  259. $library_id = $form_state['values']['library_id'];
  260. $re_name = trim($form_state['values']['re_name']);
  261. $re_uname = trim($form_state['values']['re_uname']);
  262. $re_accession = trim($form_state['values']['re_accession']);
  263. $db_id = $form_state['values']['db_id'];
  264. $rel_type = $form_state['values']['rel_type'];
  265. $re_subject = trim($form_state['values']['re_subject']);
  266. $parent_type = trim($form_state['values']['parent_type']);
  267. if ($method == 0) {
  268. $method = 'Insert only';
  269. }
  270. if ($method == 1) {
  271. $method = 'Update only';
  272. }
  273. if ($method == 2) {
  274. $method = 'Insert and update';
  275. }
  276. if ($match_type == 0) {
  277. $match_type = 'Name';
  278. }
  279. if ($match_type == 1) {
  280. $match_type = 'Unique name';
  281. }
  282. if ($re_name and !$re_uname and strcmp($match_type, 'Unique name')==0) {
  283. form_set_error('re_uname', t("You must provide a regular expression to identify the sequence unique name"));
  284. }
  285. if (!$re_name and $re_uname and strcmp($match_type, 'Name')==0) {
  286. form_set_error('re_name', t("You must provide a regular expression to identify the sequence name"));
  287. }
  288. // check to see if the file is located local to Drupal
  289. $fasta_file = trim($fasta_file);
  290. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $fasta_file;
  291. if (!file_exists($dfile)) {
  292. // if not local to Drupal, the file must be someplace else, just use
  293. // the full path provided
  294. $dfile = $fasta_file;
  295. }
  296. if (!file_exists($dfile)) {
  297. 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."));
  298. }
  299. // make sure if a relationship is specified that all fields are provided.
  300. if (($rel_type or $parent_type) and !$re_subject) {
  301. form_set_error('re_subject', t("Please provide a regular expression for the parent"));
  302. }
  303. if (($rel_type or $re_subject) and !$parent_type) {
  304. form_set_error('parent_type', t("Please provide a SO term for the parent"));
  305. }
  306. if (($parent_type or $re_subject) and !$rel_type) {
  307. form_set_error('rel_type', t("Please select a relationship type"));
  308. }
  309. // make sure if a database is specified that all fields are provided
  310. if ($db_id and !$re_accession) {
  311. form_set_error('re_accession', t("Please provide a regular expression for the accession"));
  312. }
  313. if ($re_accession and !$db_id) {
  314. form_set_error('db_id', t("Please select a database"));
  315. }
  316. // check to make sure the types exists
  317. $cvtermsql = "SELECT CVT.cvterm_id
  318. FROM {cvterm} CVT
  319. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  320. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  321. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  322. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $type, $type));
  323. if (!$cvterm) {
  324. 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."));
  325. }
  326. if ($rel_type) {
  327. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  328. if (!$cvterm) {
  329. 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."));
  330. }
  331. }
  332. // check to make sure the 'relationship' and 'sequence' ontologies are loaded
  333. $form_state['storage']['dfile'] = $dfile;
  334. }
  335. /**
  336. *
  337. *
  338. * @ingroup fasta_loader
  339. */
  340. function tripal_feature_fasta_load_form_submit($form, &$form_state) {
  341. global $user;
  342. $dfile = $form_state['storage']['dfile'];
  343. $organism_id = $form_state['values']['organism_id'];
  344. $type = trim($form_state['values']['seqtype']);
  345. $method = trim($form_state['values']['method']);
  346. $match_type = trim($form_state['values']['match_type']);
  347. $library_id = $form_state['values']['library_id'];
  348. $re_name = trim($form_state['values']['re_name']);
  349. $re_uname = trim($form_state['values']['re_uname']);
  350. $re_accession = trim($form_state['values']['re_accession']);
  351. $db_id = $form_state['values']['db_id'];
  352. $rel_type = $form_state['values']['rel_type'];
  353. $re_subject = trim($form_state['values']['re_subject']);
  354. $parent_type = trim($form_state['values']['parent_type']);
  355. $analysis_id = $form_state['values']['analysis_id'];
  356. if ($method == 0) {
  357. $method = 'Insert only';
  358. }
  359. if ($method == 1) {
  360. $method = 'Update only';
  361. }
  362. if ($method == 2) {
  363. $method = 'Insert and update';
  364. }
  365. if ($match_type == 0) {
  366. $match_type = 'Name';
  367. }
  368. if ($match_type == 1) {
  369. $match_type = 'Unique name';
  370. }
  371. $args = array($dfile, $organism_id, $type, $library_id, $re_name, $re_uname,
  372. $re_accession, $db_id, $rel_type, $re_subject, $parent_type, $method,
  373. $user->uid, $analysis_id, $match_type);
  374. $fname = preg_replace("/.*\/(.*)/", "$1", $dfile);
  375. tripal_add_job("Import FASTA file: $fname", 'tripal_feature',
  376. 'tripal_feature_load_fasta', $args, $user->uid);
  377. }
  378. /**
  379. *
  380. *
  381. * @ingroup fasta_loader
  382. */
  383. function tripal_feature_load_fasta($dfile, $organism_id, $type,
  384. $library_id, $re_name, $re_uname, $re_accession, $db_id, $rel_type,
  385. $re_subject, $parent_type, $method, $uid, $analysis_id,
  386. $match_type, $job = NULL) {
  387. // begin the transaction
  388. $connection = tripal_db_start_transaction();
  389. // if we cannot get a connection then let the user know the loading will be slow
  390. if (!$connection) {
  391. print "A persistant connection was not obtained. Loading will be slow\n";
  392. }
  393. else {
  394. print "\nNOTE: Loading of this FASTA file is performed using a database transaction. \n" .
  395. "If the load fails or is terminated prematurely then the entire set of \n" .
  396. "insertions/updates is rolled back and will not be found in the database\n\n";
  397. }
  398. // first get the type for this sequence
  399. $cvtermsql = "SELECT CVT.cvterm_id
  400. FROM {cvterm} CVT
  401. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  402. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  403. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  404. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $type, $type));
  405. if (!$cvterm) {
  406. watchdog("T_fasta_loader", "Cannot find the term type: '%type'", array('%type' => $type), WATCHDOG_ERROR);
  407. return 0;
  408. }
  409. if ($parent_type) {
  410. $parentcvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  411. if (!$parentcvterm) {
  412. watchdog("T_fasta_loader", "Cannot find the paretne term type: '%type'", array('%type' => $parentcvterm), WATCHDOG_ERROR);
  413. return 0;
  414. }
  415. }
  416. if ($rel_type) {
  417. $relcvterm = db_fetch_object(chado_query($cvtermsql, 'relationship', $rel_type, $rel_type));
  418. if (!$relcvterm) {
  419. watchdog("T_fasta_loader", "Cannot find the relationship term type: '%type'", array('%type' => $relcvterm), WATCHDOG_ERROR);
  420. return 0;
  421. }
  422. }
  423. print "Opening FASTA file $dfile\n";
  424. //$lines = file($dfile, FILE_SKIP_EMPTY_LINES);
  425. $fh = fopen($dfile, 'r');
  426. if (!$fh) {
  427. watchdog('T_fasta_loader', "cannot open file: %dfile", array('%dfile' => $dfile), WATCHDOG_ERROR);
  428. return 0;
  429. }
  430. $filesize = filesize($dfile);
  431. $i = 0;
  432. $name = '';
  433. $uname = '';
  434. $residues = '';
  435. $interval = intval($filesize * 0.01);
  436. if ($interval < 1) {
  437. $interval = 1;
  438. }
  439. $inv_read = 0;
  440. //foreach ($lines as $line_num => $line) {
  441. while ($line = fgets($fh)) {
  442. $i++; // update the line count
  443. $num_read += drupal_strlen($line);
  444. $intv_read += drupal_strlen($line);
  445. // if we encounter a definition line then get the name, uniquename,
  446. // accession and relationship subject from the definition line
  447. if (preg_match('/^>/', $line)) {
  448. // if we have a feature name then we are starting a new sequence
  449. // so lets handle the previous one before moving on
  450. if ($name or $uname) {
  451. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  452. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  453. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
  454. $residues = '';
  455. $name = '';
  456. $uname = '';
  457. }
  458. $line = preg_replace("/^>/", '', $line);
  459. // get the feature name
  460. if ($re_name) {
  461. if (!preg_match("/$re_name/", $line, $matches)) {
  462. print "WARNING: Regular expression for the feature name finds nothing\n";
  463. }
  464. $name = trim($matches[1]);
  465. }
  466. else {
  467. // if the match_type is name and no regular expression was provided
  468. // then use the first word as the name, otherwise we don't set the name
  469. if (strcmp($match_type, 'Name')==0) {
  470. preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches);
  471. $name = trim($matches[1]);
  472. }
  473. }
  474. // get the feature unique name
  475. if ($re_uname) {
  476. if (!preg_match("/$re_uname/", $line, $matches)) {
  477. print "WARNING: Regular expression for the feature unique name finds nothing\n";
  478. }
  479. $uname = trim($matches[1]);
  480. }
  481. else {
  482. // if the match_type is name and no regular expression was provided
  483. // then use the first word as the name, otherwise, we don't set the unqiuename
  484. if (strcmp($match_type, 'Unique name')==0) {
  485. preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches);
  486. $uname = trim($matches[1]);
  487. }
  488. }
  489. // get the accession
  490. preg_match("/$re_accession/", $line, $matches);
  491. $accession = trim($matches[1]);
  492. // get the relationship subject
  493. preg_match("/$re_subject/", $line, $matches);
  494. $subject = trim($matches[1]);
  495. }
  496. else {
  497. $residues .= trim($line);
  498. // update the job status every % features
  499. if ($job and $intv_read >= $interval) {
  500. $intv_read = 0;
  501. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  502. if ($name) {
  503. print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Parsing: $name\r";
  504. }
  505. else {
  506. print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Parsing: $uname\r";
  507. }
  508. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  509. }
  510. }
  511. }
  512. // now load the last sequence in the file
  513. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  514. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  515. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
  516. // commit the transaction
  517. tripal_db_commit_transaction();
  518. print "Done\n";
  519. }
  520. /**
  521. *
  522. *
  523. * @ingroup fasta_loader
  524. */
  525. function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $accession,
  526. $parent, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  527. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm) {
  528. // check to see if this feature already exists if the match_type is 'Name'
  529. if (strcmp($match_type, 'Name')==0) {
  530. $values = array(
  531. 'organism_id' => $organism_id,
  532. 'name' => $name,
  533. 'type_id' => $cvterm->cvterm_id,
  534. );
  535. $options = array('statement_name' => 'sel_feature_ornaty');
  536. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  537. if (count($results) > 1) {
  538. watchdog('T_fasta_loader', "Multiple features exist with the name '%name' of type
  539. '%type' for the organism. skipping", array('%name' => $name, '%type' => $type));
  540. return 0;
  541. }
  542. if (count($results) == 1) {
  543. $feature = $results[0];
  544. }
  545. }
  546. // check to see if this feature already exists if the match_type is 'Unique Name'
  547. if (strcmp($match_type, 'Unique name')==0) {
  548. $values = array(
  549. 'organism_id' => $organism_id,
  550. 'uniquename' => $uname,
  551. 'type_id' => $cvterm->cvterm_id,
  552. );
  553. $options = array('statement_name' => 'sel_feature_oruqty');
  554. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  555. if (count($results) > 1) {
  556. watchdog('T_fasta_loader', "Multiple features exist with the name '%name' of type
  557. '%type' for the organism. skipping", array('%name' => $name, '%type' => $type));
  558. return 0;
  559. }
  560. if (count($results) == 1) {
  561. $feature = $results[0];
  562. }
  563. }
  564. // if we don't have a feature and we're doing an insert then do the insert
  565. $inserted = 0;
  566. if (!$feature and (strcmp($method, 'Insert only')==0 or strcmp($method, 'Insert and update')==0)) {
  567. // if we have a unique name but not a name then set them to be the same
  568. // and vice versa
  569. if (!$uname) {
  570. $uname = $name;
  571. }
  572. elseif (!$name) {
  573. $name = $uname;
  574. }
  575. // insert the feature
  576. $values = array(
  577. 'organism_id' => $organism_id,
  578. 'name' => $name,
  579. 'uniquename' => $uname,
  580. 'residues' => $residues,
  581. 'seqlen' => drupal_strlen($residues),
  582. 'md5checksum' => md5($residues),
  583. 'type_id' => $cvterm->cvterm_id,
  584. 'is_analysis' => 'FALSE',
  585. 'is_obsolete' => 'FALSE',
  586. );
  587. $options = array('statement_name' => 'ins_feature_all');
  588. $success = tripal_core_chado_insert('feature', $values, $options);
  589. if (!$success) {
  590. watchdog('T_fasta_loader', "Failed to insert feature '%name (%uname)'",
  591. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  592. return 0;
  593. }
  594. // now get the feature we just inserted
  595. $values = array(
  596. 'organism_id' => $organism_id,
  597. 'uniquename' => $uname,
  598. 'type_id' => $cvterm->cvterm_id,
  599. );
  600. $options = array('statement_name' => 'sel_feature_oruqty');
  601. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  602. if (count($results) == 1) {
  603. $inserted = 1;
  604. $feature = $results[0];
  605. }
  606. else {
  607. watchdog('T_fasta_loader', "Failed to retreive newly inserted feature '%name (%uname)'",
  608. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  609. return 0;
  610. }
  611. }
  612. // if we don't have a feature and the uesr wants to do an update then fail
  613. if (!$feature and (strcmp($method, 'Update only')==0 or drupal_strcmp($method, 'Insert and update')==0)) {
  614. watchdog('T_fasta_loader', "Failed to find feature '%name' ('%uiname') while matching on " .
  615. drupal_strtolower($match_type), array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  616. return 0;
  617. }
  618. // if we do have a feature and this is an update then proceed with the update
  619. if ($feature and !$inserted and (strcmp($method, 'Update only')==0 or strcmp($method, 'Insert and update')==0)) {
  620. // if the user wants to match on the Name field
  621. if (strcmp($match_type, 'Name')==0) {
  622. // if we're matching on the name but do not have a new unique name then we don't want to update the uniquename.
  623. $values = array();
  624. if ($uname) {
  625. $values = array(
  626. 'uniquename' => $uname,
  627. 'residues' => $residues,
  628. 'seqlen' => drupal_strlen($residues),
  629. 'md5checksum' => md5($residues),
  630. 'is_analysis' => 'false',
  631. 'is_obsolete' => 'false',
  632. );
  633. $match = array(
  634. 'name' => $name,
  635. 'organism_id' => $organism_id,
  636. 'type_id' => $cvterm->cvterm_id,
  637. );
  638. $options = array('statement_name' => 'upd_feature_resemdisis_naorty_un');
  639. }
  640. // if we have a unique name then update it after matching by the name
  641. else {
  642. $values = array(
  643. 'residues' => $residues,
  644. 'seqlen' => drupal_strlen($residues),
  645. 'md5checksum' => md5($residues),
  646. 'is_analysis' => 'false',
  647. 'is_obsolete' => 'false',
  648. );
  649. $match = array(
  650. 'name' => $name,
  651. 'organism_id' => $organism_id,
  652. 'type_id' => $cvterm->cvterm_id,
  653. );
  654. $options = array('statement_name' => 'upd_feature_unresemdisis_naorty');
  655. }
  656. $success = tripal_core_chado_update('feature', $match, $values, $options);
  657. if (!$success) {
  658. watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')",
  659. array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  660. return 0;
  661. }
  662. }
  663. if (strcmp($match_type, 'Unique name')==0) {
  664. // if we're matching on the uniquename but do not have a new name then we don't want to update the name.
  665. $values = array();
  666. if ($name) {
  667. $values = array(
  668. 'name' => $name,
  669. 'residues' => $residues,
  670. 'seqlen' => drupal_strlen($residues),
  671. 'md5checksum' => md5($residues),
  672. 'is_analysis' => 'false',
  673. 'is_obsolete' => 'false',
  674. );
  675. $match = array(
  676. 'uniquename' => $uname,
  677. 'organism_id' => $organism_id,
  678. 'type_id' => $cvterm->cvterm_id,
  679. );
  680. $options = array('statement_name' => 'upd_feature_resemdisis_unorty_na');
  681. }
  682. // if we have a unique name then update it after matching by the name
  683. else {
  684. $values = array(
  685. 'residues' => $residues,
  686. 'seqlen' => drupal_strlen($residues),
  687. 'md5checksum' => md5($residues),
  688. 'is_analysis' => 'false',
  689. 'is_obsolete' => 'false',
  690. );
  691. $match = array(
  692. 'uniquename' => $uname,
  693. 'organism_id' => $organism_id,
  694. 'type_id' => $cvterm->cvterm_id,
  695. );
  696. $options = array('statement_name' => 'upd_feature_naresemdisis_unorty');
  697. }
  698. $success = tripal_core_chado_update('feature', $match, $values, $options);
  699. if (!$success) {
  700. watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')",
  701. array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  702. return 0;
  703. }
  704. }
  705. }
  706. // add in the analysis link
  707. if ($analysis_id) {
  708. // if the association doens't alredy exist then add one
  709. $values = array(
  710. 'analysis_id' => $analysis_id,
  711. 'feature_id' => $feature->feature_id,
  712. );
  713. $sel_options = array('statement_name' => 'sel_analysisfeature_anfe');
  714. $results = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $values, $sel_options);
  715. if (count($results) == 0) {
  716. $ins_options = array('statement_name' => 'ins_analysisfeature_anfe');
  717. $success = tripal_core_chado_insert('analysisfeature', $values, $ins_options);
  718. if (!$success) {
  719. watchdog('T_fasta_loader', "Failed to associate analysis and feature '%name' ('%name')",
  720. array('%name' => $name, '%uname' => $uname), WATCHDOG_ERROR);
  721. return 0;
  722. }
  723. }
  724. }
  725. // now add the database cross reference
  726. if ($db_id) {
  727. // check to see if this accession reference exists, if not add it
  728. $values = array(
  729. 'db_id' => $db_id,
  730. 'accession' => $accession
  731. );
  732. $sel_options = array('statement_name' => 'sel_dbxref_dbac');
  733. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
  734. // if the accession doesn't exist then add it
  735. if (count($results) == 0) {
  736. $ins_options = array('statement_name' => 'ins_dbxref_dbac');
  737. $results = tripal_core_chado_insert('dbxref', $values, $ins_options);
  738. if (!$results) {
  739. watchdog('T_fasta_loader', "Failed to add database accession '%accession'",
  740. array('%accession' => $accession), WATCHDOG_ERROR);
  741. return 0;
  742. }
  743. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
  744. if (count($results) == 1) {
  745. $dbxref = $results[0];
  746. }
  747. else {
  748. watchdog('T_fasta_loader', "Failed to retreive newly inserted dbxref '%name (%uname)'",
  749. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  750. return 0;
  751. }
  752. }
  753. // check to see if the feature dbxref record exists if not, then add it
  754. $values = array(
  755. 'feature_id' => $feature->feature_id,
  756. 'dbxref_id' => $dbxref->dbxref_id
  757. );
  758. $sel_options = array('statement_name' => 'sel_featuredbxref_fedb');
  759. $results = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $sel_options);
  760. if (count($results) == 0) {
  761. $ins_options = array('statement_name' => 'ins_featuredbxref_fedb');
  762. $success = tripal_core_chado_insert('feature_dbxref', $values, $ins_options);
  763. if (!$success) {
  764. watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature",
  765. array('%accession' => $accession), WATCHDOG_ERROR);
  766. return 0;
  767. }
  768. }
  769. }
  770. // now add in the relationship if one exists. If not, then add it
  771. if ($rel_type) {
  772. $values = array(
  773. 'organism_id' => $organism_id,
  774. 'uniquename' => $parent,
  775. 'type_id' => $parentcvterm->cvterm_id,
  776. );
  777. $options = array('statement_name' => 'sel_feature_oruqty');
  778. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  779. if (count($results) != 1) {
  780. watchdog('T_fasta_loader', "Cannot find a unique fature for the parent '%parent' of type
  781. '%type' for the feature.", array('%parent' => $parent, '%type' => $parent_type));
  782. return 0;
  783. }
  784. $parent_feature = $results[0];
  785. // check to see if the relationship already exists if not then add it
  786. $values = array(
  787. 'subject_id' => $feature->feature_id,
  788. 'ojbect_id' => $parent_feature->feature_id,
  789. 'type_id' => $relcvterm->cvterm_id,
  790. );
  791. $sel_options = array('statement_name' => 'sel_featurerelationship_suojty');
  792. $results = tripal_core_chado_select('feature_relationship', array('feature_relationships_id'), $values, $sel_options);
  793. if (count($results) == 0) {
  794. $ins_options = array('statement_name' => 'sel_featurerelationship_suojty');
  795. $success = tripal_core_chado_insert('feature_relationship', $values, $ins_options);
  796. if (!$success) {
  797. watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature",
  798. array('%accession' => $accession), WATCHDOG_ERROR);
  799. return 0;
  800. }
  801. }
  802. }
  803. }