Monday, April 28, 2008

Average a bunch of lines (C++)

A Short C++ program to average a bunch of entries on a bunch of lines. I.e.:




1 2 3 4 5 6 END OF LINE

7 8 9 10 11 12 END OF LINE

13 14 15 16 17 18 END OF LINE



Would average 1 7 13. 2 8 and 14 etc...

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc,char **argv) {

ifstream infile(argv[1]);

int count=0;
vector<double> sums(1000,0);
for(;!infile.eof();) {
string instring;
getline(infile,instring);

stringstream ss(instring);

for(int n=0;!ss.eof();n++) {
double cur;
ss >> cur;
sums[n]+= cur;
}
count++;
}

for(vector<double>::iterator i = sums.begin();i != sums.end();i++) {
cout << (*i)/count << " ";
}
cout << endl;

}

print 5th through 40th entries on a line using awk

awk '{n=5; for(n=5;n<40;n++) printf $n " "; printf "\n"}'

Friday, April 25, 2008

grep for a tab character

Did I do this already?

You need to get a tab on to the console. On many consoles press ctrl-V then tab, and a tab will be inserted in to the console.

Wednesday, April 23, 2008

Pull files out of a runfolder and stick them here named by cycle

Won't mean anything to anyone but me probably...

for ((i=1; i<=36; i++)); do for j in ./C$i.1/*; do cp $j ./`basename $j .tif`_$i.tif; done; done


Make a GA style folder:

for ((i=1; i<=36; i++)); do mkdir ./lane1tile1/C$i.1; for j in ./C$i.1/s_1_1_*; do cp $j ./lane1tile1/C$i.1/`basename $j`; done; done

map const accessor

I wanted to access map in a const form. However the default [] accessor doesn't allow you to do that, because it will create a new entry in map if the thing you are looking for doesn't exist. This program describes the problem:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

class MyMapEncap {
public:

map<string,vector<int> > mymap;

const vector<int> &getvec(string id) const {
return mymap[id];
}
};

int main() {
MyMapEncap e;

e.getvec("RAW");

}



The solution is to use the find method which returns an iterator to the thing your looking for in the above example like this:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

class MyMapEncap {
public:

map<string,vector<int> > mymap;

const vector<int> &getvec(string id) const {
return (*mymap.find(id)).second;
}
};

int main() {
MyMapEncap e;

e.getvec("RAW");

}



Sunday, April 20, 2008

striping html

I wanted to download this and covert it to a flat text file, and basic html file. Here's the script to do it, basically runs it though html2text, skips everything up to "ACKNOWLEDGEMENTS" then removes any line with "continue..." at the start. Then strips off the first and last lines (which are blank).

html2text -nobs ch1-a.html | awk '{if(p==1) if($1 != "continue...") print $0; if($1 == "ACKNOWLEDGEMENTS") p=1; n++;}' | sed '1d;$d' >> ch1-a.txt


Here's the complete version which iterates over all the chNUMBER-* files in a directory, creates a single textfile for each chapter then converts that to html:

#!/bin/bash

for ((chapnum=1; chapnum<=24; chapnum++)) do
for i in ./ch$chapnum-*
do
html2text -nobs $i | awk '{if(p==1) if($1 != "continue...") print $0; if($1 == "ACKNOWLEDGEMENTS") p=1; n++;}' | sed '1d;$d' >> $chapnum.txt
done
txt2html $chapnum.txt > $chapnum.html
done

Saturday, April 19, 2008

OM digial back


I've been wanting to play with the idea of a digital conversion for old manual SLRs for a while. I don't know if it's a practical proposition but it's a fun project. The biggest difficulty will probably be getting and interfacing a large enough CCD.



As a first step I'm been playing with cheap Labtec webcams. I bought a bunch of these for something like 3 quid each for ebuyer. They are only 320x240 and 25fps but they'll do for a proof of concept. They also have good Linux support which is a plus.



Here's an image from the webcam:




I'm only interested in the CCD, not the optics so I strip them down:




and wrap them in a bunch of insulating tape so nothing shorts out:






OK, now we need to mod the OM itself. I'm using an OM2 here, it doesn't really matter which OM you use, for our purposes it's really just a mount for the lens. So to start with we need to jam the shutter open. OM cameras have "B" mode. This keeps the shutter open as long as the trigger is depressed. To constantly depress the trigger without hacking the camera I put a screw in the trigger and used a bunch of cable ties to pull it down.







Next mod the camera back.







Remove the back, and the sprung plate that lies against the film.











Next I drilled out the back. I used my Dremmel. Dremmels're really cool for this kind of thing. I used the piller attachment, they call it the "workstation":





When your cutting out a hole of the CCD you better use those goggles!





Final cut out back should look something like this (but better):







Now put it all together:











That's it! It works ok, obviously because the CCD is so small compared to 35mm film you end up imaging only a small part of the full frame. It basically looks like it's zoomed in all the time. Here's an example image:









Next up I'll try a better CCD. I've bought a Phillips SPC900NC for this purpose, it's a really nice webcam (1.3Mega pixel, and says it can do 60fps). The images look really nice...



Ideally I'd like to interface all this to a embedded processor like a gumstix, so rather than being tethered to a PC it's more like a real digital camera.

Monday, April 14, 2008

Dependant names

Using a template argument as a template argument for something else sometimes doesn't work, for example the following code:

#include <iostream>

using namespace std;

template<class _prec=double>
class myclass {
public:

typedef int atype;
};

template<class _prec=double>
class myclass2 {
public:

_prec method() {
myclass<_prec>::atype t = 0;
return t;
}
};

int main() {

myclass2<> m;

}


Gives this compilation error under g++:

templateargasarg.cpp: In member function '_prec myclass2<_prec>::method()':
templateargasarg.cpp:17: error: expected `;' before 't'
templateargasarg.cpp:18: error: 't' was not declared in this scope


It's something to do with dependant names, google it. I don't fully understand it. All I know is that as usual a typename fixes it i.e.:

    myclass<_prec>::atype t = 0;


becomes:

   typename myclass<_prec>::atype t = 0;


and all is well. My comp.lang.c++ thread is here.

Tuesday, April 1, 2008

grep with OR

You need to quote the search string and escape | to be used as an OR operator eg:

grep "Offset\|Stretch\|Alignment" superamazingfile

Count number of A,T,G and Cs in first base of each sequence in a fastq file

awked!

awk '{if(n%4 == 1) print $0;n++;}' s_4_sequence.txt | sort | awk '{first = substr($0,4,1); if(first=="A") as++; if(first=="T") ts++; if(first=="C") cs++; if(first="G") gs++;}END{print as; print ts; print gs; print cs;}'


you don't need the sort, and the fastq file is called s_4_sequence.txt here.