openModeller  Version 1.4.0
OccurrencesReader.cpp
Go to the documentation of this file.
00001 
00030 #include <openmodeller/occ_io/OccurrencesReader.hh>
00031 #include <openmodeller/Occurrences.hh>  // List of occurrences.
00032 
00033 #include <vector>
00034 using std::vector;
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 
00040 /*********************/
00041 /*** get Presences ***/
00042 OccurrencesPtr
00043 OccurrencesReader::getPresences( const char *groupId )
00044 {
00045   // If group was not specified, return empty set
00046   if ( ! groupId ) {
00047 
00048     return new OccurrencesImpl( 1 );
00049   }
00050 
00051   LstOccurrences::iterator ocs = _presences.begin();
00052   LstOccurrences::iterator end = _presences.end();
00053 
00054   while ( ocs != end ) {
00055 
00056     OccurrencesPtr oc = *ocs;
00057 
00058     if ( ! strcasecmp( groupId, oc->label() ) ) {
00059 
00060       _presences.erase( ocs );
00061 
00062       return oc;
00063     }
00064 
00065     ++ocs;
00066   }
00067 
00068   return new OccurrencesImpl( 1 );
00069 }
00070 
00071 
00072 /********************/
00073 /*** get Absences ***/
00074 OccurrencesPtr
00075 OccurrencesReader::getAbsences( const char *groupId )
00076 {
00077   // If group was not specified, return empty set
00078   if ( ! groupId ) {
00079 
00080     return new OccurrencesImpl( 0 );
00081   }
00082 
00083   LstOccurrences::iterator ocs = _absences.begin();
00084   LstOccurrences::iterator end = _absences.end();
00085 
00086   while ( ocs != end ) {
00087 
00088     OccurrencesPtr oc = *ocs;
00089 
00090     if ( ! strcasecmp( groupId, oc->label() ) ) {
00091 
00092       _absences.erase( ocs );
00093 
00094       return oc;
00095     }
00096 
00097     ++ocs;
00098   }
00099 
00100   return new OccurrencesImpl( 0 );
00101 }
00102 
00103 
00104 /*************/
00105 /*** print ***/
00106 void
00107 OccurrencesReader::printOccurrences( const std::string& msg )
00108 {
00109   printf( "%s", msg.c_str() );
00110 
00111   printf( "\nPresences" );
00112 
00113   LstOccurrences::const_iterator ocs = _presences.begin();
00114   LstOccurrences::const_iterator end = _presences.end();
00115 
00116   while ( ocs != end ) {
00117 
00118     (*ocs)->dump( "\n****************" );
00119     ++ocs;
00120   }
00121 
00122   printf( "\nAbsences" );
00123 
00124   ocs = _absences.begin();
00125   end = _absences.end();
00126 
00127   while ( ocs != end ) {
00128 
00129     (*ocs)->dump( "\n****************" );
00130     ++ocs;
00131   }
00132 }
00133 
00134 /**********************/
00135 /*** add Occurrence ***/
00136 int
00137 OccurrencesReader::_addOccurrence( const char *id, const char *groupId, Coord lg, Coord lt,
00138                                    Scalar error, Scalar abundance,
00139                                    int num_attributes, Scalar *attributes )
00140 {
00141   if ( abundance > 0 ) {
00142 
00143     _addPresence( id, groupId, lg, lt, error, abundance, num_attributes, attributes );
00144   }
00145   else {
00146 
00147     _addAbsence( id, groupId, lg, lt, error, num_attributes, attributes );
00148   }
00149 
00150   return 1;
00151 }
00152 
00153 /********************/
00154 /*** add Presence ***/
00155 int
00156 OccurrencesReader::_addPresence( const char *id, const char *groupId, Coord lg, Coord lt,
00157                                  Scalar error, Scalar abundance,
00158                                  int num_attributes, Scalar *attributes )
00159 {
00160   // If a presence group with the same name already exists, 
00161   // just add another occurrence to it.
00162 
00163   // We search backwards because new Occurrences are pushed
00164   // onto the back, so most likely, the Occurrences we are looking
00165   // for is at the back.
00166   LstOccurrences::const_reverse_iterator ocs = _presences.rbegin();
00167   LstOccurrences::const_reverse_iterator end = _presences.rend();
00168   while ( ocs != end ) {
00169 
00170     OccurrencesPtr const & group = *ocs;
00171 
00172     if ( ! strcasecmp( group->label(), groupId ) ) {
00173 
00174       group->createOccurrence( id, lg, lt, error, abundance, num_attributes, attributes );
00175       return 0;
00176     }
00177 
00178     ++ocs;
00179   }
00180 
00181   // If the occurrences group does not exist, create it.
00182   OccurrencesImpl *newgroup = new OccurrencesImpl( groupId, _coord_system );
00183   newgroup->createOccurrence( id, lg, lt, error, abundance, num_attributes, attributes );
00184   _presences.push_back( newgroup );
00185 
00186   return 1;
00187 }
00188 
00189 /*******************/
00190 /*** add Absence ***/
00191 int
00192 OccurrencesReader::_addAbsence( const char *id, const char *groupId, Coord lg, Coord lt,
00193                                 Scalar error, int num_attributes, Scalar *attributes )
00194 {
00195   // If an absence group with the same name already exists, 
00196   // just add another occurrence to it.
00197 
00198   // We search backwards because new Occurrences are pushed
00199   // onto the back, so most likely, the Occurrences we are looking
00200   // for is at the back.
00201   LstOccurrences::const_reverse_iterator ocs = _absences.rbegin();
00202   LstOccurrences::const_reverse_iterator end = _absences.rend();
00203   while ( ocs != end ) {
00204 
00205       OccurrencesPtr const & group = *ocs;
00206 
00207       if ( ! strcasecmp( group->label(), groupId ) ) {
00208 
00209         group->createOccurrence( id, lg, lt, error, 0.0, num_attributes, attributes );
00210         return 0;
00211       }
00212 
00213       ++ocs;
00214   }
00215 
00216   // If the occurrences group does not exist, create it.
00217   OccurrencesImpl *newgroup = new OccurrencesImpl( groupId, _coord_system );
00218   newgroup->createOccurrence( id, lg, lt, error, 0.0, num_attributes, attributes );
00219   _absences.push_back( newgroup );
00220 
00221   return 1;
00222 }
00223