X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsurfacing%2FDomainParsimonyCalculator.java;h=4658371a9d0097ffddcb081f532bde07840424d9;hb=4d86cc1858160830406c6428cea1c45128216646;hp=2396b905c9026f947b5c58e479e57b5f66e49d71;hpb=eee996a6476a1e3d84c07f8f690dcde3ff4b2ef5;p=jalview.git diff --git a/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java b/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java index 2396b90..4658371 100644 --- a/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java +++ b/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java @@ -26,13 +26,17 @@ package org.forester.surfacing; +import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import org.forester.application.surfacing; import org.forester.evoinference.matrix.character.BasicCharacterStateMatrix; import org.forester.evoinference.matrix.character.CharacterStateMatrix; import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates; @@ -208,10 +212,54 @@ public final class DomainParsimonyCalculator { setTotalLosses( fitch.getTotalLosses() ); setTotalUnchanged( fitch.getTotalUnchanged() ); } + + private void executeFitchParsimonyOnSecondaryFeatures( + final boolean use_last, + final boolean randomize, + final long random_number_seed ) { + reset(); + if ( use_last ) { + System.out.println( " Fitch parsimony: use_last = true" ); + } + final FitchParsimony fitch = new FitchParsimony(); + fitch.setRandomize( randomize ); + if ( randomize ) { + fitch.setRandomNumberSeed( random_number_seed ); + } + fitch.setUseLast( use_last ); + fitch.setReturnGainLossMatrix( true ); + fitch.setReturnInternalStates( true ); + + final Map> map = getDomainIdToSecondaryFeaturesMap(); + final Map newmap = new HashMap(); + final Iterator>> it = map.entrySet().iterator(); + while (it.hasNext()) { + final Map.Entry> pair = (Map.Entry>)it.next(); + if ( pair.getValue().size() != 1 ) { + throw new IllegalArgumentException( pair.getKey().getId() + " mapps to " + pair.getValue().size() + " items" ); + } + newmap.put( pair.getKey(), ( String ) pair.getValue().toArray()[ 0 ] ); + } + + CharacterStateMatrix states =createMatrixOfSecondaryFeatureBinaryDomainCombinationPresenceOrAbsence( getGenomeWideCombinableDomainsList(), + newmap ); + + fitch.execute( getPhylogeny(), states ); + setGainLossMatrix( fitch.getGainLossMatrix() ); + setBinaryInternalStatesMatrix( fitch.getInternalStatesMatrix() ); + setCost( fitch.getCost() ); + setTotalGains( fitch.getTotalGains() ); + setTotalLosses( fitch.getTotalLosses() ); + setTotalUnchanged( fitch.getTotalUnchanged() ); + } public void executeFitchParsimonyOnBinaryDomainCombintion( final boolean use_last ) { executeFitchParsimony( false, use_last, false, 0 ); } + + public void executeFitchParsimonyOnBinaryDomainCombintionOnSecondaryFeatures( final boolean use_last ) { + executeFitchParsimonyOnSecondaryFeatures( use_last, false, 0 ); + } public void executeFitchParsimonyOnBinaryDomainCombintion( final long random_number_seed ) { executeFitchParsimony( false, false, true, random_number_seed ); @@ -498,6 +546,183 @@ public final class DomainParsimonyCalculator { return new DomainParsimonyCalculator( phylogeny, gwcd_list, domain_id_to_secondary_features_map ); } + + /** + * For folds instead of Pfam-domains, for example + * + * + * @param gwcd_list + * @return + */ + static CharacterStateMatrix createMatrixOfSecondaryFeaturePresenceOrAbsence( final List gwcd_list, + final Map> domain_id_to_second_features_map, + final Map mapping_results_map ) { + if ( gwcd_list.isEmpty() ) { + throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); + } + if ( ( domain_id_to_second_features_map == null ) || domain_id_to_second_features_map.isEmpty() ) { + throw new IllegalArgumentException( "domain id to secondary features map is null or empty" ); + } + final int number_of_identifiers = gwcd_list.size(); + final SortedSet all_secondary_features = new TreeSet(); + for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { + int mapped = 0; + int not_mapped = 0; + for( final DomainId domain : gwcd.getAllDomainIds() ) { + if ( domain_id_to_second_features_map.containsKey( domain ) ) { + all_secondary_features.addAll( domain_id_to_second_features_map.get( domain ) ); + mapped++; + } + else { + not_mapped++; + } + } + if ( mapping_results_map != null ) { + final MappingResults mr = new MappingResults(); + mr.setDescription( gwcd.getSpecies().getSpeciesId() ); + mr.setSumOfSuccesses( mapped ); + mr.setSumOfFailures( not_mapped ); + mapping_results_map.put( gwcd.getSpecies(), mr ); + } + } + final int number_of_characters = all_secondary_features.size(); + final CharacterStateMatrix matrix = new BasicCharacterStateMatrix( number_of_identifiers, + number_of_characters ); + int character_index = 0; + for( final String second_id : all_secondary_features ) { + matrix.setCharacter( character_index++, second_id ); + } + int identifier_index = 0; + final Set all_identifiers = new HashSet(); + for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { + final String species_id = gwcd.getSpecies().getSpeciesId(); + if ( all_identifiers.contains( species_id ) ) { + throw new IllegalArgumentException( "species [" + species_id + "] is not unique" ); + } + all_identifiers.add( species_id ); + matrix.setIdentifier( identifier_index, species_id ); + final Set all_second_per_gwcd = new HashSet(); + for( final DomainId domain : gwcd.getAllDomainIds() ) { + if ( domain_id_to_second_features_map.containsKey( domain ) ) { + all_second_per_gwcd.addAll( domain_id_to_second_features_map.get( domain ) ); + } + } + for( int ci = 0; ci < matrix.getNumberOfCharacters(); ++ci ) { + if ( all_second_per_gwcd.contains( matrix.getCharacter( ci ) ) ) { + matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.PRESENT ); + } + else { + matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.ABSENT ); + } + } + ++identifier_index; + } + return matrix; + } + + + public static CharacterStateMatrix createMatrixOfSecondaryFeatureBinaryDomainCombinationPresenceOrAbsence( final List gwcd_list, + final Map domain_id_to_second_features_map) { + if ( gwcd_list.isEmpty() ) { + throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); + } + if ( ( domain_id_to_second_features_map == null ) || domain_id_to_second_features_map.isEmpty() ) { + throw new IllegalArgumentException( "domain id to secondary features map is null or empty" ); + } + final int number_of_identifiers = gwcd_list.size(); + final SortedSet all_binary_combinations_mapped = new TreeSet(); + final Set[] binary_combinations_per_genome_mapped = new HashSet[ number_of_identifiers ]; + int identifier_index = 0; + + final SortedSet no_mappings = new TreeSet(); + for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { + binary_combinations_per_genome_mapped[ identifier_index ] = new HashSet(); + for( final BinaryDomainCombination bc : gwcd.toBinaryDomainCombinations() ) { + + final BinaryDomainCombination mapped_bc = mapBinaryDomainCombination( domain_id_to_second_features_map, + bc, + no_mappings ); + all_binary_combinations_mapped.add( mapped_bc ); + binary_combinations_per_genome_mapped[ identifier_index ].add( mapped_bc ); + } + ++identifier_index; + } + + if ( !no_mappings.isEmpty() ) { + ForesterUtil.programMessage( surfacing.PRG_NAME, "No mappings for the following (" + no_mappings.size() + "):" ); + for( final String id : no_mappings ) { + ForesterUtil.programMessage( surfacing.PRG_NAME, id); + } + } + + final int number_of_characters = all_binary_combinations_mapped.size(); + final CharacterStateMatrix matrix = new BasicCharacterStateMatrix( number_of_identifiers, + number_of_characters ); + int character_index = 0; + for( final BinaryDomainCombination bc : all_binary_combinations_mapped ) { + matrix.setCharacter( character_index++, bc.toString() ); + } + identifier_index = 0; + final Set all_identifiers = new HashSet(); + for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { + final String species_id = gwcd.getSpecies().getSpeciesId(); + if ( all_identifiers.contains( species_id ) ) { + throw new AssertionError( "species [" + species_id + "] is not unique" ); + } + all_identifiers.add( species_id ); + matrix.setIdentifier( identifier_index, species_id ); + for( int ci = 0; ci < matrix.getNumberOfCharacters(); ++ci ) { + BinaryDomainCombination bc = null; + if ( gwcd.getDomainCombinationType() == DomainCombinationType.DIRECTED_ADJACTANT ) { + bc = AdjactantDirectedBinaryDomainCombination.createInstance( matrix.getCharacter( ci ) ); + } + else if ( gwcd.getDomainCombinationType() == DomainCombinationType.DIRECTED ) { + bc = DirectedBinaryDomainCombination.createInstance( matrix.getCharacter( ci ) ); + } + else { + bc = BasicBinaryDomainCombination.createInstance( matrix.getCharacter( ci ) ); + } + if ( binary_combinations_per_genome_mapped[ identifier_index ].contains( bc ) ) { + matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.PRESENT ); + } + else { + matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.ABSENT ); + } + } + ++identifier_index; + } + return matrix; + } + + private static BinaryDomainCombination mapBinaryDomainCombination( final Map domain_id_to_second_features_map, + final BinaryDomainCombination bc, + final SortedSet no_mappings ) { + String id0 = ""; + String id1 = ""; + + if ( !domain_id_to_second_features_map.containsKey( bc.getId0() ) ) { + + no_mappings.add(bc.getId0().getId() ); + id0 = bc.getId0().getId(); + } + else { + id0 = domain_id_to_second_features_map.get( bc.getId0()); + } + if ( !domain_id_to_second_features_map.containsKey( bc.getId1() ) ) { + + no_mappings.add(bc.getId1().getId() ); + id1 = bc.getId1().getId(); + } + else { + id1 = domain_id_to_second_features_map.get( bc.getId1()); + } + + return new BasicBinaryDomainCombination( id0, id1 ); + } + + + + public static CharacterStateMatrix createMatrixOfBinaryDomainCombinationPresenceOrAbsence( final List gwcd_list ) { if ( gwcd_list.isEmpty() ) { throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); @@ -621,78 +846,7 @@ public final class DomainParsimonyCalculator { return matrix; } - /** - * For folds instead of Pfam-domains, for example - * - * - * @param gwcd_list - * @return - */ - static CharacterStateMatrix createMatrixOfSecondaryFeaturePresenceOrAbsence( final List gwcd_list, - final Map> domain_id_to_second_features_map, - final Map mapping_results_map ) { - if ( gwcd_list.isEmpty() ) { - throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); - } - if ( ( domain_id_to_second_features_map == null ) || domain_id_to_second_features_map.isEmpty() ) { - throw new IllegalArgumentException( "domain id to secondary features map is null or empty" ); - } - final int number_of_identifiers = gwcd_list.size(); - final SortedSet all_secondary_features = new TreeSet(); - for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { - int mapped = 0; - int not_mapped = 0; - for( final DomainId domain : gwcd.getAllDomainIds() ) { - if ( domain_id_to_second_features_map.containsKey( domain ) ) { - all_secondary_features.addAll( domain_id_to_second_features_map.get( domain ) ); - mapped++; - } - else { - not_mapped++; - } - } - if ( mapping_results_map != null ) { - final MappingResults mr = new MappingResults(); - mr.setDescription( gwcd.getSpecies().getSpeciesId() ); - mr.setSumOfSuccesses( mapped ); - mr.setSumOfFailures( not_mapped ); - mapping_results_map.put( gwcd.getSpecies(), mr ); - } - } - final int number_of_characters = all_secondary_features.size(); - final CharacterStateMatrix matrix = new BasicCharacterStateMatrix( number_of_identifiers, - number_of_characters ); - int character_index = 0; - for( final String second_id : all_secondary_features ) { - matrix.setCharacter( character_index++, second_id ); - } - int identifier_index = 0; - final Set all_identifiers = new HashSet(); - for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { - final String species_id = gwcd.getSpecies().getSpeciesId(); - if ( all_identifiers.contains( species_id ) ) { - throw new IllegalArgumentException( "species [" + species_id + "] is not unique" ); - } - all_identifiers.add( species_id ); - matrix.setIdentifier( identifier_index, species_id ); - final Set all_second_per_gwcd = new HashSet(); - for( final DomainId domain : gwcd.getAllDomainIds() ) { - if ( domain_id_to_second_features_map.containsKey( domain ) ) { - all_second_per_gwcd.addAll( domain_id_to_second_features_map.get( domain ) ); - } - } - for( int ci = 0; ci < matrix.getNumberOfCharacters(); ++ci ) { - if ( all_second_per_gwcd.contains( matrix.getCharacter( ci ) ) ) { - matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.PRESENT ); - } - else { - matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.ABSENT ); - } - } - ++identifier_index; - } - return matrix; - } + private static int getStateSumDeltaOnNode( final String node_identifier, final CharacterStateMatrix gain_loss_matrix,