00001 #include <qmessagebox.h>
00002
00003 #include "trapperparser.h"
00004 #include "trapperdoc.h"
00005 #include "generaldata.h"
00006 #include "featuredata.h"
00007 #include "qdir.h"
00008 #include "readsinrect.h"
00009 #include <limits>
00010 #include "optimizelayout.h"
00011
00012 using namespace std;
00013
00014 bool TrapperParser::startElement(const QString& namespaceURI,
00015 const QString& localName,
00016 const QString& qName,
00017 const QXmlAttributes& attributes)
00018 {
00019
00020
00021
00022 if ( qName == "TRAPPER" ) {
00023 format_ok = true;
00024 return true;
00025 }
00026 else if ( qName == "contig" && format_ok ) {
00027
00028
00029
00030
00031 curr_start_row = 1;
00032
00033
00034 QString contigName = attributes.value("name");
00035 cerr<<"Importing contig "<<contigName<<endl;
00036
00037
00038
00039 QStringList contigNamesAlreadyPresent = contigNamesInProjectDir();
00040 if ( contigNamesAlreadyPresent.contains( contigName ) ) {
00041 QString message;
00042 message = QString("The contig name \"%1\" was specified in the import file, but that contig is already present in the project dir. Skipping this one").arg(contigName);
00043 QMessageBox::warning(0,"",message);
00044 return false;
00045 }
00046
00047 QString fName = projDir + "/" + contigName;
00048
00049
00050
00051
00052 current_doc = new TrapperDoc(env);
00053 current_doc->openDocument(fName);
00054
00055 return true;
00056 }
00057 else if ( qName == "section" && format_ok ) {
00058 optimize_layout(curr_start_row, curr_set, current_doc);
00059 curr_set.clear();
00060
00061
00062 Database::SecondaryIterator<ReadData>* row_it = new Database::SecondaryIterator<ReadData>( "pos", current_doc, "ReadData" );
00063 int ret_row = row_it->last();
00064 ReadData* r_row = (ret_row != DB_NOTFOUND) ? row_it->answer() : 0;
00065 if ( r_row ) {
00066 curr_start_row = r_row->row() + 5;
00067 }
00068 delete row_it;
00069 return true;
00070 }
00071 else if ( format_ok ) {
00072
00073
00074
00075 bool no_row(false);
00076
00077
00078
00079 Database::Creator<GeneralData> creator( current_doc, qName.ascii() );
00080
00081 creator.data()->readAttributes( attributes );
00082
00083 if ( qName != "ReadData" ) {
00084
00085 FeatureData* fdata = dynamic_cast<FeatureData*>(creator.data());
00086 assert( fdata );
00087 fdata->setReadRecno( current_read_recno );
00088
00089 }
00090 else {
00091 if ( attributes.value("row") == "" ) {
00092
00093
00094 no_row = true;
00095
00096
00097
00098
00099 ReadData* rdata = dynamic_cast<ReadData*>(creator.data());
00100 assert( rdata );
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 rdata->setRow( curr_start_row );
00124 }
00125 }
00126
00127 db_recno_t new_recno = creator.create( false );
00128 if ( qName == "ReadData" ) {
00129
00130 current_read_recno = new_recno;
00131 if ( no_row ) {
00132 curr_set.insert(new_recno);
00133 }
00134
00135 }
00136 return true;
00137 }
00138 return false;
00139 }
00140
00141 bool TrapperParser::endElement( const QString & namespaceURI, const QString & localName,
00142 const QString & qName)
00143 {
00144 if ( qName == "contig" ) {
00145
00146 optimize_layout(curr_start_row, curr_set, current_doc);
00147 curr_set.clear();
00148
00149
00150 delete current_doc;
00151
00152 }
00153 return true;
00154 }
00155
00156
00157
00158 QStringList TrapperParser::contigNamesInProjectDir()
00159 {
00160 QString contigName;
00161 QDir d( projDir );
00162 Q_ASSERT( d.exists() );
00163 d.setFilter( QDir::Dirs | QDir::NoSymLinks );
00164 d.setSorting( QDir::Name );
00165 QStringList slist = d.entryList();
00166 return slist;
00167 }