module leeadn

! Pedro A. Bernaola-Galvan, Dept. of Applied Physics II    
! University of Malaga, Malaga (SPAIN)
! E-MAIL: rick@ctima.uma.es
!
!
! Shannon Entropy, Jensen-Shannon divergence
! and related functions
!
! Last updated: Jun-10-99

!
!     Copyright 2003-2008 Pedro Bernaola-Galvan, Pedro Carpena & Jose L. Oliver
!     This program is distributed under the terms of the GNU General Public License (GPL)

!     This file is part of IsoFinder

!     IsoFinder is free software: you can redistribute it and/or modify
!     it under the terms of the GNU General Public License as published by
!     the Free Software Foundation, either version 3 of the License, or
!     (at your option) any later version.

!     IsoFinder is distributed in the hope that it will be useful,
!     but WITHOUT ANY WARRANTY; without even the implied warranty of
!     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!     GNU General Public License for more details.

!     You should have received a copy of the GNU General Public License
!     along with IsoFinder.  If not, see <http://www.gnu.org/licenses/>.

!***************************************************************************

!     References:

!     J L Oliver, P Carpena, M Hackenberg, P Bernaola-Galván  (2004)  
!     IsoFinder : computational prediction of isochores in genome sequences
!     Nucleic Acids Research  32:  W287-W292   

!     IsoFinder project: http://isofinder.sourceforge.net/
!     IsoFinder web server: http://bioinfo2.ugr.es/IsoF/isofinder.html

!     Contacts:

!     Pedro Bernaola(1), Pedro Carpena(1) & Jose L. Oliver(2)
!     (1) Dpto. de Física Aplicada II, Universidad de Málaga, Spain
!         rick@ctima.uma.es, pcarpena@ctima.uma.es
!     (2) Dpto. de Genética, Universidad de Granada, Spain
!         oliver@ugr.es

!***************************************************************************

CONTAINS


!**************************************************


SUBROUTINE lee_adn(adn,size,enes,fen,ini,fin,tipo,f_words)
character(*)   fen
integer*1      adn(:)
integer*4      size,letra,enes,ini,fin,cont,n,i,f_words(0:)
character*83   linea
character*32   t_bases
character*2    tipo


t_bases='ATCGNRYSWKMVHDBXatcgnryswkmvhdbx'


open (1,file=fen)
if (fin==0) fin = 1000000000

linea='kkk'

do while (linea(1:2) .ne. 'SQ' .and. linea(1:6) .ne. 'ORIGIN' .and. linea(1:1) .ne. '>')
   read (1,'(A83)') linea
enddo

size=0
enes=0
cont=0
f_words=0

1  read(1,'(A83)',end=10) linea

  ! if (size>103230000) print*, trim(linea)
   n=len_trim(linea)

   do i=1,n
      letra=index(t_bases,linea(i:i))

      if (letra>0) then
        cont=cont+1
        if (cont>fin) goto 10
        if (cont>=ini) then
          size=size+1
          !if (size>103230000) print*,size

          if (letra>16) letra=letra-16
          if (letra>=5) then
             letra=5
             enes=enes+1
          endif
          if (letra>4) then
     letra=-1
          else
             if (tipo=='SW') then
        if ((letra==1).or.(letra==2)) then
                  letra=0
                else
                  letra=1
                endif
              else if (tipo=='RY') then
                if ((letra==1).or.(letra==4)) then
                  letra=0
                else
                  letra=1
                endif
              else
                letra=letra-1
              endif

              f_words(letra)=f_words(letra)+1
          endif
          adn(size)=letra
        endif
      endif
   enddo
   go to 1
10 continue
rewind(1)
close(1)

end SUBROUTINE lee_adn

!****************************************************************


end MODULE leeadn
