ab9901d31b80752847d765b4dc2aaa1a686c6449
[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 c               sigma_d(k,i)=rescore(k,i) !  right expression ?
362                sigma_d(k,i)=rescore3(k,i) !  right expression ?
363                if (sigma_d(k,i).ne.0)
364      &          sigma_d(k,i)=1.0d0/(sigma_d(k,i)*sigma_d(k,i))
365
366 c              sigma_d(k,i)=hmscore(k)*rescore(k,i) !  right expression ?
367 c              sigma_d(k,i)=sigma_d(k,i)*sigma_d(k,i)
368 c              read (ientin,*) sigma_d(k,i) ! 1st variant
369           enddo
370         endif
371       enddo
372 c
373 c remove distance restraints not used in any model from the list
374 c shift data in all arrays
375 c
376       if (waga_dist.ne.0.0d0) then
377         ii=0
378         liiflag=.true.
379         do i=nnt,nct-2 
380          do j=i+2,nct 
381           ii=ii+1
382           if (ii_in_use(ii).eq.0.and.liiflag) then
383             liiflag=.false.
384             iistart=ii
385           endif
386           if (ii_in_use(ii).ne.0.and..not.liiflag.or.
387      &                   .not.liiflag.and.ii.eq.lim_odl) then
388              if (ii.eq.lim_odl) then
389               iishift=ii-iistart+1
390              else
391               iishift=ii-iistart
392              endif
393              liiflag=.true.
394              do ki=iistart,lim_odl-iishift
395               ires_homo(ki)=ires_homo(ki+iishift)
396               jres_homo(ki)=jres_homo(ki+iishift)
397               ii_in_use(ki)=ii_in_use(ki+iishift)
398               do k=1,constr_homology
399                odl(k,ki)=odl(k,ki+iishift)
400                sigma_odl(k,ki)=sigma_odl(k,ki+iishift)
401                l_homo(k,ki)=l_homo(k,ki+iishift)
402               enddo
403              enddo
404              ii=ii-iishift
405              lim_odl=lim_odl-iishift
406           endif
407          enddo
408         enddo
409       endif
410
411       endif ! .not. klapaucjusz     
412
413       if (constr_homology.gt.0) call homology_partition
414       if (constr_homology.gt.0) call init_int_table
415 cd      write (iout,*) "homology_partition: lim_theta= ",lim_theta,
416 cd     & "lim_xx=",lim_xx
417 c     write (iout,*) "ithet_start =",ithet_start,"ithet_end =",ithet_end
418 c     write (iout,*) "loc_start =",loc_start,"loc_end =",loc_end
419 c
420 c Print restraints
421 c
422       if (.not.lprn) return
423 cd      write(iout,*) "waga_theta",waga_theta,"waga_d",waga_d
424 c      if(me.eq.king .or. .not. out1file .and. fg_rank.eq.0) then
425        write (iout,*) "Distance restraints from templates"
426        do ii=1,lim_odl
427        write(iout,'(3i5,100(2f8.2,1x,l1,4x))') 
428      &  ii,ires_homo(ii),jres_homo(ii),
429      &  (odl(ki,ii),1.0d0/dsqrt(sigma_odl(ki,ii)),l_homo(ki,ii),
430      &  ki=1,constr_homology)
431        enddo
432        write (iout,*) "Dihedral angle restraints from templates"
433        do i=nnt+3,nct
434         write (iout,'(i5,a4,100(2f8.2,4x))') i,restyp(itype(i)),
435      &      (rad2deg*dih(ki,i),
436      &      rad2deg/dsqrt(sigma_dih(ki,i)),ki=1,constr_homology)
437        enddo
438        write (iout,*) "Virtual-bond angle restraints from templates"
439        do i=nnt+2,nct
440         write (iout,'(i5,a4,100(2f8.2,4x))') i,restyp(itype(i)),
441      &      (rad2deg*thetatpl(ki,i),
442      &      rad2deg/dsqrt(sigma_theta(ki,i)),ki=1,constr_homology)
443        enddo
444        write (iout,*) "SC restraints from templates"
445        do i=nnt,nct
446         write(iout,'(i5,100(4f8.2,4x))') i,
447      &  (xxtpl(ki,i),yytpl(ki,i),zztpl(ki,i),
448      &   1.0d0/dsqrt(sigma_d(ki,i)),ki=1,constr_homology)
449        enddo
450 c      endif
451 c -----------------------------------------------------------------
452       return
453       end
454 c----------------------------------------------------------------------
455       subroutine read_klapaucjusz
456
457       include 'DIMENSIONS'
458       include 'DIMENSIONS.ZSCOPT'
459       include 'DIMENSIONS.FREE'
460 #ifdef MPI
461       include 'mpif.h'
462 #endif
463       include 'COMMON.SETUP'
464       include 'COMMON.CONTROL'
465       include 'COMMON.HOMOLOGY'
466       include 'COMMON.CHAIN'
467       include 'COMMON.IOUNITS'
468       include 'COMMON.GEO'
469       include 'COMMON.INTERACT'
470       include 'COMMON.NAMES'
471       include 'COMMON.HOMRESTR'
472       character*256 fragfile
473       integer ninclust(maxclust),inclust(max_template,maxclust),
474      &  nresclust(maxclust),iresclust(maxres,maxclust)
475
476       character*2 kic2
477       character*24 model_ki_dist, model_ki_angle
478       character*500 controlcard
479       integer ki, i, j, k, l, ii_in_use(maxdim),i_tmp,idomain_tmp
480       integer idomain(max_template,maxres)
481       logical lprn /.true./
482       integer ilen
483       external ilen
484       logical liiflag
485 c
486 c
487       double precision rescore_tmp,x12,y12,z12,rescore2_tmp
488       double precision, dimension (max_template,maxres) :: rescore
489       double precision, dimension (max_template,maxres) :: rescore2
490       character*24 tpl_k_rescore
491
492 c
493 c For new homol impl
494 c
495       include 'COMMON.VAR'
496 c
497       call getenv("FRAGFILE",fragfile) 
498       open(ientin,file=fragfile,status="old",err=10)
499       read(ientin,*) constr_homology,nclust
500       l_homo = .false.
501       sigma_theta=0.0
502       sigma_d=0.0
503       sigma_dih=0.0
504 c Read pdb files 
505       do k=1,constr_homology 
506         read(ientin,'(a)') pdbfile
507         write (iout,'(a,5x,a)') 'KLAPAUCJUSZ: Opening PDB file',
508      &  pdbfile(:ilen(pdbfile))
509         open(ipdbin,file=pdbfile,status='old',err=33)
510         goto 34
511   33    write (iout,'(a,5x,a)') 'Error opening PDB file',
512      &  pdbfile(:ilen(pdbfile))
513         stop
514   34    continue
515         unres_pdb=.false.
516         call readpdb_template(k)
517 c        do i=1,2*nres
518 c          do j=1,3
519 c            chomo(j,i,k)=c(j,i)
520 c          enddo
521 c        enddo
522         do i=1,nres
523           rescore(k,i)=0.2d0
524           rescore2(k,i)=1.0d0
525         enddo
526       enddo
527 c Read clusters
528       do i=1,nclust
529         read(ientin,*) ninclust(i),nresclust(i)
530         read(ientin,*) (inclust(k,i),k=1,ninclust(i))
531         read(ientin,*) (iresclust(k,i),k=1,nresclust(i))
532       enddo
533 c
534 c Loop over clusters
535 c
536       do l=1,nclust
537         do ll = 1,ninclust(l)
538         
539         k = inclust(ll,l)
540         do i=1,nres
541           idomain(k,i)=0
542         enddo
543         do i=1,nresclust(l)
544           if (nnt.gt.1)  then
545             idomain(k,iresclust(i,l)+1) = 1
546           else
547             idomain(k,iresclust(i,l)) = 1
548           endif
549         enddo
550 c
551 c     Distance restraints
552 c
553 c          ... --> odl(k,ii)
554 C Copy the coordinates from reference coordinates (?)
555         do i=1,2*nres
556           do j=1,3
557             c(j,i)=chomo(j,i,k)
558 c           write (iout,*) "c(",j,i,") =",c(j,i)
559           enddo
560         enddo
561         call int_from_cart(.true.,.false.)
562         call sc_loc_geom(.false.)
563         do i=1,nres
564           thetaref(i)=theta(i)
565           phiref(i)=phi(i)
566         enddo
567         if (waga_dist.ne.0.0d0) then
568           ii=0
569           do i = nnt,nct-2 
570             do j=i+2,nct 
571
572               x12=c(1,i)-c(1,j)
573               y12=c(2,i)-c(2,j)
574               z12=c(3,i)-c(3,j)
575               distal=dsqrt(x12*x12+y12*y12+z12*z12) 
576 c              write (iout,*) k,i,j,distal,dist2_cut
577
578             if (idomain(k,i).eq.idomain(k,j).and.idomain(k,i).ne.0
579      &            .and. distal.le.dist2_cut ) then
580
581               ii=ii+1
582               ii_in_use(ii)=1
583               l_homo(k,ii)=.true.
584
585 c             write (iout,*) "k",k
586 c             write (iout,*) "i",i," j",j," constr_homology",
587 c    &                       constr_homology
588               ires_homo(ii)=i
589               jres_homo(ii)=j
590               odl(k,ii)=distal
591               if (read2sigma) then
592                 sigma_odl(k,ii)=0
593                 do ik=i,j
594                  sigma_odl(k,ii)=sigma_odl(k,ii)+rescore2(k,ik)
595                 enddo
596                 sigma_odl(k,ii)=sigma_odl(k,ii)/(j-i+1)
597                 if (odl(k,ii).gt.dist_cut) sigma_odl(k,ii) = 
598      &        sigma_odl(k,ii)*dexp(0.5d0*(odl(k,ii)/dist_cut)**2-0.5d0)
599               else
600                 if (odl(k,ii).le.dist_cut) then
601                  sigma_odl(k,ii)=rescore(k,i)+rescore(k,j) 
602                 else
603 #ifdef OLDSIGMA
604                  sigma_odl(k,ii)=(rescore(k,i)+rescore(k,j))* 
605      &                      dexp(0.5d0*(odl(k,ii)/dist_cut)**2)
606 #else
607                  sigma_odl(k,ii)=(rescore(k,i)+rescore(k,j))* 
608      &                      dexp(0.5d0*(odl(k,ii)/dist_cut)**2-0.5d0)
609 #endif
610                 endif
611               endif
612               sigma_odl(k,ii)=1.0d0/(sigma_odl(k,ii)*sigma_odl(k,ii)) 
613             else
614               ii=ii+1
615 c              l_homo(k,ii)=.false.
616             endif
617             enddo
618           enddo
619         lim_odl=ii
620         endif
621 c
622 c     Theta, dihedral and SC retraints
623 c
624         if (waga_angle.gt.0.0d0) then
625           do i = nnt+3,nct 
626             if (idomain(k,i).eq.0) then 
627 c               sigma_dih(k,i)=0.0
628                cycle
629             endif
630             dih(k,i)=phiref(i)
631             sigma_dih(k,i)=(rescore(k,i)+rescore(k,i-1)+
632      &                     rescore(k,i-2)+rescore(k,i-3))/4.0
633 c            write (iout,*) "k",k," l",l," i",i," rescore",rescore(k,i),
634 c     &       " sigma_dihed",sigma_dih(k,i)
635             if (sigma_dih(k,i).ne.0)
636      &       sigma_dih(k,i)=1.0d0/(sigma_dih(k,i)*sigma_dih(k,i))
637           enddo
638           lim_dih=nct-nnt-2 
639         endif
640
641         if (waga_theta.gt.0.0d0) then
642           do i = nnt+2,nct
643              if (idomain(k,i).eq.0) then  
644 c              sigma_theta(k,i)=0.0
645               cycle
646              endif
647              thetatpl(k,i)=thetaref(i)
648              sigma_theta(k,i)=(rescore(k,i)+rescore(k,i-1)+
649      &                        rescore(k,i-2))/3.0
650              if (sigma_theta(k,i).ne.0)
651      &       sigma_theta(k,i)=1.0d0/(sigma_theta(k,i)*sigma_theta(k,i))
652           enddo
653         endif
654
655         if (waga_d.gt.0.0d0) then
656           do i = nnt,nct
657                if (itype(i).eq.10) cycle 
658                if (idomain(k,i).eq.0 ) then 
659 c                  sigma_d(k,i)=0.0
660                   cycle
661                endif
662                xxtpl(k,i)=xxref(i)
663                yytpl(k,i)=yyref(i)
664                zztpl(k,i)=zzref(i)
665                sigma_d(k,i)=rescore(k,i)
666                if (sigma_d(k,i).ne.0)
667      &          sigma_d(k,i)=1.0d0/(sigma_d(k,i)*sigma_d(k,i))
668                if (i-nnt+1.gt.lim_xx) lim_xx=i-nnt+1
669           enddo
670         endif
671       enddo ! l
672       enddo ! ll
673 c
674 c remove distance restraints not used in any model from the list
675 c shift data in all arrays
676 c
677       if (waga_dist.ne.0.0d0) then
678         ii=0
679         liiflag=.true.
680         do i=nnt,nct-2 
681          do j=i+2,nct 
682           ii=ii+1
683           if (ii_in_use(ii).eq.0.and.liiflag) then
684             liiflag=.false.
685             iistart=ii
686           endif
687           if (ii_in_use(ii).ne.0.and..not.liiflag.or.
688      &                   .not.liiflag.and.ii.eq.lim_odl) then
689              if (ii.eq.lim_odl) then
690               iishift=ii-iistart+1
691              else
692               iishift=ii-iistart
693              endif
694              liiflag=.true.
695              do ki=iistart,lim_odl-iishift
696               ires_homo(ki)=ires_homo(ki+iishift)
697               jres_homo(ki)=jres_homo(ki+iishift)
698               ii_in_use(ki)=ii_in_use(ki+iishift)
699               do k=1,constr_homology
700                odl(k,ki)=odl(k,ki+iishift)
701                sigma_odl(k,ki)=sigma_odl(k,ki+iishift)
702                l_homo(k,ki)=l_homo(k,ki+iishift)
703               enddo
704              enddo
705              ii=ii-iishift
706              lim_odl=lim_odl-iishift
707           endif
708          enddo
709         enddo
710       endif
711 #ifdef DEBUG
712       write (iout,*) "ires_homo and jres_homo arrays, lim_odl",lim_odl
713       do i=1,lim_odl
714         write (iout,*) i,ires_homo(i),jres_homo(i)
715       enddo
716 #endif
717       return
718    10 stop "Error in fragment file"
719       end