fasta_loader.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup fasta_loader FASTA Feature Loader
  8. * @ingroup tripal_feature
  9. * @{
  10. * Provides fasta loading functionality. Creates features based on their specification in a fasta file.
  11. * @}
  12. *
  13. */
  14. /**
  15. *
  16. *
  17. * @ingroup fasta_loader
  18. */
  19. function tripal_feature_fasta_load_form( ) {
  20. $form['fasta_file']= array(
  21. '#type' => 'textfield',
  22. '#title' => t('FASTA File'),
  23. '#description' => t('Please enter the full system path for the FASTA file, or a path within the Drupal
  24. installation (e.g. /sites/default/files/xyz.obo). The path must be accessible to the
  25. server on which this Drupal instance is running.'),
  26. '#required' => TRUE,
  27. );
  28. // get the list of organisms
  29. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  30. $org_rset = chado_query($sql);
  31. $organisms = array();
  32. $organisms[''] = '';
  33. while ($organism = db_fetch_object($org_rset)) {
  34. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  35. }
  36. $form['organism_id'] = array(
  37. '#title' => t('Organism'),
  38. '#type' => t('select'),
  39. '#description' => t("Choose the organism to which these sequences are associated"),
  40. '#required' => TRUE,
  41. '#options' => $organisms,
  42. );
  43. $form['seqtype']= array(
  44. '#type' => 'textfield',
  45. '#title' => t('Sequence Type'),
  46. '#required' => TRUE,
  47. '#description' => t('Please enter the Sequence Ontology (SO) term name that describes the sequences in the FASTA file (e.g. gene, mRNA, protein, etc...)'),
  48. );
  49. // get the list of organisms
  50. $sql = "SELECT L.library_id, L.name, CVT.name as type
  51. FROM {library} L
  52. INNER JOIN {cvterm} CVT ON L.type_id = CVT.cvterm_id
  53. ORDER BY name";
  54. $lib_rset = chado_query($sql);
  55. $libraries = array();
  56. $libraries[''] = '';
  57. while ($library = db_fetch_object($lib_rset)) {
  58. $libraries[$library->library_id] = "$library->name ($library->type)";
  59. }
  60. // $form['library_id'] = array (
  61. // '#title' => t('Library'),
  62. // '#type' => t('select'),
  63. // '#description' => t("Choose the library to which these sequences are associated "),
  64. // '#required' => FALSE,
  65. // '#options' => $libraries,
  66. // '#weight' => 5,
  67. // );
  68. $form['method']= array(
  69. '#type' => 'radios',
  70. '#title' => 'Method',
  71. '#required' => TRUE,
  72. '#options' => array(
  73. t('Insert only'),
  74. t('Update only'),
  75. t('Insert and update'),
  76. ),
  77. '#description' => t('Select how features in the FASTA file are handled.
  78. Select "Insert only" to insert the new features. If a feature already
  79. exists with the same name or unique name and type then it is skipped.
  80. Select "Update only" to only update featues that already exist in the
  81. database. Select "Insert and Update" to insert features that do
  82. not exist and upate those that do.'),
  83. '#default_value' => 2,
  84. );
  85. $form['match_type']= array(
  86. '#type' => 'radios',
  87. '#title' => 'Name Match Type',
  88. '#required' => TRUE,
  89. '#options' => array(
  90. t('Name'),
  91. t('Unique name'),
  92. ),
  93. '#description' => t('Used for "updates only" or "insert and update" methods. Not required if method type is "insert".
  94. Feature data is stored in Chado with both a human-readable
  95. name and a unique name. If the features in your FASTA file are uniquely identified using
  96. a human-readable name then select the "Name" button. If your features are
  97. uniquely identified using the unique name then select the "Unique name" button. If you
  98. loaded your features first using the GFF loader then the unique name of each
  99. features were indicated by the "ID=" attribute and the name by the "Name=" attribute.
  100. By default, the FASTA loader will use the first word (character string
  101. before the first space) as the name for your feature. If
  102. this does not uniquely identify your feature consider specifying a regular expression in the advanced section below.
  103. Additionally, you may import both a name and a unique name for each sequence using the advanced options.'),
  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. // we need to get the table schema to make sure we don't overrun the
  441. // size of fields with what our regular expressions retrieve
  442. $feature_tbl = tripal_core_get_chado_table_schema('feature');
  443. $dbxref_tbl = tripal_core_get_chado_table_schema('dbxref');
  444. //foreach ($lines as $line_num => $line) {
  445. while ($line = fgets($fh)) {
  446. $i++; // update the line count
  447. $num_read += drupal_strlen($line);
  448. $intv_read += drupal_strlen($line);
  449. // if we encounter a definition line then get the name, uniquename,
  450. // accession and relationship subject from the definition line
  451. if (preg_match('/^>/', $line)) {
  452. // if we have a feature name then we are starting a new sequence
  453. // so lets handle the previous one before moving on
  454. if ($name or $uname) {
  455. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  456. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  457. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
  458. $residues = '';
  459. $name = '';
  460. $uname = '';
  461. }
  462. $line = preg_replace("/^>/", '', $line); // remove the > symbol from the defline
  463. // get the feature name
  464. if ($re_name) {
  465. if (!preg_match("/$re_name/", $line, $matches)) {
  466. watchdog('trp-fasta', "ERROR: Regular expression for the feature name finds nothing. Line %line.", array('%line' => $i), 'error');
  467. }
  468. elseif (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
  469. watchdog('trp-fasta', "WARNING: Regular expression retrieves a value too long for the feature name. Line %line.", array('%line' => $i), 'error');
  470. }
  471. else {
  472. $name = trim($matches[1]);
  473. }
  474. }
  475. else {
  476. // if the match_type is name and no regular expression was provided
  477. // then use the first word as the name, otherwise we don't set the name
  478. if (strcmp($match_type, 'Name')==0) {
  479. if(preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches)){
  480. if (strlen($matches[1]) > $feature_tbl['fields']['name']['length']) {
  481. watchdog('trp-fasta', "WARNING: Regular expression retrieves a feature name too long for the feature name. Line %line.", array('%line' => $i), 'error');
  482. }
  483. else {
  484. $name = trim($matches[1]);
  485. }
  486. }
  487. else {
  488. watchdog('trp-fasta', "ERROR: Cannot find a feature name. Line %line.", array('%line' => $i), 'error');
  489. }
  490. }
  491. }
  492. // get the feature unique name
  493. if ($re_uname) {
  494. if (!preg_match("/$re_uname/", $line, $matches)) {
  495. watchdog('trp-fasta', "ERROR: Regular expression for the feature unique name finds nothing. Line %line.", array('%line' => $i), 'error');
  496. }
  497. $uname = trim($matches[1]);
  498. }
  499. else {
  500. // if the match_type is name and no regular expression was provided
  501. // then use the first word as the name, otherwise, we don't set the unqiuename
  502. if (strcmp($match_type, 'Unique name')==0) {
  503. if(preg_match("/^\s*(.*?)[\s\|].*$/", $line, $matches)){
  504. $uname = trim($matches[1]);
  505. }
  506. else {
  507. watchdog('trp-fasta', "ERROR: Cannot find a feature unique name. Line %line.", array('%line' => $i), 'error');
  508. }
  509. }
  510. }
  511. // get the accession
  512. preg_match("/$re_accession/", $line, $matches);
  513. if (strlen($matches[1]) > $dbxref_tbl['fields']['accession']['length']) {
  514. watchdog('trp-fasta', "WARNING: Regular expression retrieves an accession too long for the feature name. Cannot add cross reference. Line %line.", array('%line' => $i), 'warning');
  515. }
  516. else {
  517. $accession = trim($matches[1]);
  518. }
  519. // get the relationship subject
  520. preg_match("/$re_subject/", $line, $matches);
  521. $subject = trim($matches[1]);
  522. }
  523. else {
  524. $residues .= trim($line);
  525. // update the job status every % features
  526. if ($job and $intv_read >= $interval) {
  527. $intv_read = 0;
  528. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  529. if ($name) {
  530. print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Current feature: $name\r";
  531. }
  532. else {
  533. print "Parsing Line $i (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes. Current feature: $uname\r";
  534. }
  535. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  536. }
  537. }
  538. }
  539. // now load the last sequence in the file
  540. tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id,
  541. $accession, $subject, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  542. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm);
  543. // commit the transaction
  544. tripal_db_commit_transaction();
  545. print "\nDone\n";
  546. }
  547. /**
  548. *
  549. *
  550. * @ingroup fasta_loader
  551. */
  552. function tripal_feature_fasta_loader_handle_feature($name, $uname, $db_id, $accession,
  553. $parent, $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm,
  554. $source, $residues, $method, $re_name, $match_type, $parentcvterm, $relcvterm) {
  555. // check to see if this feature already exists if the match_type is 'Name'
  556. if (strcmp($match_type, 'Name')==0) {
  557. $values = array(
  558. 'organism_id' => $organism_id,
  559. 'name' => $name,
  560. 'type_id' => $cvterm->cvterm_id,
  561. );
  562. $options = array('statement_name' => 'sel_feature_ornaty');
  563. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  564. if (count($results) > 1) {
  565. watchdog('T_fasta_loader', "Multiple features exist with the name '%name' of type
  566. '%type' for the organism. skipping", array('%name' => $name, '%type' => $type));
  567. return 0;
  568. }
  569. if (count($results) == 1) {
  570. $feature = $results[0];
  571. }
  572. }
  573. // check to see if this feature already exists if the match_type is 'Unique Name'
  574. if (strcmp($match_type, 'Unique name')==0) {
  575. $values = array(
  576. 'organism_id' => $organism_id,
  577. 'uniquename' => $uname,
  578. 'type_id' => $cvterm->cvterm_id,
  579. );
  580. $options = array('statement_name' => 'sel_feature_oruqty');
  581. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  582. if (count($results) > 1) {
  583. watchdog('T_fasta_loader', "Multiple features exist with the name '%name' of type
  584. '%type' for the organism. skipping", array('%name' => $name, '%type' => $type));
  585. return 0;
  586. }
  587. if (count($results) == 1) {
  588. $feature = $results[0];
  589. }
  590. // if the feature exists but this is an "insert only" method then skip this feature
  591. if ($feature and (strcmp($method, 'Insert only')==0)) {
  592. watchdog('T_fasta_loader', "Feature already exists '%name' ('%uname') while matching on %type. Skipping insert.",
  593. array('%name' => $name, '%uname' => $uname, '%type' => drupal_strtolower($match_type)), WATCHDOG_WARNING);
  594. return 0;
  595. }
  596. }
  597. // if we don't have a feature and we're doing an insert then do the insert
  598. $inserted = 0;
  599. if (!$feature and (strcmp($method, 'Insert only')==0 or strcmp($method, 'Insert and update')==0)) {
  600. // if we have a unique name but not a name then set them to be the same and vice versa
  601. if (!$uname) {
  602. $uname = $name;
  603. }
  604. elseif (!$name) {
  605. $name = $uname;
  606. }
  607. // insert the feature
  608. $values = array(
  609. 'organism_id' => $organism_id,
  610. 'name' => $name,
  611. 'uniquename' => $uname,
  612. 'residues' => $residues,
  613. 'seqlen' => drupal_strlen($residues),
  614. 'md5checksum' => md5($residues),
  615. 'type_id' => $cvterm->cvterm_id,
  616. 'is_analysis' => 'FALSE',
  617. 'is_obsolete' => 'FALSE',
  618. );
  619. $options = array('statement_name' => 'ins_feature_all');
  620. $success = tripal_core_chado_insert('feature', $values, $options);
  621. if (!$success) {
  622. watchdog('T_fasta_loader', "Failed to insert feature '%name (%uname)'",
  623. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  624. return 0;
  625. }
  626. // now get the feature we just inserted
  627. $values = array(
  628. 'organism_id' => $organism_id,
  629. 'uniquename' => $uname,
  630. 'type_id' => $cvterm->cvterm_id,
  631. );
  632. $options = array('statement_name' => 'sel_feature_oruqty');
  633. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  634. if (count($results) == 1) {
  635. $inserted = 1;
  636. $feature = $results[0];
  637. }
  638. else {
  639. watchdog('T_fasta_loader', "Failed to retreive newly inserted feature '%name (%uname)'",
  640. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  641. return 0;
  642. }
  643. }
  644. // if we don't have a feature and the user wants to do an update then fail
  645. if (!$feature and (strcmp($method, 'Update only')==0 or drupal_strcmp($method, 'Insert and update')==0)) {
  646. watchdog('T_fasta_loader', "Failed to find feature '%name' ('%uname') while matching on " .
  647. drupal_strtolower($match_type), array('%name' => $name, '%uname' => $uname), WATCHDOG_ERROR);
  648. return 0;
  649. }
  650. // if we do have a feature and this is an update then proceed with the update
  651. if ($feature and !$inserted and (strcmp($method, 'Update only')==0 or strcmp($method, 'Insert and update')==0)) {
  652. // if the user wants to match on the Name field
  653. if (strcmp($match_type, 'Name')==0) {
  654. // if we're matching on the name but do not have a unique name then we don't want to update the uniquename.
  655. $values = array();
  656. if ($uname) {
  657. // first check to make sure that by changing the unique name of this feature that we won't conflict with
  658. // another existing feature of the same name
  659. $values = array(
  660. 'organism_id' => $organism_id,
  661. 'uniquename' => $uname,
  662. 'type_id' => $cvterm->cvterm_id,
  663. );
  664. $options = array('statement_name' => 'sel_feature_oruqty');
  665. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  666. if (count($results) > 0) {
  667. watchdog('T_fasta_loader', "Cannot update the feature '%name' with a uniquename of '%uname' and type of '%type' as it
  668. conflicts with an existing feature with the same uniquename and type.",
  669. array('%name' => $name, '%uname' => $uname, '%type' => $type));
  670. return 0;
  671. }
  672. // the changes to the uniquename don't conflict so proceed with the update
  673. $values = array(
  674. 'uniquename' => $uname,
  675. 'residues' => $residues,
  676. 'seqlen' => drupal_strlen($residues),
  677. 'md5checksum' => md5($residues),
  678. 'is_analysis' => 'false',
  679. 'is_obsolete' => 'false',
  680. );
  681. $match = array(
  682. 'name' => $name,
  683. 'organism_id' => $organism_id,
  684. 'type_id' => $cvterm->cvterm_id,
  685. );
  686. $options = array('statement_name' => 'upd_feature_resemdisis_naorty_un');
  687. }
  688. // if we do not have a new unique name then don't change the existing uniquename field
  689. else {
  690. $values = array(
  691. 'residues' => $residues,
  692. 'seqlen' => drupal_strlen($residues),
  693. 'md5checksum' => md5($residues),
  694. 'is_analysis' => 'false',
  695. 'is_obsolete' => 'false',
  696. );
  697. $match = array(
  698. 'name' => $name,
  699. 'organism_id' => $organism_id,
  700. 'type_id' => $cvterm->cvterm_id,
  701. );
  702. $options = array('statement_name' => 'upd_feature_unresemdisis_naorty');
  703. }
  704. // perform the update
  705. $success = tripal_core_chado_update('feature', $match, $values, $options);
  706. if (!$success) {
  707. watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')",
  708. array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  709. return 0;
  710. }
  711. }
  712. if (strcmp($match_type, 'Unique name')==0) {
  713. // if we're matching on the uniquename but do not have a new name then we don't want to update the name.
  714. $values = array();
  715. if ($name) {
  716. $values = array(
  717. 'name' => $name,
  718. 'residues' => $residues,
  719. 'seqlen' => drupal_strlen($residues),
  720. 'md5checksum' => md5($residues),
  721. 'is_analysis' => 'false',
  722. 'is_obsolete' => 'false',
  723. );
  724. $match = array(
  725. 'uniquename' => $uname,
  726. 'organism_id' => $organism_id,
  727. 'type_id' => $cvterm->cvterm_id,
  728. );
  729. $options = array('statement_name' => 'upd_feature_resemdisis_unorty_na');
  730. }
  731. // if we have a unique name then update it after matching by the name
  732. else {
  733. $values = array(
  734. 'residues' => $residues,
  735. 'seqlen' => drupal_strlen($residues),
  736. 'md5checksum' => md5($residues),
  737. 'is_analysis' => 'false',
  738. 'is_obsolete' => 'false',
  739. );
  740. $match = array(
  741. 'uniquename' => $uname,
  742. 'organism_id' => $organism_id,
  743. 'type_id' => $cvterm->cvterm_id,
  744. );
  745. $options = array('statement_name' => 'upd_feature_naresemdisis_unorty');
  746. }
  747. $success = tripal_core_chado_update('feature', $match, $values, $options);
  748. if (!$success) {
  749. watchdog('T_fasta_loader', "Failed to update feature '%name' ('%name')",
  750. array('%name' => $name, '%uiname' => $uname), WATCHDOG_ERROR);
  751. return 0;
  752. }
  753. }
  754. }
  755. // add in the analysis link
  756. if ($analysis_id) {
  757. // if the association doens't alredy exist then add one
  758. $values = array(
  759. 'analysis_id' => $analysis_id,
  760. 'feature_id' => $feature->feature_id,
  761. );
  762. $sel_options = array('statement_name' => 'sel_analysisfeature_anfe');
  763. $results = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $values, $sel_options);
  764. if (count($results) == 0) {
  765. $ins_options = array('statement_name' => 'ins_analysisfeature_anfe');
  766. $success = tripal_core_chado_insert('analysisfeature', $values, $ins_options);
  767. if (!$success) {
  768. watchdog('T_fasta_loader', "Failed to associate analysis and feature '%name' ('%name')",
  769. array('%name' => $name, '%uname' => $uname), WATCHDOG_ERROR);
  770. return 0;
  771. }
  772. }
  773. }
  774. // now add the database cross reference
  775. if ($db_id) {
  776. // check to see if this accession reference exists, if not add it
  777. $values = array(
  778. 'db_id' => $db_id,
  779. 'accession' => $accession
  780. );
  781. $sel_options = array('statement_name' => 'sel_dbxref_dbac');
  782. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
  783. // if the accession doesn't exist then add it
  784. if (count($results) == 0) {
  785. $ins_options = array('statement_name' => 'ins_dbxref_dbac');
  786. $results = tripal_core_chado_insert('dbxref', $values, $ins_options);
  787. if (!$results) {
  788. watchdog('T_fasta_loader', "Failed to add database accession '%accession'",
  789. array('%accession' => $accession), WATCHDOG_ERROR);
  790. return 0;
  791. }
  792. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $values, $sel_options);
  793. if (count($results) == 1) {
  794. $dbxref = $results[0];
  795. }
  796. else {
  797. watchdog('T_fasta_loader', "Failed to retreive newly inserted dbxref '%name (%uname)'",
  798. array('%name' => $name, '%uname' => $numane), WATCHDOG_ERROR);
  799. return 0;
  800. }
  801. }
  802. else {
  803. $dbxref = $results[0];
  804. }
  805. // check to see if the feature dbxref record exists if not, then add it
  806. $values = array(
  807. 'feature_id' => $feature->feature_id,
  808. 'dbxref_id' => $dbxref->dbxref_id
  809. );
  810. $sel_options = array('statement_name' => 'sel_featuredbxref_fedb');
  811. $results = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $sel_options);
  812. if (count($results) == 0) {
  813. $ins_options = array('statement_name' => 'ins_featuredbxref_fedb');
  814. $success = tripal_core_chado_insert('feature_dbxref', $values, $ins_options);
  815. if (!$success) {
  816. watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature",
  817. array('%accession' => $accession), WATCHDOG_ERROR);
  818. return 0;
  819. }
  820. }
  821. }
  822. // now add in the relationship if one exists. If not, then add it
  823. if ($rel_type) {
  824. $values = array(
  825. 'organism_id' => $organism_id,
  826. 'uniquename' => $parent,
  827. 'type_id' => $parentcvterm->cvterm_id,
  828. );
  829. $options = array('statement_name' => 'sel_feature_oruqty');
  830. $results = tripal_core_chado_select('feature', array('feature_id'), $values, $options);
  831. if (count($results) != 1) {
  832. watchdog('T_fasta_loader', "Cannot find a unique fature for the parent '%parent' of type
  833. '%type' for the feature.", array('%parent' => $parent, '%type' => $parent_type));
  834. return 0;
  835. }
  836. $parent_feature = $results[0];
  837. // check to see if the relationship already exists if not then add it
  838. $values = array(
  839. 'subject_id' => $feature->feature_id,
  840. 'object_id' => $parent_feature->feature_id,
  841. 'type_id' => $relcvterm->cvterm_id,
  842. );
  843. $sel_options = array('statement_name' => 'sel_featurerelationship_suojty');
  844. $results = tripal_core_chado_select('feature_relationship', array('feature_relationship_id'), $values, $sel_options);
  845. if (count($results) == 0) {
  846. $ins_options = array('statement_name' => 'ins_featurerelationship_suojty');
  847. $success = tripal_core_chado_insert('feature_relationship', $values, $ins_options);
  848. if (!$success) {
  849. watchdog('T_fasta_loader', "Failed to add associate database accession '%accession' with feature",
  850. array('%accession' => $accession), WATCHDOG_ERROR);
  851. return 0;
  852. }
  853. }
  854. }
  855. }