// @title SeqReader.ck // @desc Read DNA sequencies // @author Brent Townshend (b@tc.com) // IMPORTANT NOTE: add this class only once or an error will be thrown // to re-shred. In that case, you have to stop and restart Virtual Machine. // HOW TO USE: use this file to declare a public class for reading in a // data file and play the data with a separate "player" file // @class SeqReader // @desc define the SeqReader public class. reads sequencing data and // serves up values expects single column of float or int data public class SeqReader { string inFilename; string buf; string spcl; 0.1 => float spclProb; 0 => int bufptr; 30 => int blockLength; FileIO in; fun int open(string filename) { in.open(filename,FileIO.READ); if( !in.good() ) { <<< "can't open file: ", filename, " for reading">>>; return false; } in.readLine() => spcl; // First line is special return true; } fun int next() { if (!in.good()) { <<<"file not open">>>; return false; } while (true) { if (bufptr int c; bufptr+1=>bufptr; if (c=='A' || c=='a') return 1; else if (c=='C' || c=='c') return 2; else if (c=='G' || c=='g') return 3; else if (c=='T' || c=='t') return 4; // Ignore any other characters } else if (bufptr < blockLength) { bufptr+1=>bufptr; return 0; } if (in.eof()) return -99; if (Math.randomf() < spclProb) spcl => buf; else in.readLine() => buf; 0 => bufptr; } } }