0

Im trying to round some double decimals, because I am getting only 0.0 as result of division. I tried using the BigDecimal way and the String.format(), however I still get O.00 at: Relation Aromatics/Aliphatics: 0,00 Relation Heterocyclics/Aliphatics: 0,00 Relation Heterocyclics/Aromatic: 0,00

How could I fix that?? the values should be: Relation Aromatics/Aliphatics: 0,14 Relation Heterocyclics/Aliphatics: 0,12 Relation Heterocyclics/Aromatic: 0,875

This is the code:

import java.io.InputStream; import java.net.URL; import java.util.Set;

import org.biojava.nbio.core.sequence.ProteinSequence; import org.biojava.nbio.core.sequence.compound.AminoAcidCompound; import org.biojava.nbio.core.sequence.io.FastaReaderHelper;

public class BioSeq { ProteinSequence seq = getSequenceForId ("P31574");

    int polarCount = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("Y", "S", "T", "N", "Q", "C","H","R","D","E","K")));
    double polar = ((double)(polarCount));
    
    int NonPolarCount = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("A","V","L","G","I","M","W","P","F")));
    double NonPolar = ((double)(NonPolarCount));
    
    int Aromatic = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("W","Y","F","H")));
    double AromaticCount = ((double)(Aromatic));
    
    int Aliphatic = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("A","V","L","I","P","G","R")));
    double AliphaticCount = ((double)(Aliphatic));
    
    int Heterocyclics = numberOfOccurrences(seq, new HashSet<>(Arrays.asList("W","H","P")));
    double HeteroCount = ((double)(Heterocyclics));
    
    double rel_polarity = (polar/NonPolar); 
    double rel_arom = (Aromatic/Aliphatic);         
    double rel_alip = (Heterocyclics/Aliphatic);    
    double rel_hetero = (Heterocyclics/Aromatic);   
            
    
    System.out.println("Полярные АМК: " + polar);
    System.out.println("Неполярные АМК: " + NonPolar);
    System.out.println("Ароматичесие АМК: " + AromaticCount);
    System.out.println("Алифатические АМК: " + AliphaticCount);
    System.out.println("Гетероциклические АМК: " + HeteroCount);
    System.out.println("_______________________");
    System.out.println("Relations and Percentages: ");
        
    System.out.println("Relation Polar/No polars: " + String.format("%.2f",rel_polarity));
    System.out.println("Relation Aromatics/Aliphatics: " + String.format("%.2f",rel_arom));
    System.out.println("Relation Heterocyclics/Aliphatics: " + String.format("%.2f",rel_alip));
    System.out.println("Relation Heterocyclics/Aromatic: " + String.format("%.2f",rel_hetero));

try {
        System.out.println(getSequenceForId("P31574"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static int numberOfOccurrences(ProteinSequence seq, Set<String> bases) {
    int count = 0;
    for (AminoAcidCompound aminoAcid : seq)
        if(bases.contains(aminoAcid.getBase()))
            count++;
    return count;
}}
  • 3
    cast your ints to double before calculating: ``(double)Heterocyclics/(double)Aromatic`` – Schokokuchen Bäcker May 10 '22 at 13:40
  • If a and b are integers, a/b uses integer division, producing an integer. It looks like the code started a naming convention to use "Count" as suffix on integer variables. But the code is inconsistent about using that convention. So some of the rel_ are using integer division. – user9712582 May 11 '22 at 15:37
  • The example code is incomplete and cannot compile. It looks like it is missing the 'main' method, and the 'getSequenceForId' method definition, as well as some imports. It so happens that a search finds 'getSequenceForId' in your previous question. https://stackoverflow.com/questions/72155659/calculating-physico-chemical-properties-of-amino-acids-in-biojava – user9712582 May 11 '22 at 15:43

1 Answers1

0

double rel_polarity = ((double)polar/(double)NonPolar)*100;

double rel_arom = ((double)Aromatic/(double)Aliphatic)*100;

double rel_alip = ((double)Heterocyclics/(double)Aliphatic)*100;

double rel_hetero = ((double)Heterocyclics/(double)Aromatic)*100;

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 11 '22 at 05:39