k-nucleotide Pharo Smalltalk program
source code
"* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
unoptimized program contributed long ago by Isaac Gouy *"!
Object subclass: #BenchmarksGame
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'benchmarks game'!
!BenchmarksGame class methodsFor: 'private'!
substringFrequencies: aString for: aLength using: aDictionary
| m |
m := aString size - aLength + 1.
1 to: m do: [:i | | fragment |
fragment := aString copyFrom: i to: i + aLength - 1.
aDictionary at: fragment put:
(aDictionary at: fragment ifAbsent: [0]) + 1
].
^aDictionary!
readFasta: sequenceName from: input
| sc gt lf prefix description buffer byte line |
sc := $; asciiValue.
gt := $> asciiValue.
lf := Character lf asciiValue.
prefix := '>',sequenceName.
"* find start of particular fasta sequence *"
[(input atEnd) or: [
(input peek = gt)
ifTrue: [((line := (input upTo: lf) asString)
findString: prefix) = 1]
ifFalse: [input skipTo: lf. false].
]
] whileFalse.
"* line-by-line read - it would be a lot faster to block read *"
description := line.
buffer := ReadWriteStream on: (String new: 1028).
[(input atEnd) or: [(byte := input peek) = gt]] whileFalse: [
(byte = sc)
ifTrue: [input upTo: lf]
ifFalse: [buffer nextPutAll: (input upTo: lf) asString]
].
^Association key: description value: buffer contents!
knucleotideFrom: input to: output
| sequence writeFrequencies writeCount |
sequence := (self readFasta: 'THREE' from: input) value asUppercase.
writeFrequencies :=
[:k | | frequencies count |
frequencies := SortedCollection sortBlock: [:a :b|
(a value = b value) ifTrue: [b key < a key] ifFalse: [b value < a value]].
count := 0.0.
(self substringFrequencies: sequence for: k using: Dictionary new)
associationsDo: [:each|
frequencies add: each. count := count + each value].
frequencies do: [:each | | percentage |
percentage := (each value / count) * 100.0.
output
nextPutAll: each key; nextPutAll: ' ';
print: percentage digits: 3; nl]].
writeCount := [:nucleotideFragment | | frequencies count |
frequencies := self substringFrequencies: sequence
for: nucleotideFragment size
using: Dictionary new.
count := frequencies at: nucleotideFragment ifAbsent: [0].
output print: count; tab; nextPutAll: nucleotideFragment; nl].
writeFrequencies value: 1. output nl.
writeFrequencies value: 2. output nl.
writeCount value: 'GGT'.
writeCount value: 'GGTA'.
writeCount value: 'GGTATT'.
writeCount value: 'GGTATTTTAATT'.
writeCount value: 'GGTATTTTAATTTATAGT'! !
!BenchmarksGame class methodsFor: 'initialize-release'!
do: n
self knucleotideFrom: Stdio stdin to: Stdio stdout! !
!StdioStream methodsFor: 'benchmarks game'!
tab
self nextPut: Character tab!
nl
self nextPut: Character lf!
print: number digits: decimalPlaces
| precision rounded |
decimalPlaces <= 0 ifTrue: [^ number rounded printString].
precision := (10 raisedTo: decimalPlaces negated) asFloat.
rounded := number roundTo: precision.
self nextPutAll:
((rounded asScaledDecimal: decimalPlaces) printString copyUpTo: $s)! !
!StdioStream instance methodsFor: 'positioning'!
skipTo: anObject
"Set the access position of the receiver to be past the next occurrence of
anObject. Answer whether anObject is found."
[self atEnd]
whileFalse: [self next = anObject ifTrue: [^true]].
^false! !
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
Pharo 12.0.0
Pharo-12.0.0+SNAPSHOT.build.1507.sha.
a4f8da8972214b9c9c39c33e826394a109911041 (64 Bit)
Compiler: 5.4.0 20160609
Wed, 29 May 2024 02:21:59 GMT
MAKE:
cp /opt/src/pharo-vm-Linux-x86_64-stable/Pharo12.0-SNAPSHOT-64bit-a4f8da8.image knucleotide.pharo_run.image
cp /opt/src/pharo-vm-Linux-x86_64-stable/Pharo12.0-SNAPSHOT-64bit-a4f8da8.changes knucleotide.pharo_run.changes
ln -s /opt/src/pharo-vm-Linux-x86_64-stable/Pharo12.0-64bit-a4f8da8.sources .
cat Include/pharo/make.st
| prog |
(SystemWindow windowsIn: World
satisfying: [:w | w model canDiscardEdits])
do: [:w | w delete].
"load program to be measured"
prog := Smalltalk getSystemAttribute: 3.
(prog notNil) ifTrue: [prog asFileReference fileIn].
ImageCleaner cleanUpForRelease.
Smalltalk garbageCollect.
SmalltalkImage current snapshot: true andQuit: true.
/opt/src/pharo-vm-Linux-x86_64-stable/pharo --headless knucleotide.pharo_run.image Include/pharo/make.st knucleotide.pharo
cat Include/pharo/main.st
BenchmarksGame do: (Smalltalk getSystemAttribute: 3) asInteger.!
SmalltalkImage current snapshot: false andQuit: true!
29.14s to complete and log all make actions
COMMAND LINE:
/opt/src/pharo-vm-Linux-x86_64-stable/pharo --headless knucleotide.pharo_run.image Include/pharo/main.st 0 < knucleotide-input25000000.txt
PROGRAM OUTPUT:
A 30.295
T 30.151
C 19.800
G 19.754
AA 9.177
TA 9.132
AT 9.131
TT 9.091
CA 6.002
AC 6.001
AG 5.987
GA 5.984
CT 5.971
TC 5.971
GT 5.957
TG 5.956
CC 3.917
GC 3.911
CG 3.909
GG 3.902
1471758 GGT
446535 GGTA
47336 GGTATT
893 GGTATTTTAATT
893 GGTATTTTAATTTATAGT