Ed's Big Plans

Computing for Science and Awesome

General Anesthesia – Risk for Women, Canada.

without comments

Update: Please see this post written the next day after looking into the matter a bit more. It’s unfortunate that the article in The Globe and Mail below had such an alarmist slant to it.

Update: Continuing to research issue– see a list of teaching hospitals in Canada: http://www.caho-hospitals.com/member_hospitals.aspx — I need to find out more: Is it true? How widespread is the problem; etc.

Sent from me to many friends via e-mail with BCC earlier today.

Hi All,

This story is deeply disturbing. Women who are under general
anesthetic in hospital recovery rooms can be subject to routine
examination by med students without consent in Canada. This is a
strict violation of everything a scientist would believe with respect
to human health and dignity.

http://www.theglobeandmail.com/life/health/time-to-end-pelvic-exams-done-without-consent/article1447337/

Please have a look and figure out if what action you’d like to take.
Spreading the word, figuring out who knows what and the extent of the
problem are good steps (self-education, personal preventative measures
etc.).

Conversely, if you can verify that this is a myth– that can also be
good (puts minds at ease).

At the very least, bringing up a petition or a stronger public
presence of the problem are both possible.

Thanks,
Eddie Ma

Written by Eddie Ma

January 29th, 2010 at 4:52 pm

Generic Functions in C# and Java

without comments

