PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. /*! \file readSeqGbk.cc
  2. * \brief functions to read Gbk file
  3. *
  4. * \version 1
  5. * \date Created : 26/10/05
  6. * \date Last Modified : Time-stamp: <2007-10-12 18:38:54 antoine>
  7. * \author A. Lucas from "Bio-R" codes
  8. * \note Licence: CeCIll
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <string>
  14. #include <string.h>
  15. #include "GeneR_globals.h"
  16. #include "GeneR_glob.h"
  17. #include "readSeqGbk.h"
  18. #include "libStrings.h"
  19. extern "C"
  20. {
  21. void ReadSeqGBKR(char **file,int *offsq,
  22. int *begseq, int *endseq, int *bufno,int * upper, int *err);
  23. void ReadSeqGBK(char ** seq, char **BqueFich, int *OffsSQ,
  24. int *DebSeq, int *FinSeq, int * upper,
  25. int *err);
  26. }
  27. /* ---------------------------------------------------- */
  28. /* ReadSeqGBKR */
  29. /* ---------------------------------------------------- */
  30. /** @brief Read Genebank file
  31. *
  32. * This function allocate a buffer
  33. * and call ReadSeqGBK. This function
  34. * is called directly by R.
  35. *
  36. * @param **file Gbk file
  37. * @param *offsq Offset sequence (i.e. position of sequence
  38. * first char in file)
  39. * @param *begseq position to begin in sequence
  40. * @param *endseq position to end in sequence
  41. * @param *bufno Integer, number of the sequence. (bufseq)
  42. * @param *upper Flag 1: all character are converted to upper case
  43. * @param *err Error Flag: to return to R.
  44. *
  45. * \sa ReadSeqGBK
  46. */
  47. /* ---------------------------------------------------- */
  48. void ReadSeqGBKR(char **file,int *offsq,
  49. int *begseq, int *endseq, int *bufno,int * upper, int *err)
  50. {
  51. int l;
  52. char * seq;
  53. l = *endseq-*begseq+2;
  54. /* Allocation */
  55. if(! GeneR_glob::instance()->allocBuffer(l, *bufno, 0, 1))
  56. {
  57. *err= 0;
  58. printf("GeneR: Error in function ReadSeqEMBLR\n");
  59. return;
  60. }
  61. /* Get address */
  62. seq = GeneR_glob::instance()->buffer(*bufno);
  63. ReadSeqGBK(&seq,file,offsq,begseq,endseq,upper,err);
  64. return;
  65. }
  66. void ReadSeqGBK(char ** seq, char **BqueFich, int *OffsSQ,
  67. int *DebSeq, int *FinSeq, int * upper,
  68. int *err)
  69. {
  70. FILE *f; /* fichier courant */
  71. char * LBuf; /* Ligne courante */
  72. char * sBuf; /* tampon pour sequence */
  73. char tBuf[MaxLLigne]; /* locale */
  74. char * sf;
  75. int LLBuf, LsBuf; /* Taille de la ligne courante & de sBuf */
  76. int PtDeb, PtFin; /* Pour extraire les nt d'une ligne */
  77. int nextNumntDebL; /* numero de nt debut de ligne suivante */
  78. int curNumntDebL; /* numero de nt debut de ligne courante */
  79. int FinEntree, Continue; /* Drapeaux de fin d'entree & contineur l'extraction */
  80. int longEnregistrement=0; /* Compte la longeur de l'enregistrememnt (verifie */
  81. /* l'intégrite */
  82. int tailleSeq=0; /* Size of all sequence */
  83. int i; /* indice de parcours du tableau */
  84. LBuf = (char * ) malloc(MaxLLigne * sizeof(char));
  85. sBuf = (char * ) malloc(MaxLLigne * sizeof(char));
  86. /* Ouvrir la banque et pointer le debut de sequence */
  87. if ( !(f = fopen(*BqueFich, "r")))
  88. {
  89. printf("GeneR.so: error while opening file\n");
  90. *err = -1;
  91. return ;
  92. }
  93. #ifndef __MINGW_H
  94. if ( (fseek(f, *OffsSQ, 0)))
  95. {
  96. printf("GeneR.so: error while seeking file\n");
  97. fclose(f);
  98. *err = -1;
  99. return ;
  100. }
  101. #else
  102. /* fseek on windows count CR ('\r') and fgets does not see them */
  103. for(i=0; i< *OffsSQ; i++)
  104. if(fgetc(f) == EOF)
  105. {
  106. printf("GeneR.so: error while seeking file\n");
  107. fclose(f);
  108. *err = -1;
  109. return ;
  110. }
  111. #endif
  112. /* Prendre la ligne courante verifier que pas "//" = indicateur fin de fichier */
  113. LBuf = fgets(LBuf,MaxLLigne,f);
  114. if (readSeqGbk::EstFinEntreebk(LBuf))
  115. {
  116. fclose(f);
  117. *err = -1;
  118. return ; /* pas de sequence - libgenbank.c*/
  119. }
  120. LLBuf = strlen(LBuf); /* taille de la ligne courante */
  121. /* Extraire le numero de nucleotide qui se trouve en début de ligne */
  122. if (!(curNumntDebL = readSeqGbk::XtNumntDebLig(LBuf))) /* libgenbank.c*/
  123. {
  124. fclose(f);
  125. *err = -1;
  126. return ;
  127. }
  128. /* Lire ligne suivante tant que le n? de nt de fin de ligne < DebSeq */
  129. /* We suppose that lines has less than 100 nucl. (usualy:60) */
  130. while ((curNumntDebL +100) < *DebSeq && !(readSeqGbk::EstFinEntreebk(LBuf))) {
  131. fgets(LBuf,MaxLLigne,f);
  132. if (!(curNumntDebL = readSeqGbk::XtNumntDebLig(LBuf)))
  133. {
  134. fclose(f);
  135. *err = -1;
  136. return ;
  137. }
  138. }
  139. /* Ici, la ligne courante contient le nt de DebSeq
  140. Compacter les nt de la ligne, extraire les nt utiles dans sBuf */
  141. /* curNumntDebL = XtNumntDebLig(lastLine);*/
  142. if (!(libStrings::SupprimerSeparateurs(LBuf, sBuf))) /* libgenbank.c */
  143. {
  144. fclose(f);
  145. *err = -1;
  146. return ;
  147. }
  148. /*
  149. * copy sbuf dans sf sauf
  150. * les premiers characters (chiffres)
  151. */
  152. i=0;
  153. while(isdigit(sBuf[i])) /* libgenbank.c */
  154. i++;
  155. sf = (char * ) (sBuf + i);
  156. LsBuf = strlen (sf);
  157. nextNumntDebL= curNumntDebL + LsBuf;
  158. PtDeb = *DebSeq - curNumntDebL;
  159. if (*FinSeq > 0 && (*FinSeq < nextNumntDebL))
  160. PtFin = *FinSeq - curNumntDebL;
  161. else
  162. PtFin = strlen(sf) - 1 ;
  163. /* strxtr(sf, tBuf, PtDeb, PtFin -PtDeb + 1); */
  164. strncpy(tBuf,sf+PtDeb,PtFin -PtDeb + 1);
  165. tBuf[PtFin -PtDeb + 1] ='\0';
  166. tailleSeq = (*FinSeq)- (*DebSeq) +1;
  167. longEnregistrement = strlen(tBuf);
  168. if(longEnregistrement> tailleSeq)
  169. {
  170. printf("Not enough allocation. [Probably: Verify type of file (fasta, emlb, gbk) and delete index file '.ix']\n");
  171. fclose(f);
  172. *err = -1;
  173. return ;
  174. }
  175. /* PousseMot(tBuf,*seq);*/ /* pour ajouter au buffer 0 - abisys.c */
  176. *seq = strcpy(*seq,tBuf);
  177. /* Recommence jusqu'a la fin de l'entree */
  178. Continue = ((nextNumntDebL <= *FinSeq) || (*FinSeq == 0) )
  179. && !(readSeqGbk::EstFinEntreebk(LBuf));
  180. while (Continue) {
  181. /* Tout cela pourrait etre optimise... */
  182. /* memcpy(lastLine, LBuf, MaxLLigne);*/
  183. LBuf = fgets(LBuf,MaxLLigne,f);
  184. FinEntree = (readSeqGbk::EstFinEntreebk(LBuf));
  185. if(!FinEntree)
  186. longEnregistrement = libStrings::Pousse_atgc(longEnregistrement,LBuf,*seq,tailleSeq);
  187. Continue = !FinEntree && (longEnregistrement < tailleSeq);
  188. }
  189. fclose(f);
  190. free(sBuf);
  191. free(LBuf);
  192. if(*upper)
  193. libStrings::sys_upper_string(*seq);
  194. return ;
  195. }
  196. namespace readSeqGbk{
  197. /* --------------------------------------------------- */
  198. /* @Function: EstFinEntreebk */
  199. /** @brief vrai si "s" commence par "//" */
  200. /* --------------------------------------------------- */
  201. int EstFinEntreebk(char *s)
  202. {
  203. /* If end of file ( = end of gbk sequence too) */
  204. if(s== NULL) return 1;
  205. if (strlen(s) < 2) return 0;
  206. else if ((s[0] == '/') && (s[1] == '/')) return 1;
  207. else return 0;
  208. }
  209. /* ------------------------------------------------------------- */
  210. /* @Function: XtNumntDebLig */
  211. /*! @brief Extrait le numero du premierr nt de la ligne
  212. * (format EMBL) et raccourcit la ligne de ce numero */
  213. /* ------------------------------------------------------------- */
  214. int XtNumntDebLig(char *t)
  215. {
  216. int n;
  217. if (sscanf(t, "%d", &n)) return n;
  218. else return 0;
  219. }
  220. }