2727
2828import java .io .File ;
2929
30- import java .util .SortedSet ;
31- import java .util .concurrent .ExecutorService ;
3230
33- import java .util .concurrent .ThreadPoolExecutor ;
3431import java .util .concurrent .atomic .AtomicBoolean ;
3532import java .util .logging .Logger ;
3633
3734
38- import org .biojava .bio .structure .Atom ;
39-
4035import org .biojava .bio .structure .Structure ;
4136
42- import org . biojava . bio . structure . StructureTools ;
43- import org .biojava .bio .structure .align .CallableStructureAlignment ;
37+
38+ import org .biojava .bio .structure .align .MultiThreadedDBSearch ;
4439import org .biojava .bio .structure .align .StructureAlignment ;
45- import org .biojava .bio .structure .align .ce .CeCPMain ;
46- import org .biojava .bio .structure .align .ce .CeMain ;
47- import org .biojava .bio .structure .align .ce .CeParameters ;
48- import org .biojava .bio .structure .align .ce .CeSideChainMain ;
49- import org .biojava .bio .structure .align .ce .ConfigStrucAligParams ;
50- import org .biojava .bio .structure .align .client .FarmJobParameters ;
51- import org .biojava .bio .structure .align .client .JFatCatClient ;
52- import org .biojava .bio .structure .align .client .PdbPair ;
53- import org .biojava .bio .structure .align .model .AFPChain ;
40+
5441import org .biojava .bio .structure .align .util .AtomCache ;
55- import org .biojava .bio .structure .align .util .SynchronizedOutFile ;
56- import org .biojava .bio .structure .align .util .UserConfiguration ;
57- import org .biojava .bio .structure .domain .DomainProvider ;
58- import org .biojava .bio .structure .domain .DomainProviderFactory ;
59- import org .biojava .bio .structure .domain .RemoteDomainProvider ;
6042
61- import org .biojava3 . core . util .ConcurrencyTools ;
43+ import org .biojava . bio . structure . align . util .UserConfiguration ;
6244
6345public class AlignmentCalcDB implements AlignmentCalculationRunnable {
6446 public static Logger logger = Logger .getLogger ("org.biojava" );
@@ -73,15 +55,17 @@ public class AlignmentCalcDB implements AlignmentCalculationRunnable {
7355 AlignmentGui parent ;
7456
7557 UserConfiguration config ;
76- SortedSet < String > representatives ;
58+
7759
7860 String outFile ;
79- File resultList ;
61+
8062 int nrCPUs ;
8163 Boolean domainSplit ;
82-
64+
8365 StructureAlignment customAlgorithm ;
84-
66+
67+ MultiThreadedDBSearch job = null ;
68+
8569 public StructureAlignment getAlgorithm () {
8670 return customAlgorithm ;
8771 }
@@ -105,167 +89,33 @@ public AlignmentCalcDB(AlignmentGui parent, Structure s1, String name1, UserCon
10589 this .domainSplit = domainSplit ;
10690 }
10791
108- public static String getLegend (String algorithmName ){
109-
110- if ( algorithmName .equalsIgnoreCase (CeMain .algorithmName ) ||
111- algorithmName .equalsIgnoreCase (CeSideChainMain .algorithmName ) ||
112- algorithmName .equalsIgnoreCase (CeCPMain .algorithmName )) {
113- return "# name1\t name2\t score\t z-score\t rmsd\t len1\t len2\t cov1\t cov2\t %ID\t Description\t " ;
114- }
115-
116- // looks like a FATCAT alignment
117-
118- return "# name1\t name2\t score\t probability\t rmsd\t len1\t len2\t cov1\t cov2\t %ID\t Description\t " ;
119-
120- }
121-
92+
93+
12294 public void run () {
12395
124- AtomCache cache = new AtomCache (config );
125-
12696 StructureAlignment algorithm = null ;
97+
12798 if ( parent != null )
12899 algorithm = parent .getStructureAlignment ();
129100 else {
130101 algorithm = customAlgorithm ;
131102 }
132- String serverLocation = FarmJobParameters .DEFAULT_SERVER_URL ;
133- if ( representatives == null ){
134- SortedSet <String > repre = JFatCatClient .getRepresentatives (serverLocation ,40 );
135- System .out .println ("got " + repre .size () + " representatives for comparison" );
136- representatives = repre ;
137- }
138-
139- String header = "# algorithm:" + algorithm .getAlgorithmName ();
140-
141- String legend = getLegend (algorithm .getAlgorithmName ());
142-
143-
144-
145- File outFileF = new File (outFile );
146- if ( ! outFileF .isDirectory ()){
147- System .err .println ( outFileF .getAbsolutePath () + " is not a directory, can't create result files in there... " );
148- interrupt ();
149- cleanup ();
150- }
151103
152- if ( name1 == null )
153- name1 = "CUSTOM" ;
154104
155- SynchronizedOutFile out ;
105+ job = new MultiThreadedDBSearch ( name1 , structure1 , outFile , algorithm , nrCPUs , domainSplit ) ;
156106
157- resultList = new File (outFileF ,"results_" + name1 + ".out" );
158-
159- try {
160-
161- out = new SynchronizedOutFile (resultList );
162-
163- out .write (header );
164- out .write (AFPChain .newline );
165- out .write (legend );
166- out .write (AFPChain .newline );
167-
168- if ( name1 .equals ("CUSTOM" )) {
169-
170- String config1 = "#param:file1=" + parent .getDBSearch ().getPDBUploadPanel ().getFilePath1 ();
171- out .write (config1 );
172- out .write (AFPChain .newline );
173-
174- String config2 = "#param:chain1=" + parent .getDBSearch ().getPDBUploadPanel ().getChain1 ();
175- out .write (config2 );
176- out .write (AFPChain .newline );
177-
178- }
179-
180- if ( algorithm .getAlgorithmName ().startsWith ("jCE" )){
181- ConfigStrucAligParams params = algorithm .getParameters ();
182- if ( params instanceof CeParameters ){
183- CeParameters ceParams = (CeParameters ) params ;
184- if ( ceParams .getScoringStrategy () != CeParameters .DEFAULT_SCORING_STRATEGY ) {
185- String scoring = "#param:scoring=" + ceParams .getScoringStrategy ();
186- out .write (scoring );
187- out .write (AFPChain .newline );
188- }
189- }
190- }
191-
192- } catch (Exception e ){
193- System .err .println ("Error while loading representative structure " + name1 );
194- e .printStackTrace ();
195- interrupt ();
196- cleanup ();
197- return ;
198- }
199-
200-
201-
202- DomainProvider domainProvider = DomainProviderFactory .getDomainProvider ();
203-
204- ConcurrencyTools .setThreadPoolSize (nrCPUs );
205-
206- Atom [] ca1 = StructureTools .getAtomCAArray (structure1 );
207-
208- int nrJobs = 0 ;
209- for (String repre : representatives ){
210-
211- if ( domainSplit ) {
212- SortedSet <String > domainNames = domainProvider .getDomainNames (repre );
213- //System.out.println(repre +" got domains: " +domainNames);
214- if ( domainNames == null || domainNames .size ()==0 ){
215- // no domains found, use whole chain.
216- submit (name1 , repre , ca1 , algorithm , outFileF , out , cache );
217- nrJobs ++;
218- continue ;
219- }
220- //System.out.println("got " + domainNames.size() + " for " + repre);
221- for ( String domain : domainNames ){
222- submit (name1 , domain , ca1 , algorithm , outFileF , out , cache );
223- nrJobs ++;
224- }
225- } else {
226- submit (name1 , repre , ca1 , algorithm , outFileF , out , cache );
227- nrJobs ++;
228- }
229-
230- }
231-
232-
233- ThreadPoolExecutor pool = ConcurrencyTools .getThreadPool ();
234- System .out .println (pool .getPoolSize ());
235-
236- long startTime = System .currentTimeMillis ();
237-
238- try {
239- while ( pool .getCompletedTaskCount () < nrJobs -1 ) {
240- //long now = System.currentTimeMillis();
241- //System.out.println( pool.getCompletedTaskCount() + " " + (now-startTime)/1000 + " " + pool.getPoolSize() + " " + pool.getActiveCount() + " " + pool.getTaskCount() );
242- // if ((now-startTime)/1000 > 60) {
243- //
244- // interrupt();
245- // System.out.println("completed: " + pool.getCompletedTaskCount());
246- // }
247-
248- if ( interrupted .get ())
249- break ;
250-
251- Thread .sleep (2000 );
107+ AtomCache cache = new AtomCache (config );
108+ System .out .println ("using cache: " + cache .getPath ());
109+ job .setAtomCache (cache );
252110
253- }
254- out .close ();
255- }
256- catch (Exception e ){
257- e .printStackTrace ();
258- interrupt ();
259- cleanup ();
111+ if ( name1 .equals ("CUSTOM" )) {
112+ job .setCustomFile1 (parent .getDBSearch ().getPDBUploadPanel ().getFilePath1 ());
113+ job .setCustomChain1 (parent .getDBSearch ().getPDBUploadPanel ().getChain1 ());
260114 }
261115
262- if (domainProvider instanceof RemoteDomainProvider ){
263- RemoteDomainProvider remote = (RemoteDomainProvider ) domainProvider ;
264- remote .flushCache ();
265- }
266- long now = System .currentTimeMillis ();
267- System .out .println ("Calculation took : " + (now -startTime )/1000 + " sec." );
268- System .out .println ( pool .getCompletedTaskCount () + " " + pool .getPoolSize () + " " + pool .getActiveCount () + " " + pool .getTaskCount () );
116+ job .run ();
117+
118+ File resultList = job .getResultFile ();
269119 // if ((now-startTime)/1000 > 30) {
270120
271121
@@ -277,44 +127,28 @@ public void run() {
277127 // }
278128 if ( parent != null ) {
279129 parent .notifyCalcFinished ();
280- DBResultTable table = new DBResultTable ();
281- table .show (resultList ,config );
130+ if ( resultList != null ) {
131+ DBResultTable table = new DBResultTable ();
132+ table .show (resultList ,config );
133+ }
282134 }
283-
284- }
285-
286135
136+ }
287137
288- private void submit (String name12 , String repre , Atom [] ca1 , StructureAlignment algorithm , File outFileF , SynchronizedOutFile out , AtomCache cache ) {
289- CallableStructureAlignment ali = new CallableStructureAlignment ();
290138
291- PdbPair pair = new PdbPair (name1 , repre );
292- try {
293- ali .setCa1 (ca1 );
294- } catch (Exception e ){
295- e .printStackTrace ();
296- ConcurrencyTools .shutdown ();
297- return ;
298- }
299- ali .setAlgorithmName (algorithm .getAlgorithmName ());
300- ali .setParameters (algorithm .getParameters ());
301- ali .setPair (pair );
302- ali .setOutFile (out );
303- ali .setOutputDir (outFileF );
304- ali .setCache (cache );
305139
306- ConcurrencyTools .submit (ali );
307140
308- }
309141
310142 /** stops what is currently happening and does not continue
311143 *
312144 *
313145 */
314146 public void interrupt () {
315147 interrupted .set (true );
316- ExecutorService pool = ConcurrencyTools .getThreadPool ();
317- pool .shutdownNow ();
148+ if ( job != null )
149+ job .interrupt ();
150+
151+
318152
319153 }
320154
@@ -328,6 +162,9 @@ public void cleanup()
328162 structure1 = null ;
329163 config = null ;
330164
165+ if ( job != null )
166+ job .cleanup ();
167+
331168 }
332169
333170 public void setNrCPUs (int useNrCPUs ) {
0 commit comments