| 1 |
// |
| 2 |
// This program is distributed in the hope that it will be useful, |
| 3 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 4 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 5 |
// GNU Library General Public License for more details. |
| 6 |
// |
| 7 |
// You should have received a copy of the GNU General Public License |
| 8 |
// along with this program; if not, write to the Free Software |
| 9 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 10 |
|
| 11 |
//created: |
| 12 |
//mer sep 06 14:23:39 CEST 2000 Langella <[email protected]> |
| 13 |
|
| 14 |
|
| 15 |
/*************************************************************************** |
| 16 |
chaineficpop.cpp - Objet pour faciliter la lecture de fichiers genepop/populations |
| 17 |
------------------- |
| 18 |
begin : ven sep 06 10:25:55 CEST 2000 |
| 19 |
copyright : (C) 2000 by Olivier Langella CNRS UPR9034 |
| 20 |
email : [email protected] |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include "chaineficpop.h" |
| 24 |
|
| 25 |
//constructeur |
| 26 |
ChaineFicPop::ChaineFicPop(istream & fichier):ChaineCar(""), _fichier(fichier) { |
| 27 |
//_fichier(fichier); |
| 28 |
_fichier.clear(); |
| 29 |
_fichier.seekg(0); |
| 30 |
|
| 31 |
_numeroligne = 0; |
| 32 |
_type_fichier = 0; |
| 33 |
_danspop = false; |
| 34 |
_findefichier = false; |
| 35 |
_boolnompop = false; |
| 36 |
} |
| 37 |
|
| 38 |
//destructeur |
| 39 |
ChaineFicPop::~ChaineFicPop() { |
| 40 |
_fichier.clear(); |
| 41 |
} |
| 42 |
|
| 43 |
int ChaineFicPop::get_titre(biolib::vecteurs::Titre & titres) { |
| 44 |
//lecture des commentaires |
| 45 |
//cerr << " ChaineFicPop::get_titre debut" << endl; |
| 46 |
// assign(""); |
| 47 |
titres.resize(0); |
| 48 |
|
| 49 |
if (estuntitre()) titres.push_back(*this); |
| 50 |
|
| 51 |
//cerr << " ChaineFicPop::get_titre debut 1" << endl; |
| 52 |
while ((get_ligne()) && (estuntitre())) { |
| 53 |
titres.push_back(*this); |
| 54 |
assign(""); |
| 55 |
}; |
| 56 |
|
| 57 |
//cerr << " ChaineFicPop::get_titre debut 2" << endl; |
| 58 |
if (_fichier.fail()) throw Anomalie(1); |
| 59 |
|
| 60 |
//cerr << " ChaineFicPop::get_titre debut 3" << endl; |
| 61 |
return (0); |
| 62 |
} |
| 63 |
|
| 64 |
int ChaineFicPop::get_locus(biolib::vecteurs::Titre & locus) { |
| 65 |
//lecture des locus |
| 66 |
|
| 67 |
locus.resize(0); |
| 68 |
|
| 69 |
if ((estunepop()==false) && (*this != "")) ajout_locus(locus); |
| 70 |
|
| 71 |
while ((get_ligne()) && (estunepop()==false)) { |
| 72 |
ajout_locus(locus); |
| 73 |
}; |
| 74 |
|
| 75 |
if (_fichier.fail()) throw Anomalie(1); |
| 76 |
if (estunepop()==false) throw Anomalie(2); |
| 77 |
|
| 78 |
return (0); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
int ChaineFicPop::get_lignepop() { |
| 83 |
if (_findefichier) return(1); |
| 84 |
get_ligne(); |
| 85 |
return(0); |
| 86 |
} |
| 87 |
|
| 88 |
int ChaineFicPop::get_nompop(biolib::vecteurs::ChaineCar & nompop) const { |
| 89 |
|
| 90 |
nompop.assign(_nompop); |
| 91 |
// nompop.Remplacer(" ","_"); |
| 92 |
/* if ((GetNbMots()) > 1) GetMot(2, nompop); |
| 93 |
else nompop.assign(""); |
| 94 |
*/ |
| 95 |
return(0); |
| 96 |
} |
| 97 |
|
| 98 |
int ChaineFicPop::get_nomind(biolib::vecteurs::ChaineCar & nomind) const { |
| 99 |
if (!(_danspop)) return (1); |
| 100 |
int pos; |
| 101 |
|
| 102 |
pos = find(",", 0); |
| 103 |
if (pos != -1) { |
| 104 |
// ChaineCar mot; |
| 105 |
|
| 106 |
nomind.assign(*this,0, pos); |
| 107 |
nomind.fsupprgauche(); |
| 108 |
nomind.fsupprdroite(); |
| 109 |
// nomind.assign(mot); |
| 110 |
} |
| 111 |
// nomind.Remplacer(" ","_"); |
| 112 |
|
| 113 |
return(0); |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
int ChaineFicPop::get_alleles(biolib::vecteurs::Titre & alleles) const { |
| 118 |
//cerr << " ChaineFicPop::get_alleles" << endl; |
| 119 |
if (!(_danspop)) return (1); |
| 120 |
alleles.resize(0); |
| 121 |
int pos; |
| 122 |
|
| 123 |
ChaineCar mots; |
| 124 |
string mot; |
| 125 |
long nbmots, i; |
| 126 |
|
| 127 |
pos = find(",", 0) + 1; |
| 128 |
if (pos != -1) { |
| 129 |
mots.assign(*this,pos, size() - pos); |
| 130 |
|
| 131 |
nbmots = mots.GetNbMots(); |
| 132 |
|
| 133 |
for (i=0; i < nbmots; i++) { |
| 134 |
mots.GetMot(i+1, mot); |
| 135 |
if (mot != "") alleles.push_back(mot); |
| 136 |
} |
| 137 |
} |
| 138 |
else return (1); |
| 139 |
|
| 140 |
//cerr << " ChaineFicPop::get_alleles fin 0" << endl; |
| 141 |
return(0); |
| 142 |
} |
| 143 |
|
| 144 |
long ChaineFicPop::get_numeroligne() const{ |
| 145 |
return(_numeroligne); |
| 146 |
} |
| 147 |
|
| 148 |
bool ChaineFicPop::get_ligne() { |
| 149 |
//cerr << " ChaineFicPop::get_ligne debut" << endl; |
| 150 |
GetLigneFlot(_fichier); |
| 151 |
_numeroligne++; |
| 152 |
|
| 153 |
if (_fichier.eof()) _findefichier = true; |
| 154 |
|
| 155 |
//cerr << " ChaineFicPop::get_ligne fin" << endl; |
| 156 |
return (_fichier.good()); |
| 157 |
} |
| 158 |
|
| 159 |
bool ChaineFicPop::estuntitre() { |
| 160 |
// détecte les signes d'un titre: """, "Title Line:" ... |
| 161 |
string titre; |
| 162 |
|
| 163 |
if (size() < 1) return (false); |
| 164 |
//cerr << " ChaineFicPop::estuntitre debut" << endl; |
| 165 |
if (operator[](0) == '"') { |
| 166 |
// if (operator[](size()-1) == '"') string titre(*this,1, size() -2); |
| 167 |
//cerr << " ChaineFicPop::estuntitre titre " << (size() - 1) << endl; |
| 168 |
titre.assign(*this,1, size() -1); |
| 169 |
//cerr << " ChaineFicPop::estuntitre titre " << titre << endl; |
| 170 |
assign(titre); |
| 171 |
return (true); |
| 172 |
} |
| 173 |
//cerr << " ChaineFicPop::estuntitre debut 2" << endl; |
| 174 |
int pos; |
| 175 |
string mot("Title line: "); |
| 176 |
|
| 177 |
//cerr << " ChaineFicPop::estuntitre debut 3" << endl; |
| 178 |
pos = find(mot, 0); |
| 179 |
if (pos != -1) { |
| 180 |
titre.assign(*this,pos + mot.size(), size() - mot.size() - pos); |
| 181 |
assign(titre); |
| 182 |
return (true); |
| 183 |
} |
| 184 |
|
| 185 |
//cerr << " ChaineFicPop::estuntitre debut 4" << endl; |
| 186 |
if (_numeroligne == 1) return(true); |
| 187 |
|
| 188 |
return (false); |
| 189 |
} |
| 190 |
|
| 191 |
bool ChaineFicPop::estunepop() { |
| 192 |
// détecte les signes d'un titre: """, "Title Line:" ... |
| 193 |
// string _chainetemp; |
| 194 |
int pos; |
| 195 |
|
| 196 |
GetMot(1, _chainetemp); |
| 197 |
if ((_chainetemp == "POP")||(_chainetemp == "pop")||(_chainetemp == "Pop")) { |
| 198 |
pos = find(",", 0); |
| 199 |
if (pos > 0) return (false); |
| 200 |
|
| 201 |
_danspop = true; |
| 202 |
if (size() > 4) { |
| 203 |
_nompop = substr(4, (size() - 4)); |
| 204 |
_boolnompop = true; //les noms de pop sont derrière le mot clé pop |
| 205 |
} |
| 206 |
else _nompop.assign(""); |
| 207 |
|
| 208 |
return (true); |
| 209 |
} |
| 210 |
return (false); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
bool ChaineFicPop::estunindividu() const { |
| 215 |
// détecte les signes d'un titre: """, "Title Line:" ... |
| 216 |
if (!(_danspop)) return (false); |
| 217 |
|
| 218 |
// string mot; |
| 219 |
|
| 220 |
if (GetNbMots(",") == 2) return (true); |
| 221 |
else return (false); |
| 222 |
/* GetMot(1, mot); |
| 223 |
if (mot[mot.size()-1] == ',') return (true); |
| 224 |
else { |
| 225 |
GetMot(2, mot); |
| 226 |
if (mot == ",") return true; |
| 227 |
} |
| 228 |
return (false); */ |
| 229 |
} |
| 230 |
|
| 231 |
void ChaineFicPop::ajout_locus(biolib::vecteurs::Titre & locus) const { |
| 232 |
long nbmots, i; |
| 233 |
string mot; |
| 234 |
|
| 235 |
nbmots = GetNbMots(); |
| 236 |
|
| 237 |
for (i=0; i < nbmots; i++) { |
| 238 |
GetMot(i+1, mot); |
| 239 |
if (mot != "") locus.push_back(mot); |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
bool ChaineFicPop::findefichier() const { |
| 245 |
return (_findefichier); |
| 246 |
} |
| 247 |
|
| 248 |
bool ChaineFicPop::get_boolnompop() const { |
| 249 |
return (_boolnompop); |
| 250 |
} |