trapperparser.cpp

Go to the documentation of this file.
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 //   cerr<<"qName: "<<qName<<endl;
00020   
00021   //Three cases:
00022   if ( qName == "TRAPPER" ) {
00023     format_ok = true;
00024     return true;
00025   }
00026   else if ( qName == "contig" && format_ok ) {
00027     /* Basically, all the functionality of TrapperApp::slotImport()
00028        has moved here. The other two cases below now contains the
00029        old functionality of TrapperDoc::import(). */
00030 
00031     curr_start_row = 1;
00032     
00033     //Use attributes to get name
00034     QString contigName = attributes.value("name");
00035     cerr<<"Importing contig "<<contigName<<endl;
00036 
00037 
00038     //Some bookkeeping to check if contig is already present
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     //Create directory for contig
00047     QString fName = projDir + "/" + contigName;
00048                 
00049 
00050     //Create directories and databases for this contig
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     //Figure out start row
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 //     cerr<<"parsing "<<qName<<endl;
00073     //Get data name from attributes and put into QString generalDataName
00074     
00075     bool no_row(false);
00076 
00077     //Create object factory for this data type
00078     
00079     Database::Creator<GeneralData> creator( current_doc, qName.ascii() );
00080 
00081     creator.data()->readAttributes( attributes );
00082     
00083     if ( qName != "ReadData" ) {//All other cases are FeatureData and need the corresponding read recno
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         //Data is readdata and row needs to be figured out
00093         
00094         no_row = true;
00095 
00096 //         ReadsInRect* readsInRect = new ReadsInRect( current_doc );
00097         
00098         
00099         ReadData* rdata = dynamic_cast<ReadData*>(creator.data());
00100         assert( rdata );
00101 //         readsInRect->setWindowCoord( rdata->startPos(), curr_start_row,  rdata->endPos() - rdata->startPos() + 1, numeric_limits<TR_DNA>::max() - curr_start_row );
00102 
00103 //         int previous_row(curr_start_row);
00104 //         int row(0);
00105 //         int num(0);
00106 //         for ( ReadData * r = readsInRect->first(); r; r = readsInRect->next() ) {
00107 //           num++;
00108 //           if ( r->row() - previous_row > 1 ) {
00109 //             row = previous_row + 1;
00110 //             break;
00111 //           }
00112 //           previous_row = r->row();
00113 //         }
00114 //         delete readsInRect;
00115 //         if ( !row ) {
00116 //           if ( num == 0 ) {
00117 //             row = curr_start_row;
00118 //           }
00119 //           else {
00120 //             row = previous_row + 1;
00121 //           }
00122 //         }
00123         rdata->setRow( curr_start_row );
00124       }
00125     }
00126 
00127     db_recno_t new_recno = creator.create( false );//Should give dna, quality etc same recno as read (as previously)
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     //Delete old doc, if any
00150     delete current_doc;
00151 //     cerr<<"end contig"<<endl;
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 }

Generated on Fri Jul 17 20:19:29 2009 for ngsview by  doxygen 1.5.1