The Computer Language
Benchmarks Game

k-nucleotide C# Mono LLVM #2 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Isaac Gouy
 * modified by Antti Lankila for generics
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

public class program {
    public static void Main(string[] args) {
	string line;
	StreamReader source = new StreamReader(Console.OpenStandardInput());
	StringBuilder input = new StringBuilder();

	while ( (line = source.ReadLine() ) != null ) {
	    if (line[0] == '>' && line.Substring(1, 5) == "THREE")
		break;
	}
	 
	while ( (line = source.ReadLine()) != null ) {
            char c = line[0];
            if (c == '>')
               break;
            if (c != ';')
               input.Append(line.ToUpper());
	}

	KNucleotide kn = new KNucleotide(input.ToString());
        input = null;
	kn.WriteFrequencies(1);
	kn.WriteFrequencies(2);

	kn.WriteCount("GGT");
	kn.WriteCount("GGTA");
	kn.WriteCount("GGTATT");
	kn.WriteCount("GGTATTTTAATT");
	kn.WriteCount("GGTATTTTAATTTATAGT");
    }
}

public class KNucleotide {
    /* freq[foo] ++ implies a get and a set. */
    internal class Value {
	internal int v;

	internal Value(int v)
	{
	    this.v = v;
	}
    }

    private Dictionary<string, Value> frequencies = new Dictionary<string, Value>();
    private string sequence;

    public KNucleotide(string s)
    {
	sequence = s;
    }

    public void WriteFrequencies(int nucleotideLength) {
	GenerateFrequencies(nucleotideLength);

	List<KeyValuePair<string, Value>> items = new List<KeyValuePair<string, Value>>(frequencies);
	items.Sort(SortByFrequencyAndCode);

	int sum = sequence.Length - nucleotideLength + 1;
	foreach (KeyValuePair<string, Value> each in items) {
	    double percent = each.Value.v * 100.0 / sum;
	    Console.WriteLine("{0} {1:f3}", each.Key, percent);
	}
	Console.WriteLine("");
    }

    public void WriteCount(string nucleotideFragment) {
	GenerateFrequencies(nucleotideFragment.Length);

	int count = 0;
	if (frequencies.ContainsKey(nucleotideFragment))
	    count = frequencies[nucleotideFragment].v;
	Console.WriteLine("{0}\t{1}", count, nucleotideFragment);
    }

    private void GenerateFrequencies(int length) {
	frequencies.Clear();
	for (int frame = 0; frame < length; frame++)
	    KFrequency(frame, length);
    }

    private void KFrequency(int readingFrame, int k) {
	int n = sequence.Length - k + 1;
	/* string.Substring is a malloc monster :( */
	if (k > 6) {
	    for (int i = readingFrame; i < n; i += k) {
		string knucleo = sequence.Substring(i, k);
		if (frequencies.ContainsKey(knucleo))
		    frequencies[knucleo].v ++;
		else
		    frequencies[knucleo] = new Value(1);
	    }
	} else {
	    for (int i = readingFrame; i < n; i += k) {
		string knucleo = sequence.Substring(i, k);
		try {
		    frequencies[knucleo].v ++;
		}
		catch (KeyNotFoundException) {
		    frequencies[knucleo] = new Value(1);
		}
	    }
	}
    }

    int SortByFrequencyAndCode(KeyValuePair<string, Value> item1, KeyValuePair<string, Value> item2) {
	int comparison = item2.Value.v.CompareTo(item1.Value.v);
	if (comparison == 0) return item1.Key.CompareTo(item2.Key);
	else return comparison;
    }
}
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Mono JIT compiler version 4.5.1 (master/3e844dd Fri May  6 19:24:07 PDT 2016)
	LLVM:          yes(3.6.0svn-mono-master/9f79399)
	GC:            sgen



Tue, 28 Jun 2016 19:04:19 GMT

MAKE:
mv knucleotide.csharpllvm-2.csharpllvm knucleotide.csharpllvm-2.cs
/usr/local/bin/mcs -unsafe+ -optimize+ -platform:x86 -out:knucleotide.csharpllvm-2.csharpllvm_run knucleotide.csharpllvm-2.cs
rm knucleotide.csharpllvm-2.cs
0.26s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm --gc=sgen knucleotide.csharpllvm-2.csharpllvm_run 0 < knucleotide-input250000.txt

PROGRAM FAILED 


PROGRAM OUTPUT:

Stacktrace:


Native stacktrace:

	/usr/local/bin/mono() [0x82080e0]
	/usr/local/bin/mono() [0x8266064]
	/usr/local/bin/mono() [0x817a319]
	[0xb76e2c20]
	/usr/local/bin/mono() [0x821bc0d]
	/usr/local/bin/mono() [0x8265a7c]
	/usr/local/bin/mono() [0x8205542]
	/usr/local/bin/mono() [0x8207057]
	/usr/local/bin/mono() [0x826485c]
	[0xb76cdc57]
	/usr/local/lib/mono/4.5/mscorlib.dll.so(+0x8ae21) [0xb50dfe21]
	[0xb7074464]

Debug info from gdb:


=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================