copy src_MD-M-SAXS-homology src-HCD-5D
[unres.git] / source / wham / src-HCD-5D / read_constr_homology.F
1       subroutine read_constr_homology
2
3       include 'DIMENSIONS'
4       include 'DIMENSIONS.ZSCOPT'
5       include 'DIMENSIONS.FREE'
6 #ifdef MPI
7       include 'mpif.h'
8 #endif
9       include 'COMMON.SETUP'
10       include 'COMMON.CONTROL'
11       include 'COMMON.CHAIN'
12       include 'COMMON.IOUNITS'
13       include 'COMMON.GEO'
14       include 'COMMON.INTERACT'
15       include 'COMMON.NAMES'
16       include 'COMMON.HOMRESTR'
17       include 'COMMON.HOMOLOGY'
18 c
19 c For new homol impl
20 c
21       include 'COMMON.VAR'
22 c     include 'include_unres/COMMON.VAR'
23 c
24
25 c     double precision odl_temp,sigma_odl_temp,waga_theta,waga_d,
26 c    &                 dist_cut
27 c     common /przechowalnia/ odl_temp(maxres,maxres,max_template),
28 c    &    sigma_odl_temp(maxres,maxres,max_template)
29       character*2 kic2
30       character*24 model_ki_dist, model_ki_angle
31       character*500 controlcard
32       integer ki, i, j, k, l, ii_in_use(maxdim),i_tmp,idomain_tmp
33       integer idomain(max_template,maxres)
34       logical lprn /.true./
35       integer ilen
36       external ilen
37       logical liiflag
38 c
39 c     FP - Nov. 2014 Temporary specifications for new vars
40 c
41       double precision rescore_tmp,x12,y12,z12,rescore2_tmp,
42      &                 rescore3_tmp
43       double precision, dimension (max_template,maxres) :: rescore
44       double precision, dimension (max_template,maxres) :: rescore2
45       double precision, dimension (max_template,maxres) :: rescore3
46       character*24 tpl_k_rescore
47 c -----------------------------------------------------------------
48 c Reading multiple PDB ref structures and calculation of retraints
49 c not using pre-computed ones stored in files model_ki_{dist,angle}
50 c FP (Nov., 2014)
51 c -----------------------------------------------------------------
52 c
53 c
54 c Alternative: reading from input
55       call card_concat(controlcard,.true.)
56       call reada(controlcard,"HOMOL_DIST",waga_dist,1.0d0)
57       call reada(controlcard,"HOMOL_ANGLE",waga_angle,1.0d0)
58       call reada(controlcard,"HOMOL_THETA",waga_theta,1.0d0) ! new
59       call reada(controlcard,"HOMOL_SCD",waga_d,1.0d0) ! new
60       call reada(controlcard,'DIST_CUT',dist_cut,5.0d0) ! for diff ways of calc sigma
61       call reada(controlcard,'DIST2_CUT',dist2_cut,9999.0d0)
62       call readi(controlcard,"HOMOL_NSET",homol_nset,1)
63       read2sigma=(index(controlcard,'READ2SIGMA').gt.0)
64       call readi(controlcard,"IHSET",ihset,1)       
65       if (homol_nset.gt.1)then
66          call card_concat(controlcard,.true.)
67          read(controlcard,*) (waga_homology(i),i=1,homol_nset) 
68 c         if(me.eq.king .or. .not. out1file .and. fg_rank.eq.0) then
69 c          write(iout,*) "iset homology_weight "
70 c         do i=1,homol_nset
71 c          write(iout,*) i,waga_homology(i)
72 c         enddo
73 c         endif
74          iset=mod(kolor,homol_nset)+1
75       else
76        iset=1
77        waga_homology(1)=1.0
78       endif
79 c     write(iout,*) "waga_homology(",iset,")",waga_homology(iset)
80
81 cd      write (iout,*) "nnt",nnt," nct",nct
82 cd      call flush(iout)
83
84
85       lim_odl=0
86       lim_dih=0
87 c
88 c  New
89 c
90       lim_theta=0
91       lim_xx=0
92 c
93 c  Reading HM global scores (prob not required)
94 c
95       do i = nnt,nct
96         do k=1,constr_homology
97          idomain(k,i)=0
98         enddo
99       enddo
100 c     open (4,file="HMscore")
101 c     do k=1,constr_homology
102 c       read (4,*,end=521) hmscore_tmp
103 c       hmscore(k)=hmscore_tmp ! Another transformation can be used 
104 c       write(*,*) "Model", k, ":", hmscore(k)
105 c     enddo
106 c521  continue
107
108       ii=0
109       do i = nnt,nct-2 
110         do j=i+2,nct 
111         ii=ii+1
112         ii_in_use(ii)=0
113         enddo
114       enddo
115 c     write(iout,*) "waga_theta",waga_theta,"waga_d",waga_d
116
117       if (read_homol_frag) then
118         call read_klapaucjusz
119       else
120
121       do k=1,constr_homology
122
123         read(inp,'(a)') pdbfile
124 c  Next stament causes error upon compilation (?)
125 c       if(me.eq.king.or. .not. out1file)
126 c         write (iout,'(2a)') 'PDB data will be read from file ',
127 c    &   pdbfile(:ilen(pdbfile))
128          write (iout,'(a,5x,a)') 'HOMOL: Opening PDB file',
129      &  pdbfile(:ilen(pdbfile))
130         open(ipdbin,file=pdbfile,status='old',err=33)
131         goto 34
132   33    write (iout,'(a,5x,a)') 'Error opening PDB file',
133      &  pdbfile(:ilen(pdbfile))
134         stop
135   34    continue
136 c        print *,'Begin reading pdb data'
137 c
138 c Files containing res sim or local scores (former containing sigmas)
139 c
140
141         write(kic2,'(bz,i2.2)') k
142
143         tpl_k_rescore="template"//kic2//".sco"
144
145         unres_pdb=.false.
146         if (read2sigma) then
147           call readpdb_template(k)
148         else
149           call readpdb
150         endif
151
152 c        call readpdb
153         do i=1,2*nres
154           do j=1,3
155             crefjlee(j,i)=c(j,i)
156           enddo
157         enddo
158 #ifdef DEBUG
159         do i=1,nres
160           write (iout,'(i5,3f8.3,5x,3f8.3)') i,(crefjlee(j,i),j=1,3),
161      &      (crefjlee(j,i+nres),j=1,3)
162         enddo
163         write (iout,*) "read_constr_homology: after reading pdb file"
164         call flush(iout)
165 #endif
166
167 c
168 c     Distance restraints
169 c
170 c          ... --> odl(k,ii)
171 C Copy the coordinates from reference coordinates (?)
172         do i=1,2*nres
173           do j=1,3
174             c(j,i)=cref(j,i)
175 c           write (iout,*) "c(",j,i,") =",c(j,i)
176           enddo
177         enddo
178 c
179 c From read_dist_constr (commented out 25/11/2014 <-> res sim)
180 c
181 c         write(iout,*) "tpl_k_rescore - ",tpl_k_rescore
182           open (ientin,file=tpl_k_rescore,status='old')
183           if (nnt.gt.1) rescore(k,1)=0.0d0
184           do irec=nnt,nct ! loop for reading res sim 
185             if (read2sigma) then
186              read (ientin,*,end=1401) i_tmp,rescore2_tmp,rescore_tmp,
187      &                                rescore3_tmp,idomain_tmp
188              i_tmp=i_tmp+nnt-1
189              idomain(k,i_tmp)=idomain_tmp
190              rescore(k,i_tmp)=rescore_tmp
191              rescore2(k,i_tmp)=rescore2_tmp
192              rescore3(k,i_tmp)=rescore3_tmp
193              write(iout,'(a7,i5,3f10.5,i5)') "rescore",
194      &                      i_tmp,rescore2_tmp,rescore_tmp,
195      &                                rescore3_tmp,idomain_tmp
196             else
197              idomain(k,irec)=1
198              read (ientin,*,end=1401) rescore_tmp
199
200 c           rescore(k,irec)=rescore_tmp+1.0d0 ! to avoid 0 values
201              rescore(k,irec)=0.5d0*(rescore_tmp+0.5d0) ! alt transf to reduce scores
202 c           write(iout,*) "rescore(",k,irec,") =",rescore(k,irec)
203             endif
204           enddo  
205  1401   continue
206         close (ientin)        
207         if (waga_dist.ne.0.0d0) then
208           ii=0
209           do i = nnt,nct-2 
210             do j=i+2,nct 
211
212               x12=c(1,i)-c(1,j)
213               y12=c(2,i)-c(2,j)
214               z12=c(3,i)-c(3,j)
215               distal=dsqrt(x12*x12+y12*y12+z12*z12) 
216 c              write (iout,*) k,i,j,distal,dist2_cut
217
218             if (idomain(k,i).eq.idomain(k,j).and.idomain(k,i).ne.0
219      &            .and. distal.le.dist2_cut ) then
220
221               ii=ii+1
222               ii_in_use(ii)=1
223               l_homo(k,ii)=.true.
224
225 c             write (iout,*) "k",k
226 c             write (iout,*) "i",i," j",j," constr_homology",
227 c    &                       constr_homology
228               ires_homo(ii)=i
229               jres_homo(ii)=j
230               odl(k,ii)=distal
231               if (read2sigma) then
232                 sigma_odl(k,ii)=0
233                 do ik=i,j
234                  sigma_odl(k,ii)=sigma_odl(k,ii)+rescore2(k,ik)
235                 enddo
236                 sigma_odl(k,ii)=sigma_odl(k,ii)/(j-i+1)
237                 if (odl(k,ii).gt.dist_cut) sigma_odl(k,ii) = 
238      &        sigma_odl(k,ii)*dexp(0.5d0*(odl(k,ii)/dist_cut)**2-0.5d0)
239               else
240                 if (odl(k,ii).le.dist_cut) then
241                  sigma_odl(k,ii)=rescore(k,i)+rescore(k,j) 
242                 else
243 #ifdef OLDSIGMA
244                  sigma_odl(k,ii)=(rescore(k,i)+rescore(k,j))* 
245      &                      dexp(0.5d0*(odl(k,ii)/dist_cut)**2)
246 #else
247                  sigma_odl(k,ii)=(rescore(k,i)+rescore(k,j))* 
248      &                      dexp(0.5d0*(odl(k,ii)/dist_cut)**2-0.5d0)
249 #endif
250                 endif
251               endif
252               sigma_odl(k,ii)=1.0d0/(sigma_odl(k,ii)*sigma_odl(k,ii)) 
253             else
254               ii=ii+1
255               l_homo(k,ii)=.false.
256             endif
257             enddo
258           enddo
259         lim_odl=ii
260         endif
261 c
262 c     Theta, dihedral and SC retraints
263 c
264         if (waga_angle.gt.0.0d0) then
265 c         open (ientin,file=tpl_k_sigma_dih,status='old')
266 c         do irec=1,maxres-3 ! loop for reading sigma_dih
267 c            read (ientin,*,end=1402) i,j,ki,l,sigma_dih(k,i+nnt-1) ! j,ki,l what for?
268 c            if (i+nnt-1.gt.lim_dih) lim_dih=i+nnt-1 ! right?
269 c            sigma_dih(k,i+nnt-1)=sigma_dih(k,i+nnt-1)* ! not inverse because of use of res. similarity
270 c    &                            sigma_dih(k,i+nnt-1)
271 c         enddo
272 c1402   continue
273 c         close (ientin)
274           do i = nnt+3,nct 
275             if (idomain(k,i).eq.0) then 
276                sigma_dih(k,i)=0.0
277                cycle
278             endif
279             dih(k,i)=phiref(i) ! right?
280 c           read (ientin,*) sigma_dih(k,i) ! original variant
281 c             write (iout,*) "dih(",k,i,") =",dih(k,i)
282 c             write(iout,*) "rescore(",k,i,") =",rescore(k,i),
283 c    &                      "rescore(",k,i-1,") =",rescore(k,i-1),
284 c    &                      "rescore(",k,i-2,") =",rescore(k,i-2),
285 c    &                      "rescore(",k,i-3,") =",rescore(k,i-3)
286
287             sigma_dih(k,i)=(rescore(k,i)+rescore(k,i-1)+
288      &                     rescore(k,i-2)+rescore(k,i-3))/4.0
289 c            if (read2sigma) sigma_dih(k,i)=sigma_dih(k,i)/4.0
290 c           write (iout,*) "Raw sigmas for dihedral angle restraints"
291 c           write (iout,'(i5,10(2f8.2,4x))') i,sigma_dih(k,i)
292 c           sigma_dih(k,i)=hmscore(k)*rescore(k,i)*rescore(k,i-1)*
293 c                          rescore(k,i-2)*rescore(k,i-3)  !  right expression ?
294 c   Instead of res sim other local measure of b/b str reliability possible
295             if (sigma_dih(k,i).ne.0)
296      &      sigma_dih(k,i)=1.0d0/(sigma_dih(k,i)*sigma_dih(k,i))
297 c           sigma_dih(k,i)=sigma_dih(k,i)*sigma_dih(k,i)
298           enddo
299           lim_dih=nct-nnt-2 
300         endif
301
302         if (waga_theta.gt.0.0d0) then
303 c         open (ientin,file=tpl_k_sigma_theta,status='old')
304 c         do irec=1,maxres-2 ! loop for reading sigma_theta, right bounds?
305 c            read (ientin,*,end=1403) i,j,ki,sigma_theta(k,i+nnt-1) ! j,ki what for?
306 c            sigma_theta(k,i+nnt-1)=sigma_theta(k,i+nnt-1)* ! not inverse because of use of res. similarity
307 c    &                              sigma_theta(k,i+nnt-1)
308 c         enddo
309 c1403   continue
310 c         close (ientin)
311
312           do i = nnt+2,nct ! right? without parallel.
313 c         do i = i=1,nres ! alternative for bounds acc to readpdb?
314 c         do i=ithet_start,ithet_end ! with FG parallel.
315              if (idomain(k,i).eq.0) then  
316               sigma_theta(k,i)=0.0
317               cycle
318              endif
319              thetatpl(k,i)=thetaref(i)
320 c            write (iout,*) "thetatpl(",k,i,") =",thetatpl(k,i)
321 c            write(iout,*)  "rescore(",k,i,") =",rescore(k,i),
322 c    &                      "rescore(",k,i-1,") =",rescore(k,i-1),
323 c    &                      "rescore(",k,i-2,") =",rescore(k,i-2)
324 c            read (ientin,*) sigma_theta(k,i) ! 1st variant
325              sigma_theta(k,i)=(rescore(k,i)+rescore(k,i-1)+
326      &                        rescore(k,i-2))/3.0
327 c             if (read2sigma) sigma_theta(k,i)=sigma_theta(k,i)/3.0
328              if (sigma_theta(k,i).ne.0)
329      &       sigma_theta(k,i)=1.0d0/(sigma_theta(k,i)*sigma_theta(k,i))
330
331 c            sigma_theta(k,i)=hmscore(k)*rescore(k,i)*rescore(k,i-1)*
332 c                             rescore(k,i-2) !  right expression ?
333 c            sigma_theta(k,i)=sigma_theta(k,i)*sigma_theta(k,i)
334           enddo
335         endif
336
337         if (waga_d.gt.0.0d0) then
338 c       open (ientin,file=tpl_k_sigma_d,status='old')
339 c         do irec=1,maxres-1 ! loop for reading sigma_theta, right bounds?
340 c            read (ientin,*,end=1404) i,j,sigma_d(k,i+nnt-1) ! j,ki what for?
341 c            sigma_d(k,i+nnt-1)=sigma_d(k,i+nnt-1)* ! not inverse because of use of res. similarity
342 c    &                          sigma_d(k,i+nnt-1)
343 c         enddo
344 c1404   continue
345
346           do i = nnt,nct ! right? without parallel.
347 c         do i=2,nres-1 ! alternative for bounds acc to readpdb?
348 c         do i=loc_start,loc_end ! with FG parallel.
349                if (itype(i).eq.10) cycle 
350                if (idomain(k,i).eq.0 ) then 
351                   sigma_d(k,i)=0.0
352                   cycle
353                endif
354                xxtpl(k,i)=xxref(i)
355                yytpl(k,i)=yyref(i)
356                zztpl(k,i)=zzref(i)
357 c              write (iout,*) "xxtpl(",k,i,") =",xxtpl(k,i)
358 c              write (iout,*) "yytpl(",k,i,") =",yytpl(k,i)
359 c              write (iout,*) "zztpl(",k,i,") =",zztpl(k,i)
360 c              write(iout,*)  "rescore(",k,i,") =",rescore(k,i)
361                sigma_d(k,i)=rescore3(k,i) !  right expression ?
362                if (sigma_d(k,i).ne.0)
363      &          sigma_d(k,i)=1.0d0/(sigma_d(k,i)*sigma_d(k,i))
364
365 c              sigma_d(k,i)=hmscore(k)*rescore(k,i) !  right expression ?
366 c              sigma_d(k,i)=sigma_d(k,i)*sigma_d(k,i)
367 c              read (ientin,*) sigma_d(k,i) ! 1st variant
368           enddo
369         endif
370       enddo
371 c
372 c remove distance restraints not used in any model from the list
373 c shift data in all arrays
374 c
375       if (waga_dist.ne.0.0d0) then
376         ii=0
377         liiflag=.true.
378         do i=nnt,nct-2 
379          do j=i+2,nct 
380           ii=ii+1
381           if (ii_in_use(ii).eq.0.and.liiflag) then
382             liiflag=.false.
383             iistart=ii
384           endif
385           if (ii_in_use(ii).ne.0.and..not.liiflag.or.
386      &                   .not.liiflag.and.ii.eq.lim_odl) then
387              if (ii.eq.lim_odl) then
388               iishift=ii-iistart+1
389              else
390               iishift=ii-iistart
391              endif
392              liiflag=.true.
393              do ki=iistart,lim_odl-iishift
394               ires_homo(ki)=ires_homo(ki+iishift)
395               jres_homo(ki)=jres_homo(ki+iishift)
396               ii_in_use(ki)=ii_in_use(ki+iishift)
397               do k=1,constr_homology
398                odl(k,ki)=odl(k,ki+iishift)
399                sigma_odl(k,ki)=sigma_odl(k,ki+iishift)
400                l_homo(k,ki)=l_homo(k,ki+iishift)
401               enddo
402              enddo
403              ii=ii-iishift
404              lim_odl=lim_odl-iishift
405           endif
406          enddo
407         enddo
408       endif
409
410       endif ! .not. klapaucjusz     
411
412       if (constr_homology.gt.0) call homology_partition
413       if (constr_homology.gt.0) call init_int_table
414 cd      write (iout,*) "homology_partition: lim_theta= ",lim_theta,
415 cd     & "lim_xx=",lim_xx
416 c     write (iout,*) "ithet_start =",ithet_start,"ithet_end =",ithet_end
417 c     write (iout,*) "loc_start =",loc_start,"loc_end =",loc_end
418 c
419 c Print restraints
420 c
421       if (.not.lprn) return
422 cd      write(iout,*) "waga_theta",waga_theta,"waga_d",waga_d
423 c      if(me.eq.king .or. .not. out1file .and. fg_rank.eq.0) then
424        write (iout,*) "Distance restraints from templates"
425        do ii=1,lim_odl
426        write(iout,'(3i5,100(2f8.2,1x,l1,4x))') 
427      &  ii,ires_homo(ii),jres_homo(ii),
428      &  (odl(ki,ii),1.0d0/dsqrt(sigma_odl(ki,ii)),l_homo(ki,ii),
429      &  ki=1,constr_homology)
430        enddo
431        write (iout,*) "Dihedral angle restraints from templates"
432        do i=nnt+3,nct
433         write (iout,'(i5,a4,100(2f8.2,4x))') i,restyp(itype(i)),
434      &      (rad2deg*dih(ki,i),
435      &      rad2deg/dsqrt(sigma_dih(ki,i)),ki=1,constr_homology)
436        enddo
437        write (iout,*) "Virtual-bond angle restraints from templates"
438        do i=nnt+2,nct
439         write (iout,'(i5,a4,100(2f8.2,4x))') i,restyp(itype(i)),
440      &      (rad2deg*thetatpl(ki,i),
441      &      rad2deg/dsqrt(sigma_theta(ki,i)),ki=1,constr_homology)
442        enddo
443        write (iout,*) "SC restraints from templates"
444        do i=nnt,nct
445         write(iout,'(i5,100(4f8.2,4x))') i,
446      &  (xxtpl(ki,i),yytpl(ki,i),zztpl(ki,i),
447      &   1.0d0/dsqrt(sigma_d(ki,i)),ki=1,constr_homology)
448        enddo
449 c      endif
450 c -----------------------------------------------------------------
451       return
452       end
453 c----------------------------------------------------------------------
454       subroutine read_klapaucjusz
455
456       include 'DIMENSIONS'
457       include 'DIMENSIONS.ZSCOPT'
458       include 'DIMENSIONS.FREE'
459 #ifdef MPI
460       include 'mpif.h'
461 #endif
462       include 'COMMON.SETUP'
463       include 'COMMON.CONTROL'
464       include 'COMMON.HOMOLOGY'
465       include 'COMMON.CHAIN'
466       include 'COMMON.IOUNITS'
467       include 'COMMON.GEO'
468       include 'COMMON.INTERACT'
469       include 'COMMON.NAMES'
470       include 'COMMON.HOMRESTR'
471       character*256 fragfile
472       integer ninclust(maxclust),inclust(max_template,maxclust),
473      &  nresclust(maxclust),iresclust(maxres,maxclust)
474
475       character*2 kic2
476       character*24 model_ki_dist, model_ki_angle
477       character*500 controlcard
478       integer ki, i, j, k, l, ii_in_use(maxdim),i_tmp,idomain_tmp
479       integer idomain(max_template,maxres)
480       logical lprn /.true./
481       integer ilen
482       external ilen
483       logical liiflag
484 c
485 c
486       double precision rescore_tmp,x12,y12,z12,rescore2_tmp
487       double precision, dimension (max_template,maxres) :: rescore
488       double precision, dimension (max_template,maxres) :: rescore2
489       character*24 tpl_k_rescore
490
491 c
492 c For new homol impl
493 c
494       include 'COMMON.VAR'
495 c
496       call getenv("FRAGFILE",fragfile) 
497       open(ientin,file=fragfile,status="old",err=10)
498       read(ientin,*) constr_homology,nclust
499       l_homo = .false.
500       sigma_theta=0.0
501       sigma_d=0.0
502       sigma_dih=0.0
503 c Read pdb files 
504       do k=1,constr_homology 
505         read(ientin,'(a)') pdbfile
506         write (iout,'(a,5x,a)') 'KLAPAUCJUSZ: Opening PDB file',
507      &  pdbfile(:ilen(pdbfile))
508         open(ipdbin,file=pdbfile,status='old',err=33)
509         goto 34
510   33    write (iout,'(a,5x,a)') 'Error opening PDB file',
511      &  pdbfile(:ilen(pdbfile))
512         stop
513   34    continue
514         unres_pdb=.false.
515         call readpdb_template(k)
516 c        do i=1,2*nres
517 c          do j=1,3
518 c            chomo(j,i,k)=c(j,i)
519 c          enddo
520 c        enddo
521         do i=1,nres
522           rescore(k,i)=0.2d0
523           rescore2(k,i)=1.0d0
524         enddo
525       enddo
526 c Read clusters
527       do i=1,nclust
528         read(ientin,*) ninclust(i),nresclust(i)
529         read(ientin,*) (inclust(k,i),k=1,ninclust(i))
530         read(ientin,*) (iresclust(k,i),k=1,nresclust(i))
531       enddo
532 c
533 c Loop over clusters
534 c
535       do l=1,nclust
536         do ll = 1,ninclust(l)
537         
538         k = inclust(ll,l)
539         do i=1,nres
540           idomain(k,i)=0
541         enddo
542         do i=1,nresclust(l)
543           if (nnt.gt.1)  then
544             idomain(k,iresclust(i,l)+1) = 1
545           else
546             idomain(k,iresclust(i,l)) = 1
547           endif
548         enddo
549 c
550 c     Distance restraints
551 c
552 c          ... --> odl(k,ii)
553 C Copy the coordinates from reference coordinates (?)
554         do i=1,2*nres
555           do j=1,3
556             c(j,i)=chomo(j,i,k)
557 c           write (iout,*) "c(",j,i,") =",c(j,i)
558           enddo
559         enddo
560         call int_from_cart(.true.,.false.)
561         call sc_loc_geom(.false.)
562         do i=1,nres
563           thetaref(i)=theta(i)
564           phiref(i)=phi(i)
565         enddo
566         if (waga_dist.ne.0.0d0) then
567           ii=0
568           do i = nnt,nct-2 
569             do j=i+2,nct 
570
571               x12=c(1,i)-c(1,j)
572               y12=c(2,i)-c(2,j)
573               z12=c(3,i)-c(3,j)
574               distal=dsqrt(x12*x12+y12*y12+z12*z12) 
575 c              write (iout,*) k,i,j,distal,dist2_cut
576
577             if (idomain(k,i).eq.idomain(k,j).and.idomain(k,i).ne.0
578      &            .and. distal.le.dist2_cut ) then
579
580               ii=ii+1
581               ii_in_use(ii)=1
582               l_homo(k,ii)=.true.
583
584 c             write (iout,*) "k",k
585 c             write (iout,*) "i",i," j",j," constr_homology",
586 c    &                       constr_homology
587               ires_homo(ii)=i
588               jres_homo(ii)=j
589               odl(k,ii)=distal
590               if (read2sigma) then
591                 sigma_odl(k,ii)=0
592                 do ik=i,j
593                  sigma_odl(k,ii)=sigma_odl(k,ii)+rescore2(k,ik)
594                 enddo
595                 sigma_odl(k,ii)=sigma_odl(k,ii)/(j-i+1)
596                 if (odl(k,ii).gt.dist_cut) sigma_odl(k,ii) = 
597      &        sigma_odl(k,ii)*dexp(0.5d0*(odl(k,ii)/dist_cut)**2-0.5d0)
598               else
599                 if (odl(k,ii).le.dist_cut) then
600                  sigma_odl(k,ii)=rescore(k,i)+rescore(k,j) 
601                 else
602 #ifdef OLDSIGMA
603                  sigma_odl(k,ii)=(rescore(k,i)+rescore(k,j))* 
604      &                      dexp(0.5d0*(odl(k,ii)/dist_cut)**2)
605 #else
606                  sigma_odl(k,ii)=(rescore(k,i)+rescore(k,j))* 
607      &                      dexp(0.5d0*(odl(k,ii)/dist_cut)**2-0.5d0)
608 #endif
609                 endif
610               endif
611               sigma_odl(k,ii)=1.0d0/(sigma_odl(k,ii)*sigma_odl(k,ii)) 
612             else
613               ii=ii+1
614 c              l_homo(k,ii)=.false.
615             endif
616             enddo
617           enddo
618         lim_odl=ii
619         endif
620 c
621 c     Theta, dihedral and SC retraints
622 c
623         if (waga_angle.gt.0.0d0) then
624           do i = nnt+3,nct 
625             if (idomain(k,i).eq.0) then 
626 c               sigma_dih(k,i)=0.0
627                cycle
628             endif
629             dih(k,i)=phiref(i)
630             sigma_dih(k,i)=(rescore(k,i)+rescore(k,i-1)+
631      &                     rescore(k,i-2)+rescore(k,i-3))/4.0
632 c            write (iout,*) "k",k," l",l," i",i," rescore",rescore(k,i),
633 c     &       " sigma_dihed",sigma_dih(k,i)
634             if (sigma_dih(k,i).ne.0)
635      &       sigma_dih(k,i)=1.0d0/(sigma_dih(k,i)*sigma_dih(k,i))
636           enddo
637           lim_dih=nct-nnt-2 
638         endif
639
640         if (waga_theta.gt.0.0d0) then
641           do i = nnt+2,nct
642              if (idomain(k,i).eq.0) then  
643 c              sigma_theta(k,i)=0.0
644               cycle
645              endif
646              thetatpl(k,i)=thetaref(i)
647              sigma_theta(k,i)=(rescore(k,i)+rescore(k,i-1)+
648      &                        rescore(k,i-2))/3.0
649              if (sigma_theta(k,i).ne.0)
650      &       sigma_theta(k,i)=1.0d0/(sigma_theta(k,i)*sigma_theta(k,i))
651           enddo
652         endif
653
654         if (waga_d.gt.0.0d0) then
655           do i = nnt,nct
656                if (itype(i).eq.10) cycle 
657                if (idomain(k,i).eq.0 ) then 
658 c                  sigma_d(k,i)=0.0
659                   cycle
660                endif
661                xxtpl(k,i)=xxref(i)
662                yytpl(k,i)=yyref(i)
663                zztpl(k,i)=zzref(i)
664                sigma_d(k,i)=rescore(k,i)
665                if (sigma_d(k,i).ne.0)
666      &          sigma_d(k,i)=1.0d0/(sigma_d(k,i)*sigma_d(k,i))
667                if (i-nnt+1.gt.lim_xx) lim_xx=i-nnt+1
668           enddo
669         endif
670       enddo ! l
671       enddo ! ll
672 c
673 c remove distance restraints not used in any model from the list
674 c shift data in all arrays
675 c
676       if (waga_dist.ne.0.0d0) then
677         ii=0
678         liiflag=.true.
679         do i=nnt,nct-2 
680          do j=i+2,nct 
681           ii=ii+1
682           if (ii_in_use(ii).eq.0.and.liiflag) then
683             liiflag=.false.
684             iistart=ii
685           endif
686           if (ii_in_use(ii).ne.0.and..not.liiflag.or.
687      &                   .not.liiflag.and.ii.eq.lim_odl) then
688              if (ii.eq.lim_odl) then
689               iishift=ii-iistart+1
690              else
691               iishift=ii-iistart
692              endif
693              liiflag=.true.
694              do ki=iistart,lim_odl-iishift
695               ires_homo(ki)=ires_homo(ki+iishift)
696               jres_homo(ki)=jres_homo(ki+iishift)
697               ii_in_use(ki)=ii_in_use(ki+iishift)
698               do k=1,constr_homology
699                odl(k,ki)=odl(k,ki+iishift)
700                sigma_odl(k,ki)=sigma_odl(k,ki+iishift)
701                l_homo(k,ki)=l_homo(k,ki+iishift)
702               enddo
703              enddo
704              ii=ii-iishift
705              lim_odl=lim_odl-iishift
706           endif
707          enddo
708         enddo
709       endif
710 #ifdef DEBUG
711       write (iout,*) "ires_homo and jres_homo arrays, lim_odl",lim_odl
712       do i=1,lim_odl
713         write (iout,*) i,ires_homo(i),jres_homo(i)
714       enddo
715 #endif
716       return
717    10 stop "Error in fragment file"
718       end