-
Notifications
You must be signed in to change notification settings - Fork 3
/
profillic-alignment-hmmbuild.cpp
1562 lines (1344 loc) · 73.3 KB
/
profillic-alignment-hmmbuild.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file profillic-alignment-hmmbuild.cpp
* \brief Profile HMM construction from an alignment profile.
* \details
* Modified by Ted Holzman from the profillic-hmmbuild program. This program accepts
* a prolific alignment profile in place of the profillic (plain) profile.
<pre>
# profillic-alignment-hmmbuild :: profile HMM construction from multiple sequence alignments and galosh profiles
# profillic-hmmer 1.0a (July 2011); http://galosh.org/
# Copyright (C) 2011 Paul T. Edlefsen, Fred Hutchinson Cancer Research Center.
# HMMER 3.1dev (November 2011); http://hmmer.org/
# Copyright (C) 2011 Howard Hughes Medical Institute.
# Freely distributed under the GNU General Public License (GPLv3).
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Usage: profillic-alignment-hmmbuild [-options] <hmmfile_out> <msafile>
Basic options:
-h : show brief help on version and usage
-n <s> : name the HMM <s>
-o <f> : direct summary output to file <f>, not stdout
-O <f> : resave annotated, possibly modified MSA to file <f>
Options for selecting alphabet rather than guessing it:
--amino : input alignment is protein sequence data
--dna : input alignment is DNA sequence data
--rna : input alignment is RNA sequence data
Alternative model construction strategies:
--fast : assign cols w/ >= symfrac residues as consensus [default]
--hand : manual construction (requires reference annotation)
--profillic-amino : input msa is an AA galosh alignment profile (from profuse)
--profillic-dna : input msa is a DNA galosh alignment profile (from profuse)
--symfrac <x> : sets sym fraction controlling --fast construction [0.5]
--fragthresh <x> : if L <= x*alen, tag sequence as a fragment [0.5]
Alternative relative sequence weighting strategies:
--wpb : Henikoff position-based weights [default]
--wgsc : Gerstein/Sonnhammer/Chothia tree weights
--wblosum : Henikoff simple filter weights
--wnone : don't do any relative weighting; set all to 1
--wgiven : use weights as given in MSA file
--wid <x> : for --wblosum: set identity cutoff [0.62] (0<=x<=1)
Alternative effective sequence weighting strategies:
--eent : adjust eff seq # to achieve relative entropy target [default]
--eclust : eff seq # is # of single linkage clusters
--enone : no effective seq # weighting: just use nseq
--eset <x> : set eff seq # for all models to <x>
--ere <x> : for --eent: set minimum rel entropy/position to <x>
--esigma <x> : for --eent: set sigma param to <x> [45.0]
--eid <x> : for --eclust: set fractional identity cutoff to <x> [0.62]
Alternative prior strategies:
--pnone : don't use any prior; parameters are frequencies
--plaplace : use a Laplace +1 prior
Handling single sequence inputs:
--single : use substitution score matrix for single-sequence protein inputs
--popen <x> : gap open probability (with --single)
--pextend <x> : gap extend probability (with --single)
--mx <s> : substitution score matrix (built-in matrices, with --single)
--mxfile <f> : read substitution score matrix from file <f> (with --single)
Control of E-value calibration:
--EmL <n> : length of sequences for MSV Gumbel mu fit [200] (n>0)
--EmN <n> : number of sequences for MSV Gumbel mu fit [200] (n>0)
--EvL <n> : length of sequences for Viterbi Gumbel mu fit [200] (n>0)
--EvN <n> : number of sequences for Viterbi Gumbel mu fit [200] (n>0)
--EfL <n> : length of sequences for Forward exp tail tau fit [100] (n>0)
--EfN <n> : number of sequences for Forward exp tail tau fit [200] (n>0)
--Eft <x> : tail mass for Forward exponential tail tau fit [0.04] (0<x<1)
Other options:
--cpu <n> : number of parallel CPU workers for multithreads
--stall : arrest after start: for attaching debugger to process
--informat <s> : assert input alifile is in format <s> (no autodetect)
--seed <n> : set RNG seed to <n> (if 0: one-time arbitrary seed) [42]
--w_beta <x> : tail mass at which window length is determined
--w_length <n> : window length
--noprior : do not apply any priors
</pre>
*/
extern "C" {
#include "p7_config.h"
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
extern "C" {
#ifdef HAVE_MPI
#include "mpi.h"
#endif
}
extern "C" {
#include "easel.h"
#include "esl_alphabet.h"
#include "esl_getopts.h"
#include "esl_mpi.h"
/// \note TAH Workaround to avoid use of C++ keyword "new" in esl_msa.h
#define new _new
#include "esl_msa.h"
#undef new
#include "esl_msafile.h"
#include "esl_msaweight.h"
#include "esl_msacluster.h"
#include "esl_stopwatch.h"
#include "esl_vectorops.h"
}
#ifdef HMMER_THREADS
#include <unistd.h>
extern "C" {
#include "esl_threads.h"
#include "esl_workqueue.h"
}
#endif /*HMMER_THREADS*/
extern "C" {
#include "hmmer.h"
}
/* /////////////// For profillic-hmmer ////////////////////////////////// */
#include "profillic-hmmer.hpp"
#include "DynamicProgramming.hpp"
#include "profillic-alignment-p7_builder.hpp"
//#include "profillic-esl_msa.hpp"
#include "profillic-alignment-esl_msafile.hpp"
/// Updated notices:
#define PROFILLIC_HMMER_VERSION "1.0a"
#define PROFILLIC_HMMER_DATE "July 2011"
#define PROFILLIC_HMMER_COPYRIGHT "Copyright (C) 2011 Paul T. Edlefsen, Fred Hutchinson Cancer Research Center."
#define PROFILLIC_HMMER_URL "http://galosh.org/"
/// Modified from hmmer.c p7_banner(..):
/** Version info - set once for whole package in configure.ac
*/
/*****************************************************************
* 1. Miscellaneous functions for H3
*****************************************************************/
/**
* <pre>
* Function: p7_banner()
*
* Synopsis: print standard HMMER application output header
*
* Incept: SRE, Wed May 23 10:45:53 2007 [Janelia]
*
* Purpose: Print the standard HMMER command line application banner
* to <fp>, constructing it from <progname> (the name of the
* program) and a short one-line description <banner>.
* For example,
* <p7_banner(stdout, "hmmsim", "collect profile HMM score distributions");>
* might result in:
*
* \begin{cchunk}
* # hmmsim :: collect profile HMM score distributions
* # HMMER 3.0 (May 2007)
* # Copyright (C) 2004-2007 HHMI Janelia Farm Research Campus
* # Freely licensed under the Janelia Software License.
* # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* \end{cchunk}
*
* <progname> would typically be an application's
* <argv[0]>, rather than a fixed string. This allows the
* program to be renamed, or called under different names
* via symlinks. Any path in the <progname> is discarded;
* for instance, if <progname> is "/usr/local/bin/hmmsim",
* "hmmsim" is used as the program name.
*
* Note:
* Needs to pick up preprocessor #define's from p7_config.h,
* as set by ./configure:
*
* symbol example
* ------ ----------------
* HMMER_VERSION "3.0"
* HMMER_DATE "May 2007"
* HMMER_COPYRIGHT "Copyright (C) 2004-2007 HHMI Janelia Farm Research Campus"
* HMMER_LICENSE "Freely licensed under the Janelia Software License."
*
* Returns: (void)
*
* </pre>
*/
void
profillic_p7_banner(FILE *fp, char *progname, char *banner)
{
char *appname = NULL;
if (esl_FileTail(progname, FALSE, &appname) != eslOK) appname = progname;
fprintf(fp, "# %s :: %s\n", appname, banner);
fprintf(fp, "# profillic-hmmer %s (%s); %s\n", PROFILLIC_HMMER_VERSION, PROFILLIC_HMMER_DATE, PROFILLIC_HMMER_URL);
fprintf(fp, "# %s\n", PROFILLIC_HMMER_COPYRIGHT);
fprintf(fp, "# HMMER %s (%s); %s\n", HMMER_VERSION, HMMER_DATE, HMMER_URL);
fprintf(fp, "# %s\n", HMMER_COPYRIGHT);
fprintf(fp, "# %s\n", HMMER_LICENSE);
fprintf(fp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
if (appname != NULL) free(appname);
return;
}
/* ////////////// End profillic-hmmer ///////////////////////////////// */
typedef struct {
#ifdef HMMER_THREADS
ESL_WORK_QUEUE *queue;
#endif /*HMMER_THREADS*/
P7_BG *bg;
P7_BUILDER *bld;
int use_priors;
} WORKER_INFO;
#ifdef HMMER_THREADS
typedef struct {
int nali;
int processed;
ESL_MSA *postmsa;
ESL_MSA *msa;
P7_HMM *hmm;
double entropy;
int force_single; /* FALSE by default, TRUE if esl_opt_IsUsed(go, "--single") ; only matters for single sequences */
} WORK_ITEM;
typedef struct _pending_s {
int nali;
ESL_MSA *postmsa;
ESL_MSA *msa;
P7_HMM *hmm;
double entropy;
struct _pending_s *next;
} PENDING_ITEM;
#endif /*HMMER_THREADS*/
#define ALPHOPTS "--amino,--dna,--rna" /* Exclusive options for alphabet choice */
#define CONOPTS "--fast,--hand,--profillic-amino,--profillic-dna" /* Exclusive options for model construction */
#define EFFOPTS "--eent,--eclust,--eset,--enone" /* Exclusive options for effective sequence number calculation */
#define WGTOPTS "--wgsc,--wblosum,--wpb,--wnone,--wgiven" /* Exclusive options for relative weighting */
static ESL_OPTIONS options[] = {
/* name type default env range toggles reqs incomp help docgroup*/
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
{ "-n", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "name the HMM <s>", 1 },
{ "-o", eslARG_OUTFILE,FALSE, NULL, NULL, NULL, NULL, NULL, "direct summary output to file <f>, not stdout", 1 },
{ "-O", eslARG_OUTFILE,FALSE, NULL, NULL, NULL, NULL, NULL, "resave annotated, possibly modified MSA to file <f>", 1 },
/* Selecting the alphabet rather than autoguessing it */
{ "--amino", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "input alignment is protein sequence data", 2 },
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "input alignment is DNA sequence data", 2 },
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "input alignment is RNA sequence data", 2 },
/* Alternate model construction strategies */
{ "--fast", eslARG_NONE,"default",NULL, NULL, CONOPTS, NULL, NULL, "assign cols w/ >= symfrac residues as consensus", 3 },
{ "--hand", eslARG_NONE, FALSE, NULL, NULL, CONOPTS, NULL, NULL, "manual construction (requires reference annotation)", 3 },
{ "--profillic-amino", eslARG_NONE, FALSE,NULL, NULL, CONOPTS, NULL, NULL, "input msa is an AA galosh alignment profile (from profuse)", 3 },
{ "--profillic-dna", eslARG_NONE, FALSE,NULL, NULL, CONOPTS, NULL, NULL, "input msa is a DNA galosh alignment profile (from profuse)", 3 },
{ "--symfrac", eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL, "--fast", NULL, "sets sym fraction controlling --fast construction", 3 },
{ "--fragthresh",eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL, NULL, NULL, "if L <= x*alen, tag sequence as a fragment", 3 },
//TAH 4/12
{ "--nseq", eslARG_INT, "0", NULL, "n>=0", NULL, NULL, NULL, "override n of seqs from msa/alignment profile", 3 },
/* Alternate relative sequence weighting strategies */
/* --wme not implemented in HMMER3 yet */
{ "--wpb", eslARG_NONE,"default",NULL, NULL, WGTOPTS, NULL, NULL, "Henikoff position-based weights", 4 },
{ "--wgsc", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "Gerstein/Sonnhammer/Chothia tree weights", 4 },
{ "--wblosum", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "Henikoff simple filter weights", 4 },
{ "--wnone", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "don't do any relative weighting; set all to 1", 4 },
{ "--wgiven", eslARG_NONE, NULL, NULL, NULL, WGTOPTS, NULL, NULL, "use weights as given in MSA file", 4 },
{ "--wid", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"--wblosum", NULL, "for --wblosum: set identity cutoff", 4 },
/* Alternative effective sequence weighting strategies */
{ "--eent", eslARG_NONE,"default",NULL, NULL, EFFOPTS, NULL, NULL, "adjust eff seq # to achieve relative entropy target", 5 },
{ "--eclust", eslARG_NONE, FALSE, NULL, NULL, EFFOPTS, NULL, NULL, "eff seq # is # of single linkage clusters", 5 },
{ "--enone", eslARG_NONE, FALSE, NULL, NULL, EFFOPTS, NULL, NULL, "no effective seq # weighting: just use nseq", 5 },
{ "--eset", eslARG_REAL, NULL, NULL, NULL, EFFOPTS, NULL, NULL, "set eff seq # for all models to <x>", 5 },
{ "--ere", eslARG_REAL, NULL, NULL,"x>0", NULL, "--eent", NULL, "for --eent: set minimum rel entropy/position to <x>", 5 },
{ "--esigma", eslARG_REAL, "45.0", NULL,"x>0", NULL, "--eent", NULL, "for --eent: set sigma param to <x>", 5 },
{ "--eid", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"--eclust", NULL, "for --eclust: set fractional identity cutoff to <x>", 5 },
/* Alternative prior strategies */
{ "--pnone", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"--plaplace", "don't use any prior; parameters are frequencies", 9 },
{ "--plaplace",eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--pnone", "use a Laplace +1 prior", 9 },
/* Single sequence methods */
{ "--single", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "", "use substitution score matrix for single-sequence protein inputs", 10 },
{ "--popen", eslARG_REAL, "0.02", NULL,"0<=x<0.5",NULL, NULL, "", "gap open probability (with --single)", 10 },
{ "--pextend", eslARG_REAL, "0.4", NULL, "0<=x<1", NULL, NULL, "", "gap extend probability (with --single)", 10 },
{ "--mx", eslARG_STRING, "BLOSUM62", NULL, NULL, NULL, NULL, "--mxfile", "substitution score matrix (built-in matrices, with --single)", 10 },
{ "--mxfile", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--mx", "read substitution score matrix from file <f> (with --single)", 10 },
/* Control of E-value calibration */
{ "--EmL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for MSV Gumbel mu fit", 6 },
{ "--EmN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for MSV Gumbel mu fit", 6 },
{ "--EvL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Viterbi Gumbel mu fit", 6 },
{ "--EvN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Viterbi Gumbel mu fit", 6 },
{ "--EfL", eslARG_INT, "100", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Forward exp tail tau fit", 6 },
{ "--EfN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Forward exp tail tau fit", 6 },
{ "--Eft", eslARG_REAL, "0.04", NULL,"0<x<1", NULL, NULL, NULL, "tail mass for Forward exponential tail tau fit", 6 },
/* Other options */
#ifdef HMMER_THREADS
{ "--cpu", eslARG_INT, NULL,"HMMER_NCPU","n>=0",NULL, NULL, NULL, "number of parallel CPU workers for multithreads", 8 },
#endif
#ifdef HAVE_MPI
{ "--mpi", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "run as an MPI parallel program", 8 },
#endif
{ "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "arrest after start: for attaching debugger to process", 8 },
{ "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert input alifile is in format <s> (no autodetect)", 8 },
{ "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 8 },
{ "--w_beta", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "tail mass at which window length is determined", 8 },
{ "--w_length", eslARG_INT, NULL, NULL, NULL, NULL, NULL, NULL, "window length ", 8 },
{ "--maxinsertlen", eslARG_INT, NULL, NULL, "n>=5", NULL, NULL, NULL, "pretend all inserts are length <= <n>", 8 },
{ "--noprior", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "do not apply any priors", 8 },
// TAH 4/12 Output hmm in linear space (instead of neg log)
{ "--linspace", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "output hmm in linear space instead of negative log", 8 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
/**
* struct cfg_s : "Global" application configuration shared by all threads/processes
*
* This structure is passed to routines within main.c, as a means of semi-encapsulation
* of shared data amongst different parallel processes (threads or MPI processes).
*/
struct cfg_s {
FILE *ofp; /* output file (default is stdout) */
char *alifile; /* name of the alignment file we're building HMMs from */
int fmt; /* format code for alifile */
ESLX_MSAFILE *afp; /* open alifile */
ESL_ALPHABET *abc; /* digital alphabet */
char *hmmName; /* hmm file name supplied from -n */
char *hmmfile; /* file to write HMM to */
FILE *hmmfp; /* HMM output file handle */
char *postmsafile; /* optional file to resave annotated, modified MSAs to */
FILE *postmsafp; /* open <postmsafile>, or NULL */
int nali; /* which # alignment this is in file (only valid in serial mode) */
int nnamed; /* number of alignments that had their own names */
int do_mpi; /* TRUE if we're doing MPI parallelization */
int nproc; /* how many MPI processes, total */
int my_rank; /* who am I, in 0..nproc-1 */
int do_stall; /* TRUE to stall the program until gdb attaches */
int use_priors; /* TRUE except when esl_opt_GetBoolean(go, "--noprior") */
int nseq; /* TAH 3/12 Assume the alignment profile was created from this many sequences */
};
static char usage[] = "[-options] <hmmfile_out> <msafile>";
static char banner[] = "profile HMM construction from multiple sequence alignments and galosh profiles";
static int profillic_usual_master(const ESL_GETOPTS *go, struct cfg_s *cfg);
template <class ProfileType>
static void profillic_serial_loop (WORKER_INFO *info, struct cfg_s *cfg, ProfileType * profile_ptr, const ESL_GETOPTS *go);
#ifdef HMMER_THREADS
static void thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, struct cfg_s *cfg, const ESL_GETOPTS *go);
static void pipeline_thread(void *arg);
#endif /*HMMER_THREADS*/
#ifdef HAVE_MPI
static void mpi_master (const ESL_GETOPTS *go, struct cfg_s *cfg);
static void mpi_worker (const ESL_GETOPTS *go, struct cfg_s *cfg);
static void mpi_init_open_failure(ESLX_MSAFILE *afp, int status);
static void mpi_init_other_failure(char *format, ...);
#endif
static int profillic_output_header(const ESL_GETOPTS *go, const struct cfg_s *cfg);
static int output_result(const struct cfg_s *cfg, char *errbuf, int msaidx, ESL_MSA *msa, P7_HMM *hmm, ESL_MSA *postmsa, double entropy);
static int set_msa_name ( struct cfg_s *cfg, char *errbuf, ESL_MSA *msa);
static int
process_commandline(int argc, char **argv, ESL_GETOPTS **ret_go, char **ret_hmmfile, char **ret_alifile)
{
ESL_GETOPTS *go = esl_getopts_Create(options);
int status;
if (esl_opt_ProcessEnvironment(go) != eslOK) { if (printf("Failed to process environment:\n%s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) { if (printf("Failed to parse command line:\n%s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
if (esl_opt_VerifyConfig(go) != eslOK) { if (printf("Failed to parse command line:\n%s\n", go->errbuf) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
/* help format: */
if (esl_opt_GetBoolean(go, "-h") == TRUE)
{
profillic_p7_banner(stdout, argv[0], banner);
esl_usage(stdout, argv[0], usage);
if (puts("\nBasic options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
if (puts("\nOptions for selecting alphabet rather than guessing it:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
if (puts("\nAlternative model construction strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 3, 2, 80);
if (puts("\nAlternative relative sequence weighting strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 4, 2, 80);
if (puts("\nAlternative effective sequence weighting strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 5, 2, 80);
if (puts("\nAlternative prior strategies:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 9, 2, 80);
if (puts("\nHandling single sequence inputs:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 10, 2, 80);
if (puts("\nControl of E-value calibration:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 6, 2, 80);
if (puts("\nOther options:") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
esl_opt_DisplayHelp(stdout, go, 8, 2, 80);
exit(0);
}
if (esl_opt_ArgNumber(go) != 2) { if (puts("Incorrect number of command line arguments.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
if ((*ret_hmmfile = esl_opt_GetArg(go, 1)) == NULL) { if (puts("Failed to get <hmmfile_out> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
if ((*ret_alifile = esl_opt_GetArg(go, 2)) == NULL) { if (puts("Failed to get <msafile> argument on command line") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
if (strcmp(*ret_hmmfile, "-") == 0)
{ if (puts("Can't write <hmmfile_out> to stdout: don't use '-'") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
if (strcmp(*ret_alifile, "-") == 0 && ! esl_opt_IsOn(go, "--informat"))
{ if (puts("Must specify --informat to read <alifile> from stdin ('-')") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed"); goto FAILURE; }
#ifdef HAVE_MPI
if (esl_opt_IsOn(go, "--mpi") && esl_opt_IsOn(go, "--cpu"))
{
int mpisetby = esl_opt_GetSetter(go, "--mpi");
int cpusetby = esl_opt_GetSetter(go, "--cpu");
if (mpisetby == cpusetby) {
if (puts("Options --cpu and --mpi are incompatible. The MPI implementation is not multithreaded.") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "write failed");
goto FAILURE;
}
}
#endif
*ret_go = go;
return eslOK;
FAILURE: /* all errors handled here are user errors, so be polite. */
esl_usage(stdout, argv[0], usage);
puts("\nwhere basic options are:");
esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
printf("\nTo see more help on other available options, do:\n %s -h\n\n", argv[0]);
esl_getopts_Destroy(go);
exit(1);
ERROR:
if (go) esl_getopts_Destroy(go);
exit(status);
}
static int
profillic_output_header(const ESL_GETOPTS *go, const struct cfg_s *cfg)
{
if (cfg->my_rank > 0) return eslOK;
profillic_p7_banner(cfg->ofp, go->argv[0], banner);
if( esl_opt_IsUsed(go, "--profillic-amino") || esl_opt_IsUsed(go, "--profillic-dna") ) {
if (fprintf(cfg->ofp, "# input galosh profile file: %s\n", cfg->alifile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
} else {
if (fprintf(cfg->ofp, "# input alignment file: %s\n", cfg->alifile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
}
if (fprintf(cfg->ofp, "# output HMM file: %s\n", cfg->hmmfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "-n") && fprintf(cfg->ofp, "# name (the single) HMM: %s\n", esl_opt_GetString(go, "-n")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "-o") && fprintf(cfg->ofp, "# output directed to file: %s\n", esl_opt_GetString(go, "-o")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "-O") && fprintf(cfg->ofp, "# processed alignment resaved to: %s\n", esl_opt_GetString(go, "-O")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--amino") && fprintf(cfg->ofp, "# input alignment is asserted as: protein\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--dna") && fprintf(cfg->ofp, "# input alignment is asserted as: DNA\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--rna") && fprintf(cfg->ofp, "# input alignment is asserted as: RNA\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--fast") && fprintf(cfg->ofp, "# model architecture construction: fast/heuristic\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--hand") && fprintf(cfg->ofp, "# model architecture construction: hand-specified by RF annotation\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--profillic-amino") && fprintf(cfg->ofp, "# model architecture construction: use input amino profile\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--profillic-dna") && fprintf(cfg->ofp, "# model architecture construction: use input dna profile\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
//TAH 3/12 nseq
if (esl_opt_IsUsed(go, "--nseq") && fprintf(cfg->ofp, "# n of sequences in profile: %d\n", esl_opt_GetInteger(go,"--nseq")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--symfrac") && fprintf(cfg->ofp, "# sym fraction for model structure: %.3f\n", esl_opt_GetReal(go, "--symfrac")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--fragthresh") && fprintf(cfg->ofp, "# seq called frag if L <= x*alen: %.3f\n", esl_opt_GetReal(go, "--fragthresh")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--wpb") && fprintf(cfg->ofp, "# relative weighting scheme: Henikoff PB\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--wgsc") && fprintf(cfg->ofp, "# relative weighting scheme: G/S/C\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--wblosum") && fprintf(cfg->ofp, "# relative weighting scheme: BLOSUM filter\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--wnone") && fprintf(cfg->ofp, "# relative weighting scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--wid") && fprintf(cfg->ofp, "# frac id cutoff for BLOSUM wgts: %f\n", esl_opt_GetReal(go, "--wid")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--eent") && fprintf(cfg->ofp, "# effective seq number scheme: entropy weighting\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--eclust") && fprintf(cfg->ofp, "# effective seq number scheme: single linkage clusters\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--enone") && fprintf(cfg->ofp, "# effective seq number scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--eset") && fprintf(cfg->ofp, "# effective seq number: set to %f\n", esl_opt_GetReal(go, "--eset")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--ere") && fprintf(cfg->ofp, "# minimum rel entropy target: %f bits\n", esl_opt_GetReal(go, "--ere")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--esigma") && fprintf(cfg->ofp, "# entropy target sigma parameter: %f bits\n", esl_opt_GetReal(go, "--esigma")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--eid") && fprintf(cfg->ofp, "# frac id cutoff for --eclust: %f\n", esl_opt_GetReal(go, "--eid")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--pnone") && fprintf(cfg->ofp, "# prior scheme: none\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--plaplace") && fprintf(cfg->ofp, "# prior scheme: Laplace +1\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--EmL") && fprintf(cfg->ofp, "# seq length for MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EmL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--EmN") && fprintf(cfg->ofp, "# seq number for MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EmN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--EvL") && fprintf(cfg->ofp, "# seq length for Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EvL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--EvN") && fprintf(cfg->ofp, "# seq number for Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(go, "--EvN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--EfL") && fprintf(cfg->ofp, "# seq length for Fwd exp tau fit: %d\n", esl_opt_GetInteger(go, "--EfL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--EfN") && fprintf(cfg->ofp, "# seq number for Fwd exp tau fit: %d\n", esl_opt_GetInteger(go, "--EfN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--Eft") && fprintf(cfg->ofp, "# tail mass for Fwd exp tau fit: %f\n", esl_opt_GetReal(go, "--Eft")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--popen") && fprintf(cfg->ofp, "# gap open probability: %f\n", esl_opt_GetReal (go, "--popen")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--pextend") && fprintf(cfg->ofp, "# gap extend probability: %f\n", esl_opt_GetReal (go, "--pextend")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--mx") && fprintf(cfg->ofp, "# subst score matrix (built-in): %s\n", esl_opt_GetString (go, "--mx")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--mxfile") && fprintf(cfg->ofp, "# subst score matrix (file): %s\n", esl_opt_GetString (go, "--mxfile")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--maxinsertlen") && fprintf(cfg->ofp, "# max insert length: %d\n", esl_opt_GetInteger (go, "--maxinsertlen")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
#ifdef HMMER_THREADS
if (esl_opt_IsUsed(go, "--cpu") && fprintf(cfg->ofp, "# number of worker threads: %d\n", esl_opt_GetInteger(go, "--cpu")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
#endif
#ifdef HAVE_MPI
if (esl_opt_IsUsed(go, "--mpi") && fprintf(cfg->ofp, "# parallelization mode: MPI\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
#endif
if (esl_opt_IsUsed(go, "--seed")) {
if (esl_opt_GetInteger(go, "--seed") == 0 && fprintf(cfg->ofp,"# random number seed: one-time arbitrary\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
else if ( fprintf(cfg->ofp,"# random number seed set to: %d\n", esl_opt_GetInteger(go, "--seed")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
}
if (esl_opt_IsUsed(go, "--w_beta") && fprintf(cfg->ofp, "# window length beta value: %g bits\n", esl_opt_GetReal(go, "--w_beta")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (esl_opt_IsUsed(go, "--w_length") && fprintf(cfg->ofp, "# window length : %d\n", esl_opt_GetInteger(go, "--w_length"))< 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
if (fprintf(cfg->ofp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
return eslOK;
}
int
main(int argc, char **argv)
{
ESL_GETOPTS *go = NULL; /* command line processing */
ESL_STOPWATCH *w = esl_stopwatch_Create();
struct cfg_s cfg;
p7_Init();
cfg.alifile = NULL;
cfg.hmmfile = NULL;
/** Parse the command line
*/
process_commandline(argc, argv, &go, &cfg.hmmfile, &cfg.alifile);
/**
* Initialize what we can in the config structure (without knowing the alphabet yet).
* Fields controlled by masters are set up in usual_master() or mpi_master()
* Fields used by workers are set up in mpi_worker()
*/
cfg.ofp = NULL;
cfg.fmt = eslMSAFILE_UNKNOWN; /* autodetect alignment format by default. */
cfg.afp = NULL;
cfg.abc = NULL;
cfg.hmmfp = NULL;
cfg.postmsafile = esl_opt_GetString(go, "-O"); /* NULL by default */
cfg.postmsafp = NULL;
cfg.nali = 0; /* this counter is incremented in masters */
cfg.nnamed = 0; /* 0 or 1 if a single MSA; == nali if multiple MSAs */
cfg.do_mpi = FALSE; /* this gets reset below, if we init MPI */
cfg.nproc = 0; /* this gets reset below, if we init MPI */
cfg.my_rank = 0; /* this gets reset below, if we init MPI */
cfg.do_stall = esl_opt_GetBoolean(go, "--stall");
cfg.hmmName = esl_opt_GetString(go, "-n"); /* NULL by default */
//TAH 4/12
cfg.nseq = esl_opt_GetInteger(go,"--nseq"); /* 0 by default */
cfg.use_priors = !esl_opt_GetBoolean(go, "--noprior");
if( esl_opt_IsUsed(go, "--profillic-amino")||esl_opt_IsUsed(go, "--profillic-dna") ) {
cfg.fmt = eslMSAFILE_PROFILLIC;
} else if (esl_opt_IsOn(go, "--informat")) {
cfg.fmt = eslx_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"));
if (cfg.fmt == eslMSAFILE_UNKNOWN) p7_Fail("%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--informat"));
}
/** This is our stall point, if we need to wait until we get a
* debugger attached to this process for debugging (especially
* useful for MPI):
*/
while (cfg.do_stall);
/** Start timing. */
esl_stopwatch_Start(w);
/* Figure out who we are, and send control there:
* we might be an MPI master, an MPI worker, or a serial program.
*/
#ifdef HAVE_MPI
if (esl_opt_GetBoolean(go, "--mpi"))
{
if( esl_opt_IsUsed(go, "--profillic-amino") || esl_opt_IsUsed(go, "--profillic-dna" ) ) {
ESL_EXCEPTION(eslEUNIMPLEMENTED, "Sorry, at present the profillic-hmmbuild software can't handle profillic profiles when compiled using MPI. Please recompile without MPI for profillic support.");
}
cfg.do_mpi = TRUE;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &(cfg.my_rank));
MPI_Comm_size(MPI_COMM_WORLD, &(cfg.nproc));
if (cfg.my_rank > 0) mpi_worker(go, &cfg);
else mpi_master(go, &cfg);
esl_stopwatch_Stop(w);
esl_stopwatch_MPIReduce(w, 0, MPI_COMM_WORLD);
MPI_Finalize();
}
else
#endif /*HAVE_MPI*/
{
profillic_usual_master(go, &cfg);
esl_stopwatch_Stop(w);
}
if (cfg.my_rank == 0) {
fputc('\n', cfg.ofp);
esl_stopwatch_Display(cfg.ofp, w, "# CPU time: ");
}
/* Clean up the shared cfg.
*/
if (cfg.my_rank == 0) {
if (esl_opt_IsOn(go, "-o")) { fclose(cfg.ofp); }
if (cfg.afp) eslx_msafile_Close(cfg.afp);
if (cfg.abc) esl_alphabet_Destroy(cfg.abc);
if (cfg.hmmfp) fclose(cfg.hmmfp);
}
esl_getopts_Destroy(go);
esl_stopwatch_Destroy(w);
return 0;
}
/**
* profillic_usual_master()
* The usual version of hmmbuild, serial or threaded
* For each MSA, build an HMM and save it.
*
* A master can only return if it's successful.
* All errors are handled immediately and fatally with p7_Fail() or equiv.
*/
static int
profillic_usual_master(const ESL_GETOPTS *go, struct cfg_s *cfg)
{
int ncpus = 0;
int infocnt = 0;
WORKER_INFO *info = NULL;
#ifdef HMMER_THREADS
WORK_ITEM *item = NULL;
ESL_THREADS *threadObj= NULL;
ESL_WORK_QUEUE *queue = NULL;
#endif
int i;
int status;
/**
* <pre>
* Open files, set alphabet.
* cfg->afp - open alignment file for input
* cfg->abc - alphabet expected or guessed in ali file
* cfg->hmmfp - open HMM file for output
* cfg->postmsafp - optional open MSA resave file, or NULL
* cfp->ofp - optional open output file, or stdout
* The mpi_master's version of this is in init_master_cfg(), with
* different error handling (necessitated by our MPI design).
* </pre>
*/
if (esl_opt_GetBoolean(go, "--amino")||esl_opt_IsUsed(go, "--profillic-amino")) cfg->abc = esl_alphabet_Create(eslAMINO);
else if (esl_opt_GetBoolean(go, "--dna")||esl_opt_IsUsed(go, "--profillic-dna")) cfg->abc = esl_alphabet_Create(eslDNA);
else if (esl_opt_GetBoolean(go, "--rna")) cfg->abc = esl_alphabet_Create(eslRNA);
else cfg->abc = NULL;
status = profillic_eslx_msafile_Open(&(cfg->abc), cfg->alifile, NULL, cfg->fmt, NULL, &(cfg->afp));
if (status != eslOK) eslx_msafile_OpenFailure(cfg->afp, status);
cfg->hmmfp = fopen(cfg->hmmfile, "w");
if (cfg->hmmfp == NULL) p7_Fail("Failed to open HMM file %s for writing", cfg->hmmfile);
if (esl_opt_IsUsed(go, "-o"))
{
cfg->ofp = fopen(esl_opt_GetString(go, "-o"), "w");
if (cfg->ofp == NULL) p7_Fail("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o"));
}
else cfg->ofp = stdout;
if (cfg->postmsafile)
{
cfg->postmsafp = fopen(cfg->postmsafile, "w");
if (cfg->postmsafp == NULL) p7_Fail("Failed to MSA resave file %s for writing", cfg->postmsafile);
}
else cfg->postmsafp = NULL;
/* Looks like the i/o is set up successfully...
* Initial output to the user
*/
profillic_output_header(go, cfg); /* cheery output header */
output_result(cfg, NULL, 0, NULL, NULL, NULL, 0.0); /* tabular results header (with no args, special-case) */
#ifdef HMMER_THREADS
/* initialize thread data */
if (esl_opt_IsOn(go, "--cpu")) ncpus = esl_opt_GetInteger(go, "--cpu");
else esl_threads_CPUCount(&ncpus);
if (ncpus > 0)
{
threadObj = esl_threads_Create(&pipeline_thread);
queue = esl_workqueue_Create(ncpus * 2);
}
#endif
infocnt = (ncpus == 0) ? 1 : ncpus;
ESL_ALLOC_CPP( WORKER_INFO, info, sizeof(*info) * infocnt);
for (i = 0; i < infocnt; ++i)
{
info[i].bg = p7_bg_Create(cfg->abc);
info[i].bld = p7_builder_Create(go, cfg->abc);
if (info[i].bld == NULL) p7_Fail("p7_builder_Create failed");
//do this here instead of in p7_builder_Create(), because it's an hmmbuild-specific option
if ( esl_opt_IsOn(go, "--maxinsertlen") )
info[i].bld->max_insert_len = esl_opt_GetInteger(go, "--maxinsertlen");
/** Default matrix is stored in the --mx option, so it's always IsOn().
* Check --mxfile first; then go to the --mx option and the default.
*/
if ( cfg->abc != NULL && cfg->abc->type == eslAMINO && esl_opt_IsUsed(go, "--single")) {
if (esl_opt_IsOn(go, "--mxfile")) status = p7_builder_SetScoreSystem (info[i].bld, esl_opt_GetString(go, "--mxfile"), NULL, esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), info[i].bg);
else status = p7_builder_LoadScoreSystem(info[i].bld, esl_opt_GetString(go, "--mx"), esl_opt_GetReal(go, "--popen"), esl_opt_GetReal(go, "--pextend"), info[i].bg);
if (status != eslOK) p7_Fail("Failed to set single query seq score system:\n%s\n", info[i].bld->errbuf);
}
/* special arguments for hmmbuild */
info[i].bld->w_len = (go != NULL && esl_opt_IsOn (go, "--w_length")) ? esl_opt_GetInteger(go, "--w_length"): -1;
info[i].bld->w_beta = (go != NULL && esl_opt_IsOn (go, "--w_beta")) ? esl_opt_GetReal (go, "--w_beta") : p7_DEFAULT_WINDOW_BETA;
if ( info[i].bld->w_beta < 0 || info[i].bld->w_beta > 1 ) esl_fatal("Invalid window-length beta value\n");
#ifdef HMMER_THREADS
info[i].queue = queue;
if (ncpus > 0) esl_threads_AddThread(threadObj, &info[i]);
#endif
info[i].use_priors = cfg->use_priors;
}
#ifdef HMMER_THREADS
for (i = 0; i < ncpus * 2; ++i)
{
ESL_ALLOC_CPP( WORK_ITEM, item, sizeof(*item));
item->nali = 0;
item->processed = FALSE;
item->postmsa = NULL;
item->msa = NULL;
item->hmm = NULL;
item->entropy = 0.0;
status = esl_workqueue_Init(queue, item);
if (status != eslOK) esl_fatal("Failed to add block to work queue");
}
#endif
#ifdef HMMER_THREADS
if ((( cfg->afp->format != eslMSAFILE_PROFILLIC )) && (ncpus > 0)) {
thread_loop(threadObj, queue, cfg, go);
} else if(cfg->fmt == eslMSAFILE_PROFILLIC) { ///TAH 3/12 change = to ==. Still works?
if( cfg->abc != NULL && cfg->abc->type == eslDNA ) {
galosh::AlignmentProfileAccessor<seqan::Dna, floatrealspace, floatrealspace, floatrealspace> profile(cfg->nseq);
//galosh::ProfileTreeRoot<seqan::Dna, floatrealspace> profile;
//TAH 2/12 for conversion to Alignment Profile.
profillic_serial_loop(info, cfg, &profile, go);
} else if( cfg->abc != NULL && cfg->abc->type == eslAMINO ) {
//galosh::ProfileTreeRoot<seqan::AminoAcid20, floatrealspace> profile;
//TAH 2/12 for conversion to Alignment Profile
galosh::AlignmentProfileAccessor<seqan::AminoAcid20, floatrealspace, floatrealspace, floatrealspace> profile(cfg->nseq);
profillic_serial_loop(info, cfg, &profile, go);
} else {
ESL_EXCEPTION(eslEUNIMPLEMENTED, "Sorry, at present the profillic-hmmbuild software can only handle amino and dna.");
}
} else {
//TAH 2/12
// profillic_serial_loop(info, cfg, (galosh::ProfileTreeRoot<seqan::Dna, floatrealspace> *)NULL, go);
profillic_serial_loop(info, cfg, (galosh::AlignmentProfileAccessor<seqan::Dna, floatrealspace,floatrealspace,floatrealspace> *)NULL, go);
}
#else
if( cfg->fmt = eslMSAFILE_PROFILLIC ) {
if( cfg->abc->type == eslDNA ) {
// TAH 2/12 for conversion to alignment profile
galosh::AlignmentProfileAccessor<seqan::Dna, floatrealspace,floatrealspace,floatrealspace> profile;
profillic_serial_loop(info, cfg, &profile, go);
} else if( cfg->abc->type == eslAMINO ) {
// TAH 2/12 for conversion to alignment profile
galosh::AlignmentProfileAccessor<seqan::AminoAcid20, floatrealspace,floatrealspace,floatrealspace> profile;
profillic_serial_loop(info, cfg, &profile, go);
}
} else {
//TAH 2/12 mod for alignment profile
profillic_serial_loop(info, cfg, (galosh::AlignmentProfileAccessor<seqan::Dna, floatrealspace, floatrealspace, floatrealspace> *)NULL, go);
}
#endif
for (i = 0; i < infocnt; ++i)
{
p7_bg_Destroy(info[i].bg);
profillic_p7_builder_Destroy(info[i].bld);
}
#ifdef HMMER_THREADS
if (ncpus > 0)
{
esl_workqueue_Reset(queue);
while (esl_workqueue_Remove(queue, (void **) &item) == eslOK)
{
free(item);
}
esl_workqueue_Destroy(queue);
esl_threads_Destroy(threadObj);
}
#endif
free(info);
return eslOK;
ERROR:
return eslFAIL;
}
#ifdef HAVE_MPI
/** mpi_master()
* The MPI version of hmmbuild.
* Follows standard pattern for a master/worker load-balanced MPI program (J1/78-79).
*
* A master can only return if it's successful.
* Errors in an MPI master come in two classes: recoverable and nonrecoverable.
*
* Recoverable errors include all worker-side errors, and any
* master-side error that do not affect MPI communication. Error
* messages from recoverable messages are delayed until we've cleanly
* shut down the workers.
*
* Unrecoverable errors are master-side errors that may affect MPI
* communication, meaning we cannot count on being able to reach the
* workers and shut them down. Unrecoverable errors result in immediate
* p7_Fail()'s, which will cause MPI to shut down the worker processes
* uncleanly.
*/
static void
mpi_master(const ESL_GETOPTS *go, struct cfg_s *cfg)
{
int have_work = TRUE; /* TRUE while alignments remain */
int nproc_working = 0; /* number of worker processes working, up to nproc-1 */
int wi; /* rank of next worker to get an alignment to work on */
char *buf = NULL; /* input/output buffer, for packed MPI messages */
int bn = 0;
ESL_MSA *msa = NULL;
P7_HMM *hmm = NULL;
P7_BG *bg = NULL;
ESL_MSA **msalist = NULL;
ESL_MSA *postmsa = NULL;
int *msaidx = NULL;
char errmsg[eslERRBUFSIZE];
int n;
int pos;
double entropy;
int status;
int xstatus = eslOK; /* changes from OK on recoverable error */
int rstatus; /* status specifically from msa read */
MPI_Status mpistatus;
/**
* <pre>
* Open files, set alphabet.
* cfg->abc - alphabet expected or guessed in ali file
* cfg->afp - open alignment file for input
* cfg->hmmfp - open HMM file for output
* cfp->ofp - optional open output file, or stdout
* cfg->postmsafp - optional open MSA resave file, or NULL
* Error handling requires first broadcasting a non-OK status to workers
* to get them to shut down cleanly.
* </pre>
*/
if (esl_opt_GetBoolean(go, "--amino")) cfg->abc = esl_alphabet_Create(eslAMINO);
else if (esl_opt_GetBoolean(go, "--dna")) cfg->abc = esl_alphabet_Create(eslDNA);
else if (esl_opt_GetBoolean(go, "--rna")) cfg->abc = esl_alphabet_Create(eslRNA);
else cfg->abc = NULL;
status = eslx_msafile_Open(&(cfg->abc), cfg->alifile, NULL, cfg->fmt, NULL, &(cfg->afp));
if (status != eslOK) mpi_init_open_failure(cfg->afp, status);
cfg->hmmfp = fopen(cfg->hmmfile, "w");
if (cfg->hmmfp == NULL) mpi_init_other_failure("Failed to open HMM file %s for writing", cfg->hmmfile);
if (esl_opt_IsUsed(go, "-o"))
{
cfg->ofp = fopen(esl_opt_GetString(go, "-o"), "w");
if (cfg->ofp == NULL) mpi_init_other_failure("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o"));
}
else cfg->ofp = stdout;
if (cfg->postmsafile)
{
cfg->postmsafp = fopen(cfg->postmsafile, "w");
if (cfg->postmsafp == NULL) mpi_init_other_failure("Failed to MSA resave file %s for writing", cfg->postmsafile);
}
else cfg->postmsafp = NULL;
/* Other initialization in the master
*/
bn = 4096;
if ((buf = malloc(sizeof(char) * bn)) == NULL) mpi_init_other_failure("allocation failed");
if ((msalist = malloc(sizeof(ESL_MSA *) * cfg->nproc)) == NULL) mpi_init_other_failure("allocation failed");
if ((msaidx = malloc(sizeof(int) * cfg->nproc)) == NULL) mpi_init_other_failure("allocation failed");
if ((bg = p7_bg_Create(cfg->abc)) == NULL) mpi_init_other_failure("allocation failed");
for (wi = 0; wi < cfg->nproc; wi++) { msalist[wi] = NULL; msaidx[wi] = 0; }
/* Looks like the master is initialized successfully...
* Tell the workers we're fine; send initial output to the user
*/
xstatus = eslOK;
MPI_Bcast(&xstatus, 1, MPI_INT, 0, MPI_COMM_WORLD);
output_header(go, cfg); /* cheery output header */
output_result(cfg, NULL, 0, NULL, NULL, NULL, 0.0); /* tabular results header (with no args, special-case) */
ESL_DPRINTF1(("MPI master is initialized\n"));
/** Worker initialization:
* Because we've already successfully initialized the master before we start
* initializing the workers, we don't expect worker initialization to fail;
* so we just receive a quick OK/error code reply from each worker to be sure,
* and don't worry about an informative message.
*/
MPI_Bcast(&(cfg->abc->type), 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Reduce(&xstatus, &status, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
if (status != eslOK) { MPI_Finalize(); p7_Fail("One or more MPI worker processes failed to initialize."); }
ESL_DPRINTF1(("%d workers are initialized\n", cfg->nproc-1));
/** Main loop: combining load workers, send/receive, clear workers loops;
* also, catch error states and die later, after clean shutdown of workers.
*
* When a recoverable error occurs, have_work = FALSE, xstatus !=
* eslOK, and errmsg is set to an informative message. No more
* errmsg's can be received after the first one. We wait for all the
* workers to clear their work units, then send them shutdown signals,
* then finally print our errmsg and exit.
*
* Unrecoverable errors just crash us out with p7_Fail().
*/
wi = 1;
while (have_work || nproc_working)
{
if (have_work)
{
rstatus = eslx_msafile_Read(cfg->afp, &msa);
if (rstatus == eslOK) { cfg->nali++; ESL_DPRINTF1(("MPI master read MSA %s\n", msa->name == NULL? "" : msa->name)); }
else if (rstatus == eslEOF) { have_work = FALSE; ESL_DPRINTF1(("MPI master has run out of MSAs (having read %d)\n", cfg->nali)); }
else { have_work = FALSE; xstatus = rstatus; ESL_DPRINTF1(("MPI master msa read has failed... start to shut down\n")); }
}
if ((have_work && nproc_working == cfg->nproc-1) || (!have_work && nproc_working > 0))
{
if (MPI_Probe(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &mpistatus) != 0) { MPI_Finalize(); p7_Fail("mpi probe failed"); }
if (MPI_Get_count(&mpistatus, MPI_PACKED, &n) != 0) { MPI_Finalize(); p7_Fail("mpi get count failed"); }
wi = mpistatus.MPI_SOURCE;
ESL_DPRINTF1(("MPI master sees a result of %d bytes from worker %d\n", n, wi));
if (n > bn) {
if ((buf = realloc(buf, sizeof(char) * n)) == NULL) p7_Fail("reallocation failed");
bn = n;
}
if (MPI_Recv(buf, bn, MPI_PACKED, wi, 0, MPI_COMM_WORLD, &mpistatus) != 0) { MPI_Finalize(); p7_Fail("mpi recv failed"); }
ESL_DPRINTF1(("MPI master has received the buffer\n"));
/** If we're in a recoverable error state, we're only clearing worker results;
* just receive them, don't unpack them or print them.
* But if our xstatus is OK, go ahead and process the result buffer.
*/
if (xstatus == eslOK)
{
pos = 0;
if (MPI_Unpack(buf, bn, &pos, &xstatus, 1, MPI_INT, MPI_COMM_WORLD) != 0) { MPI_Finalize(); p7_Fail("mpi unpack failed");}
if (xstatus == eslOK) /* worker reported success. Get the HMM. */
{
ESL_DPRINTF1(("MPI master sees that the result buffer contains an HMM\n"));
if (p7_hmm_MPIUnpack(buf, bn, &pos, MPI_COMM_WORLD, &(cfg->abc), &hmm) != eslOK) { MPI_Finalize(); p7_Fail("HMM unpack failed"); }
ESL_DPRINTF1(("MPI master has unpacked the HMM\n"));
if (cfg->postmsafile != NULL) {
if (esl_msa_MPIUnpack(cfg->abc, buf, bn, &pos, MPI_COMM_WORLD, &postmsa) != eslOK) { MPI_Finalize(); p7_Fail("postmsa unpack failed");}
}
entropy = p7_MeanMatchRelativeEntropy(hmm, bg);
if ((status = output_result(cfg, errmsg, msaidx[wi], msalist[wi], hmm, postmsa, entropy)) != eslOK) xstatus = status;
esl_msa_Destroy(postmsa); postmsa = NULL;
p7_hmm_Destroy(hmm); hmm = NULL;