Bioinformatics/base count: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Wren-trait -> Wren-iterate)
Line 1,372: Line 1,372:
Total = 500</syntaxhighlight>
Total = 500</syntaxhighlight>
=={{header|Java}}==
=={{header|Java}}==
<p>
This can be quickly achieved using a <kbd>for-loop</kbd> and the <code>String.toCharArray</code> method.<br />
Additionally, use a <code>BufferedReader</code> to utilize the <code>readLine</code> method.
</p>
<p>
To "pretty print" the output, we can use a <code>String</code> formatter.
</p>
<syntaxhighlight lang="java">
void printBaseCount(String string) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(string));
int index = 0;
String sequence;
int A = 0, C = 0, G = 0, T = 0;
int a, c, g, t;
while ((sequence = reader.readLine()) != null) {
System.out.printf("%d %s ", index++, sequence);
a = c = g = t = 0;
for (char base : sequence.toCharArray()) {
switch (base) {
case 'A' -> {
A++;
a++;
}
case 'C' -> {
C++;
c++;
}
case 'G' -> {
G++;
g++;
}
case 'T' -> {
T++;
t++;
}
}
}
System.out.printf("[A %2d, C %2d, G %2d, T %2d]%n", a, c, g, t);
}
reader.close();
int total = A + C + G + T;
System.out.printf("%nTotal of %d bases%n", total);
System.out.printf("A %3d (%.2f%%)%n", A, ((double) A / total) * 100);
System.out.printf("C %3d (%.2f%%)%n", C, ((double) C / total) * 100);
System.out.printf("G %3d (%.2f%%)%n", G, ((double) G / total) * 100);
System.out.printf("T %3d (%.2f%%)%n", T, ((double) T / total) * 100);
}
</syntaxhighlight>
<pre>
0 CGTAAAAAATTACAACGTCCTTTGGCTATCTCTTAAACTCCTGCTAAATG [A 16, C 12, G 6, T 16]
1 CTCGTGCTTTCCAATTATGTAAGCGTTCCGAGACGGGGTGGTCGATTCTG [A 8, C 11, G 15, T 16]
2 AGGACAAAGGTCAAGATGGAGCGCATCGAACGCAATAAGGATCATTTGAT [A 19, C 8, G 14, T 9]
3 GGGACGTTTCGTCGACAAAGTCTTGTTTCGAGAGTAACGGCTACCGTCTT [A 10, C 11, G 14, T 15]
4 CGATTCTGCTTATAACACTATGTTCTTATGAAATGGATGTTCTGAGTTGG [A 12, C 7, G 11, T 20]
5 TCAGTCCCAATGTGCGGGGTTTCTTTTAGTACGTCGGGAGTGGTATTATA [A 9, C 8, G 15, T 18]
6 TTTAATTTTTCTATATAGCGATCTGTATTTAAGCAATTCATTTAGGTTAT [A 14, C 5, G 6, T 25]
7 CGCCGCGATGCTCGGTTCGGACCGCCAAGCATCTGGCTCCACTGCTAGTG [A 7, C 18, G 15, T 10]
8 TCCTAAATTTGAATGGCAAACACAAATAAGATTTAGCAATTCGTGTAGAC [A 20, C 8, G 8, T 14]
9 GACCGGGGACTTGCATGATGGGAGCAGCTTTGTTAAACTACGAACGTAAT [A 14, C 9, G 15, T 12]

Total of 500 bases
A 129 (25.80%)
C 97 (19.40%)
G 119 (23.80%)
T 155 (31.00%)
</pre>
<br />
Alternately<br />
For counting the bases, we simply use a <code>HashMap</code>, and then use the <code>Map.merge</code>, inserting <code>1</code>, and using <code>Integer::sum</code> as the aggregation function. This effectively creates a <code>Map</code> that keeps a running count for us. Java ''does'' provide the <code>groupingBy</code> and <code>counting</code> collectors, which would ''generally'' make these kinds of operation easier. However, <code>String</code>’s <code>chars()</code> method returns a <code>IntStream</code>, which generally just makes everything more complicated. Or verbose. Or inefficient. Ultimately, doing it by hand is easier and more efficient than with streams. The best tool for this job though would be Guava’s <code>MultiSet</code>, which is a dedicated Key to Count container.
For counting the bases, we simply use a <code>HashMap</code>, and then use the <code>Map.merge</code>, inserting <code>1</code>, and using <code>Integer::sum</code> as the aggregation function. This effectively creates a <code>Map</code> that keeps a running count for us. Java ''does'' provide the <code>groupingBy</code> and <code>counting</code> collectors, which would ''generally'' make these kinds of operation easier. However, <code>String</code>’s <code>chars()</code> method returns a <code>IntStream</code>, which generally just makes everything more complicated. Or verbose. Or inefficient. Ultimately, doing it by hand is easier and more efficient than with streams. The best tool for this job though would be Guava’s <code>MultiSet</code>, which is a dedicated Key to Count container.


Line 1,445: Line 1,513:
SUM: 500
SUM: 500
</pre>
</pre>

=={{header|JavaScript}}==
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">const rowLength = 50;
<syntaxhighlight lang="javascript">const rowLength = 50;