ARB
GenomeImport.cxx
Go to the documentation of this file.
1 // ================================================================ //
2 // //
3 // File : GenomeImport.cxx //
4 // Purpose : //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in November 2006 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // ================================================================ //
11 
12 #include "tools.h"
13 #include "DBwriter.h"
14 
15 #include <cerrno>
16 #define AW_RENAME_SKIP_GUI
17 #include <AW_rename.hxx>
18 #include <aw_question.hxx>
19 #include <arb_stdstr.h>
20 #include <arb_file.h>
21 #include <arb_diff.h>
22 
23 using namespace std;
24 
25 GB_ERROR GI_importGenomeFile(ImportSession& session, const char *file_name, const char *ali_name) {
27  try {
28  if (strcmp(ali_name, "ali_genom") != 0) throw "Alignment has to be 'ali_genom'";
29 
30  FILE *in = fopen(file_name, "rb");
31  if (!in) throw GBS_global_string("Can't read file '%s' (Reason: %s)", file_name, strerror(errno));
32 
33  BufferedFileReader flatfile(file_name, in);
34  flatfile.showFilenameInLineError(false);
35 
36  DBwriter db_writer(session, ali_name);
37 
39  {
40  string line;
41  if (!flatfile.getLine(line)) throw flatfile.lineError("File is empty");
42 
43  if (beginsWith(line, "LOCUS")) importer = new GenebankImporter(flatfile, db_writer);
44  else if (beginsWith(line, "ID")) importer = new EmblImporter (flatfile, db_writer);
45  else throw flatfile.lineError("Wrong format. Expected 'LOCUS' or 'ID'");
46 
47  flatfile.backLine(line);
48  }
49 
50  importer->import();
51  }
52  catch (const string &err) { error = GBS_static_string(err.c_str()); }
53  catch (const char *err) { error = err; }
54  catch (...) { error = "Unknown exception during import (program error)"; }
55  return error;
56 }
57 
58 
59 
60 
61 ImportSession::ImportSession(GBDATA *gb_species_data_, int estimated_genomes_count)
62  : gb_species_data(gb_species_data_)
63 {
64  und_species = new UniqueNameDetector(gb_species_data, estimated_genomes_count);
65  ok_to_ignore_wrong_start_codon = new AW_repeated_question;
66 }
67 
70  delete und_species;
72 }
73 
74 // --------------------------------------------------------------------------------
75 
76 #ifdef UNIT_TESTS
77 #ifndef TEST_UNIT_H
78 #include <test_unit.h>
79 #endif
80 
81 // #define TEST_AUTO_UPDATE // uncomment to auto-update expected result-database
82 
83 void TEST_SLOW_import_genome_flatfile() {
84  GB_shell shell;
85  GBDATA *gb_main = GB_open("nosuch.arb", "wc");
86 
87  // import flatfile
88  {
89  GB_transaction ta(gb_main);
90  GBDATA *gb_species_data = GBT_find_or_create(gb_main, "species_data", 7);
91 
92  ImportSession isess(gb_species_data, 1);
93  TEST_EXPECT_NO_ERROR(GI_importGenomeFile(isess, "AB735678.txt", "ali_genom"));
94  }
95 
96  // save database and compare with expectation
97  {
98  const char *savename = "AB735678.arb";
99  const char *expected = "AB735678_expected.arb";
100 
101  TEST_EXPECT_NO_ERROR(GB_save_as(gb_main, savename, "a"));
102 #if defined(TEST_AUTO_UPDATE)
103  TEST_COPY_FILE(savename, expected);
104 #else
105  TEST_EXPECT_TEXTFILES_EQUAL(savename, expected);
106 #endif // TEST_AUTO_UPDATE
108  }
109 
110  GB_close(gb_main);
111 }
112 
113 #endif // UNIT_TESTS
114 
115 // --------------------------------------------------------------------------------
const char * GB_ERROR
Definition: arb_core.h:25
GBDATA * gb_species_data
Definition: GenomeImport.h:22
GBDATA * GB_open(const char *path, const char *opent)
Definition: ad_load.cxx:1363
static void deleteStaticData()
Definition: DBwriter.cxx:419
bool getLine(string &line)
static ArbImporter * importer
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
STL namespace.
int GB_unlink(const char *path)
Definition: arb_file.cxx:188
void showFilenameInLineError(bool show)
UniqueNameDetector * und_species
Definition: GenomeImport.h:23
GBDATA * gb_species_data
Definition: adname.cxx:33
void backLine(const string &line)
Generic smart pointer.
Definition: smartptr.h:149
GB_ERROR GB_save_as(GBDATA *gbd, const char *path, const char *savetype)
string lineError(const string &msg) const
static void error(const char *msg)
Definition: mkptypes.cxx:96
GB_ERROR GI_importGenomeFile(ImportSession &session, const char *file_name, const char *ali_name)
#define TEST_EXPECT_ZERO_OR_SHOW_ERRNO(iocond)
Definition: test_unit.h:1090
bool beginsWith(const std::string &str, const std::string &start)
Definition: arb_stdstr.h:22
GBDATA * GBT_find_or_create(GBDATA *father, const char *key, long delete_level)
Definition: adtools.cxx:42
const char * GBS_static_string(const char *str)
Definition: arb_msg.cxx:212
#define TEST_EXPECT_NO_ERROR(call)
Definition: test_unit.h:1118
static int line
Definition: arb_a2ps.c:296
#define NULp
Definition: cxxforward.h:116
ImportSession(GBDATA *gb_species_data_, int estimated_genomes_count)
GB_transaction ta(gb_var)
GBDATA * gb_main
Definition: adname.cxx:32
AW_repeated_question * ok_to_ignore_wrong_start_codon
Definition: GenomeImport.h:24
void import()
Definition: Importer.cxx:234
#define TEST_EXPECT_TEXTFILES_EQUAL(fgot, fwant)
Definition: test_unit.h:1424
void GB_close(GBDATA *gbd)
Definition: arbdb.cxx:655