/GeneR/GeneR/src/readSeqGbk.cc
C++ | 287 lines | 159 code | 67 blank | 61 comment | 33 complexity | e85c43e47e8e6839f906aeaa93bcc00e MD5 | raw file
Possible License(s): LGPL-2.0
- /*! \file readSeqGbk.cc
- * \brief functions to read Gbk file
- *
- * \version 1
- * \date Created : 26/10/05
- * \date Last Modified : Time-stamp: <2007-10-12 18:38:54 antoine>
- * \author A. Lucas from "Bio-R" codes
- * \note Licence: CeCIll
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string>
- #include <string.h>
- #include "GeneR_globals.h"
- #include "GeneR_glob.h"
- #include "readSeqGbk.h"
- #include "libStrings.h"
- extern "C"
- {
- void ReadSeqGBKR(char **file,int *offsq,
- int *begseq, int *endseq, int *bufno,int * upper, int *err);
- void ReadSeqGBK(char ** seq, char **BqueFich, int *OffsSQ,
- int *DebSeq, int *FinSeq, int * upper,
- int *err);
- }
- /* ---------------------------------------------------- */
- /* ReadSeqGBKR */
- /* ---------------------------------------------------- */
- /** @brief Read Genebank file
- *
- * This function allocate a buffer
- * and call ReadSeqGBK. This function
- * is called directly by R.
- *
- * @param **file Gbk file
- * @param *offsq Offset sequence (i.e. position of sequence
- * first char in file)
- * @param *begseq position to begin in sequence
- * @param *endseq position to end in sequence
- * @param *bufno Integer, number of the sequence. (bufseq)
- * @param *upper Flag 1: all character are converted to upper case
- * @param *err Error Flag: to return to R.
- *
- * \sa ReadSeqGBK
- */
- /* ---------------------------------------------------- */
- void ReadSeqGBKR(char **file,int *offsq,
- int *begseq, int *endseq, int *bufno,int * upper, int *err)
- {
- int l;
- char * seq;
- l = *endseq-*begseq+2;
-
- /* Allocation */
- if(! GeneR_glob::instance()->allocBuffer(l, *bufno, 0, 1))
- {
- *err= 0;
- printf("GeneR: Error in function ReadSeqEMBLR\n");
- return;
- }
- /* Get address */
- seq = GeneR_glob::instance()->buffer(*bufno);
- ReadSeqGBK(&seq,file,offsq,begseq,endseq,upper,err);
- return;
- }
- void ReadSeqGBK(char ** seq, char **BqueFich, int *OffsSQ,
- int *DebSeq, int *FinSeq, int * upper,
- int *err)
-
-
- {
- FILE *f; /* fichier courant */
- char * LBuf; /* Ligne courante */
- char * sBuf; /* tampon pour sequence */
- char tBuf[MaxLLigne]; /* locale */
- char * sf;
- int LLBuf, LsBuf; /* Taille de la ligne courante & de sBuf */
- int PtDeb, PtFin; /* Pour extraire les nt d'une ligne */
- int nextNumntDebL; /* numero de nt debut de ligne suivante */
- int curNumntDebL; /* numero de nt debut de ligne courante */
- int FinEntree, Continue; /* Drapeaux de fin d'entree & contineur l'extraction */
- int longEnregistrement=0; /* Compte la longeur de l'enregistrememnt (verifie */
- /* l'intégrite */
- int tailleSeq=0; /* Size of all sequence */
-
- int i; /* indice de parcours du tableau */
- LBuf = (char * ) malloc(MaxLLigne * sizeof(char));
- sBuf = (char * ) malloc(MaxLLigne * sizeof(char));
- /* Ouvrir la banque et pointer le debut de sequence */
- if ( !(f = fopen(*BqueFich, "r")))
- {
- printf("GeneR.so: error while opening file\n");
- *err = -1;
- return ;
- }
-
- #ifndef __MINGW_H
- if ( (fseek(f, *OffsSQ, 0)))
- {
- printf("GeneR.so: error while seeking file\n");
- fclose(f);
- *err = -1;
- return ;
- }
- #else
- /* fseek on windows count CR ('\r') and fgets does not see them */
- for(i=0; i< *OffsSQ; i++)
- if(fgetc(f) == EOF)
- {
- printf("GeneR.so: error while seeking file\n");
- fclose(f);
- *err = -1;
- return ;
- }
- #endif
-
- /* Prendre la ligne courante verifier que pas "//" = indicateur fin de fichier */
-
- LBuf = fgets(LBuf,MaxLLigne,f);
- if (readSeqGbk::EstFinEntreebk(LBuf))
- {
- fclose(f);
- *err = -1;
- return ; /* pas de sequence - libgenbank.c*/
- }
- LLBuf = strlen(LBuf); /* taille de la ligne courante */
-
- /* Extraire le numero de nucleotide qui se trouve en début de ligne */
- if (!(curNumntDebL = readSeqGbk::XtNumntDebLig(LBuf))) /* libgenbank.c*/
- {
- fclose(f);
- *err = -1;
- return ;
- }
-
- /* Lire ligne suivante tant que le n? de nt de fin de ligne < DebSeq */
- /* We suppose that lines has less than 100 nucl. (usualy:60) */
- while ((curNumntDebL +100) < *DebSeq && !(readSeqGbk::EstFinEntreebk(LBuf))) {
- fgets(LBuf,MaxLLigne,f);
- if (!(curNumntDebL = readSeqGbk::XtNumntDebLig(LBuf)))
- {
- fclose(f);
- *err = -1;
- return ;
- }
- }
-
- /* Ici, la ligne courante contient le nt de DebSeq
- Compacter les nt de la ligne, extraire les nt utiles dans sBuf */
-
- /* curNumntDebL = XtNumntDebLig(lastLine);*/
-
-
- if (!(libStrings::SupprimerSeparateurs(LBuf, sBuf))) /* libgenbank.c */
- {
- fclose(f);
- *err = -1;
- return ;
- }
- /*
- * copy sbuf dans sf sauf
- * les premiers characters (chiffres)
- */
- i=0;
- while(isdigit(sBuf[i])) /* libgenbank.c */
- i++;
- sf = (char * ) (sBuf + i);
- LsBuf = strlen (sf);
- nextNumntDebL= curNumntDebL + LsBuf;
- PtDeb = *DebSeq - curNumntDebL;
-
- if (*FinSeq > 0 && (*FinSeq < nextNumntDebL))
- PtFin = *FinSeq - curNumntDebL;
- else
- PtFin = strlen(sf) - 1 ;
-
- /* strxtr(sf, tBuf, PtDeb, PtFin -PtDeb + 1); */
- strncpy(tBuf,sf+PtDeb,PtFin -PtDeb + 1);
- tBuf[PtFin -PtDeb + 1] ='\0';
- tailleSeq = (*FinSeq)- (*DebSeq) +1;
- longEnregistrement = strlen(tBuf);
- if(longEnregistrement> tailleSeq)
- {
- printf("Not enough allocation. [Probably: Verify type of file (fasta, emlb, gbk) and delete index file '.ix']\n");
- fclose(f);
- *err = -1;
- return ;
- }
-
- /* PousseMot(tBuf,*seq);*/ /* pour ajouter au buffer 0 - abisys.c */
- *seq = strcpy(*seq,tBuf);
- /* Recommence jusqu'a la fin de l'entree */
- Continue = ((nextNumntDebL <= *FinSeq) || (*FinSeq == 0) )
- && !(readSeqGbk::EstFinEntreebk(LBuf));
- while (Continue) {
- /* Tout cela pourrait etre optimise... */
- /* memcpy(lastLine, LBuf, MaxLLigne);*/
- LBuf = fgets(LBuf,MaxLLigne,f);
- FinEntree = (readSeqGbk::EstFinEntreebk(LBuf));
- if(!FinEntree)
- longEnregistrement = libStrings::Pousse_atgc(longEnregistrement,LBuf,*seq,tailleSeq);
-
- Continue = !FinEntree && (longEnregistrement < tailleSeq);
- }
- fclose(f);
- free(sBuf);
- free(LBuf);
-
- if(*upper)
- libStrings::sys_upper_string(*seq);
- return ;
- }
- namespace readSeqGbk{
- /* --------------------------------------------------- */
- /* @Function: EstFinEntreebk */
- /** @brief vrai si "s" commence par "//" */
- /* --------------------------------------------------- */
- int EstFinEntreebk(char *s)
- {
- /* If end of file ( = end of gbk sequence too) */
- if(s== NULL) return 1;
-
- if (strlen(s) < 2) return 0;
- else if ((s[0] == '/') && (s[1] == '/')) return 1;
- else return 0;
- }
- /* ------------------------------------------------------------- */
- /* @Function: XtNumntDebLig */
- /*! @brief Extrait le numero du premierr nt de la ligne
- * (format EMBL) et raccourcit la ligne de ce numero */
- /* ------------------------------------------------------------- */
-
- int XtNumntDebLig(char *t)
- {
- int n;
-
- if (sscanf(t, "%d", &n)) return n;
- else return 0;
- }
- }