001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.domain.pdp;
022
023import org.biojava.nbio.structure.Atom;
024
025import java.util.ArrayList;
026import java.util.List;
027
028
029public class CutDomain {
030
031        int ndom;
032
033        List<Domain> domains;
034
035        public static boolean verbose = true;
036
037        int[][] dist;
038        Atom[] ca;
039
040        public CutDomain(Atom[]ca, PDPDistanceMatrix pdpMatrix){
041                dist = pdpMatrix.getDist();
042                this.ca = ca;
043
044                ndom = 0;
045
046                domains = new ArrayList<Domain>();
047
048        }
049
050
051        public  void cutDomain(Domain dom, CutSites cut_sites, PDPDistanceMatrix pdpMatrix){
052
053                if ( verbose )
054                System.out.println("  B ... beginning of cutDomain " +dom + " cutsites: " + cut_sites );
055
056                /* recursive function to cut input domain into two domains */
057
058
059                int i,site;
060
061                Domain dom1 = new Domain();
062                Domain dom2 = new Domain();
063
064                CutValues val = new CutValues();
065                val.s_min = 100;
066                val.site2 = 0;
067                val.first_cut = true;
068
069
070                Cut cut = new Cut();
071
072                site = cut.cut(ca,dom,val, dist, pdpMatrix);
073                if ( verbose )
074                System.out.println("  S ... site " + dom + " : site: " + site + " val : " + val);
075
076                if(site<0) {
077
078                        /* function cut makes a decision where to cut , returns -1 if no cut */
079                        //memcpy(&domains[ndom],&dom,sizeof(struct Domain));
080                        domains.add(dom);
081                        dom.score = val.s_min;
082                        //dom = domains[ndom];
083                        ndom++;
084                        return;
085                }
086
087                if(verbose)
088                        System.out.printf("   C ... Cutting at position(s): %d %d %f%n%n",site,val.site2,dom.score);
089
090                cut_sites.cut_sites[cut_sites.ncuts++] = site;
091
092                /* create new domains: dom1 and dom2*/
093                dom1.size = 0;
094                dom1.nseg = 0;
095                dom2.size = 0;
096                dom2.nseg = 0;
097                if(val.site2==0) { /* single cut*/
098                        for(i=0;i<dom.nseg;i++) {
099                                if(site>dom.getSegmentAtPos(i).getTo()) {
100                                        dom1.getSegmentAtPos(dom1.nseg).setTo(dom.getSegmentAtPos(i).getTo());
101                                        dom1.getSegmentAtPos(dom1.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
102                                        dom1.nseg++;
103                                        dom1.size+=(dom.getSegmentAtPos(i).getTo() - dom.getSegmentAtPos(i).getFrom() + 1);
104                                }
105                                else if(site<dom.getSegmentAtPos(i).getFrom()) {
106                                        dom2.getSegmentAtPos(dom2.nseg).setTo(dom.getSegmentAtPos(i).getTo());
107                                        dom2.getSegmentAtPos(dom2.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
108                                        dom2.nseg++;
109                                        dom2.size+=(dom.getSegmentAtPos(i).getTo() - dom.getSegmentAtPos(i).getFrom() + 1);
110                                }
111                                else if(site>dom.getSegmentAtPos(i).getFrom() &&
112                                                site<dom.getSegmentAtPos(i).getTo()) {
113                                        dom1.getSegmentAtPos(dom1.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
114                                        dom1.getSegmentAtPos(dom1.nseg).setTo(site-1);
115                                        dom1.nseg++;
116                                        dom1.size+=(site-dom.getSegmentAtPos(i).getFrom());
117                                        dom2.getSegmentAtPos(dom2.nseg).setTo(dom.getSegmentAtPos(i).getTo());
118                                        dom2.getSegmentAtPos(dom2.nseg).setFrom(site);
119                                        dom2.nseg++;
120                                        dom2.size+=(dom.getSegmentAtPos(i).getTo()-site+1);
121                                }
122                        }
123                }
124                else if(val.site2>0) { /* double cut */
125                        for(i=0;i<dom.nseg;i++) {
126                                if(site>dom.getSegmentAtPos(i).getTo()||val.site2<dom.getSegmentAtPos(i).getFrom()) {
127                                        dom1.getSegmentAtPos(dom1.nseg).setTo(dom.getSegmentAtPos(i).getTo());
128                                        dom1.getSegmentAtPos(dom1.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
129                                        dom1.nseg++;
130                                        dom1.size+=(dom.getSegmentAtPos(i).getTo() - dom.getSegmentAtPos(i).getFrom() + 1);
131                                }
132                                else if(site<dom.getSegmentAtPos(i).getFrom()&&val.site2>dom.getSegmentAtPos(i).getTo()) {
133                                        dom2.getSegmentAtPos(dom1.nseg).setTo(dom.getSegmentAtPos(i).getTo());
134                                        dom2.getSegmentAtPos(dom1.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
135                                        dom2.nseg++;
136                                        dom2.size+=(dom.getSegmentAtPos(i).getTo() - dom.getSegmentAtPos(i).getFrom() + 1);
137                                }
138                                else if(site>dom.getSegmentAtPos(i).getFrom() &&
139                                                site<dom.getSegmentAtPos(i).getTo()) {
140                                        dom1.getSegmentAtPos(dom1.nseg).setTo(site);
141                                        dom1.getSegmentAtPos(dom1.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
142                                        dom1.size+=(dom1.getSegmentAtPos(dom1.nseg).getTo() - dom1.getSegmentAtPos(dom1.nseg).getFrom() + 1);
143                                        dom1.nseg++;
144                                        dom2.getSegmentAtPos(dom2.nseg).setFrom(site+1);
145                                        if(val.site2>dom.getSegmentAtPos(i).getFrom() &&
146                                                        val.site2<dom.getSegmentAtPos(i).getTo()) {
147                                                dom2.getSegmentAtPos(dom2.nseg).setTo(val.site2-1);
148                                                dom2.size+=(dom2.getSegmentAtPos(dom2.nseg).getTo() - dom2.getSegmentAtPos(dom2.nseg).getFrom() + 1);
149                                                dom2.nseg++;
150                                                dom1.getSegmentAtPos(dom1.nseg).setFrom( val.site2);
151                                                dom1.getSegmentAtPos(dom1.nseg).setTo( dom.getSegmentAtPos(i).getTo());
152                                                dom1.size+=(dom1.getSegmentAtPos(dom1.nseg).getTo() - dom1.getSegmentAtPos(dom1.nseg).getFrom() + 1);
153                                                dom1.nseg++;
154                                        }
155                                        else {
156                                                dom2.getSegmentAtPos(dom2.nseg).setTo(dom.getSegmentAtPos(i).getTo());
157                                                dom2.size+=(dom2.getSegmentAtPos(dom2.nseg).getTo() - dom2.getSegmentAtPos(dom2.nseg).getFrom() + 1);
158                                                dom2.nseg++;
159                                        }
160                                }
161                                else if(val.site2>dom.getSegmentAtPos(i).getFrom() &&
162                                                val.site2<dom.getSegmentAtPos(i).getTo()) {
163                                        dom2.getSegmentAtPos(dom2.nseg).setTo(val.site2-1);
164                                        dom2.getSegmentAtPos(dom2.nseg).setFrom(dom.getSegmentAtPos(i).getFrom());
165                                        dom2.size+=(dom2.getSegmentAtPos(dom2.nseg).getTo() - dom2.getSegmentAtPos(dom2.nseg).getFrom() + 1);
166                                        dom2.nseg++;
167                                        dom1.getSegmentAtPos(dom1.nseg).setFrom(val.site2);
168                                        dom1.getSegmentAtPos(dom1.nseg).setTo( dom.getSegmentAtPos(i).getTo());
169                                        dom1.size+=(dom1.getSegmentAtPos(dom1.nseg).getTo() - dom1.getSegmentAtPos(dom1.nseg).getFrom() + 1);
170                                        dom1.nseg++;
171                                }
172                        }
173                }
174                if(verbose)
175                        System.out.println(String.format("  CUTR dom1 ...  nseg %d",dom1.nseg));
176
177                if ( verbose)
178                for(i=0;i<dom1.nseg;i++)
179                        System.out.println(String.format("      F ... from %d to %d",dom1.getSegmentAtPos(i).getFrom(),dom1.getSegmentAtPos(i).getTo()));
180
181                cutDomain(dom1, cut_sites, pdpMatrix);
182
183                if(verbose)
184                        System.out.println(String.format("  C ... cutr dom2: nseg %d",dom2.nseg));
185                if(verbose)
186                        for(i=0;i<dom2.nseg;i++)
187                         System.out.println(String.format("     F ... from %d to %d",dom2.getSegmentAtPos(i).getFrom(),dom2.getSegmentAtPos(i).getTo()));
188
189                cutDomain(dom2, cut_sites, pdpMatrix);
190
191                //System.out.println("end of cutDomain 0 " +dom);
192                //System.out.println("end of cutDomain 1 " +dom1);
193                //System.out.println("end of cutDomain 2 " +dom2);
194
195        }
196
197
198        public List<Domain> getDomains() {
199
200                return domains;
201        }
202
203
204
205}