>>> Attached: ( Main.java — in Java | Main.cs — in C# ) <<<

Updated: (1) Made code more readable. (2) Removed unnecessary package (Java) and namespace (C#) and added a function that returns a generic type as well. (3) Attached compilable demo source code in separate files.

The most fun and productive concept in object oriented programming is generics — for me anyway. In C, one could deploy generics hazardously with code that casts the contents of memory addresses with a putative struct. The first field gives away what that chunk of memory is supposed to be at run time (usually, it’s a typdef int or an enum). I still do that when it’s called for, but it’s quite delicate and often leads to insidious bugs that don’t crash immediately. At least one would know what code to suspect when crashes do happen.

In C# and Java, two languages that derive from C — we find full safe support of generics. Generic classes (the things that collections are made of) are interesting, and I’m sure most who have used either of these languages have already played with them and have found them useful. One of the things that don’t receive a healthy dose of spotlight is Generic Functions (“Generic Methods” if you like).

I’ll compare two segments of code, one in C# and one in Java that do exactly the same thing — demonstrate two trivial functions printArrayList() and getElement(). The function printArrayList() prints out the contents of an ArrayList (Java) or a List (C#). The function getElement() retrieves an element from a list. This shows how single generic functions can operate on collections, each with a different defined type without the need for unsafe casting. The only assumption the code makes is that each object in a list implements the toString() method (needed for the printing function).

Note naming convention: In Java, methods are just members of an object, so they are named in lowercase. In C#, methods are capitalized. We will refer to methods by the Java convention here to keep things consistent.

Setting Up in Main…

Let’s declare and fill a few lists for this demonstration. Three generic list objects, cow, dog and elephant are constructed in a for loop. Each gets ten elements. Each list contains objects of a particular type; cow contains integers, dog contains doubles and elephant contains strings.

Java Code C# Code
ArrayList<Integer> cow = new ArrayList();
ArrayList<Double> dog = new ArrayList();
ArrayList<String> elephant = new ArrayList();
List<int> cow = new List();
List<double> dog = new List();
List<string> elephant = new List();

Notice that Java does not autobox the type in the angel brackets so you can’t give it the primitives int and double. In C#, this is allowed plus string is also a primitive. Remember: In both Java and C#, primitives are emulated — they are first class objects that are only different from other objects in that they are pass-by-value rather than pass-by-reference.

Appending ten items to each list. Shown below is the Java version — in C#, change “add()” to “Add()”.

for(int i = 0; i < 10; i ++) {
    cow.add(3 * i);
    dog.add(0.25 * i);
    if(i % 2 == 0)
        elephant.add("Even");
    else
        elephant.add("Odd");
}

The below is the code we want to make work — We’ll call printArrayList() to print out all of the elements in each list, then we’ll call getElement() to return a specific element from each list. Notice that this is the Java version below — in C#, we capitalize method names and use Console.Writeline() instead of System.out.println().

System.out.println("== Generic List Printer ==");

printArrayList(cow);
printArrayList(dog);
printArrayList(elephant);

System.out.println();

System.out.println("== Generic Element Accessing ==");

int    cow_at_7      = getElement(cow, 7);
double dog_at_2      = getElement(dog, 2);
String elephant_at_4 = getElement(elephant, 4);

System.out.println("Cow at 7      = " + cow_at_7);
System.out.println("Dog at 2      = " + dog_at_2);
System.out.println("Elephant at 4 = " + elephant_at_4);

Note that in C#, we may use the keyword “var” instead of typing out the types for cow_at_7, dog_at_2, and elephant_at_4 — the compiler infers the type for us. This is different from unsafely casting with “Object”, as the compiler infers the narrowest possible type and substitutes in that correct type.

Onto the methods …

Below is the Java version of printArrayList().

static <A> void printArrayList(ArrayList<A> animalList) {
    for(A a : animalList)
        System.out.print(a + "\t");
    System.out.println();
}

Below is the C# version of PrintArrayList().

static void PrintArrayList<A>(List<A> animalList) {
    foreach(A a in animalList)
        Console.Write(a + "\t");
    Console.WriteLine();
}

Notice that printArrayList() is a method that specifies a generic type <A>, but only in its argument list. In Java, <A> appears before the function’s type and in C#, this appears after the function name. In this case, it’s obvious what we would do if we have functions that return specific types — we just substitute the type where the keyword “void” is. So what happens when we want to return the generic type? That’s what getElement() will demonstrate.

Below is the Java version of getElement().

static <A> A getElement(ArrayList<A> what, int which) {
    return what.get(which);

Below is the C# version of GetElement().

static A GetElement<A>(List<A> what, int which) {
    return what[which];

Yes, these are both trivial functions, as you could have easily called ArrayList.get() in Java and List[] in C# respectively — but it does the job in this demonstration. In the Java version, the generic type <A> is placed before the type of the function, A. Don’t let that confuse you, just recall how we specified the return type when it wasn’t the generic type. In C#, we place the generic type <A> after the function name just as before.

Below is the output you should expect if you run the main function.

C# Output

== Generic List Printer ==
0	3	6	9	12	15	18	21	24	27
0	0.25	0.5	0.75	1	1.25	1.5	1.75	2	2.25
Even	Odd	Even	Odd	Even	Odd	Even	Odd	Even	Odd	

== Generic Element Accessing ==
Cow at 7      = 21
Dog at 2      = 0.5
Elephant at 4 = Even

The Java output is the same, except the values that are doubles are always printed with a trailing “.0″ even if it is numerically equal to an integer.

Written by Eddie Ma

January 29th, 2010 at 11:41 am

First UWiGEM modeling meeting: Wed. Jan. 21 @6pm

without comments

Update: The Gleave Library, B1-273 has been booked for this occasion!

Brief: The two software projects that will precede the main body of Waterloo iGEM 2010 will be discussed on Wednesday January 21 from 6pm to 8pm. I will update this post once a room booking has been confirmed.

Related topic here on the Waterloo iGEM Discussion Board.

Written by Eddie Ma

January 25th, 2010 at 8:06 pm

Fight e-mail address typos with MD5 hashes :P

without comments

Silly: One day, Dave messaged me and said that his e-mails weren’t getting through to me. I was puzzled as he was using the g-talk service, meaning he has my valid g-mail handle. As it turns out, the device he was on didn’t have copy and paste available for that field, and he simply couldn’t see the ‘t’ in my address.

Incidentally, that’s one dysfunction of the human visual system I’d like to purge– reading something over and over, only to make exactly the same lexicographical mistake each time. It’s an evil broken heuristic… remember kids, it’s a ‘feature’, not a ‘bug’.

I then messaged him the MD5 hash of my e-mail address along with the one for the incorrect address…

Correct:   2f776881db0ca037c145e74a6c41721c
Incorrect: 35c56bee61e9914929f4a3242d44c339

Don’t worry, there’s no practical value in doing this :P

Note: to get the MD5 of an arbitrary string on a *nix system, just type this in the terminal…

echo "arbitrary string" | MD5

It almost amuses me as much as catting arbitrary binary files…

Written by Eddie Ma

January 22nd, 2010 at 9:54 am

Posted in Silly

Tagged with , , ,

fsMSA Algorithm Context

without comments

What started as a meeting between me and my advisors ended up being a ball of unresolved questions about the cultural context of multiple sequence alignment and phylogenetic trees. While I had a good idea of what the field and its researchers had looked into and developed, I hadn’t a grasp of how far along we were. The result is the presentation I’ve just finished. In it, I discuss what I consider to be a representative sampling of the alignment and phylogenetic tree building algorithms available right now, at this very instant.

(PDF not posted, contact me if interested.)

Written by Eddie Ma

January 13th, 2010 at 12:12 pm

Python Crash Course – Lesson 1

without comments

The first lecture of my Python Crash Course went really well! I ran it two evenings ago in the Dean’s Conference Room.

In gearing the very first lecture for absolute beginners, I had very little to cater to BIC (Bioinformatics Club) members. I however took the opportunity to discuss with them about the SOLVER group (more on that later); many of which seemed interested.

Overall there were roughly a dozen people that turned out, including Ariana, my TA partner from last term. There were about four iGEM members and six BIC members.

I also took the opportunity to poll for the kinds of things that students wanted to learn. Here are my findings.

  • Object Orientation is something everyone wants to know– especially the people coming in with a Javascript, PERL, C, C++ and Scheme background; I was surprised that the C++ people didn’t get exposure to thinking in objects earlier.
  • The beginners came in two groups. First, there are the ones who are happy to learn anything as long as it can be applied later.
  • The second group of beginners want to data crunch PDBs, SDFs, FASTAs, Nucleotides etc.

In week two, we’ll take care of object orientation and in week three, we’ll take care of everything anyone ever needs to know about input output in order to do data crunching. I have added a link in the navigation of this blog for the Python Crash Courseware which will eventually include all the PDFs, code modules and examples used in class.

Oh right, I don’t know if I’ll get around to it– but I am missing instructions for setting environment variables in Windows. Perhaps I will add it later when I have time.

(iGEM attendees were John Heil, Danielle Nash, Tiffany and Lina; BIC members included Fiona, James and about four others whose names I have forgotten.)

Edit: Direct link to Python Crash Courseware; Direct link to Week 1: A Mad Mad Introduction, PDF.

Written by Eddie Ma

January 9th, 2010 at 9:00 pm