in progress...
[jalview.git] / forester / java / src / org / forester / application / surfacing.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.application;
28
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.Writer;
34 import java.util.ArrayList;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.SortedMap;
41 import java.util.SortedSet;
42 import java.util.TreeMap;
43 import java.util.TreeSet;
44
45 import org.forester.go.GoId;
46 import org.forester.go.GoNameSpace;
47 import org.forester.go.GoTerm;
48 import org.forester.go.GoUtils;
49 import org.forester.go.OBOparser;
50 import org.forester.go.PfamToGoMapping;
51 import org.forester.go.PfamToGoParser;
52 import org.forester.io.parsers.HmmscanPerDomainTableParser;
53 import org.forester.io.parsers.HmmscanPerDomainTableParser.INDIVIDUAL_SCORE_CUTOFF;
54 import org.forester.phylogeny.Phylogeny;
55 import org.forester.protein.BinaryDomainCombination;
56 import org.forester.protein.Domain;
57 import org.forester.protein.Protein;
58 import org.forester.species.BasicSpecies;
59 import org.forester.species.Species;
60 import org.forester.surfacing.BasicDomainSimilarityCalculator;
61 import org.forester.surfacing.BasicGenomeWideCombinableDomains;
62 import org.forester.surfacing.CombinationsBasedPairwiseDomainSimilarityCalculator;
63 import org.forester.surfacing.DomainCountsBasedPairwiseSimilarityCalculator;
64 import org.forester.surfacing.DomainLengthsTable;
65 import org.forester.surfacing.DomainParsimonyCalculator;
66 import org.forester.surfacing.DomainSimilarity;
67 import org.forester.surfacing.DomainSimilarity.DomainSimilarityScoring;
68 import org.forester.surfacing.DomainSimilarity.PRINT_OPTION;
69 import org.forester.surfacing.DomainSimilarityCalculator;
70 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
71 import org.forester.surfacing.GenomeWideCombinableDomains;
72 import org.forester.surfacing.GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder;
73 import org.forester.surfacing.MappingResults;
74 import org.forester.surfacing.MinimalDomainomeCalculator;
75 import org.forester.surfacing.PairwiseDomainSimilarityCalculator;
76 import org.forester.surfacing.PairwiseGenomeComparator;
77 import org.forester.surfacing.ProteinCountsBasedPairwiseDomainSimilarityCalculator;
78 import org.forester.surfacing.SurfacingUtil;
79 import org.forester.util.BasicDescriptiveStatistics;
80 import org.forester.util.BasicTable;
81 import org.forester.util.BasicTableParser;
82 import org.forester.util.CommandLineArguments;
83 import org.forester.util.DescriptiveStatistics;
84 import org.forester.util.ForesterConstants;
85 import org.forester.util.ForesterUtil;
86
87 public class surfacing {
88
89     private static final int                                        MINIMAL_NUMBER_OF_SIMILARITIES_FOR_SPLITTING                                  = 1000;
90     public final static String                                      DOMAIN_COMBINITONS_OUTPUT_OPTION_FOR_GRAPH_ANALYSIS                           = "graph_analysis_out";
91     public final static String                                      DOMAIN_COMBINITONS_COUNTS_OUTPUT_OPTION                                       = "dcc";
92     public final static String                                      DOMAIN_COMBINITONS_OUTPUTFILE_SUFFIX_FOR_GRAPH_ANALYSIS                       = "_dc.dot";
93     public final static String                                      PARSIMONY_OUTPUT_FITCH_PRESENT_BC_OUTPUTFILE_SUFFIX_FOR_GRAPH_ANALYSIS        = "_fitch_present_dc.dot";
94     public final static String                                      DOMAIN_COMBINITON_COUNTS_OUTPUTFILE_SUFFIX                                    = ".dcc";
95     // gain/loss:
96     public final static String                                      PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_DOMAINS                                      = "_dollo_gl_d";
97     public final static String                                      PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_BINARY_COMBINATIONS                          = "_dollo_gl_dc";
98     public final static String                                      PARSIMONY_OUTPUT_GL_SUFFIX_FITCH_DOMAINS                                      = "_fitch_gl_d";
99     public final static String                                      PARSIMONY_OUTPUT_GL_SUFFIX_FITCH_BINARY_COMBINATIONS                          = "_fitch_gl_dc";
100     // gain/loss counts:
101     public final static String                                      PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_DOMAINS                               = "_dollo_glc_d";
102     public final static String                                      PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_BINARY_COMBINATIONS                   = "_dollo_glc_dc";
103     public final static String                                      PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_FITCH_DOMAINS                               = "_fitch_glc_d";
104     public final static String                                      PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_FITCH_BINARY_COMBINATIONS                   = "_fitch_glc_dc";
105     // tables:
106     public final static String                                      PARSIMONY_OUTPUT_FITCH_GAINS_BC                                               = "_fitch_gains_dc";
107     public final static String                                      PARSIMONY_OUTPUT_FITCH_GAINS_HTML_BC                                          = "_fitch_gains_dc.html";
108     public final static String                                      PARSIMONY_OUTPUT_FITCH_LOSSES_BC                                              = "_fitch_losses_dc";
109     public final static String                                      PARSIMONY_OUTPUT_FITCH_LOSSES_HTML_BC                                         = "_fitch_losses_dc.html";
110     public final static String                                      PARSIMONY_OUTPUT_FITCH_PRESENT_BC                                             = "_fitch_present_dc";
111     public final static String                                      PARSIMONY_OUTPUT_FITCH_PRESENT_HTML_BC                                        = "_fitch_present_dc.html";
112     public final static String                                      PARSIMONY_OUTPUT_DOLLO_GAINS_D                                                = "_dollo_gains_d";
113     public final static String                                      PARSIMONY_OUTPUT_DOLLO_GAINS_HTML_D                                           = "_dollo_gains_d.html";
114     public final static String                                      PARSIMONY_OUTPUT_DOLLO_LOSSES_D                                               = "_dollo_losses_d";
115     public final static String                                      PARSIMONY_OUTPUT_DOLLO_LOSSES_HTML_D                                          = "_dollo_losses_d.html";
116     public final static String                                      PARSIMONY_OUTPUT_DOLLO_PRESENT_D                                              = "_dollo_present_d";
117     public final static String                                      PARSIMONY_OUTPUT_DOLLO_PRESENT_HTML_D                                         = "_dollo_present_d.html";
118     public final static String                                      DOMAINS_PRESENT_NEXUS                                                         = "_dom.nex";
119     public final static String                                      BDC_PRESENT_NEXUS                                                             = "_dc.nex";
120     // ---
121     public final static String                                      PRG_NAME                                                                      = "surfacing";
122     public static final String                                      DOMAINS_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO                                    = "_d_dollo"
123             + ForesterConstants.PHYLO_XML_SUFFIX;
124     public static final String                                      DOMAINS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH                                    = "_d_fitch"
125             + ForesterConstants.PHYLO_XML_SUFFIX;
126     public static final String                                      BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO                 = "_dc_dollo"
127             + ForesterConstants.PHYLO_XML_SUFFIX;
128     public static final String                                      BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH                 = "_dc_fitch"
129             + ForesterConstants.PHYLO_XML_SUFFIX;
130     public static final String                                      NEXUS_EXTERNAL_DOMAINS                                                        = "_dom.nex";
131     public static final String                                      NEXUS_EXTERNAL_DOMAIN_COMBINATIONS                                            = "_dc.nex";
132     public static final String                                      NEXUS_SECONDARY_FEATURES                                                      = "_secondary_features.nex";
133     public static final String                                      PARSIMONY_OUTPUT_GL_SUFFIX_DOLLO_SECONDARY_FEATURES                           = "_dollo_gl_secondary_features";
134     public static final String                                      PARSIMONY_OUTPUT_GL_COUNTS_SUFFIX_DOLLO_SECONDARY_FEATURES                    = "_dollo_glc_secondary_features";
135     public static final String                                      PARSIMONY_OUTPUT_DOLLO_GAINS_SECONDARY_FEATURES                               = "_dollo_gains_secondary_features";
136     public static final String                                      PARSIMONY_OUTPUT_DOLLO_LOSSES_SECONDARY_FEATURES                              = "_dollo_losses_secondary_features";
137     public static final String                                      PARSIMONY_OUTPUT_DOLLO_PRESENT_SECONDARY_FEATURES                             = "_dollo_present_secondary_features";
138     public static final String                                      SECONDARY_FEATURES_PARSIMONY_TREE_OUTPUT_SUFFIX_DOLLO                         = "_secondary_features_dollo"
139             + ForesterConstants.PHYLO_XML_SUFFIX;
140     public static final String                                      PARSIMONY_OUTPUT_DOLLO_ALL_GOID_D_ALL_NAMESPACES                              = "_dollo_goid_d";
141     public static final String                                      PARSIMONY_OUTPUT_FITCH_ALL_GOID_BC_ALL_NAMESPACES                             = "_fitch_goid_dc";
142     final static private String                                     HELP_OPTION_1                                                                 = "help";
143     final static private String                                     HELP_OPTION_2                                                                 = "h";
144     final static private String                                     OUTPUT_DIR_OPTION                                                             = "out_dir";
145     final static private String                                     SCORING_OPTION                                                                = "scoring";
146     private static final DomainSimilarityScoring                    SCORING_DEFAULT                                                               = DomainSimilarity.DomainSimilarityScoring.COMBINATIONS;
147     final static private String                                     SCORING_DOMAIN_COUNT_BASED                                                    = "domains";
148     final static private String                                     SCORING_PROTEIN_COUNT_BASED                                                   = "proteins";
149     final static private String                                     SCORING_COMBINATION_BASED                                                     = "combinations";
150     final static private String                                     DETAILEDNESS_OPTION                                                           = "detail";
151     private final static Detailedness                               DETAILEDNESS_DEFAULT                                                          = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
152     final static private String                                     SPECIES_MATRIX_OPTION                                                         = "smatrix";
153     final static private String                                     DETAILEDNESS_BASIC                                                            = "basic";
154     final static private String                                     DETAILEDNESS_LIST_IDS                                                         = "list_ids";
155     final static private String                                     DETAILEDNESS_PUNCTILIOUS                                                      = "punctilious";
156     final static private String                                     DOMAIN_SIMILARITY_SORT_OPTION                                                 = "sort";
157     private static final DomainSimilarity.DomainSimilaritySortField DOMAIN_SORT_FILD_DEFAULT                                                      = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
158     final static private String                                     DOMAIN_SIMILARITY_SORT_MIN                                                    = "min";
159     final static private String                                     DOMAIN_SIMILARITY_SORT_MAX                                                    = "max";
160     final static private String                                     DOMAIN_SIMILARITY_SORT_SD                                                     = "sd";
161     final static private String                                     DOMAIN_SIMILARITY_SORT_MEAN                                                   = "mean";
162     final static private String                                     DOMAIN_SIMILARITY_SORT_DIFF                                                   = "diff";
163     final static private String                                     DOMAIN_SIMILARITY_SORT_COUNTS_DIFF                                            = "count_diff";
164     final static private String                                     DOMAIN_SIMILARITY_SORT_ABS_COUNTS_DIFF                                        = "abs_count_diff";
165     final static private String                                     DOMAIN_SIMILARITY_SORT_SPECIES_COUNT                                          = "species";
166     final static private String                                     DOMAIN_SIMILARITY_SORT_ALPHA                                                  = "alpha";
167     final static private String                                     DOMAIN_SIMILARITY_SORT_BY_SPECIES_COUNT_FIRST_OPTION                          = "species_first";
168     final static private String                                     DOMAIN_COUNT_SORT_OPTION                                                      = "dc_sort";
169     private static final GenomeWideCombinableDomainsSortOrder       DOMAINS_SORT_ORDER_DEFAULT                                                    = GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder.ALPHABETICAL_KEY_ID;
170     final static private String                                     DOMAIN_COUNT_SORT_ALPHA                                                       = "alpha";
171     final static private String                                     DOMAIN_COUNT_SORT_KEY_DOMAIN_COUNT                                            = "dom";
172     final static private String                                     DOMAIN_COUNT_SORT_KEY_DOMAIN_PROTEINS_COUNT                                   = "prot";
173     final static private String                                     DOMAIN_COUNT_SORT_COMBINATIONS_COUNT                                          = "comb";
174     final static private String                                     CUTOFF_SCORE_FILE_OPTION                                                      = "cos";
175     final static private String                                     NOT_IGNORE_DUFS_OPTION                                                        = "dufs";
176     final static private String                                     MAX_FS_E_VALUE_OPTION                                                         = "fs_e";
177     final static private String                                     MAX_I_E_VALUE_OPTION                                                          = "ie";
178     final static private String                                     MIN_REL_ENV_LENGTH_RATIO_OPTION                                               = "mrel";
179     final static private String                                     MAX_ALLOWED_OVERLAP_OPTION                                                    = "mo";
180     final static private String                                     NO_ENGULFING_OVERLAP_OPTION                                                   = "no_eo";
181     final static private String                                     IGNORE_COMBINATION_WITH_SAME_OPTION                                           = "ignore_self_comb";
182     final static private String                                     PERFORM_DC_REGAIN_PROTEINS_STATS_OPTION                                       = "dc_regain_stats";
183     final static private String                                     DA_ANALYSIS_OPTION                                                            = "da_analyis";
184     final static private String                                     USE_LAST_IN_FITCH_OPTION                                                      = "last";
185     public final static String                                      PAIRWISE_DOMAIN_COMPARISONS_PREFIX                                            = "pwc_";
186     final static private String                                     PAIRWISE_DOMAIN_COMPARISONS_OPTION                                            = "pwc";
187     final static private String                                     OUTPUT_FILE_OPTION                                                            = "o";
188     final static private String                                     PFAM_TO_GO_FILE_USE_OPTION                                                    = "p2g";
189     final static private String                                     GO_OBO_FILE_USE_OPTION                                                        = "obo";
190     final static private String                                     GO_NAMESPACE_LIMIT_OPTION                                                     = "go_namespace";
191     final static private String                                     GO_NAMESPACE_LIMIT_OPTION_MOLECULAR_FUNCTION                                  = "molecular_function";
192     final static private String                                     GO_NAMESPACE_LIMIT_OPTION_BIOLOGICAL_PROCESS                                  = "biological_process";
193     final static private String                                     GO_NAMESPACE_LIMIT_OPTION_CELLULAR_COMPONENT                                  = "cellular_component";
194     final static private String                                     SECONDARY_FEATURES_PARSIMONY_MAP_FILE                                         = "secondary";
195     final static private String                                     DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_TAB_DELIMITED                           = "simple_tab";
196     final static private String                                     DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_HTML                                    = "simple_html";
197     final static private String                                     DOMAIN_SIMILARITY_PRINT_OPTION_DETAILED_HTML                                  = "detailed_html";
198     final static private String                                     DOMAIN_SIMILARITY_PRINT_OPTION                                                = "ds_output";
199     private static final PRINT_OPTION                               DOMAIN_SIMILARITY_PRINT_OPTION_DEFAULT                                        = DomainSimilarity.PRINT_OPTION.HTML;
200     final static private String                                     IGNORE_DOMAINS_WITHOUT_COMBINATIONS_IN_ALL_SPECIES_OPTION                     = "ignore_singlet_domains";
201     final static private String                                     IGNORE_VIRAL_IDS                                                              = "ignore_viral_ids";
202     final static private boolean                                    IGNORE_DOMAINS_WITHOUT_COMBINATIONS_IN_ALL_SPECIES_DEFAULT                    = false;
203     final static private String                                     IGNORE_DOMAINS_SPECIFIC_TO_ONE_SPECIES_OPTION                                 = "ignore_species_specific_domains";
204     final static private boolean                                    IGNORE_DOMAINS_SPECIFIC_TO_ONE_SPECIES_OPTION_DEFAULT                         = false;
205     final static private String                                     MATRIX_MEAN_SCORE_BASED_GENOME_DISTANCE_SUFFIX                                = "_mean_score.pwd";
206     final static private String                                     MATRIX_SHARED_DOMAINS_BASED_GENOME_DISTANCE_SUFFIX                            = "_domains.pwd";
207     final static private String                                     MATRIX_SHARED_BIN_COMBINATIONS_BASED_GENOME_DISTANCE_SUFFIX                   = "_bin_combinations.pwd";
208     final static private String                                     NJ_TREE_MEAN_SCORE_BASED_GENOME_DISTANCE_SUFFIX                               = "_mean_score_NJ"
209             + ForesterConstants.PHYLO_XML_SUFFIX;
210     final static private String                                     NJ_TREE_SHARED_DOMAINS_BASED_GENOME_DISTANCE_SUFFIX                           = "_domains_NJ"
211             + ForesterConstants.PHYLO_XML_SUFFIX;
212     final static private String                                     NJ_TREE_SHARED_BIN_COMBINATIONS_BASED_GENOME_DISTANCE_SUFFIX                  = "_bin_combinations_NJ"
213             + ForesterConstants.PHYLO_XML_SUFFIX;
214     final static private String                                     FILTER_POSITIVE_OPTION                                                        = "pos_filter";
215     final static private String                                     FILTER_NEGATIVE_OPTION                                                        = "neg_filter";
216     final static private String                                     FILTER_NEGATIVE_DOMAINS_OPTION                                                = "neg_dom_filter";
217     final static private String                                     INPUT_GENOMES_FILE_OPTION                                                     = "genomes";
218     final static private String                                     INPUT_SPECIES_TREE_OPTION                                                     = "species_tree";
219     final static private String                                     SEQ_EXTRACT_OPTION                                                            = "prot_extract";
220     final static private String                                     PRG_VERSION                                                                   = "2.501";
221     final static private String                                     PRG_DATE                                                                      = "170327";
222     final static private String                                     E_MAIL                                                                        = "czmasek@burnham.org";
223     final static private String                                     WWW                                                                           = "https://sites.google.com/site/cmzmasek/home/software/forester/surfacing";
224     final static private boolean                                    IGNORE_DUFS_DEFAULT                                                           = true;
225     final static private boolean                                    IGNORE_COMBINATION_WITH_SAME_DEFAULLT                                         = false;
226     final static private double                                     MAX_E_VALUE_DEFAULT                                                           = -1;
227     public final static int                                         MAX_ALLOWED_OVERLAP_DEFAULT                                                   = -1;
228     private static final String                                     RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION                                        = "random_seed";
229     private static final String                                     CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS                                      = "consider_bdc_direction";
230     private static final String                                     CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS_AND_ADJACENCY                        = "consider_bdc_adj";
231     public static final String                                      SEQ_EXTRACT_SUFFIX                                                            = ".prot";
232     public static final String                                      PLUS_MINUS_ANALYSIS_OPTION                                                    = "plus_minus";
233     public static final String                                      PLUS_MINUS_DOM_SUFFIX                                                         = "_plus_minus_dom.txt";
234     public static final String                                      PLUS_MINUS_DOM_SUFFIX_HTML                                                    = "_plus_minus_dom.html";
235     public static final String                                      PLUS_MINUS_DC_SUFFIX_HTML                                                     = "_plus_minus_dc.html";
236     public static final int                                         PLUS_MINUS_ANALYSIS_MIN_DIFF_DEFAULT                                          = 0;
237     public static final double                                      PLUS_MINUS_ANALYSIS_FACTOR_DEFAULT                                            = 1.0;
238     public static final String                                      PLUS_MINUS_ALL_GO_IDS_DOM_SUFFIX                                              = "_plus_minus_go_ids_all.txt";
239     public static final String                                      PLUS_MINUS_PASSING_GO_IDS_DOM_SUFFIX                                          = "_plus_minus_go_ids_passing.txt";
240     private static final String                                     OUTPUT_LIST_OF_ALL_PROTEINS_OPTIONS                                           = "all_prot";
241     final static private String                                     OUTPUT_LIST_OF_ALL_PROTEINS_PER_DOMAIN_E_VALUE_OPTION                         = "all_prot_e";
242     public static final boolean                                     VERBOSE                                                                       = false;
243     private static final String                                     OUTPUT_DOMAIN_COMBINATIONS_GAINED_MORE_THAN_ONCE_ANALYSIS_SUFFIX              = "_fitch_dc_gains_counts";
244     private static final String                                     OUTPUT_DOMAIN_COMBINATIONS_LOST_MORE_THAN_ONCE_ANALYSIS_SUFFIX                = "_fitch_dc_losses_counts";
245     private static final String                                     DOMAIN_LENGTHS_ANALYSIS_SUFFIX                                                = "_domain_lengths_analysis";
246     private static final String                                     PERFORM_DOMAIN_LENGTH_ANALYSIS_OPTION                                         = "dla";
247     public static final String                                      ALL_PFAMS_ENCOUNTERED_SUFFIX                                                  = "_all_encountered_pfams";
248     public static final String                                      ALL_PFAMS_ENCOUNTERED_WITH_GO_ANNOTATION_SUFFIX                               = "_all_encountered_pfams_with_go_annotation";
249     public static final String                                      ENCOUNTERED_PFAMS_SUMMARY_SUFFIX                                              = "_encountered_pfams_summary";
250     public static final String                                      ALL_PFAMS_GAINED_AS_DOMAINS_SUFFIX                                            = "_all_pfams_gained_as_domains";
251     public static final String                                      ALL_PFAMS_LOST_AS_DOMAINS_SUFFIX                                              = "_all_pfams_lost_as_domains";
252     public static final String                                      ALL_PFAMS_GAINED_AS_DC_SUFFIX                                                 = "_all_pfams_gained_as_dc";
253     public static final String                                      ALL_PFAMS_LOST_AS_DC_SUFFIX                                                   = "_all_pfams_lost_as_dc";
254     public static final String                                      BASE_DIRECTORY_PER_NODE_DOMAIN_GAIN_LOSS_FILES                                = "PER_NODE_EVENTS";
255     public static final String                                      BASE_DIRECTORY_PER_SUBTREE_DOMAIN_GAIN_LOSS_FILES                             = "PER_SUBTREE_EVENTS";
256     public static final String                                      D_PROMISCUITY_FILE_SUFFIX                                                     = "_domain_promiscuities";
257     private static final String                                     LOG_FILE_SUFFIX                                                               = "_log.txt";
258     private static final String                                     DATA_FILE_SUFFIX                                                              = "_domain_combination_data.txt";
259     private static final String                                     DATA_FILE_DESC                                                                = "#SPECIES\tPRTEIN_ID\tN_TERM_DOMAIN\tC_TERM_DOMAIN\tN_TERM_DOMAIN_PER_DOMAIN_E_VALUE\tC_TERM_DOMAIN_PER_DOMAIN_E_VALUE\tN_TERM_DOMAIN_COUNTS_PER_PROTEIN\tC_TERM_DOMAIN_COUNTS_PER_PROTEIN";
260     private static final String                                     WRITE_TO_NEXUS_OPTION                                                         = "nexus";
261     private static final String                                     PERFORM_DC_FITCH                                                              = "dc_pars";
262     private static final INDIVIDUAL_SCORE_CUTOFF                    INDIVIDUAL_SCORE_CUTOFF_DEFAULT                                               = INDIVIDUAL_SCORE_CUTOFF.FULL_SEQUENCE;                                                                                                                                                      //TODO look at me! change?
263     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_COUNTS_OUTPUT_SUFFIX                          = "_indep_dc_gains_fitch_counts.txt";
264     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_DC_OUTPUT_SUFFIX                              = "_indep_dc_gains_fitch_lists.txt";
265     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_OUTPUT_SUFFIX               = "_indep_dc_gains_fitch_lists_for_go_mapping.txt";
266     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_OUTPUT_UNIQUE_SUFFIX        = "_indep_dc_gains_fitch_lists_for_go_mapping_unique.txt";
267     public static final String                                      LIMIT_SPEC_FOR_PROT_EX                                                        = null;                                                                                                                                                                                       // e.g. "HUMAN"; set to null for not using this feature (default).
268     public static final String                                      BINARY_DOMAIN_COMBINATIONS_PARSIMONY_TREE_OUTPUT_SUFFIX_FITCH_MAPPED          = "_dc_MAPPED_secondary_features_fitch"
269             + ForesterConstants.PHYLO_XML_SUFFIX;
270     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_COUNTS_MAPPED_OUTPUT_SUFFIX                   = "_indep_dc_gains_fitch_counts_MAPPED.txt";
271     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_DC_MAPPED_OUTPUT_SUFFIX                       = "_indep_dc_gains_fitch_lists_MAPPED.txt";
272     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_MAPPED_OUTPUT_SUFFIX        = "_indep_dc_gains_fitch_lists_for_go_mapping_MAPPED.txt";
273     public static final String                                      INDEPENDENT_DC_GAINS_FITCH_PARS_DC_FOR_GO_MAPPING_MAPPED_OUTPUT_UNIQUE_SUFFIX = "_indep_dc_gains_fitch_lists_for_go_mapping_unique_MAPPED.txt";
274     private static final boolean                                    CALC_SIMILARITY_SCORES                                                        = false;
275     private static final String                                     SEPARATOR_FOR_DA                                                              = "--";
276
277     @SuppressWarnings( "unchecked")
278     public static void main( final String args[] ) {
279         final long start_time = new Date().getTime();
280         // final StringBuffer log = new StringBuffer();
281         final StringBuilder html_desc = new StringBuilder();
282         ForesterUtil.printProgramInformation( surfacing.PRG_NAME,
283                                               surfacing.PRG_VERSION,
284                                               surfacing.PRG_DATE,
285                                               surfacing.E_MAIL,
286                                               surfacing.WWW );
287         final String nl = ForesterUtil.LINE_SEPARATOR;
288         html_desc.append( "<table>" + nl );
289         html_desc.append( "<tr><td>Produced by:</td><td>" + surfacing.PRG_NAME + "</td></tr>" + nl );
290         html_desc.append( "<tr><td>Version:</td><td>" + surfacing.PRG_VERSION + "</td></tr>" + nl );
291         html_desc.append( "<tr><td>Release Date:</td><td>" + surfacing.PRG_DATE + "</td></tr>" + nl );
292         html_desc.append( "<tr><td>Contact:</td><td>" + surfacing.E_MAIL + "</td></tr>" + nl );
293         html_desc.append( "<tr><td>WWW:</td><td>" + surfacing.WWW + "</td></tr>" + nl );
294         CommandLineArguments cla = null;
295         try {
296             cla = new CommandLineArguments( args );
297         }
298         catch ( final Exception e ) {
299             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
300         }
301         if ( cla.isOptionSet( surfacing.HELP_OPTION_1 ) || cla.isOptionSet( surfacing.HELP_OPTION_2 ) ) {
302             surfacing.printHelp();
303             System.exit( 0 );
304         }
305         if ( ( args.length < 1 ) ) {
306             surfacing.printHelp();
307             System.exit( -1 );
308         }
309         final List<String> allowed_options = new ArrayList<String>();
310         allowed_options.add( surfacing.NOT_IGNORE_DUFS_OPTION );
311         allowed_options.add( surfacing.MAX_FS_E_VALUE_OPTION );
312         allowed_options.add( surfacing.MAX_I_E_VALUE_OPTION );
313         allowed_options.add( surfacing.MIN_REL_ENV_LENGTH_RATIO_OPTION );
314         allowed_options.add( surfacing.DETAILEDNESS_OPTION );
315         allowed_options.add( surfacing.OUTPUT_FILE_OPTION );
316         allowed_options.add( surfacing.DOMAIN_SIMILARITY_SORT_OPTION );
317         allowed_options.add( surfacing.SPECIES_MATRIX_OPTION );
318         allowed_options.add( surfacing.SCORING_OPTION );
319         allowed_options.add( surfacing.MAX_ALLOWED_OVERLAP_OPTION );
320         allowed_options.add( surfacing.NO_ENGULFING_OVERLAP_OPTION );
321         allowed_options.add( surfacing.DOMAIN_COUNT_SORT_OPTION );
322         allowed_options.add( surfacing.CUTOFF_SCORE_FILE_OPTION );
323         allowed_options.add( surfacing.DOMAIN_SIMILARITY_SORT_BY_SPECIES_COUNT_FIRST_OPTION );
324         allowed_options.add( surfacing.OUTPUT_DIR_OPTION );
325         allowed_options.add( surfacing.IGNORE_COMBINATION_WITH_SAME_OPTION );
326         allowed_options.add( surfacing.PFAM_TO_GO_FILE_USE_OPTION );
327         allowed_options.add( surfacing.GO_OBO_FILE_USE_OPTION );
328         allowed_options.add( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION );
329         allowed_options.add( surfacing.GO_NAMESPACE_LIMIT_OPTION );
330         allowed_options.add( surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION );
331         allowed_options.add( surfacing.IGNORE_DOMAINS_WITHOUT_COMBINATIONS_IN_ALL_SPECIES_OPTION );
332         allowed_options.add( surfacing.CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS );
333         allowed_options.add( INPUT_SPECIES_TREE_OPTION );
334         allowed_options.add( FILTER_POSITIVE_OPTION );
335         allowed_options.add( FILTER_NEGATIVE_OPTION );
336         allowed_options.add( INPUT_GENOMES_FILE_OPTION );
337         allowed_options.add( RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION );
338         allowed_options.add( FILTER_NEGATIVE_DOMAINS_OPTION );
339         allowed_options.add( IGNORE_VIRAL_IDS );
340         allowed_options.add( SEQ_EXTRACT_OPTION );
341         allowed_options.add( OUTPUT_LIST_OF_ALL_PROTEINS_PER_DOMAIN_E_VALUE_OPTION );
342         allowed_options.add( SECONDARY_FEATURES_PARSIMONY_MAP_FILE );
343         allowed_options.add( PLUS_MINUS_ANALYSIS_OPTION );
344         allowed_options.add( DOMAIN_COMBINITONS_OUTPUT_OPTION_FOR_GRAPH_ANALYSIS );
345         allowed_options.add( DOMAIN_COMBINITONS_COUNTS_OUTPUT_OPTION );
346         allowed_options.add( OUTPUT_LIST_OF_ALL_PROTEINS_OPTIONS );
347         allowed_options.add( CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS_AND_ADJACENCY );
348         allowed_options.add( WRITE_TO_NEXUS_OPTION );
349         allowed_options.add( PERFORM_DC_REGAIN_PROTEINS_STATS_OPTION );
350         allowed_options.add( DA_ANALYSIS_OPTION );
351         allowed_options.add( USE_LAST_IN_FITCH_OPTION );
352         allowed_options.add( PERFORM_DC_FITCH );
353         allowed_options.add( PERFORM_DOMAIN_LENGTH_ANALYSIS_OPTION );
354         boolean ignore_dufs = surfacing.IGNORE_DUFS_DEFAULT;
355         boolean ignore_combination_with_same = surfacing.IGNORE_COMBINATION_WITH_SAME_DEFAULLT;
356         double fs_e_value_max = surfacing.MAX_E_VALUE_DEFAULT;
357         double ie_value_max = surfacing.MAX_E_VALUE_DEFAULT;
358         double rel_env_length_ratio_cutoff = -1;
359         int max_allowed_overlap = surfacing.MAX_ALLOWED_OVERLAP_DEFAULT;
360         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
361         if ( dissallowed_options.length() > 0 ) {
362             ForesterUtil.fatalError( surfacing.PRG_NAME, "unknown option(s): " + dissallowed_options );
363         }
364         boolean use_last_in_fitch_parsimony = false;
365         if ( cla.isOptionSet( USE_LAST_IN_FITCH_OPTION ) ) {
366             use_last_in_fitch_parsimony = true;
367         }
368         boolean write_to_nexus = false;
369         if ( cla.isOptionSet( WRITE_TO_NEXUS_OPTION ) ) {
370             write_to_nexus = true;
371         }
372         boolean perform_dc_fich = false;
373         if ( cla.isOptionSet( PERFORM_DC_FITCH ) ) {
374             perform_dc_fich = true;
375         }
376         boolean perform_dc_regain_proteins_stats = false;
377         if ( cla.isOptionSet( PERFORM_DC_REGAIN_PROTEINS_STATS_OPTION ) ) {
378             perform_dc_regain_proteins_stats = true;
379         }
380         boolean da_analysis = false;
381         if ( cla.isOptionSet( DA_ANALYSIS_OPTION ) ) {
382             da_analysis = true;
383         }
384         boolean output_binary_domain_combinationsfor_graph_analysis = false;
385         if ( cla.isOptionSet( DOMAIN_COMBINITONS_OUTPUT_OPTION_FOR_GRAPH_ANALYSIS ) ) {
386             output_binary_domain_combinationsfor_graph_analysis = true;
387         }
388         boolean output_binary_domain_combinationsfor_counts = false;
389         if ( cla.isOptionSet( DOMAIN_COMBINITONS_COUNTS_OUTPUT_OPTION ) ) {
390             output_binary_domain_combinationsfor_counts = true;
391         }
392         if ( cla.isOptionSet( surfacing.MAX_FS_E_VALUE_OPTION ) ) {
393             try {
394                 fs_e_value_max = cla.getOptionValueAsDouble( surfacing.MAX_FS_E_VALUE_OPTION );
395             }
396             catch ( final Exception e ) {
397                 ForesterUtil.fatalError( surfacing.PRG_NAME, "no acceptable value for E-value maximum" );
398             }
399         }
400         if ( cla.isOptionSet( surfacing.MIN_REL_ENV_LENGTH_RATIO_OPTION ) ) {
401             try {
402                 rel_env_length_ratio_cutoff = cla.getOptionValueAsDouble( surfacing.MIN_REL_ENV_LENGTH_RATIO_OPTION );
403             }
404             catch ( final Exception e ) {
405                 ForesterUtil.fatalError( surfacing.PRG_NAME, "no acceptable value for min rel env length ratio" );
406             }
407         }
408         if ( cla.isOptionSet( surfacing.MAX_I_E_VALUE_OPTION ) ) {
409             try {
410                 ie_value_max = cla.getOptionValueAsDouble( surfacing.MAX_I_E_VALUE_OPTION );
411             }
412             catch ( final Exception e ) {
413                 ForesterUtil.fatalError( surfacing.PRG_NAME, "no acceptable value for E-value maximum" );
414             }
415         }
416         if ( cla.isOptionSet( surfacing.MAX_ALLOWED_OVERLAP_OPTION ) ) {
417             try {
418                 max_allowed_overlap = cla.getOptionValueAsInt( surfacing.MAX_ALLOWED_OVERLAP_OPTION );
419             }
420             catch ( final Exception e ) {
421                 ForesterUtil.fatalError( surfacing.PRG_NAME, "no acceptable value for maximal allowed domain overlap" );
422             }
423         }
424         boolean no_engulfing_overlaps = false;
425         if ( cla.isOptionSet( surfacing.NO_ENGULFING_OVERLAP_OPTION ) ) {
426             no_engulfing_overlaps = true;
427         }
428         boolean ignore_virus_like_ids = false;
429         if ( cla.isOptionSet( surfacing.IGNORE_VIRAL_IDS ) ) {
430             ignore_virus_like_ids = true;
431         }
432         if ( cla.isOptionSet( surfacing.NOT_IGNORE_DUFS_OPTION ) ) {
433             ignore_dufs = false;
434         }
435         if ( cla.isOptionSet( surfacing.IGNORE_COMBINATION_WITH_SAME_OPTION ) ) {
436             ignore_combination_with_same = true;
437         }
438         boolean domain_length_analysis = false;
439         if ( cla.isOptionSet( surfacing.PERFORM_DOMAIN_LENGTH_ANALYSIS_OPTION ) ) {
440             domain_length_analysis = true;
441         }
442         boolean ignore_domains_without_combs_in_all_spec = IGNORE_DOMAINS_WITHOUT_COMBINATIONS_IN_ALL_SPECIES_DEFAULT;
443         if ( cla.isOptionSet( surfacing.IGNORE_DOMAINS_WITHOUT_COMBINATIONS_IN_ALL_SPECIES_OPTION ) ) {
444             ignore_domains_without_combs_in_all_spec = true;
445         }
446         boolean ignore_species_specific_domains = IGNORE_DOMAINS_SPECIFIC_TO_ONE_SPECIES_OPTION_DEFAULT;
447         if ( cla.isOptionSet( surfacing.IGNORE_DOMAINS_SPECIFIC_TO_ONE_SPECIES_OPTION ) ) {
448             ignore_species_specific_domains = true;
449         }
450         if ( !cla.isOptionValueSet( surfacing.INPUT_SPECIES_TREE_OPTION ) ) {
451             ForesterUtil.fatalError( surfacing.PRG_NAME,
452                                      "no input species tree file given: " + surfacing.INPUT_SPECIES_TREE_OPTION
453                                              + "=<file>" );
454         }
455         File output_file = null;
456         if ( cla.isOptionSet( surfacing.OUTPUT_FILE_OPTION ) ) {
457             if ( !cla.isOptionValueSet( surfacing.OUTPUT_FILE_OPTION ) ) {
458                 ForesterUtil.fatalError( surfacing.PRG_NAME,
459                                          "no value for domain combinations similarities output file: -"
460                                                  + surfacing.OUTPUT_FILE_OPTION + "=<file>" );
461             }
462             output_file = new File( cla.getOptionValue( surfacing.OUTPUT_FILE_OPTION ) );
463             SurfacingUtil.checkForOutputFileWriteability( output_file );
464         }
465         File cutoff_scores_file = null;
466         Map<String, Double> individual_score_cutoffs = null;
467         if ( cla.isOptionSet( surfacing.CUTOFF_SCORE_FILE_OPTION ) ) {
468             if ( !cla.isOptionValueSet( surfacing.CUTOFF_SCORE_FILE_OPTION ) ) {
469                 ForesterUtil.fatalError( surfacing.PRG_NAME,
470                                          "no value for individual domain score cutoffs file: -"
471                                                  + surfacing.CUTOFF_SCORE_FILE_OPTION + "=<file>" );
472             }
473             cutoff_scores_file = new File( cla.getOptionValue( surfacing.CUTOFF_SCORE_FILE_OPTION ) );
474             final String error = ForesterUtil.isReadableFile( cutoff_scores_file );
475             if ( !ForesterUtil.isEmpty( error ) ) {
476                 ForesterUtil.fatalError( surfacing.PRG_NAME,
477                                          "cannot read individual domain score cutoffs file: " + error );
478             }
479             try {
480                 final BasicTable<String> scores_table = BasicTableParser.parse( cutoff_scores_file, ' ' );
481                 individual_score_cutoffs = scores_table.getColumnsAsMapDouble( 0, 1 );
482             }
483             catch ( final IOException e ) {
484                 ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot read from individual score cutoffs file: " + e );
485             }
486         }
487         BinaryDomainCombination.DomainCombinationType dc_type = BinaryDomainCombination.DomainCombinationType.BASIC;
488         if ( cla.isOptionSet( surfacing.CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS ) ) {
489             dc_type = BinaryDomainCombination.DomainCombinationType.DIRECTED;
490         }
491         if ( cla.isOptionSet( surfacing.CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS_AND_ADJACENCY ) ) {
492             dc_type = BinaryDomainCombination.DomainCombinationType.DIRECTED_ADJACTANT;
493         }
494         File out_dir = null;
495         if ( cla.isOptionSet( surfacing.OUTPUT_DIR_OPTION ) ) {
496             if ( !cla.isOptionValueSet( surfacing.OUTPUT_DIR_OPTION ) ) {
497                 ForesterUtil.fatalError( surfacing.PRG_NAME,
498                                          "no value for output directory: -" + surfacing.OUTPUT_DIR_OPTION + "=<dir>" );
499             }
500             out_dir = new File( cla.getOptionValue( surfacing.OUTPUT_DIR_OPTION ) );
501             if ( out_dir.exists() && ( out_dir.listFiles().length > 0 ) ) {
502                 ForesterUtil.fatalError( surfacing.PRG_NAME, "\"" + out_dir + "\" aready exists and is not empty" );
503             }
504             if ( !out_dir.exists() ) {
505                 final boolean success = out_dir.mkdir();
506                 if ( !success || !out_dir.exists() ) {
507                     ForesterUtil.fatalError( surfacing.PRG_NAME, "failed to create \"" + out_dir + "\"" );
508                 }
509             }
510             if ( !out_dir.canWrite() ) {
511                 ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot write to \"" + out_dir + "\"" );
512             }
513         }
514         File positive_filter_file = null;
515         File negative_filter_file = null;
516         File negative_domains_filter_file = null;
517         if ( cla.isOptionSet( surfacing.FILTER_NEGATIVE_OPTION )
518                 && cla.isOptionSet( surfacing.FILTER_POSITIVE_OPTION ) ) {
519             ForesterUtil.fatalError( surfacing.PRG_NAME, "attempt to use both negative and positive protein filter" );
520         }
521         if ( cla.isOptionSet( surfacing.FILTER_NEGATIVE_DOMAINS_OPTION )
522                 && ( cla.isOptionSet( surfacing.FILTER_NEGATIVE_OPTION )
523                         || cla.isOptionSet( surfacing.FILTER_POSITIVE_OPTION ) ) ) {
524             ForesterUtil
525                     .fatalError( surfacing.PRG_NAME,
526                                  "attempt to use both negative or positive protein filter together wirh a negative domains filter" );
527         }
528         if ( cla.isOptionSet( surfacing.FILTER_NEGATIVE_OPTION ) ) {
529             if ( !cla.isOptionValueSet( surfacing.FILTER_NEGATIVE_OPTION ) ) {
530                 ForesterUtil
531                         .fatalError( surfacing.PRG_NAME,
532                                      "no value for negative filter: -" + surfacing.FILTER_NEGATIVE_OPTION + "=<file>" );
533             }
534             negative_filter_file = new File( cla.getOptionValue( surfacing.FILTER_NEGATIVE_OPTION ) );
535             final String msg = ForesterUtil.isReadableFile( negative_filter_file );
536             if ( !ForesterUtil.isEmpty( msg ) ) {
537                 ForesterUtil.fatalError( surfacing.PRG_NAME,
538                                          "can not read from \"" + negative_filter_file + "\": " + msg );
539             }
540         }
541         else if ( cla.isOptionSet( surfacing.FILTER_POSITIVE_OPTION ) ) {
542             if ( !cla.isOptionValueSet( surfacing.FILTER_POSITIVE_OPTION ) ) {
543                 ForesterUtil
544                         .fatalError( surfacing.PRG_NAME,
545                                      "no value for positive filter: -" + surfacing.FILTER_POSITIVE_OPTION + "=<file>" );
546             }
547             positive_filter_file = new File( cla.getOptionValue( surfacing.FILTER_POSITIVE_OPTION ) );
548             final String msg = ForesterUtil.isReadableFile( positive_filter_file );
549             if ( !ForesterUtil.isEmpty( msg ) ) {
550                 ForesterUtil.fatalError( surfacing.PRG_NAME,
551                                          "can not read from \"" + positive_filter_file + "\": " + msg );
552             }
553         }
554         else if ( cla.isOptionSet( surfacing.FILTER_NEGATIVE_DOMAINS_OPTION ) ) {
555             if ( !cla.isOptionValueSet( surfacing.FILTER_NEGATIVE_DOMAINS_OPTION ) ) {
556                 ForesterUtil.fatalError( surfacing.PRG_NAME,
557                                          "no value for negative domains filter: -"
558                                                  + surfacing.FILTER_NEGATIVE_DOMAINS_OPTION + "=<file>" );
559             }
560             negative_domains_filter_file = new File( cla.getOptionValue( surfacing.FILTER_NEGATIVE_DOMAINS_OPTION ) );
561             final String msg = ForesterUtil.isReadableFile( negative_domains_filter_file );
562             if ( !ForesterUtil.isEmpty( msg ) ) {
563                 ForesterUtil.fatalError( surfacing.PRG_NAME,
564                                          "can not read from \"" + negative_domains_filter_file + "\": " + msg );
565             }
566         }
567         final List<String> plus_minus_analysis_high_copy_base_species = new ArrayList<String>();
568         final List<String> plus_minus_analysis_high_copy_target_species = new ArrayList<String>();
569         final List<String> plus_minus_analysis_high_low_copy_species = new ArrayList<String>();
570         final List<Object> plus_minus_analysis_numbers = new ArrayList<Object>();
571         SurfacingUtil.processPlusMinusAnalysisOption( cla,
572                                                       plus_minus_analysis_high_copy_base_species,
573                                                       plus_minus_analysis_high_copy_target_species,
574                                                       plus_minus_analysis_high_low_copy_species,
575                                                       plus_minus_analysis_numbers );
576         File input_genomes_file = null;
577         if ( cla.isOptionSet( surfacing.INPUT_GENOMES_FILE_OPTION ) ) {
578             if ( !cla.isOptionValueSet( surfacing.INPUT_GENOMES_FILE_OPTION ) ) {
579                 ForesterUtil.fatalError( surfacing.PRG_NAME,
580                                          "no value for input genomes file: -" + surfacing.INPUT_GENOMES_FILE_OPTION
581                                                  + "=<file>" );
582             }
583             input_genomes_file = new File( cla.getOptionValue( surfacing.INPUT_GENOMES_FILE_OPTION ) );
584             final String msg = ForesterUtil.isReadableFile( input_genomes_file );
585             if ( !ForesterUtil.isEmpty( msg ) ) {
586                 ForesterUtil.fatalError( surfacing.PRG_NAME,
587                                          "can not read from \"" + input_genomes_file + "\": " + msg );
588             }
589         }
590         else {
591             ForesterUtil
592                     .fatalError( surfacing.PRG_NAME,
593                                  "no input genomes file given: " + surfacing.INPUT_GENOMES_FILE_OPTION + "=<file>" );
594         }
595         DomainSimilarity.DomainSimilarityScoring scoring = SCORING_DEFAULT;
596         if ( cla.isOptionSet( surfacing.SCORING_OPTION ) ) {
597             if ( !cla.isOptionValueSet( surfacing.SCORING_OPTION ) ) {
598                 ForesterUtil
599                         .fatalError( surfacing.PRG_NAME,
600                                      "no value for scoring method for domain combinations similarity calculation: -"
601                                              + surfacing.SCORING_OPTION + "=<" + surfacing.SCORING_DOMAIN_COUNT_BASED
602                                              + "|" + surfacing.SCORING_PROTEIN_COUNT_BASED + "|"
603                                              + surfacing.SCORING_COMBINATION_BASED + ">\"" );
604             }
605             final String scoring_str = cla.getOptionValue( surfacing.SCORING_OPTION );
606             if ( scoring_str.equals( surfacing.SCORING_DOMAIN_COUNT_BASED ) ) {
607                 scoring = DomainSimilarity.DomainSimilarityScoring.DOMAINS;
608             }
609             else if ( scoring_str.equals( surfacing.SCORING_COMBINATION_BASED ) ) {
610                 scoring = DomainSimilarity.DomainSimilarityScoring.COMBINATIONS;
611             }
612             else if ( scoring_str.equals( surfacing.SCORING_PROTEIN_COUNT_BASED ) ) {
613                 scoring = DomainSimilarity.DomainSimilarityScoring.PROTEINS;
614             }
615             else {
616                 ForesterUtil
617                         .fatalError( surfacing.PRG_NAME,
618                                      "unknown value \"" + scoring_str
619                                              + "\" for scoring method for domain combinations similarity calculation: \"-"
620                                              + surfacing.SCORING_OPTION + "=<" + surfacing.SCORING_DOMAIN_COUNT_BASED
621                                              + "|" + surfacing.SCORING_PROTEIN_COUNT_BASED + "|"
622                                              + surfacing.SCORING_COMBINATION_BASED + ">\"" );
623             }
624         }
625         boolean sort_by_species_count_first = false;
626         if ( cla.isOptionSet( surfacing.DOMAIN_SIMILARITY_SORT_BY_SPECIES_COUNT_FIRST_OPTION ) ) {
627             sort_by_species_count_first = true;
628         }
629         boolean species_matrix = false;
630         if ( cla.isOptionSet( surfacing.SPECIES_MATRIX_OPTION ) ) {
631             species_matrix = true;
632         }
633         boolean output_protein_lists_for_all_domains = false;
634         double output_list_of_all_proteins_per_domain_e_value_max = -1;
635         if ( cla.isOptionSet( surfacing.OUTPUT_LIST_OF_ALL_PROTEINS_OPTIONS ) ) {
636             output_protein_lists_for_all_domains = true;
637             if ( cla.isOptionSet( surfacing.OUTPUT_LIST_OF_ALL_PROTEINS_PER_DOMAIN_E_VALUE_OPTION ) ) {
638                 try {
639                     output_list_of_all_proteins_per_domain_e_value_max = cla
640                             .getOptionValueAsDouble( surfacing.OUTPUT_LIST_OF_ALL_PROTEINS_PER_DOMAIN_E_VALUE_OPTION );
641                 }
642                 catch ( final Exception e ) {
643                     ForesterUtil.fatalError( surfacing.PRG_NAME, "no acceptable value for per domain E-value maximum" );
644                 }
645             }
646         }
647         Detailedness detailedness = DETAILEDNESS_DEFAULT;
648         if ( cla.isOptionSet( surfacing.DETAILEDNESS_OPTION ) ) {
649             if ( !cla.isOptionValueSet( surfacing.DETAILEDNESS_OPTION ) ) {
650                 ForesterUtil.fatalError( surfacing.PRG_NAME,
651                                          "no value for -" + surfacing.DETAILEDNESS_OPTION + "=<"
652                                                  + surfacing.DETAILEDNESS_BASIC + "|" + surfacing.DETAILEDNESS_LIST_IDS
653                                                  + "|" + surfacing.DETAILEDNESS_PUNCTILIOUS + ">\"" );
654             }
655             final String detness = cla.getOptionValue( surfacing.DETAILEDNESS_OPTION ).toLowerCase();
656             if ( detness.equals( surfacing.DETAILEDNESS_BASIC ) ) {
657                 detailedness = DomainSimilarityCalculator.Detailedness.BASIC;
658             }
659             else if ( detness.equals( surfacing.DETAILEDNESS_LIST_IDS ) ) {
660                 detailedness = DomainSimilarityCalculator.Detailedness.LIST_COMBINING_DOMAIN_FOR_EACH_SPECIES;
661             }
662             else if ( detness.equals( surfacing.DETAILEDNESS_PUNCTILIOUS ) ) {
663                 detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
664             }
665             else {
666                 ForesterUtil.fatalError( surfacing.PRG_NAME,
667                                          "unknown value \"" + detness + "\" for detailedness: \"-"
668                                                  + surfacing.DETAILEDNESS_OPTION + "=<" + surfacing.DETAILEDNESS_BASIC
669                                                  + "|" + surfacing.DETAILEDNESS_LIST_IDS + "|"
670                                                  + surfacing.DETAILEDNESS_PUNCTILIOUS + ">\"" );
671             }
672         }
673         String automated_pairwise_comparison_suffix = null;
674         boolean perform_pwc = false;
675         boolean write_pwc_files = false;
676         if ( cla.isOptionSet( surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION ) ) {
677             perform_pwc = true;
678             if ( !cla.isOptionValueSet( surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION ) ) {
679                 write_pwc_files = false;
680             }
681             else {
682                 write_pwc_files = true;
683                 automated_pairwise_comparison_suffix = "_"
684                         + cla.getOptionValue( surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION );
685             }
686         }
687         String query_domain_ids = null;
688         if ( cla.isOptionSet( surfacing.SEQ_EXTRACT_OPTION ) ) {
689             if ( !cla.isOptionValueSet( surfacing.SEQ_EXTRACT_OPTION ) ) {
690                 ForesterUtil
691                         .fatalError( surfacing.PRG_NAME,
692                                      "no domain ids given for sequences with given domains to be extracted : -"
693                                              + surfacing.SEQ_EXTRACT_OPTION
694                                              + "=<ordered domain sequences, domain ids separated by '~', sequences separated by '#'>" );
695             }
696             query_domain_ids = cla.getOptionValue( surfacing.SEQ_EXTRACT_OPTION );
697         }
698         DomainSimilarity.DomainSimilaritySortField domain_similarity_sort_field = DOMAIN_SORT_FILD_DEFAULT;
699         DomainSimilarity.DomainSimilaritySortField domain_similarity_sort_field_for_automated_pwc = DOMAIN_SORT_FILD_DEFAULT;
700         if ( cla.isOptionSet( surfacing.DOMAIN_SIMILARITY_SORT_OPTION ) ) {
701             if ( !cla.isOptionValueSet( surfacing.DOMAIN_SIMILARITY_SORT_OPTION ) ) {
702                 ForesterUtil.fatalError( surfacing.PRG_NAME,
703                                          "no value for domain combinations similarities sorting: -"
704                                                  + surfacing.DOMAIN_SIMILARITY_SORT_OPTION + "=<"
705                                                  + surfacing.DOMAIN_SIMILARITY_SORT_ALPHA + "|"
706                                                  + surfacing.DOMAIN_SIMILARITY_SORT_MAX + "|"
707                                                  + surfacing.DOMAIN_SIMILARITY_SORT_MIN + "|"
708                                                  + surfacing.DOMAIN_SIMILARITY_SORT_MEAN + "|"
709                                                  + surfacing.DOMAIN_SIMILARITY_SORT_DIFF + "|"
710                                                  + surfacing.DOMAIN_SIMILARITY_SORT_ABS_COUNTS_DIFF + "|"
711                                                  + surfacing.DOMAIN_SIMILARITY_SORT_COUNTS_DIFF + "|"
712                                                  + surfacing.DOMAIN_SIMILARITY_SORT_SPECIES_COUNT + "|"
713                                                  + surfacing.DOMAIN_SIMILARITY_SORT_SD + ">\"" );
714             }
715             final String sort_str = cla.getOptionValue( surfacing.DOMAIN_SIMILARITY_SORT_OPTION ).toLowerCase();
716             if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_ALPHA ) ) {
717                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
718                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
719             }
720             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_MAX ) ) {
721                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.MAX;
722                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
723             }
724             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_MIN ) ) {
725                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.MIN;
726                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
727             }
728             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_MEAN ) ) {
729                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.MEAN;
730                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.MEAN;
731             }
732             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_SPECIES_COUNT ) ) {
733                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.SPECIES_COUNT;
734                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
735             }
736             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_SD ) ) {
737                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.SD;
738                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.DOMAIN_ID;
739             }
740             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_DIFF ) ) {
741                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.MAX_DIFFERENCE;
742                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.MAX_DIFFERENCE;
743             }
744             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_ABS_COUNTS_DIFF ) ) {
745                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE;
746                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE;
747             }
748             else if ( sort_str.equals( surfacing.DOMAIN_SIMILARITY_SORT_COUNTS_DIFF ) ) {
749                 domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE;
750                 domain_similarity_sort_field_for_automated_pwc = DomainSimilarity.DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE;
751             }
752             else {
753                 ForesterUtil.fatalError( surfacing.PRG_NAME,
754                                          "unknown value \"" + sort_str
755                                                  + "\" for domain combinations similarities sorting: \"-"
756                                                  + surfacing.DOMAIN_SIMILARITY_SORT_OPTION + "=<"
757                                                  + surfacing.DOMAIN_SIMILARITY_SORT_ALPHA + "|"
758                                                  + surfacing.DOMAIN_SIMILARITY_SORT_MAX + "|"
759                                                  + surfacing.DOMAIN_SIMILARITY_SORT_MIN + "|"
760                                                  + surfacing.DOMAIN_SIMILARITY_SORT_MEAN + "|"
761                                                  + surfacing.DOMAIN_SIMILARITY_SORT_DIFF + "|"
762                                                  + surfacing.DOMAIN_SIMILARITY_SORT_ABS_COUNTS_DIFF + "|"
763                                                  + surfacing.DOMAIN_SIMILARITY_SORT_COUNTS_DIFF + "|" + "|"
764                                                  + surfacing.DOMAIN_SIMILARITY_SORT_SPECIES_COUNT + "|"
765                                                  + surfacing.DOMAIN_SIMILARITY_SORT_SD + ">\"" );
766             }
767         }
768         DomainSimilarity.PRINT_OPTION domain_similarity_print_option = DOMAIN_SIMILARITY_PRINT_OPTION_DEFAULT;
769         if ( cla.isOptionSet( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION ) ) {
770             if ( !cla.isOptionValueSet( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION ) ) {
771                 ForesterUtil
772                         .fatalError( surfacing.PRG_NAME,
773                                      "no value for print option: -"
774                                              + surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_DETAILED_HTML + "|"
775                                              + surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_HTML + "|"
776                                              + surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_TAB_DELIMITED + ">\"" );
777             }
778             final String sort = cla.getOptionValue( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION ).toLowerCase();
779             if ( sort.equals( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_DETAILED_HTML ) ) {
780                 domain_similarity_print_option = DomainSimilarity.PRINT_OPTION.HTML;
781             }
782             else if ( sort.equals( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_HTML ) ) {
783                 ForesterUtil.fatalError( surfacing.PRG_NAME, "simple HTML output not implemented yet :(" );
784             }
785             else if ( sort.equals( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_TAB_DELIMITED ) ) {
786                 domain_similarity_print_option = DomainSimilarity.PRINT_OPTION.SIMPLE_TAB_DELIMITED;
787             }
788             else {
789                 ForesterUtil
790                         .fatalError( surfacing.PRG_NAME,
791                                      "unknown value \"" + sort + "\" for print option: -"
792                                              + surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_DETAILED_HTML + "|"
793                                              + surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_HTML + "|"
794                                              + surfacing.DOMAIN_SIMILARITY_PRINT_OPTION_SIMPLE_TAB_DELIMITED + ">\"" );
795             }
796         }
797         GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder dc_sort_order = DOMAINS_SORT_ORDER_DEFAULT;
798         if ( cla.isOptionSet( surfacing.DOMAIN_COUNT_SORT_OPTION ) ) {
799             if ( !cla.isOptionValueSet( surfacing.DOMAIN_COUNT_SORT_OPTION ) ) {
800                 ForesterUtil.fatalError( surfacing.PRG_NAME,
801                                          "no value for sorting of domain counts: -" + surfacing.DOMAIN_COUNT_SORT_OPTION
802                                                  + "=<" + surfacing.DOMAIN_COUNT_SORT_ALPHA + "|"
803                                                  + surfacing.DOMAIN_COUNT_SORT_KEY_DOMAIN_COUNT + "|"
804                                                  + surfacing.DOMAIN_COUNT_SORT_KEY_DOMAIN_PROTEINS_COUNT + "|"
805                                                  + surfacing.DOMAIN_COUNT_SORT_COMBINATIONS_COUNT + ">\"" );
806             }
807             final String sort = cla.getOptionValue( surfacing.DOMAIN_COUNT_SORT_OPTION ).toLowerCase();
808             if ( sort.equals( surfacing.DOMAIN_COUNT_SORT_ALPHA ) ) {
809                 dc_sort_order = GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder.ALPHABETICAL_KEY_ID;
810             }
811             else if ( sort.equals( surfacing.DOMAIN_COUNT_SORT_KEY_DOMAIN_COUNT ) ) {
812                 dc_sort_order = GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder.KEY_DOMAIN_COUNT;
813             }
814             else if ( sort.equals( surfacing.DOMAIN_COUNT_SORT_KEY_DOMAIN_PROTEINS_COUNT ) ) {
815                 dc_sort_order = GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder.KEY_DOMAIN_PROTEINS_COUNT;
816             }
817             else if ( sort.equals( surfacing.DOMAIN_COUNT_SORT_COMBINATIONS_COUNT ) ) {
818                 dc_sort_order = GenomeWideCombinableDomains.GenomeWideCombinableDomainsSortOrder.COMBINATIONS_COUNT;
819             }
820             else {
821                 ForesterUtil.fatalError( surfacing.PRG_NAME,
822                                          "unknown value \"" + sort + "\" for sorting of domain counts: \"-"
823                                                  + surfacing.DOMAIN_COUNT_SORT_OPTION + "=<"
824                                                  + surfacing.DOMAIN_COUNT_SORT_ALPHA + "|"
825                                                  + surfacing.DOMAIN_COUNT_SORT_KEY_DOMAIN_COUNT + "|"
826                                                  + surfacing.DOMAIN_COUNT_SORT_KEY_DOMAIN_PROTEINS_COUNT + "|"
827                                                  + surfacing.DOMAIN_COUNT_SORT_COMBINATIONS_COUNT + ">\"" );
828             }
829         }
830         final String[][] input_file_properties = SurfacingUtil.processInputGenomesFile( input_genomes_file );
831         final int number_of_genomes = input_file_properties.length;
832         if ( number_of_genomes < 2 ) {
833             ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot analyze less than two files" );
834         }
835         if ( ( number_of_genomes < 3 ) && perform_pwc ) {
836             ForesterUtil
837                     .fatalError( surfacing.PRG_NAME,
838                                  "cannot use : -" + surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION
839                                          + "=<suffix> to turn on pairwise analyses with less than three input files" );
840         }
841         SurfacingUtil.checkWriteabilityForPairwiseComparisons( domain_similarity_print_option,
842                                                                input_file_properties,
843                                                                automated_pairwise_comparison_suffix,
844                                                                out_dir );
845         for( int i = 0; i < number_of_genomes; i++ ) {
846             File dcc_outfile = new File( input_file_properties[ i ][ 1 ]
847                     + surfacing.DOMAIN_COMBINITON_COUNTS_OUTPUTFILE_SUFFIX );
848             if ( out_dir != null ) {
849                 dcc_outfile = new File( out_dir + ForesterUtil.FILE_SEPARATOR + dcc_outfile );
850             }
851             SurfacingUtil.checkForOutputFileWriteability( dcc_outfile );
852         }
853         File pfam_to_go_file = new File( "pfam2go.txt" );
854         if ( cla.isOptionSet( surfacing.PFAM_TO_GO_FILE_USE_OPTION ) ) {
855             if ( !cla.isOptionValueSet( surfacing.PFAM_TO_GO_FILE_USE_OPTION ) ) {
856                 ForesterUtil.fatalError( surfacing.PRG_NAME,
857                                          "no value for Pfam to GO mapping file: -"
858                                                  + surfacing.PFAM_TO_GO_FILE_USE_OPTION + "=<file>" );
859             }
860             pfam_to_go_file = new File( cla.getOptionValue( surfacing.PFAM_TO_GO_FILE_USE_OPTION ) );
861         }
862         final String error1 = ForesterUtil.isReadableFile( pfam_to_go_file );
863         if ( !ForesterUtil.isEmpty( error1 ) ) {
864             ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot read Pfam to GO mapping file: " + error1 );
865         }
866         Map<String, List<GoId>> domain_id_to_go_ids_map = null;
867         int domain_id_to_go_ids_count = 0;
868         try {
869             final PfamToGoParser parser = new PfamToGoParser( pfam_to_go_file );
870             final List<PfamToGoMapping> pfam_to_go_mappings = parser.parse();
871             domain_id_to_go_ids_map = SurfacingUtil.createDomainIdToGoIdMap( pfam_to_go_mappings );
872             if ( parser.getMappingCount() < domain_id_to_go_ids_map.size() ) {
873                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME,
874                                                    "parser.getMappingCount() < domain_id_to_go_ids_map.size()" );
875             }
876             domain_id_to_go_ids_count = parser.getMappingCount();
877         }
878         catch ( final IOException e ) {
879             ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot read from Pfam to GO mapping file: " + e );
880         }
881         File go_obo_file = new File( "go.obo" );
882         if ( cla.isOptionSet( surfacing.GO_OBO_FILE_USE_OPTION ) ) {
883             if ( !cla.isOptionValueSet( surfacing.GO_OBO_FILE_USE_OPTION ) ) {
884                 ForesterUtil.fatalError( surfacing.PRG_NAME,
885                                          "no value for GO OBO file: -" + surfacing.GO_OBO_FILE_USE_OPTION + "=<file>" );
886             }
887             go_obo_file = new File( cla.getOptionValue( surfacing.GO_OBO_FILE_USE_OPTION ) );
888         }
889         final String error2 = ForesterUtil.isReadableFile( go_obo_file );
890         if ( !ForesterUtil.isEmpty( error2 ) ) {
891             ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot read GO OBO file: " + error2 );
892         }
893         List<GoTerm> go_terms = null;
894         try {
895             final OBOparser parser = new OBOparser( go_obo_file, OBOparser.ReturnType.BASIC_GO_TERM );
896             go_terms = parser.parse();
897             if ( parser.getGoTermCount() != go_terms.size() ) {
898                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "parser.getGoTermCount() != go_terms.size()" );
899             }
900         }
901         catch ( final IOException e ) {
902             ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot read from GO OBO file: " + e );
903         }
904         Map<GoId, GoTerm> go_id_to_term_map = null;
905         if ( ( ( domain_id_to_go_ids_map != null ) && ( domain_id_to_go_ids_map.size() > 0 ) )
906                 && ( ( go_terms != null ) && ( go_terms.size() > 0 ) ) ) {
907             go_id_to_term_map = GoUtils.createGoIdToGoTermMap( go_terms );
908         }
909         GoNameSpace go_namespace_limit = null;
910         if ( cla.isOptionSet( surfacing.GO_NAMESPACE_LIMIT_OPTION ) ) {
911             if ( ( go_id_to_term_map == null ) || go_id_to_term_map.isEmpty() ) {
912                 ForesterUtil.fatalError( surfacing.PRG_NAME,
913                                          "cannot use GO namespace limit (-" + surfacing.GO_NAMESPACE_LIMIT_OPTION
914                                                  + "=<namespace>) without Pfam to GO mapping file ("
915                                                  + surfacing.PFAM_TO_GO_FILE_USE_OPTION + "=<file>) and GO OBO file (-"
916                                                  + surfacing.GO_OBO_FILE_USE_OPTION + "=<file>)" );
917             }
918             if ( !cla.isOptionValueSet( surfacing.GO_NAMESPACE_LIMIT_OPTION ) ) {
919                 ForesterUtil.fatalError( surfacing.PRG_NAME,
920                                          "no value for GO namespace limit: \"-" + surfacing.GO_NAMESPACE_LIMIT_OPTION
921                                                  + "=<" + surfacing.GO_NAMESPACE_LIMIT_OPTION_MOLECULAR_FUNCTION + "|"
922                                                  + surfacing.GO_NAMESPACE_LIMIT_OPTION_BIOLOGICAL_PROCESS + "|"
923                                                  + surfacing.GO_NAMESPACE_LIMIT_OPTION_CELLULAR_COMPONENT + ">\"" );
924             }
925             final String go_namespace_limit_str = cla.getOptionValue( surfacing.GO_NAMESPACE_LIMIT_OPTION )
926                     .toLowerCase();
927             if ( go_namespace_limit_str.equals( surfacing.GO_NAMESPACE_LIMIT_OPTION_MOLECULAR_FUNCTION ) ) {
928                 go_namespace_limit = GoNameSpace.createMolecularFunction();
929             }
930             else if ( go_namespace_limit_str.equals( surfacing.GO_NAMESPACE_LIMIT_OPTION_BIOLOGICAL_PROCESS ) ) {
931                 go_namespace_limit = GoNameSpace.createBiologicalProcess();
932             }
933             else if ( go_namespace_limit_str.equals( surfacing.GO_NAMESPACE_LIMIT_OPTION_CELLULAR_COMPONENT ) ) {
934                 go_namespace_limit = GoNameSpace.createCellularComponent();
935             }
936             else {
937                 ForesterUtil.fatalError( surfacing.PRG_NAME,
938                                          "unknown value \"" + go_namespace_limit_str + "\" for GO namespace limit: \"-"
939                                                  + surfacing.GO_NAMESPACE_LIMIT_OPTION + "=<"
940                                                  + surfacing.GO_NAMESPACE_LIMIT_OPTION_MOLECULAR_FUNCTION + "|"
941                                                  + surfacing.GO_NAMESPACE_LIMIT_OPTION_BIOLOGICAL_PROCESS + "|"
942                                                  + surfacing.GO_NAMESPACE_LIMIT_OPTION_CELLULAR_COMPONENT + ">\"" );
943             }
944         }
945         if ( ( domain_similarity_sort_field == DomainSimilarity.DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE )
946                 && ( number_of_genomes > 2 ) ) {
947             domain_similarity_sort_field = DomainSimilarity.DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE;
948         }
949         File[] intree_files = null;
950         Phylogeny[] intrees = null;
951         if ( cla.isOptionSet( surfacing.INPUT_SPECIES_TREE_OPTION ) ) {
952             if ( number_of_genomes < 3 ) {
953                 ForesterUtil.fatalError( surfacing.PRG_NAME,
954                                          "cannot infer gains and losses on input species trees (-"
955                                                  + surfacing.INPUT_SPECIES_TREE_OPTION + " without pairwise analyses ("
956                                                  + surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION
957                                                  + "=<suffix for pairwise comparison output files>)" );
958             }
959             if ( !cla.isOptionValueSet( surfacing.INPUT_SPECIES_TREE_OPTION ) ) {
960                 ForesterUtil.fatalError( surfacing.PRG_NAME,
961                                          "no value for input tree: -" + surfacing.INPUT_SPECIES_TREE_OPTION
962                                                  + "=<tree file in phyloXML format>" );
963             }
964             final String intrees_str = cla.getOptionValue( surfacing.INPUT_SPECIES_TREE_OPTION );
965             if ( intrees_str.indexOf( "#" ) > 0 ) {
966                 final String[] intrees_strs = intrees_str.split( "#" );
967                 intree_files = new File[ intrees_strs.length ];
968                 int i = 0;
969                 for( final String s : intrees_strs ) {
970                     intree_files[ i++ ] = new File( s.trim() );
971                 }
972             }
973             else {
974                 intree_files = new File[ 1 ];
975                 intree_files[ 0 ] = new File( intrees_str );
976             }
977             intrees = SurfacingUtil.obtainAndPreProcessIntrees( intree_files,
978                                                                 number_of_genomes,
979                                                                 input_file_properties );
980         }
981         final Phylogeny intree_0_orig = SurfacingUtil.obtainFirstIntree( intree_files[ 0 ] );
982         long random_number_seed_for_fitch_parsimony = 0l;
983         boolean radomize_fitch_parsimony = false;
984         if ( cla.isOptionSet( surfacing.RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION ) ) {
985             if ( !cla.isOptionValueSet( surfacing.RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION ) ) {
986                 ForesterUtil.fatalError( surfacing.PRG_NAME,
987                                          "no value for random number seed: -"
988                                                  + surfacing.RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION + "=<seed>" );
989             }
990             try {
991                 random_number_seed_for_fitch_parsimony = cla
992                         .getOptionValueAsLong( RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION );
993             }
994             catch ( final IOException e ) {
995                 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
996             }
997             radomize_fitch_parsimony = true;
998         }
999         SortedSet<String> filter = null;
1000         if ( ( positive_filter_file != null ) || ( negative_filter_file != null )
1001                 || ( negative_domains_filter_file != null ) ) {
1002             filter = new TreeSet<String>();
1003             if ( positive_filter_file != null ) {
1004                 SurfacingUtil.processFilter( positive_filter_file, filter );
1005             }
1006             else if ( negative_filter_file != null ) {
1007                 SurfacingUtil.processFilter( negative_filter_file, filter );
1008             }
1009             else if ( negative_domains_filter_file != null ) {
1010                 SurfacingUtil.processFilter( negative_domains_filter_file, filter );
1011             }
1012         }
1013         Map<String, Set<String>>[] domain_id_to_secondary_features_maps = null;
1014         File[] secondary_features_map_files = null;
1015         final File domain_lengths_analysis_outfile = new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file
1016                 + DOMAIN_LENGTHS_ANALYSIS_SUFFIX );
1017         if ( domain_length_analysis ) {
1018             SurfacingUtil.checkForOutputFileWriteability( domain_lengths_analysis_outfile );
1019         }
1020         if ( cla.isOptionSet( surfacing.SECONDARY_FEATURES_PARSIMONY_MAP_FILE ) ) {
1021             if ( !cla.isOptionValueSet( surfacing.SECONDARY_FEATURES_PARSIMONY_MAP_FILE ) ) {
1022                 ForesterUtil.fatalError( surfacing.PRG_NAME,
1023                                          "no value for secondary features map file: -"
1024                                                  + surfacing.SECONDARY_FEATURES_PARSIMONY_MAP_FILE + "=<file>" );
1025             }
1026             final String[] secondary_features_map_files_strs = cla
1027                     .getOptionValue( surfacing.SECONDARY_FEATURES_PARSIMONY_MAP_FILE ).split( "#" );
1028             secondary_features_map_files = new File[ secondary_features_map_files_strs.length ];
1029             domain_id_to_secondary_features_maps = new Map[ secondary_features_map_files_strs.length ];
1030             int i = 0;
1031             for( final String secondary_features_map_files_str : secondary_features_map_files_strs ) {
1032                 secondary_features_map_files[ i ] = new File( secondary_features_map_files_str );
1033                 final String error = ForesterUtil.isReadableFile( secondary_features_map_files[ i ] );
1034                 if ( !ForesterUtil.isEmpty( error ) ) {
1035                     ForesterUtil.fatalError( surfacing.PRG_NAME, "cannot read secondary features map file: " + error );
1036                 }
1037                 try {
1038                     domain_id_to_secondary_features_maps[ i ] = SurfacingUtil
1039                             .createDomainIdToSecondaryFeaturesMap( secondary_features_map_files[ i ] );
1040                 }
1041                 catch ( final IOException e ) {
1042                     ForesterUtil.fatalError( surfacing.PRG_NAME,
1043                                              "cannot read secondary features map file: " + e.getMessage() );
1044                 }
1045                 catch ( final Exception e ) {
1046                     ForesterUtil.fatalError( surfacing.PRG_NAME,
1047                                              "problem with contents of features map file ["
1048                                                      + secondary_features_map_files[ i ] + "]: " + e.getMessage() );
1049                 }
1050                 i++;
1051             }
1052         }
1053         if ( out_dir == null ) {
1054             ForesterUtil.fatalError( surfacing.PRG_NAME,
1055                                      "no output directory indicated (-" + surfacing.OUTPUT_DIR_OPTION + "=<dir>)" );
1056         }
1057         if ( output_file == null ) {
1058             ForesterUtil.fatalError( surfacing.PRG_NAME,
1059                                      "no name for (main) output file indicated (-" + surfacing.OUTPUT_FILE_OPTION
1060                                              + "=<file>)" );
1061         }
1062         if ( ( domain_id_to_go_ids_map == null ) || domain_id_to_go_ids_map.isEmpty() ) {
1063             ForesterUtil.fatalError( surfacing.PRG_NAME,
1064                                      "no (acceptable) Pfam to GO id mapping file provided ('pfam2go file') (-"
1065                                              + surfacing.PFAM_TO_GO_FILE_USE_OPTION + "=<file>)" );
1066         }
1067         if ( ( go_id_to_term_map == null ) || go_id_to_term_map.isEmpty() ) {
1068             ForesterUtil.fatalError( surfacing.PRG_NAME,
1069                                      "no (acceptable) go id to term mapping file provided ('GO OBO file') (-"
1070                                              + surfacing.GO_OBO_FILE_USE_OPTION + "=<file>)" );
1071         }
1072         System.out.println( "Output directory            : " + out_dir );
1073         System.out.println( "Input genomes from          : " + input_genomes_file );
1074         html_desc.append( "<tr><td>Input genomes from:</td><td>" + input_genomes_file + "</td></tr>" + nl );
1075         if ( positive_filter_file != null ) {
1076             final int filter_size = filter.size();
1077             System.out.println( "Positive protein filter     : " + positive_filter_file + " [" + filter_size
1078                     + " domain ids]" );
1079             html_desc.append( "<tr><td>Positive protein filter:</td><td>" + positive_filter_file + " [" + filter_size
1080                     + " domain ids]</td></tr>" + nl );
1081         }
1082         if ( negative_filter_file != null ) {
1083             final int filter_size = filter.size();
1084             System.out.println( "Negative protein filter     : " + negative_filter_file + " [" + filter_size
1085                     + " domain ids]" );
1086             html_desc.append( "<tr><td>Negative protein filter:</td><td>" + negative_filter_file + " [" + filter_size
1087                     + " domain ids]</td></tr>" + nl );
1088         }
1089         if ( negative_domains_filter_file != null ) {
1090             final int filter_size = filter.size();
1091             System.out.println( "Negative domain filter      : " + negative_domains_filter_file + " [" + filter_size
1092                     + " domain ids]" );
1093             html_desc.append( "<tr><td>Negative domain filter:</td><td>" + negative_domains_filter_file + " ["
1094                     + filter_size + " domain ids]</td></tr>" + nl );
1095         }
1096         if ( plus_minus_analysis_high_copy_base_species.size() > 0 ) {
1097             String plus0 = "";
1098             for( final String s : plus_minus_analysis_high_copy_base_species ) {
1099                 plus0 += "+" + s + " ";
1100             }
1101             String plus1 = "";
1102             for( final String s : plus_minus_analysis_high_copy_target_species ) {
1103                 plus1 += "*" + s + " ";
1104             }
1105             String minus = "";
1106             for( final String s : plus_minus_analysis_high_low_copy_species ) {
1107                 minus += "-" + s + " ";
1108             }
1109             System.out.println( "Plus-minus analysis         : " + plus1 + "&& " + plus0 + "&& " + minus );
1110             html_desc.append( "<tr><td>Plus-minus analysis:</td><td>" + plus1 + "&& " + plus0 + "&& " + minus
1111                     + "</td></tr>" + nl );
1112         }
1113         if ( cutoff_scores_file != null ) {
1114             System.out.println( "Cutoff scores file          : " + cutoff_scores_file );
1115             html_desc.append( "<tr><td>Cutoff scores file:</td><td>" + cutoff_scores_file + "</td></tr>" + nl );
1116         }
1117         if ( ie_value_max >= 0.0 ) {
1118             System.out.println( "iE-value maximum (incl)     : " + ie_value_max );
1119             html_desc.append( "<tr><td>iE-value maximum (inclusive):</td><td>" + ie_value_max + "</td></tr>" + nl );
1120         }
1121         if ( rel_env_length_ratio_cutoff > 0.0 ) {
1122             System.out.println( "Rel env length ratio min    : " + rel_env_length_ratio_cutoff );
1123             html_desc.append( "<tr><td>Relative hmm envelope length ratio min (inclusive):</td><td>"
1124                     + rel_env_length_ratio_cutoff + "</td></tr>" + nl );
1125         }
1126         if ( fs_e_value_max >= 0.0 ) {
1127             System.out.println( "FS E-value maximum (incl)   : " + fs_e_value_max );
1128             html_desc.append( "<tr><td>FS E-value maximum (inclusive):</td><td>" + fs_e_value_max + "</td></tr>" + nl );
1129         }
1130         if ( output_protein_lists_for_all_domains ) {
1131             System.out.println( "Domain E-value max          : " + output_list_of_all_proteins_per_domain_e_value_max );
1132             html_desc.append( "<tr><td>Protein lists: E-value maximum per domain (inclusive):</td><td>"
1133                     + output_list_of_all_proteins_per_domain_e_value_max + "</td></tr>" + nl );
1134         }
1135         System.out.println( "Ignore DUFs                 : " + ignore_dufs );
1136         if ( ignore_virus_like_ids ) {
1137             System.out.println( "Ignore virus like ids       : " + ignore_virus_like_ids );
1138             html_desc.append( "<tr><td>Ignore virus, phage, transposition related ids:</td><td>" + ignore_virus_like_ids
1139                     + "</td></tr>" + nl );
1140         }
1141         html_desc.append( "<tr><td>Ignore DUFs:</td><td>" + ignore_dufs + "</td></tr>" + nl );
1142         if ( max_allowed_overlap != surfacing.MAX_ALLOWED_OVERLAP_DEFAULT ) {
1143             System.out.println( "Max allowed domain overlap  : " + max_allowed_overlap );
1144             html_desc
1145                     .append( "<tr><td>Max allowed domain overlap:</td><td>" + max_allowed_overlap + "</td></tr>" + nl );
1146         }
1147         if ( no_engulfing_overlaps ) {
1148             System.out.println( "Ignore engulfed domains     : " + no_engulfing_overlaps );
1149             html_desc.append( "<tr><td>Ignore (lower confidence) engulfed domains:</td><td>" + no_engulfing_overlaps
1150                     + "</td></tr>" + nl );
1151         }
1152         System.out.println( "Ignore singlet domains      : " + ignore_domains_without_combs_in_all_spec );
1153         html_desc
1154                 .append( "<tr><td>Ignore singlet domains for domain combination similarity analyses (not for parsimony analyses):</td><td>"
1155                         + ignore_domains_without_combs_in_all_spec + "</td></tr>" + nl );
1156         System.out.println( "Ignore species specific doms: " + ignore_species_specific_domains );
1157         html_desc
1158                 .append( "<tr><td>Ignore species specific domains for domain combination similarity analyses (not for parsimony analyses):</td><td>"
1159                         + ignore_species_specific_domains + "</td></tr>" + nl );
1160         System.out.println( "Ignore combination with self: " + ignore_combination_with_same );
1161         html_desc.append( "<tr><td>Ignore combination with self for domain combination similarity analyses:</td><td>"
1162                 + ignore_combination_with_same + "</td></tr>" + nl );
1163         System.out.println( "Consider directedness       : "
1164                 + ( dc_type != BinaryDomainCombination.DomainCombinationType.BASIC ) );
1165         html_desc.append( "<tr><td>Consider directedness of binary domain combinations:</td><td>"
1166                 + ( dc_type != BinaryDomainCombination.DomainCombinationType.BASIC ) + "</td></tr>" + nl );
1167         if ( dc_type != BinaryDomainCombination.DomainCombinationType.BASIC ) {
1168             System.out.println( "Consider adjacency          : "
1169                     + ( dc_type == BinaryDomainCombination.DomainCombinationType.DIRECTED_ADJACTANT ) );
1170             html_desc.append( "<tr><td>Consider djacency of binary domain combinations:</td><td>"
1171                     + ( dc_type == BinaryDomainCombination.DomainCombinationType.DIRECTED_ADJACTANT ) + "</td></tr>"
1172                     + nl );
1173         }
1174         System.out.println( "Fitch parsimony of DCs      : " + perform_dc_fich );
1175         html_desc.append( "<tr><td>Fitch parsimony of DCs:</td><td>" + perform_dc_fich + "</td></tr>" + nl );
1176         if ( perform_dc_fich ) {
1177             System.out.println( "Use last in Fitch parsimony : " + use_last_in_fitch_parsimony );
1178             html_desc.append( "<tr><td>Use last in Fitch parsimony:</td><td>" + use_last_in_fitch_parsimony
1179                     + "</td></tr>" + nl );
1180         }
1181         System.out.println( "Write to Nexus files        : " + write_to_nexus );
1182         html_desc.append( "<tr><td>Write to Nexus files:</td><td>" + write_to_nexus + "</td></tr>" + nl );
1183         if ( perform_dc_fich ) {
1184             System.out.println( "DC regain prot stats        : " + perform_dc_regain_proteins_stats );
1185             html_desc.append( "<tr><td>DC regain prot stats:</td><td>" + perform_dc_regain_proteins_stats + "</td></tr>"
1186                     + nl );
1187         }
1188         System.out.println( "DA analysis                 : " + da_analysis );
1189         html_desc.append( "<tr><td>DA analysis :</td><td>" + da_analysis + "</td></tr>" + nl );
1190         System.out.print( "Domain counts sort order    : " );
1191         html_desc.append( "<tr><td>Domain counts sort order:</td><td>" );
1192         switch ( dc_sort_order ) {
1193             case ALPHABETICAL_KEY_ID:
1194                 System.out.println( "alphabetical" );
1195                 html_desc.append( "alphabetical" + "</td></tr>" + nl );
1196                 break;
1197             case KEY_DOMAIN_COUNT:
1198                 System.out.println( "domain count" );
1199                 html_desc.append( "domain count" + "</td></tr>" + nl );
1200                 break;
1201             case KEY_DOMAIN_PROTEINS_COUNT:
1202                 System.out.println( "domain proteins count" );
1203                 html_desc.append( "domain proteins count" + "</td></tr>" + nl );
1204                 break;
1205             case COMBINATIONS_COUNT:
1206                 System.out.println( "domain combinations count" );
1207                 html_desc.append( "domain combinations count" + "</td></tr>" + nl );
1208                 break;
1209             default:
1210                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "unknown value for dc sort order" );
1211         }
1212         if ( domain_id_to_go_ids_map != null ) {
1213             System.out.println( "Pfam to GO mappings from    : " + pfam_to_go_file + " [" + domain_id_to_go_ids_count
1214                     + " mappings]" );
1215             html_desc.append( "<tr><td>Pfam to GO mappings from:</td><td>" + pfam_to_go_file + " ["
1216                     + domain_id_to_go_ids_count + " mappings]" + "</td></tr>" + nl );
1217         }
1218         if ( go_terms != null ) {
1219             System.out.println( "GO terms from               : " + go_obo_file + " [" + go_terms.size() + " terms]" );
1220             html_desc.append( "<tr><td>GO terms from:</td><td>" + go_obo_file + " [" + go_terms.size() + " terms]"
1221                     + "</td></tr>" + nl );
1222         }
1223         if ( go_namespace_limit != null ) {
1224             System.out.println( "Limit GO terms to           : " + go_namespace_limit.toString() );
1225             html_desc.append( "<tr><td>Limit GO terms to</td><td>" + go_namespace_limit + "</td></tr>" + nl );
1226         }
1227         if ( perform_pwc ) {
1228             System.out.println( "Suffix for PWC files        : " + automated_pairwise_comparison_suffix );
1229             html_desc.append( "<tr><td>Suffix for PWC files</td><td>" + automated_pairwise_comparison_suffix
1230                     + "</td></tr>" + nl );
1231         }
1232         if ( out_dir != null ) {
1233             System.out.println( "Output directory            : " + out_dir );
1234         }
1235         if ( query_domain_ids != null ) {
1236             System.out.println( "Query domains (ordered)     : " + query_domain_ids );
1237             html_desc.append( "<tr><td></td><td>" + query_domain_ids + "</td></tr>" + nl );
1238         }
1239         System.out.println( "Write similarities to       : " + output_file );
1240         System.out.print( "  Scoring method            : " );
1241         html_desc.append( "<tr><td>Scoring method:</td><td>" );
1242         switch ( scoring ) {
1243             case COMBINATIONS:
1244                 System.out.println( "domain combinations based" );
1245                 html_desc.append( "domain combinations based" + "</td></tr>" + nl );
1246                 break;
1247             case DOMAINS:
1248                 System.out.println( "domain counts based" );
1249                 html_desc.append( "domain counts based" + "</td></tr>" + nl );
1250                 break;
1251             case PROTEINS:
1252                 System.out.println( "domain proteins counts based" );
1253                 html_desc.append( "domain proteins counts based" + "</td></tr>" + nl );
1254                 break;
1255             default:
1256                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "unknown value for sorting for scoring" );
1257         }
1258         System.out.print( "  Sort by                   : " );
1259         html_desc.append( "<tr><td>Sort by:</td><td>" );
1260         switch ( domain_similarity_sort_field ) {
1261             case MIN:
1262                 System.out.print( "score minimum" );
1263                 html_desc.append( "score minimum" );
1264                 break;
1265             case MAX:
1266                 System.out.print( "score maximum" );
1267                 html_desc.append( "score maximum" );
1268                 break;
1269             case MEAN:
1270                 System.out.print( "score mean" );
1271                 html_desc.append( "score mean" );
1272                 break;
1273             case SD:
1274                 System.out.print( "score standard deviation" );
1275                 html_desc.append( "score standard deviation" );
1276                 break;
1277             case SPECIES_COUNT:
1278                 System.out.print( "species number" );
1279                 html_desc.append( "species number" );
1280                 break;
1281             case DOMAIN_ID:
1282                 System.out.print( "alphabetical domain identifier" );
1283                 html_desc.append( "alphabetical domain identifier" );
1284                 break;
1285             case MAX_DIFFERENCE:
1286                 System.out.print( "(maximal) difference" );
1287                 html_desc.append( "(maximal) difference" );
1288                 break;
1289             case ABS_MAX_COUNTS_DIFFERENCE:
1290                 System.out.print( "absolute (maximal) counts difference" );
1291                 html_desc.append( "absolute (maximal) counts difference" );
1292                 break;
1293             case MAX_COUNTS_DIFFERENCE:
1294                 System.out.print( "(maximal) counts difference" );
1295                 html_desc.append( "(maximal) counts  difference" );
1296                 break;
1297             default:
1298                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "unknown value for sorting for similarities" );
1299         }
1300         if ( sort_by_species_count_first ) {
1301             System.out.println( " (sort by species count first)" );
1302             html_desc.append( " (sort by species count first)" );
1303         }
1304         else {
1305             System.out.println();
1306         }
1307         html_desc.append( "</td></tr>" + nl );
1308         System.out.print( "  Detailedness              : " );
1309         switch ( detailedness ) {
1310             case BASIC:
1311                 System.out.println( "basic" );
1312                 break;
1313             case LIST_COMBINING_DOMAIN_FOR_EACH_SPECIES:
1314                 System.out.println( "list combining domains for each species" );
1315                 break;
1316             case PUNCTILIOUS:
1317                 System.out.println( "punctilious" );
1318                 break;
1319             default:
1320                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "unknown value for sorting for detailedness" );
1321         }
1322         System.out.print( "  Print option              : " );
1323         switch ( domain_similarity_print_option ) {
1324             case HTML:
1325                 System.out.println( "HTML" );
1326                 break;
1327             case SIMPLE_TAB_DELIMITED:
1328                 System.out.println( "simple tab delimited" );
1329                 break;
1330             default:
1331                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "unknown value for print option" );
1332         }
1333         System.out.print( "  Species matrix            : " + species_matrix );
1334         System.out.println();
1335         final File dc_data_file = new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file + DATA_FILE_SUFFIX );
1336         System.out.println( "Domain comb data output     : " + dc_data_file );
1337         html_desc.append( "<tr><td>Domain combination data output:</td><td> " + dc_data_file + " </td></tr>" );
1338         System.out.println();
1339         if ( perform_pwc ) {
1340             System.out.println( "Pairwise comparisons: " );
1341             html_desc.append( "<tr><td>Pairwise comparisons:</td><td></td></tr>" );
1342             System.out.print( "  Sort by                   : " );
1343             html_desc.append( "<tr><td>Sort by:</td><td>" );
1344             switch ( domain_similarity_sort_field_for_automated_pwc ) {
1345                 case MEAN:
1346                     System.out.print( "score mean" );
1347                     html_desc.append( "score mean" );
1348                     break;
1349                 case DOMAIN_ID:
1350                     System.out.print( "alphabetical domain identifier" );
1351                     html_desc.append( "alphabetical domain identifier" );
1352                     break;
1353                 case MAX_DIFFERENCE:
1354                     System.out.print( "difference" );
1355                     html_desc.append( "difference" );
1356                     break;
1357                 case ABS_MAX_COUNTS_DIFFERENCE:
1358                     System.out.print( "absolute counts difference" );
1359                     html_desc.append( "absolute counts difference" );
1360                     break;
1361                 case MAX_COUNTS_DIFFERENCE:
1362                     System.out.print( "counts difference" );
1363                     html_desc.append( "counts difference" );
1364                     break;
1365                 default:
1366                     ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME,
1367                                                        "unknown value for sorting for similarities" );
1368             }
1369             System.out.println();
1370             html_desc.append( "</td></tr>" + nl );
1371             if ( ( intrees != null ) && ( intrees.length > 0 ) ) {
1372                 for( final File intree_file : intree_files ) {
1373                     html_desc.append( "<tr><td>Intree for gain/loss parsimony analysis:</td><td>" + intree_file
1374                             + "</td></tr>" + nl );
1375                     System.out.println( "  Intree for gain/loss pars.: " + intree_file );
1376                 }
1377             }
1378             if ( radomize_fitch_parsimony ) {
1379                 html_desc.append( "<tr><td>    Random number seed for Fitch parsimony analysis:</td><td>"
1380                         + random_number_seed_for_fitch_parsimony + "</td></tr>" + nl );
1381                 System.out.println( "    Random number seed      : " + random_number_seed_for_fitch_parsimony );
1382             }
1383             if ( ( domain_id_to_secondary_features_maps != null )
1384                     && ( domain_id_to_secondary_features_maps.length > 0 ) ) {
1385                 for( int i = 0; i < secondary_features_map_files.length; i++ ) {
1386                     html_desc.append( "<tr><td>Secondary features map file:</td><td>"
1387                             + secondary_features_map_files[ i ] + "</td></tr>" + nl );
1388                     System.out.println( "Secondary features map file : " + secondary_features_map_files[ i ]
1389                             + " [mappings for " + domain_id_to_secondary_features_maps[ i ].size() + " domain ids]" );
1390                     if ( VERBOSE ) {
1391                         System.out.println();
1392                         System.out.println( "Domain ids to secondary features map:" );
1393                         for( final String domain_id : domain_id_to_secondary_features_maps[ i ].keySet() ) {
1394                             System.out.print( domain_id );
1395                             System.out.print( " => " );
1396                             for( final String sec : domain_id_to_secondary_features_maps[ i ].get( domain_id ) ) {
1397                                 System.out.print( sec );
1398                                 System.out.print( " " );
1399                             }
1400                             System.out.println();
1401                         }
1402                     }
1403                 }
1404             }
1405         } // if ( perform_pwc ) {
1406         System.out.println();
1407         html_desc.append( "<tr><td>Command line:</td><td>" + nl + nl + cla.getCommandLineArgsAsString() + nl + nl
1408                 + "</td></tr>" + nl );
1409         System.out.println( "Command line                : " + cla.getCommandLineArgsAsString() );
1410         BufferedWriter[] query_domains_writer_ary = null;
1411         List<String>[] query_domain_ids_array = null;
1412         if ( query_domain_ids != null ) {
1413             final String[] query_domain_ids_str_array = query_domain_ids.split( "#" );
1414             query_domain_ids_array = new ArrayList[ query_domain_ids_str_array.length ];
1415             query_domains_writer_ary = new BufferedWriter[ query_domain_ids_str_array.length ];
1416             for( int i = 0; i < query_domain_ids_str_array.length; i++ ) {
1417                 String query_domain_ids_str = query_domain_ids_str_array[ i ];
1418                 final String[] query_domain_ids_str_ary = query_domain_ids_str.split( "~" );
1419                 final List<String> query = new ArrayList<String>();
1420                 for( final String element : query_domain_ids_str_ary ) {
1421                     query.add( element );
1422                 }
1423                 query_domain_ids_array[ i ] = query;
1424                 query_domain_ids_str = query_domain_ids_str.replace( '~', '_' );
1425                 String protein_names_writer_str = query_domain_ids_str + surfacing.SEQ_EXTRACT_SUFFIX;
1426                 if ( out_dir != null ) {
1427                     protein_names_writer_str = out_dir + ForesterUtil.FILE_SEPARATOR + protein_names_writer_str;
1428                 }
1429                 try {
1430                     query_domains_writer_ary[ i ] = new BufferedWriter( new FileWriter( protein_names_writer_str ) );
1431                 }
1432                 catch ( final IOException e ) {
1433                     ForesterUtil.fatalError( surfacing.PRG_NAME,
1434                                              "Could not open [" + protein_names_writer_str + "]: "
1435                                                      + e.getLocalizedMessage() );
1436                 }
1437             }
1438         }
1439         SortedMap<Species, List<Protein>> protein_lists_per_species = null; //This will only be created if needed.
1440         boolean need_protein_lists_per_species = false;
1441         if ( ( plus_minus_analysis_high_copy_base_species.size() > 0 ) || output_protein_lists_for_all_domains
1442                 || true ) { //TODO
1443             need_protein_lists_per_species = true;
1444         }
1445         if ( need_protein_lists_per_species ) {
1446             protein_lists_per_species = new TreeMap<Species, List<Protein>>();
1447         }
1448         List<GenomeWideCombinableDomains> gwcd_list = new ArrayList<GenomeWideCombinableDomains>( number_of_genomes );
1449         final SortedSet<String> all_domains_encountered = new TreeSet<String>();
1450         final SortedSet<BinaryDomainCombination> all_bin_domain_combinations_encountered = new TreeSet<BinaryDomainCombination>();
1451         List<BinaryDomainCombination> all_bin_domain_combinations_gained_fitch = null;
1452         List<BinaryDomainCombination> all_bin_domain_combinations_lost_fitch = null;
1453         if ( ( intrees != null ) && ( intrees.length == 1 ) ) {
1454             all_bin_domain_combinations_gained_fitch = new ArrayList<BinaryDomainCombination>();
1455             all_bin_domain_combinations_lost_fitch = new ArrayList<BinaryDomainCombination>();
1456         }
1457         final File per_genome_domain_promiscuity_statistics_file = new File( out_dir + ForesterUtil.FILE_SEPARATOR
1458                 + output_file + D_PROMISCUITY_FILE_SUFFIX );
1459         BufferedWriter per_genome_domain_promiscuity_statistics_writer = null;
1460         try {
1461             per_genome_domain_promiscuity_statistics_writer = new BufferedWriter( new FileWriter( per_genome_domain_promiscuity_statistics_file ) );
1462             per_genome_domain_promiscuity_statistics_writer.write( "Species:\t" );
1463             per_genome_domain_promiscuity_statistics_writer.write( "Mean:\t" );
1464             per_genome_domain_promiscuity_statistics_writer.write( "SD:\t" );
1465             per_genome_domain_promiscuity_statistics_writer.write( "Median:\t" );
1466             per_genome_domain_promiscuity_statistics_writer.write( "Min:\t" );
1467             per_genome_domain_promiscuity_statistics_writer.write( "Max:\t" );
1468             per_genome_domain_promiscuity_statistics_writer.write( "N:\t" );
1469             per_genome_domain_promiscuity_statistics_writer
1470                     .write( "Max Promiscuous Domains:" + ForesterUtil.LINE_SEPARATOR );
1471         }
1472         catch ( final IOException e2 ) {
1473             ForesterUtil.fatalError( surfacing.PRG_NAME, e2.getMessage() );
1474         }
1475         final File log_file = new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file + LOG_FILE_SUFFIX );
1476         BufferedWriter log_writer = null;
1477         try {
1478             log_writer = new BufferedWriter( new FileWriter( log_file ) );
1479         }
1480         catch ( final IOException e2 ) {
1481             ForesterUtil.fatalError( surfacing.PRG_NAME, e2.getMessage() );
1482         }
1483         BufferedWriter dc_data_writer = null;
1484         try {
1485             dc_data_writer = new BufferedWriter( new FileWriter( dc_data_file ) );
1486             dc_data_writer.write( DATA_FILE_DESC );
1487             dc_data_writer.write( ForesterUtil.LINE_SEPARATOR );
1488         }
1489         catch ( final IOException e2 ) {
1490             ForesterUtil.fatalError( surfacing.PRG_NAME, e2.getMessage() );
1491         }
1492         DescriptiveStatistics protein_coverage_stats = new BasicDescriptiveStatistics();
1493         DescriptiveStatistics all_genomes_domains_per_potein_stats = new BasicDescriptiveStatistics();
1494         final SortedMap<Integer, Integer> all_genomes_domains_per_potein_histo = new TreeMap<Integer, Integer>();
1495         final SortedSet<String> domains_which_are_always_single = new TreeSet<String>();
1496         final SortedSet<String> domains_which_are_sometimes_single_sometimes_not = new TreeSet<String>();
1497         final SortedSet<String> domains_which_never_single = new TreeSet<String>();
1498         BufferedWriter domains_per_potein_stats_writer = null;
1499         try {
1500             domains_per_potein_stats_writer = new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR
1501                     + output_file + "_domains_per_potein_stats.txt" ) );
1502             domains_per_potein_stats_writer.write( "Genome" );
1503             domains_per_potein_stats_writer.write( "\t" );
1504             domains_per_potein_stats_writer.write( "Mean" );
1505             domains_per_potein_stats_writer.write( "\t" );
1506             domains_per_potein_stats_writer.write( "SD" );
1507             domains_per_potein_stats_writer.write( "\t" );
1508             domains_per_potein_stats_writer.write( "Median" );
1509             domains_per_potein_stats_writer.write( "\t" );
1510             domains_per_potein_stats_writer.write( "N" );
1511             domains_per_potein_stats_writer.write( "\t" );
1512             domains_per_potein_stats_writer.write( "Min" );
1513             domains_per_potein_stats_writer.write( "\t" );
1514             domains_per_potein_stats_writer.write( "Max" );
1515             domains_per_potein_stats_writer.write( "\n" );
1516         }
1517         catch ( final IOException e3 ) {
1518             e3.printStackTrace();
1519         }
1520         Map<String, DescriptiveStatistics> protein_length_stats_by_dc = null;
1521         Map<String, DescriptiveStatistics> domain_number_stats_by_dc = null;
1522         final Map<String, DescriptiveStatistics> domain_length_stats_by_domain = new HashMap<String, DescriptiveStatistics>();
1523         if ( perform_dc_regain_proteins_stats ) {
1524             protein_length_stats_by_dc = new HashMap<String, DescriptiveStatistics>();
1525             domain_number_stats_by_dc = new HashMap<String, DescriptiveStatistics>();
1526         }
1527         DomainLengthsTable domain_lengths_table = null;
1528         if ( domain_length_analysis ) {
1529             domain_lengths_table = new DomainLengthsTable();
1530         }
1531         // Main loop:
1532         final SortedMap<String, Set<String>> distinct_domain_architecutures_per_genome = new TreeMap<String, Set<String>>();
1533         final SortedMap<String, Integer> distinct_domain_architecuture_counts = new TreeMap<String, Integer>();
1534         for( int i = 0; i < number_of_genomes; ++i ) {
1535             System.out.println();
1536             System.out.println( ( i + 1 ) + "/" + number_of_genomes );
1537             SurfacingUtil.log( ( i + 1 ) + "/" + number_of_genomes, log_writer );
1538             System.out.println( "Processing                                     : " + input_file_properties[ i ][ 1 ]
1539                     + " [" + input_file_properties[ i ][ 0 ] + "]" );
1540             SurfacingUtil.log( "Genome                                         : " + input_file_properties[ i ][ 1 ]
1541                     + " [" + input_file_properties[ i ][ 0 ] + "]", log_writer );
1542             HmmscanPerDomainTableParser parser = null;
1543             INDIVIDUAL_SCORE_CUTOFF ind_score_cutoff = INDIVIDUAL_SCORE_CUTOFF.NONE;
1544             if ( individual_score_cutoffs != null ) {
1545                 ind_score_cutoff = INDIVIDUAL_SCORE_CUTOFF_DEFAULT;
1546             }
1547             if ( ( positive_filter_file != null ) || ( negative_filter_file != null )
1548                     || ( negative_domains_filter_file != null ) ) {
1549                 HmmscanPerDomainTableParser.FilterType filter_type = HmmscanPerDomainTableParser.FilterType.NONE;
1550                 if ( positive_filter_file != null ) {
1551                     filter_type = HmmscanPerDomainTableParser.FilterType.POSITIVE_PROTEIN;
1552                 }
1553                 else if ( negative_filter_file != null ) {
1554                     filter_type = HmmscanPerDomainTableParser.FilterType.NEGATIVE_PROTEIN;
1555                 }
1556                 else if ( negative_domains_filter_file != null ) {
1557                     filter_type = HmmscanPerDomainTableParser.FilterType.NEGATIVE_DOMAIN;
1558                 }
1559                 parser = new HmmscanPerDomainTableParser( new File( input_file_properties[ i ][ 0 ] ),
1560                                                           input_file_properties[ i ][ 1 ],
1561                                                           filter,
1562                                                           filter_type,
1563                                                           ind_score_cutoff,
1564                                                           true );
1565             }
1566             else {
1567                 parser = new HmmscanPerDomainTableParser( new File( input_file_properties[ i ][ 0 ] ),
1568                                                           input_file_properties[ i ][ 1 ],
1569                                                           ind_score_cutoff,
1570                                                           true );
1571             }
1572             if ( fs_e_value_max >= 0.0 ) {
1573                 parser.setFsEValueMaximum( fs_e_value_max );
1574             }
1575             if ( ie_value_max >= 0.0 ) {
1576                 parser.setIEValueMaximum( ie_value_max );
1577             }
1578             if ( rel_env_length_ratio_cutoff > 0.0 ) {
1579                 parser.setRelEnvLengthRatioCutoff( rel_env_length_ratio_cutoff );
1580             }
1581             parser.setIgnoreDufs( ignore_dufs );
1582             parser.setIgnoreVirusLikeIds( ignore_virus_like_ids );
1583             parser.setIgnoreEngulfedDomains( no_engulfing_overlaps );
1584             if ( max_allowed_overlap != surfacing.MAX_ALLOWED_OVERLAP_DEFAULT ) {
1585                 parser.setMaxAllowedOverlap( max_allowed_overlap );
1586             }
1587             parser.setReturnType( HmmscanPerDomainTableParser.ReturnType.UNORDERED_PROTEIN_DOMAIN_COLLECTION_PER_PROTEIN );
1588             if ( individual_score_cutoffs != null ) {
1589                 parser.setIndividualScoreCutoffs( individual_score_cutoffs );
1590             }
1591             List<Protein> protein_list = null;
1592             try {
1593                 protein_list = parser.parse();
1594             }
1595             catch ( final IOException e ) {
1596                 ForesterUtil.fatalError( surfacing.PRG_NAME, e.getMessage() );
1597             }
1598             catch ( final Exception e ) {
1599                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, e.getMessage(), e );
1600             }
1601             if ( VERBOSE ) {
1602                 System.out.println( "Domains ignored due to negative domain filter: " );
1603                 ForesterUtil.printCountingMap( parser.getDomainsIgnoredDueToNegativeDomainFilterCountsMap() );
1604                 System.out.println( "Domains ignored due to virus like id: " );
1605                 ForesterUtil.printCountingMap( parser.getDomainsIgnoredDueToVirusLikeIdCountsMap() );
1606             }
1607             final double coverage = ( double ) protein_list.size() / parser.getProteinsEncountered();
1608             protein_coverage_stats.addValue( coverage );
1609             int distinct_das = -1;
1610             if ( da_analysis ) {
1611                 final String genome = input_file_properties[ i ][ 0 ];
1612                 distinct_das = SurfacingUtil.storeDomainArchitectures( genome,
1613                                                                        distinct_domain_architecutures_per_genome,
1614                                                                        protein_list,
1615                                                                        distinct_domain_architecuture_counts );
1616             }
1617             System.out.println( "Number of proteins encountered                 : " + parser.getProteinsEncountered() );
1618             SurfacingUtil.log( "Number of proteins encountered                 : " + parser.getProteinsEncountered(),
1619                                log_writer );
1620             System.out.println( "Number of proteins stored                      : " + protein_list.size() );
1621             SurfacingUtil.log( "Number of proteins stored                      : " + protein_list.size(), log_writer );
1622             System.out.println( "Coverage                                       : "
1623                     + ForesterUtil.roundToInt( 100.0 * coverage ) + "%" );
1624             SurfacingUtil.log( "Coverage                                       : "
1625                     + ForesterUtil.roundToInt( 100.0 * coverage ) + "%", log_writer );
1626             System.out.println( "Domains encountered                            : " + parser.getDomainsEncountered() );
1627             SurfacingUtil.log( "Domains encountered                            : " + parser.getDomainsEncountered(),
1628                                log_writer );
1629             System.out.println( "Domains stored                                 : " + parser.getDomainsStored() );
1630             SurfacingUtil.log( "Domains stored                                 : " + parser.getDomainsStored(),
1631                                log_writer );
1632             System.out.println( "Distinct domains stored                        : "
1633                     + parser.getDomainsStoredSet().size() );
1634             SurfacingUtil
1635                     .log( "Distinct domains stored                        : " + parser.getDomainsStoredSet().size(),
1636                           log_writer );
1637             System.out.println( "Domains ignored due to individual score cutoffs: "
1638                     + parser.getDomainsIgnoredDueToIndividualScoreCutoff() );
1639             SurfacingUtil.log( "Domains ignored due to individual score cutoffs: "
1640                     + parser.getDomainsIgnoredDueToIndividualScoreCutoff(), log_writer );
1641             System.out.println( "Domains ignored due to FS E-value              : "
1642                     + parser.getDomainsIgnoredDueToFsEval() );
1643             SurfacingUtil
1644                     .log( "Domains ignored due to FS E-value              : " + parser.getDomainsIgnoredDueToFsEval(),
1645                           log_writer );
1646             System.out.println( "Domains ignored due to iE-value                : "
1647                     + parser.getDomainsIgnoredDueToIEval() );
1648             SurfacingUtil
1649                     .log( "Domains ignored due to iE-value                : " + parser.getDomainsIgnoredDueToIEval(),
1650                           log_writer );
1651             System.out.println( "Domains ignored due to rel env length ratio    : "
1652                     + parser.getDomainsIgnoredDueToRelEnvLengthRatioCutoff() );
1653             SurfacingUtil.log( "Domains ignored due to rel env length ratio    : "
1654                     + parser.getDomainsIgnoredDueToRelEnvLengthRatioCutoff(), log_writer );
1655             System.out.println( "Domains ignored due to DUF designation         : "
1656                     + parser.getDomainsIgnoredDueToDuf() );
1657             SurfacingUtil.log( "Domains ignored due to DUF designation         : " + parser.getDomainsIgnoredDueToDuf(),
1658                                log_writer );
1659             if ( ignore_virus_like_ids ) {
1660                 System.out.println( "Domains ignored due virus like ids             : "
1661                         + parser.getDomainsIgnoredDueToVirusLikeIds() );
1662                 SurfacingUtil.log( "Domains ignored due virus like ids             : "
1663                         + parser.getDomainsIgnoredDueToVirusLikeIds(), log_writer );
1664             }
1665             System.out.println( "Domains ignored due negative domain filter     : "
1666                     + parser.getDomainsIgnoredDueToNegativeDomainFilter() );
1667             SurfacingUtil.log( "Domains ignored due negative domain filter     : "
1668                     + parser.getDomainsIgnoredDueToNegativeDomainFilter(), log_writer );
1669             System.out.println( "Domains ignored due to overlap                 : "
1670                     + parser.getDomainsIgnoredDueToOverlap() );
1671             SurfacingUtil
1672                     .log( "Domains ignored due to overlap                 : " + parser.getDomainsIgnoredDueToOverlap(),
1673                           log_writer );
1674             if ( negative_filter_file != null ) {
1675                 System.out.println( "Proteins ignored due to negative filter        : "
1676                         + parser.getProteinsIgnoredDueToFilter() );
1677                 SurfacingUtil.log( "Proteins ignored due to negative filter        : "
1678                         + parser.getProteinsIgnoredDueToFilter(), log_writer );
1679             }
1680             if ( positive_filter_file != null ) {
1681                 System.out.println( "Proteins ignored due to positive filter        : "
1682                         + parser.getProteinsIgnoredDueToFilter() );
1683                 SurfacingUtil.log( "Proteins ignored due to positive filter        : "
1684                         + parser.getProteinsIgnoredDueToFilter(), log_writer );
1685             }
1686             if ( da_analysis ) {
1687                 System.out.println( "Distinct domain architectures stored           : " + distinct_das );
1688                 SurfacingUtil.log( "Distinct domain architectures stored           : " + distinct_das, log_writer );
1689             }
1690             System.out.println( "Time for processing                            : " + parser.getTime() + "ms" );
1691             SurfacingUtil.log( "", log_writer );
1692             try {
1693                 int count = 0;
1694                 for( final Protein protein : protein_list ) {
1695                     dc_data_writer
1696                             .write( SurfacingUtil.proteinToDomainCombinations( protein, count + "", "\t" ).toString() );
1697                     ++count;
1698                     for( final Domain d : protein.getProteinDomains() ) {
1699                         final String d_str = d.getDomainId().toString();
1700                         if ( !domain_length_stats_by_domain.containsKey( d_str ) ) {
1701                             domain_length_stats_by_domain.put( d_str, new BasicDescriptiveStatistics() );
1702                         }
1703                         domain_length_stats_by_domain.get( d_str ).addValue( d.getLength() );
1704                     }
1705                 }
1706             }
1707             catch ( final IOException e ) {
1708                 ForesterUtil.fatalError( surfacing.PRG_NAME, e.toString() );
1709             }
1710             SurfacingUtil.domainsPerProteinsStatistics( input_file_properties[ i ][ 1 ],
1711                                                         protein_list,
1712                                                         all_genomes_domains_per_potein_stats,
1713                                                         all_genomes_domains_per_potein_histo,
1714                                                         domains_which_are_always_single,
1715                                                         domains_which_are_sometimes_single_sometimes_not,
1716                                                         domains_which_never_single,
1717                                                         domains_per_potein_stats_writer );
1718             if ( domain_length_analysis ) {
1719                 domain_lengths_table.addLengths( protein_list );
1720             }
1721             if ( !da_analysis ) {
1722                 gwcd_list.add( BasicGenomeWideCombinableDomains.createInstance( protein_list,
1723                                                                                 ignore_combination_with_same,
1724                                                                                 new BasicSpecies( input_file_properties[ i ][ 1 ] ),
1725                                                                                 domain_id_to_go_ids_map,
1726                                                                                 dc_type,
1727                                                                                 protein_length_stats_by_dc,
1728                                                                                 domain_number_stats_by_dc ) );
1729                 if ( gwcd_list.get( i ).getSize() > 0 ) {
1730                     if ( output_binary_domain_combinationsfor_counts ) {
1731                         SurfacingUtil.writeDomainCombinationsCountsFile( input_file_properties,
1732                                                                          out_dir,
1733                                                                          per_genome_domain_promiscuity_statistics_writer,
1734                                                                          gwcd_list.get( i ),
1735                                                                          i,
1736                                                                          dc_sort_order );
1737                     }
1738                     if ( output_binary_domain_combinationsfor_graph_analysis ) {
1739                         SurfacingUtil.writeBinaryDomainCombinationsFileForGraphAnalysis( input_file_properties,
1740                                                                                          out_dir,
1741                                                                                          gwcd_list.get( i ),
1742                                                                                          i,
1743                                                                                          dc_sort_order );
1744                     }
1745                     SurfacingUtil.addAllDomainIdsToSet( gwcd_list.get( i ), all_domains_encountered );
1746                     SurfacingUtil.addAllBinaryDomainCombinationToSet( gwcd_list.get( i ),
1747                                                                       all_bin_domain_combinations_encountered );
1748                 }
1749             }
1750             if ( query_domains_writer_ary != null ) {
1751                 for( int j = 0; j < query_domain_ids_array.length; j++ ) {
1752                     try {
1753                         SurfacingUtil.extractProteinNames( protein_list,
1754                                                            query_domain_ids_array[ j ],
1755                                                            query_domains_writer_ary[ j ],
1756                                                            "\t",
1757                                                            LIMIT_SPEC_FOR_PROT_EX );
1758                         query_domains_writer_ary[ j ].flush();
1759                     }
1760                     catch ( final IOException e ) {
1761                         e.printStackTrace();
1762                     }
1763                 }
1764             }
1765             if ( need_protein_lists_per_species ) {
1766                 protein_lists_per_species.put( new BasicSpecies( input_file_properties[ i ][ 1 ] ), protein_list );
1767             }
1768             try {
1769                 log_writer.flush();
1770             }
1771             catch ( final IOException e2 ) {
1772                 ForesterUtil.fatalError( surfacing.PRG_NAME, e2.getLocalizedMessage() );
1773             }
1774             System.gc();
1775         } // for( int i = 0; i < number_of_genomes; ++i ) {
1776         ForesterUtil
1777                 .programMessage( PRG_NAME,
1778                                  "Wrote domain promiscuities to: " + per_genome_domain_promiscuity_statistics_file );
1779         try {
1780             MinimalDomainomeCalculator.calc( false,
1781                                              intrees[ 0 ],
1782                                              protein_lists_per_species,
1783                                              SEPARATOR_FOR_DA,
1784                                              -1,
1785                                              out_dir.toString() + "/" + output_file,
1786                                              true );
1787         }
1788         catch ( IOException e ) {
1789             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
1790         }
1791         try {
1792             MinimalDomainomeCalculator.calc( true,
1793                                              intrees[ 0 ],
1794                                              protein_lists_per_species,
1795                                              SEPARATOR_FOR_DA,
1796                                              -1,
1797                                              out_dir.toString() + "/" + output_file,
1798                                              true );
1799         }
1800         catch ( IOException e ) {
1801             ForesterUtil.fatalError( surfacing.PRG_NAME, e.getLocalizedMessage() );
1802         }
1803         if ( da_analysis ) {
1804             SurfacingUtil.performDomainArchitectureAnalysis( distinct_domain_architecutures_per_genome,
1805                                                              distinct_domain_architecuture_counts,
1806                                                              10,
1807                                                              new File( out_dir.toString() + "/" + output_file
1808                                                                      + "_DA_counts.txt" ),
1809                                                              new File( out_dir.toString() + "/" + output_file
1810                                                                      + "_unique_DAs.txt" ) );
1811             distinct_domain_architecutures_per_genome.clear();
1812             distinct_domain_architecuture_counts.clear();
1813             System.gc();
1814         }
1815         try {
1816             domains_per_potein_stats_writer.write( "ALL" );
1817             domains_per_potein_stats_writer.write( "\t" );
1818             domains_per_potein_stats_writer.write( all_genomes_domains_per_potein_stats.arithmeticMean() + "" );
1819             domains_per_potein_stats_writer.write( "\t" );
1820             domains_per_potein_stats_writer
1821                     .write( all_genomes_domains_per_potein_stats.sampleStandardDeviation() + "" );
1822             domains_per_potein_stats_writer.write( "\t" );
1823             if ( all_genomes_domains_per_potein_stats.getN() <= 300 ) {
1824                 domains_per_potein_stats_writer.write( all_genomes_domains_per_potein_stats.median() + "" );
1825                 domains_per_potein_stats_writer.write( "\t" );
1826             }
1827             domains_per_potein_stats_writer.write( all_genomes_domains_per_potein_stats.getN() + "" );
1828             domains_per_potein_stats_writer.write( "\t" );
1829             domains_per_potein_stats_writer.write( all_genomes_domains_per_potein_stats.getMin() + "" );
1830             domains_per_potein_stats_writer.write( "\t" );
1831             domains_per_potein_stats_writer.write( all_genomes_domains_per_potein_stats.getMax() + "" );
1832             domains_per_potein_stats_writer.write( "\n" );
1833             domains_per_potein_stats_writer.close();
1834             all_genomes_domains_per_potein_stats = null;
1835             SurfacingUtil.printOutPercentageOfMultidomainProteins( all_genomes_domains_per_potein_histo, log_writer );
1836             ForesterUtil.map2file(
1837                                    new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file
1838                                            + "_all_genomes_domains_per_potein_histo.txt" ),
1839                                    all_genomes_domains_per_potein_histo,
1840                                    "\t",
1841                                    "\n" );
1842             ForesterUtil.collection2file(
1843                                           new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file
1844                                                   + "_domains_always_single_.txt" ),
1845                                           domains_which_are_always_single,
1846                                           "\n" );
1847             ForesterUtil.collection2file(
1848                                           new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file
1849                                                   + "_domains_single_or_combined.txt" ),
1850                                           domains_which_are_sometimes_single_sometimes_not,
1851                                           "\n" );
1852             ForesterUtil.collection2file(
1853                                           new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file
1854                                                   + "_domains_always_combined.txt" ),
1855                                           domains_which_never_single,
1856                                           "\n" );
1857             ForesterUtil.programMessage( PRG_NAME,
1858                                          "Average of proteins with a least one domain assigned: "
1859                                                  + ( 100 * protein_coverage_stats.arithmeticMean() ) + "% (+/-"
1860                                                  + ( 100 * protein_coverage_stats.sampleStandardDeviation() ) + "%)" );
1861             ForesterUtil.programMessage( PRG_NAME,
1862                                          "Range of proteins with a least one domain assigned: "
1863                                                  + ( 100 * protein_coverage_stats.getMin() ) + "%-"
1864                                                  + ( 100 * protein_coverage_stats.getMax() ) + "%" );
1865             SurfacingUtil.log(
1866                                "Average of prot with a least one dom assigned  : "
1867                                        + ( 100 * protein_coverage_stats.arithmeticMean() ) + "% (+/-"
1868                                        + ( 100 * protein_coverage_stats.sampleStandardDeviation() ) + "%)",
1869                                log_writer );
1870             SurfacingUtil.log( "Range of prot with a least one dom assigned    : "
1871                     + ( 100 * protein_coverage_stats.getMin() ) + "%-" + ( 100 * protein_coverage_stats.getMax() )
1872                     + "%", log_writer );
1873             protein_coverage_stats = null;
1874         }
1875         catch ( final IOException e2 ) {
1876             ForesterUtil.fatalError( surfacing.PRG_NAME, e2.getLocalizedMessage() );
1877         }
1878         if ( query_domains_writer_ary != null ) {
1879             for( int j = 0; j < query_domain_ids_array.length; j++ ) {
1880                 try {
1881                     query_domains_writer_ary[ j ].close();
1882                 }
1883                 catch ( final IOException e ) {
1884                     ForesterUtil.fatalError( surfacing.PRG_NAME, e.toString() );
1885                 }
1886             }
1887         }
1888         try {
1889             per_genome_domain_promiscuity_statistics_writer.close();
1890             dc_data_writer.close();
1891             log_writer.close();
1892         }
1893         catch ( final IOException e2 ) {
1894             ForesterUtil.fatalError( surfacing.PRG_NAME, e2.getLocalizedMessage() );
1895         }
1896         if ( domain_length_analysis ) {
1897             try {
1898                 SurfacingUtil.executeDomainLengthAnalysis( input_file_properties,
1899                                                            number_of_genomes,
1900                                                            domain_lengths_table,
1901                                                            domain_lengths_analysis_outfile );
1902             }
1903             catch ( final IOException e1 ) {
1904                 ForesterUtil.fatalError( surfacing.PRG_NAME, e1.toString() );
1905             }
1906             System.out.println();
1907             ForesterUtil.programMessage( PRG_NAME, "Wrote domain length data to: " + domain_lengths_analysis_outfile );
1908             System.out.println();
1909         }
1910         domain_lengths_table = null;
1911         final long analysis_start_time = new Date().getTime();
1912         PairwiseDomainSimilarityCalculator pw_calc = null;
1913         final DomainSimilarityCalculator calc = new BasicDomainSimilarityCalculator( domain_similarity_sort_field,
1914                                                                                      sort_by_species_count_first,
1915                                                                                      number_of_genomes == 2,
1916                                                                                      CALC_SIMILARITY_SCORES,
1917                                                                                      true );
1918         switch ( scoring ) {
1919             case COMBINATIONS:
1920                 pw_calc = new CombinationsBasedPairwiseDomainSimilarityCalculator();
1921                 break;
1922             case DOMAINS:
1923                 pw_calc = new DomainCountsBasedPairwiseSimilarityCalculator();
1924                 break;
1925             case PROTEINS:
1926                 pw_calc = new ProteinCountsBasedPairwiseDomainSimilarityCalculator();
1927                 break;
1928             default:
1929                 ForesterUtil.unexpectedFatalError( surfacing.PRG_NAME, "unknown value for sorting for scoring" );
1930         }
1931         DomainSimilarityCalculator.GoAnnotationOutput go_annotation_output = DomainSimilarityCalculator.GoAnnotationOutput.NONE;
1932         if ( domain_id_to_go_ids_map != null ) {
1933             go_annotation_output = DomainSimilarityCalculator.GoAnnotationOutput.ALL;
1934         }
1935         final SortedSet<DomainSimilarity> similarities = calc.calculateSimilarities( pw_calc,
1936                                                                                      gwcd_list,
1937                                                                                      ignore_domains_without_combs_in_all_spec,
1938                                                                                      ignore_species_specific_domains );
1939         SurfacingUtil.decoratePrintableDomainSimilarities( similarities, detailedness );
1940         final Map<String, Integer> tax_code_to_id_map = SurfacingUtil.createTaxCodeToIdMap( intrees[ 0 ] );
1941         try {
1942             String my_outfile = output_file.toString();
1943             Map<Character, Writer> split_writers = null;
1944             Writer writer = null;
1945             if ( similarities.size() > MINIMAL_NUMBER_OF_SIMILARITIES_FOR_SPLITTING ) {
1946                 if ( my_outfile.endsWith( ".html" ) ) {
1947                     my_outfile = my_outfile.substring( 0, my_outfile.length() - 5 );
1948                 }
1949                 split_writers = new HashMap<Character, Writer>();
1950                 SurfacingUtil.createSplitWriters( out_dir, my_outfile, split_writers );
1951             }
1952             else if ( !my_outfile.endsWith( ".html" ) ) {
1953                 my_outfile += ".html";
1954                 writer = new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile ) );
1955             }
1956             List<Species> species_order = null;
1957             if ( species_matrix ) {
1958                 species_order = new ArrayList<Species>();
1959                 for( int i = 0; i < number_of_genomes; i++ ) {
1960                     species_order.add( new BasicSpecies( input_file_properties[ i ][ 1 ] ) );
1961                 }
1962             }
1963             html_desc.append( "<tr><td>Sum of all distinct binary combinations:</td><td>"
1964                     + all_bin_domain_combinations_encountered.size() + "</td></tr>" + nl );
1965             html_desc.append( "<tr><td>Sum of all distinct domains:</td><td>" + all_domains_encountered.size()
1966                     + "</td></tr>" + nl );
1967             html_desc.append( "<tr><td>Analysis date/time:</td><td>"
1968                     + new java.text.SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" ).format( new java.util.Date() )
1969                     + "</td></tr>" + nl );
1970             html_desc.append( "</table>" + nl );
1971             final Writer simple_tab_writer = new BufferedWriter( new FileWriter( out_dir + ForesterUtil.FILE_SEPARATOR
1972                     + my_outfile.replaceFirst( ".html", ".tsv" ) ) );
1973             SurfacingUtil.writeDomainSimilaritiesToFile( html_desc,
1974                                                          new StringBuilder( number_of_genomes + " genomes" ),
1975                                                          simple_tab_writer,
1976                                                          writer,
1977                                                          split_writers,
1978                                                          similarities,
1979                                                          number_of_genomes == 2,
1980                                                          species_order,
1981                                                          domain_similarity_print_option,
1982                                                          scoring,
1983                                                          true,
1984                                                          tax_code_to_id_map,
1985                                                          intree_0_orig,
1986                                                          positive_filter_file != null ? filter : null );
1987             simple_tab_writer.close();
1988             ForesterUtil.programMessage( surfacing.PRG_NAME,
1989                                          "Wrote main output (includes domain similarities) to: \""
1990                                                  + ( out_dir == null ? my_outfile
1991                                                          : out_dir + ForesterUtil.FILE_SEPARATOR + my_outfile )
1992                                                  + "\"" );
1993         }
1994         catch ( final IOException e ) {
1995             ForesterUtil
1996                     .fatalError( surfacing.PRG_NAME,
1997                                  "Failed to write similarites to: \"" + output_file + "\" [" + e.getMessage() + "]" );
1998         }
1999         System.out.println();
2000         final Species[] species = new Species[ number_of_genomes ];
2001         for( int i = 0; i < number_of_genomes; ++i ) {
2002             species[ i ] = new BasicSpecies( input_file_properties[ i ][ 1 ] );
2003         }
2004         List<Phylogeny> inferred_trees = null;
2005         if ( ( number_of_genomes > 2 ) && perform_pwc ) {
2006             final PairwiseGenomeComparator pwgc = new PairwiseGenomeComparator();
2007             pwgc.performPairwiseComparisons( html_desc,
2008                                              sort_by_species_count_first,
2009                                              detailedness,
2010                                              ignore_domains_without_combs_in_all_spec,
2011                                              ignore_species_specific_domains,
2012                                              domain_similarity_sort_field_for_automated_pwc,
2013                                              domain_similarity_print_option,
2014                                              scoring,
2015                                              domain_id_to_go_ids_map,
2016                                              go_id_to_term_map,
2017                                              go_namespace_limit,
2018                                              species,
2019                                              number_of_genomes,
2020                                              gwcd_list,
2021                                              pw_calc,
2022                                              automated_pairwise_comparison_suffix,
2023                                              true,
2024                                              surfacing.PAIRWISE_DOMAIN_COMPARISONS_PREFIX,
2025                                              surfacing.PRG_NAME,
2026                                              out_dir,
2027                                              write_pwc_files,
2028                                              tax_code_to_id_map,
2029                                              CALC_SIMILARITY_SCORES,
2030                                              intree_0_orig );
2031             String matrix_output_file = new String( output_file.toString() );
2032             if ( matrix_output_file.indexOf( '.' ) > 1 ) {
2033                 matrix_output_file = matrix_output_file.substring( 0, matrix_output_file.indexOf( '.' ) );
2034             }
2035             if ( out_dir != null ) {
2036                 matrix_output_file = out_dir + ForesterUtil.FILE_SEPARATOR + matrix_output_file;
2037                 output_file = new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file );
2038             }
2039             SurfacingUtil.writeMatrixToFile(
2040                                              new File( matrix_output_file
2041                                                      + surfacing.MATRIX_MEAN_SCORE_BASED_GENOME_DISTANCE_SUFFIX ),
2042                                              pwgc.getDomainDistanceScoresMeans() );
2043             SurfacingUtil.writeMatrixToFile(
2044                                              new File( matrix_output_file
2045                                                      + surfacing.MATRIX_SHARED_BIN_COMBINATIONS_BASED_GENOME_DISTANCE_SUFFIX ),
2046                                              pwgc.getSharedBinaryCombinationsBasedDistances() );
2047             SurfacingUtil.writeMatrixToFile(
2048                                              new File( matrix_output_file
2049                                                      + surfacing.MATRIX_SHARED_DOMAINS_BASED_GENOME_DISTANCE_SUFFIX ),
2050                                              pwgc.getSharedDomainsBasedDistances() );
2051             final Phylogeny nj_gd = SurfacingUtil.createNjTreeBasedOnMatrixToFile( new File( matrix_output_file
2052                     + surfacing.NJ_TREE_MEAN_SCORE_BASED_GENOME_DISTANCE_SUFFIX ),
2053                                                                                    pwgc.getDomainDistanceScoresMeans()
2054                                                                                            .get( 0 ) );
2055             final Phylogeny nj_bc = SurfacingUtil.createNjTreeBasedOnMatrixToFile(
2056                                                                                    new File( matrix_output_file
2057                                                                                            + surfacing.NJ_TREE_SHARED_BIN_COMBINATIONS_BASED_GENOME_DISTANCE_SUFFIX ),
2058                                                                                    pwgc.getSharedBinaryCombinationsBasedDistances()
2059                                                                                            .get( 0 ) );
2060             final Phylogeny nj_d = SurfacingUtil.createNjTreeBasedOnMatrixToFile(
2061                                                                                   new File( matrix_output_file
2062                                                                                           + surfacing.NJ_TREE_SHARED_DOMAINS_BASED_GENOME_DISTANCE_SUFFIX ),
2063                                                                                   pwgc.getSharedDomainsBasedDistances()
2064                                                                                           .get( 0 ) );
2065             inferred_trees = new ArrayList<Phylogeny>();
2066             inferred_trees.add( nj_gd );
2067             inferred_trees.add( nj_bc );
2068             inferred_trees.add( nj_d );
2069         } // if ( ( output_file != null ) && ( number_of_genomes > 2 ) && !isEmpty( automated_pairwise_comparison_suffix ) )
2070         if ( ( out_dir != null ) && ( !perform_pwc ) ) {
2071             output_file = new File( out_dir + ForesterUtil.FILE_SEPARATOR + output_file );
2072         }
2073         if ( write_to_nexus ) {
2074             SurfacingUtil.writePresentToNexus( output_file, positive_filter_file, filter, gwcd_list );
2075         }
2076         if ( ( ( intrees != null ) && ( intrees.length > 0 ) ) && ( number_of_genomes > 2 ) ) {
2077             final StringBuilder parameters_sb = SurfacingUtil.createParametersAsString( ignore_dufs,
2078                                                                                         ie_value_max,
2079                                                                                         fs_e_value_max,
2080                                                                                         max_allowed_overlap,
2081                                                                                         no_engulfing_overlaps,
2082                                                                                         cutoff_scores_file,
2083                                                                                         dc_type );
2084             String s = "_";
2085             if ( radomize_fitch_parsimony ) {
2086                 s += random_number_seed_for_fitch_parsimony + "_";
2087             }
2088             int i = 0;
2089             for( final Phylogeny intree : intrees ) {
2090                 final String outfile_name = ForesterUtil.removeSuffix( output_file.toString() ) + s
2091                         + ForesterUtil.removeSuffix( intree_files[ i ].toString() );
2092                 final DomainParsimonyCalculator domain_parsimony = DomainParsimonyCalculator
2093                         .createInstance( intree, gwcd_list );
2094                 SurfacingUtil.executeParsimonyAnalysis( random_number_seed_for_fitch_parsimony,
2095                                                         radomize_fitch_parsimony,
2096                                                         outfile_name,
2097                                                         domain_parsimony,
2098                                                         intree,
2099                                                         domain_id_to_go_ids_map,
2100                                                         go_id_to_term_map,
2101                                                         go_namespace_limit,
2102                                                         parameters_sb.toString(),
2103                                                         domain_id_to_secondary_features_maps,
2104                                                         positive_filter_file == null ? null : filter,
2105                                                         output_binary_domain_combinationsfor_graph_analysis,
2106                                                         all_bin_domain_combinations_gained_fitch,
2107                                                         all_bin_domain_combinations_lost_fitch,
2108                                                         dc_type,
2109                                                         protein_length_stats_by_dc,
2110                                                         domain_number_stats_by_dc,
2111                                                         domain_length_stats_by_domain,
2112                                                         tax_code_to_id_map,
2113                                                         write_to_nexus,
2114                                                         use_last_in_fitch_parsimony,
2115                                                         perform_dc_fich );
2116                 // Listing of all domain combinations gained is only done if only one input tree is used.
2117                 if ( ( domain_id_to_secondary_features_maps != null )
2118                         && ( domain_id_to_secondary_features_maps.length > 0 ) ) {
2119                     int j = 0;
2120                     for( final Map<String, Set<String>> domain_id_to_secondary_features_map : domain_id_to_secondary_features_maps ) {
2121                         final Map<Species, MappingResults> mapping_results_map = new TreeMap<Species, MappingResults>();
2122                         final DomainParsimonyCalculator secondary_features_parsimony = DomainParsimonyCalculator
2123                                 .createInstance( intree, gwcd_list, domain_id_to_secondary_features_map );
2124                         SurfacingUtil.executeParsimonyAnalysisForSecondaryFeatures( outfile_name + "_"
2125                                 + secondary_features_map_files[ j++ ],
2126                                                                                     secondary_features_parsimony,
2127                                                                                     intree,
2128                                                                                     parameters_sb.toString(),
2129                                                                                     mapping_results_map,
2130                                                                                     use_last_in_fitch_parsimony );
2131                         if ( i == 0 ) {
2132                             System.out.println();
2133                             System.out.println( "Mapping to secondary features:" );
2134                             for( final Species spec : mapping_results_map.keySet() ) {
2135                                 final MappingResults mapping_results = mapping_results_map.get( spec );
2136                                 final int total_domains = mapping_results.getSumOfFailures()
2137                                         + mapping_results.getSumOfSuccesses();
2138                                 System.out.print( spec + ":" );
2139                                 System.out.print( " mapped domains = " + mapping_results.getSumOfSuccesses() );
2140                                 System.out.print( ", not mapped domains = " + mapping_results.getSumOfFailures() );
2141                                 if ( total_domains > 0 ) {
2142                                     System.out.println( ", mapped ratio = "
2143                                             + ( ( 100 * mapping_results.getSumOfSuccesses() ) / total_domains ) + "%" );
2144                                 }
2145                                 else {
2146                                     System.out.println( ", mapped ratio = n/a (total domains = 0 )" );
2147                                 }
2148                             }
2149                         }
2150                     }
2151                 }
2152                 i++;
2153             } // for( final Phylogeny intree : intrees ) {
2154         }
2155         if ( plus_minus_analysis_high_copy_base_species.size() > 0 ) {
2156             SurfacingUtil.executePlusMinusAnalysis( output_file,
2157                                                     plus_minus_analysis_high_copy_base_species,
2158                                                     plus_minus_analysis_high_copy_target_species,
2159                                                     plus_minus_analysis_high_low_copy_species,
2160                                                     gwcd_list,
2161                                                     protein_lists_per_species,
2162                                                     domain_id_to_go_ids_map,
2163                                                     go_id_to_term_map,
2164                                                     plus_minus_analysis_numbers );
2165         }
2166         if ( output_protein_lists_for_all_domains ) {
2167             SurfacingUtil.writeProteinListsForAllSpecies( out_dir,
2168                                                           protein_lists_per_species,
2169                                                           gwcd_list,
2170                                                           output_list_of_all_proteins_per_domain_e_value_max,
2171                                                           positive_filter_file != null ? filter : null );
2172         }
2173         gwcd_list = null;
2174         if ( all_bin_domain_combinations_gained_fitch != null ) {
2175             try {
2176                 SurfacingUtil.executeFitchGainsAnalysis(
2177                                                          new File( output_file
2178                                                                  + surfacing.OUTPUT_DOMAIN_COMBINATIONS_GAINED_MORE_THAN_ONCE_ANALYSIS_SUFFIX ),
2179                                                          all_bin_domain_combinations_gained_fitch,
2180                                                          all_domains_encountered.size(),
2181                                                          all_bin_domain_combinations_encountered,
2182                                                          true );
2183             }
2184             catch ( final IOException e ) {
2185                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
2186             }
2187         }
2188         if ( all_bin_domain_combinations_lost_fitch != null ) {
2189             try {
2190                 SurfacingUtil.executeFitchGainsAnalysis(
2191                                                          new File( output_file
2192                                                                  + surfacing.OUTPUT_DOMAIN_COMBINATIONS_LOST_MORE_THAN_ONCE_ANALYSIS_SUFFIX ),
2193                                                          all_bin_domain_combinations_lost_fitch,
2194                                                          all_domains_encountered.size(),
2195                                                          all_bin_domain_combinations_encountered,
2196                                                          false );
2197             }
2198             catch ( final IOException e ) {
2199                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
2200             }
2201         }
2202         final Runtime rt = java.lang.Runtime.getRuntime();
2203         final long free_memory = rt.freeMemory() / 1000000;
2204         final long total_memory = rt.totalMemory() / 1000000;
2205         ForesterUtil.programMessage( PRG_NAME,
2206                                      "Time for analysis : " + ( new Date().getTime() - analysis_start_time ) + "ms" );
2207         ForesterUtil.programMessage( PRG_NAME, "Total running time: " + ( new Date().getTime() - start_time ) + "ms " );
2208         ForesterUtil
2209                 .programMessage( PRG_NAME,
2210                                  "Free memory       : " + free_memory + "MB, total memory: " + total_memory + "MB" );
2211         ForesterUtil.programMessage( PRG_NAME, "If this application is useful to you, please cite:" );
2212         ForesterUtil.programMessage( PRG_NAME, surfacing.WWW );
2213         ForesterUtil
2214                 .programMessage( PRG_NAME,
2215                                  "[next step for phylogenomic analysis pipeline (example, in \"DAS\" dir): % mse.rb .prot . FL_seqs DA_seqs ../../genome_locations.txt]" );
2216         ForesterUtil.programMessage( PRG_NAME, "OK" );
2217         System.out.println();
2218     }
2219
2220     private static void printHelp() {
2221         System.out.println();
2222         System.out.println( "Usage:" );
2223         System.out.println();
2224         System.out.println( "% java -Xms256m -Xmx512m -cp forester.jar org.forester.applications." + surfacing.PRG_NAME
2225                 + " [options] <phylogen(y|ies) infile> [external node name 1] [name 2] ... [name n]" );
2226         System.out.println();
2227         System.out.println( " Note: This software might need a significant amount of memory (heap space);" );
2228         System.out
2229                 .println( "       hence use \"-Xms128m -Xmx512m\" (or more) to prevent a \"java.lang.OutOfMemoryError\"." );
2230         System.out.println();
2231         System.out.println( " Options: " );
2232         System.out.println( surfacing.DETAILEDNESS_OPTION + ": level of detail for similarities output file (default:"
2233                 + DETAILEDNESS_DEFAULT + ")" );
2234         System.out.println( surfacing.IGNORE_COMBINATION_WITH_SAME_OPTION
2235                 + ": to ignore combinations with self (default: not to ignore)" );
2236         System.out.println( surfacing.IGNORE_DOMAINS_WITHOUT_COMBINATIONS_IN_ALL_SPECIES_OPTION
2237                 + ": to ignore domains without combinations in any species (for similarity calc purposes, not for parsimony analyses) (default: not to ignore)" );
2238         System.out.println( surfacing.IGNORE_DOMAINS_SPECIFIC_TO_ONE_SPECIES_OPTION
2239                 + ": to ignore domains specific to one species (for similarity calc purposes, not for parsimony analyses) (default: not to ignore)" );
2240         System.out.println( surfacing.NOT_IGNORE_DUFS_OPTION
2241                 + ": to _not_ ignore DUFs (domains with unknown function) (default: ignore DUFs)" );
2242         System.out.println( surfacing.IGNORE_VIRAL_IDS
2243                 + ": to ignore domains with ids containing 'vir', 'retro', 'transpos', 'phage', or starting with 'rv' or 'gag_'" );
2244         System.out.println( surfacing.DOMAIN_SIMILARITY_SORT_OPTION + ": sorting for similarities (default: "
2245                 + DOMAIN_SORT_FILD_DEFAULT + ")" );
2246         System.out.println( surfacing.OUTPUT_FILE_OPTION + ": name for (main) output file (mandatory)" );
2247         System.out.println( surfacing.MAX_I_E_VALUE_OPTION + ": max (inclusive) iE-value" );
2248         System.out.println( surfacing.MAX_FS_E_VALUE_OPTION + ": max (inclusive) FS E-value" );
2249         System.out.println( surfacing.MIN_REL_ENV_LENGTH_RATIO_OPTION
2250                 + ": min (inclusive) relative envelope length ratio" );
2251         System.out.println( surfacing.MAX_ALLOWED_OVERLAP_OPTION + ": maximal allowed domain overlap" );
2252         System.out.println( surfacing.NO_ENGULFING_OVERLAP_OPTION + ": to ignore engulfed lower confidence domains" );
2253         System.out.println( surfacing.SPECIES_MATRIX_OPTION + ": species matrix" );
2254         System.out.println( surfacing.SCORING_OPTION + ": scoring (default:" + SCORING_DEFAULT + ")" );
2255         System.out.println( surfacing.DOMAIN_COUNT_SORT_OPTION + ": sorting for domain counts (default:"
2256                 + DOMAINS_SORT_ORDER_DEFAULT + ")" );
2257         System.out.println( surfacing.DOMAIN_SIMILARITY_PRINT_OPTION + ": domain similarity print option (default:"
2258                 + DOMAIN_SIMILARITY_PRINT_OPTION_DEFAULT + ")" );
2259         System.out.println( surfacing.CUTOFF_SCORE_FILE_OPTION + ": cutoff score file" );
2260         System.out.println( surfacing.DOMAIN_SIMILARITY_SORT_BY_SPECIES_COUNT_FIRST_OPTION
2261                 + ": sort by species count first" );
2262         System.out.println( surfacing.OUTPUT_DIR_OPTION + ": output directory" );
2263         System.out.println( surfacing.PFAM_TO_GO_FILE_USE_OPTION + ": Pfam to GO mapping file" );
2264         System.out.println( surfacing.GO_OBO_FILE_USE_OPTION + ": GO terms file (OBO format)" );
2265         System.out.println( surfacing.GO_NAMESPACE_LIMIT_OPTION + ": limit GO term to one GO namespace" );
2266         System.out.println( surfacing.PAIRWISE_DOMAIN_COMPARISONS_OPTION
2267                 + "[=<suffix for pairwise comparison output files>]: to perform pairwise comparison based analyses" );
2268         System.out.println( surfacing.INPUT_SPECIES_TREE_OPTION
2269                 + ": species tree, to perform (Dollo, Fitch) parismony analyses" );
2270         System.out.println( surfacing.INPUT_SPECIES_TREE_OPTION
2271                 + "=<treefiles in phyloXML format, separated by #>: to infer domain/binary domain combination gains/losses on given species trees" );
2272         System.out.println( surfacing.FILTER_POSITIVE_OPTION
2273                 + "=<file>: to filter out proteins not containing at least one domain listed in <file>" );
2274         System.out.println( surfacing.FILTER_NEGATIVE_OPTION
2275                 + "=<file>: to filter out proteins containing at least one domain listed in <file>" );
2276         System.out.println( surfacing.FILTER_NEGATIVE_DOMAINS_OPTION
2277                 + "=<file>: to filter out (ignore) domains listed in <file>" );
2278         System.out.println( surfacing.INPUT_GENOMES_FILE_OPTION + "=<file>: to read input files from <file>" );
2279         System.out.println( surfacing.RANDOM_SEED_FOR_FITCH_PARSIMONY_OPTION
2280                 + "=<seed>: seed for random number generator for Fitch Parsimony analysis (type: long, default: no randomization - given a choice, prefer absence" );
2281         System.out.println( surfacing.CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS
2282                 + ": to consider directedness in binary combinations: e.g. A-B != B-A" );
2283         System.out.println( surfacing.CONSIDER_DOMAIN_COMBINATION_DIRECTEDNESS_AND_ADJACENCY
2284                 + ": to consider directedness and adjacency in binary combinations" );
2285         System.out.println( surfacing.SEQ_EXTRACT_OPTION
2286                 + "=<domain ids (Pfam names)>: to extract sequence names of sequences containing matching domains and/or domain-sequences (order N to C) (domain separator: '~', domain sequences speparator: '#', e.g. 'NACHT#BIR~CARD')" );
2287         System.out.println( surfacing.SECONDARY_FEATURES_PARSIMONY_MAP_FILE
2288                 + "=<file>: to perfom parsimony analysis on secondary features" );
2289         System.out.println( surfacing.PLUS_MINUS_ANALYSIS_OPTION + "=<file>: to presence/absence genome analysis" );
2290         System.out.println( surfacing.DOMAIN_COMBINITONS_COUNTS_OUTPUT_OPTION
2291                 + ": to output binary domain counts (as individual files)" );
2292         System.out.println( surfacing.DOMAIN_COMBINITONS_OUTPUT_OPTION_FOR_GRAPH_ANALYSIS
2293                 + ": to output binary domain combinations for (downstream) graph analysis" );
2294         System.out.println( surfacing.OUTPUT_LIST_OF_ALL_PROTEINS_OPTIONS + ": to output all proteins per domain" );
2295         System.out.println( surfacing.OUTPUT_LIST_OF_ALL_PROTEINS_PER_DOMAIN_E_VALUE_OPTION
2296                 + ": e value max per domain for output of all proteins per domain" );
2297         System.out.println( surfacing.USE_LAST_IN_FITCH_OPTION + ": to use last in Fitch parsimony" );
2298         System.out.println( surfacing.WRITE_TO_NEXUS_OPTION + ": to output in Nexus format" );
2299         System.out.println( PERFORM_DC_FITCH + ": to perform DC Fitch parsimony" );
2300         System.out.println( PERFORM_DC_REGAIN_PROTEINS_STATS_OPTION + ": to perform DC regain protein statistics" );
2301         System.out.println( DA_ANALYSIS_OPTION + ": to perform DA analysis" );
2302         System.out.println( PERFORM_DOMAIN_LENGTH_ANALYSIS_OPTION + ": to perform domain length analysis" );
2303         System.out.println();
2304         System.out.println();
2305         System.out
2306                 .println( "Example 1: surfacing -p2g=pfam2go.txt -obo=go.obo -species_tree=tol_156.xml -no_eo -ie=0.01 -dufs -genomes=genomes_all.txt -pos_filter=tf_1.txt -out_dir=_tf1 -o=tf1" );
2307         System.out.println();
2308         System.out
2309                 .println( "Example 2: surfacing -p2g=pfam2go.txt -obo=go.obo -species_tree=tol_156.xml -last -ignore_viral_ids -no_eo -ie=0.1 -dufs -genomes=genomes_all.txt -pos_filter=tf_1.txt -all_prot -all_prot_e=0.1 -out_dir=_tf1_e01_ape01 -o=tf1_e01_ape01" );
2310         System.out.println();
2311     }
2312 }