source: tags/ms_r16q2/GENOM_IMPORT/GenomeImport.cxx

Last change on this file was 11844, checked in by westram, 10 years ago
File size: 3.8 KB
Line 
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
23using namespace std;
24
25GB_ERROR GI_importGenomeFile(ImportSession& session, const char *file_name, const char *ali_name) {
26    GB_ERROR error = 0;
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
38        SmartPtr<Importer> importer;
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
61ImportSession::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
68ImportSession::~ImportSession() {
69    delete ok_to_ignore_wrong_start_codon;
70    delete und_species;
71    DBwriter::deleteStaticData();
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
83void 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
107        TEST_EXPECT_ZERO_OR_SHOW_ERRNO(GB_unlink(savename));
108    }
109
110    GB_close(gb_main);
111}
112
113#endif // UNIT_TESTS
114
115// --------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.