Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computational Biology, Part B C, C++, Java Review Zia Khan Robert F. Murphy Copyright  2001. All rights reserved.

Similar presentations


Presentation on theme: "Computational Biology, Part B C, C++, Java Review Zia Khan Robert F. Murphy Copyright  2001. All rights reserved."— Presentation transcript:

1 Computational Biology, Part B C, C++, Java Review Zia Khan Robert F. Murphy Copyright  2001. All rights reserved.

2 What exactly are we reviewing? Stuff you should know already. C, C++, Java C, C++, Java  Pointers and References  File I/O  String Manupulation  Simple line-by-line parsing Command-line Arguments Command-line Arguments

3 Pointers and References: What do I need to know? C/C++ Pointers C/C++ Pointers  Allocating Memory  Using pointers to struct's and class's  Passing struct's and class's to functions Java References Java References  Allocating Memory  Passing around class's to and from functions

4 Pointers: Allocating Memory C  char *alloc_string = calloc(sizeof(char) * string_length); // clears memory  struct AntProtein *antgut = malloc(sizeof(struct AntProtein));  free(antgut); // to free allocated memory C++ C++  char *alloc_string = new char[string_length];  AntProtein *antgut = new AntProtein;  AntObject *bullet_ant = new AntObject("Fear me I am bullet ant!!!");  delete[] alloc_string; // don't forget the [] when you free arrays  delete antgut;

5 Pointers: Using struct's and class's C: All you can do is access members C: All you can do is access members  struct ANTstruct*ant = malloc(sizeof(ANTstruct));  ant->leg_count = 6; C++: Get Members and Call Member Functions C++: Get Members and Call Member Functions  ANTObject *ant = new AntObject("Bullet ant");  ant->leg_count = 6; // if leg_count is public you can do this  ant->Run();

6 Pointers: Functions It's all about passing a variable that allows direct access to to a point in memory. That means you can change the memory around. It's all about passing a variable that allows direct access to to a point in memory. That means you can change the memory around. Passing pointers to a function is "passing by reference." You want to change what you passed around. Passing pointers to a function is "passing by reference." You want to change what you passed around. If you don't like working too hard, you're probably going to pass around struct's and class's and maybe arrays. I will show you examples on the next slide. If you don't like working too hard, you're probably going to pass around struct's and class's and maybe arrays. I will show you examples on the next slide. A pointer's memory sticks around even if you leave the scope of a function. Deallocate when you've passed it to some other function. A pointer's memory sticks around even if you leave the scope of a function. Deallocate when you've passed it to some other function. Anything can be changed into a pointer by using the ampersand "&pointer" Anything can be changed into a pointer by using the ampersand "&pointer" You really should know about this already. If you totally forgot, USE JAVA instead. If you don't know Java this is a really good time to learn it. You really should know about this already. If you totally forgot, USE JAVA instead. If you don't know Java this is a really good time to learn it. Pointers can and will make you cry. Pointers can and will make you cry.

7 Pointers: Functions (examples) C/C++ Passing Around Arrays C/C++ Passing Around Arrays  void study_ants(int *antspecies){  *(antspecies + northamerica) = toomanytocount;}  void study_ants(int antspecies[]){  Antspecies[northamerica] = toomanytocount;} C Passing Around struct's C Passing Around struct's  int red_ant ( struct AntSTRUCT *ant){  if(ant->color == red) return 1;  Return 0;}

8 Pointers: Functions (examples, continued) C++ Passing Around class's C++ Passing Around class's  void start_foraging(AntObject *ant){  ant->forage();}  AntEgg *Queen(){  return new AmEgg;}  Don't make functions that look like this unless you know what your doingAntEgg &Queen() or AntEgg&Queen() How to cheat and pass by reference in C++!!!! How to cheat and pass by reference in C++!!!!  void cheater_cheater_pumpkin_eater(pumpkin &p){  pumpkin.smashing(); pumpkin.changeme = 1;}

9 References: Allocating Memory Treat references as "placeholders" for java objects. Treat references as "placeholders" for java objects. Ant bullet_ant = new bullet_ant(Ant.FEIRCE); Ant bullet_ant = new bullet_ant(Ant.FEIRCE); int[][] antgrid; int[][] antgrid;  antgrid = new int[10][8]; You don't need to free memory in Java the garbage collector will do it for you. You don't need to free memory in Java the garbage collector will do it for you.

10 References: Functions This will not work the way it should. References have nothing to do with memory addresses. This will not work the way it should. References have nothing to do with memory addresses.  void broken_swap(Object a, Object b){  Object tmp;  tmp = a; a = b; b = tmp; } To return multiple objects. Delcare a new class. To return multiple objects. Delcare a new class.  class ReturnME{ Object a; Object b};  ReturnMe multipleObjects(){  ReturnME me = new ReturnMe();  me.a = new Object(); me.b = new Object();  return me;}

11 File I/O: Opening a File Java Java  import Java.io.*;  BufferedReader file = new BufferedReader(new FileReader("AntDNA.txt")); C  #include  #include  FILE *file = fopen("AntDNA.txt", "r+"); /* open for reading and writing */ C++ C++  #include and #include  #include and #include  fstream file("AntDNA.txt");

12 File I/O: Reading a Line Java Java  String line = file.readLine(); C  Give me a second. It's on the next slide. C++ C++  #include and #include  #include and #include  char line[256]; file.getline(line, 255);

13 File I/O: Reading a Line (in C) int fgetline(FILE *f, char *s, int l){ int i; char ch; for(i = 0; i < l; i++){ ch = (char)fgetc(); if(ch == EOF) return -1; if(ch == '\n') break; s[i] = ch; } return l; }

14 File I/O: Closing a File Java Java  file.close(); C  fclose(file); C++ C++  file.close(); Closing and opening a file repositions the file pointer to the beginning of the file. You shouldn't have to do any fancy file random access. If you do, you're working to hard. Closing and opening a file repositions the file pointer to the beginning of the file. You shouldn't have to do any fancy file random access. If you do, you're working to hard.

15 Strings: Varities C: good old char* C: good old char*  char *str = malloc(sizeof(char) * 31); // 30 character string, dnamically allocated  char str[31];  char *str = "Computational Biology"; // length of string + null characterC C++: the string class C++: the string class  #include  #include  string my_str;  const char *my_old_c_self = my_str.c_str(); // when you need a char* Java: java.lang.String Java: java.lang.String  String my_string = "Use me!";

16 Strings: Manipulation Length Length  C: strlen(str);  C++: my_str.length();  Java: my_sring.length(); Concatination Concatination  C: strcat(str1, str2);  C++: ant = wasp + evolution;  Java: sing = a + song; More Tricks More Tricks  C: "man string" on a Unix machine  C++: http://www.sgi.com/Technology/STL/basic_string.html  Java: Consult the API reference!!  http://java.sun.com/docs

17 Parsing: Line-by-Line Easiest and dirtiest way of parsing a file. Easiest and dirtiest way of parsing a file.  1. Read in a line from a file.  2. Parse the information in that line.  3. If necessary keep reading more lines.

18 Parsing: Control Loop We want to keep reading a file until we reach the end of the file. We want to keep reading a file until we reach the end of the file. C: char line[256]; C: char line[256];  while(!feof(file)){ fgetline(file, line,255);// see slide for getline }; C++: string line; C++: string line;  while(!file.eof()){ line = file.getline(); // do stuff }; Java: String line; Java: String line;  while(true){ try{ line = file.readline(); }  catch(IOException e){ break; } // do stuff };

19 Parsing: Data From A Line Example: "DATA 03510 BLAH 0.325" Example: "DATA 03510 BLAH 0.325" C: #include C: #include  char *tok = strtok(line); // get a token (e.g. DATA, 03510, etc.)  Int n = atoi(tok); // convert token to an integer  float f = atof(tok); // convert token to a float C++: #include C++: #include  string d; int c; string d2; float f;  strstream parseline(line.c_str());  parseline >> d >> c >> d2 >> f;  if(parseline.bad()) return; // error

20 Parsing: Data From A Line (cont) Java: import java.io.*; // you can also use java.util.StringTokenizer Java: import java.io.*; // you can also use java.util.StringTokenizer  String d; int c;  StreamTokenizer parser = new StreamTokenizer(new StringReader(line));  if(parser.nextToken() == StreamTokenizer.TT_WORD)  d = parser.sval;  if(parser.nextToken() == StreamTokenizer.TT_NUMBER) c = (int)parser.nval;c = (int)parser.nval;

21 Parsing: Data From Multiple Lines You need something called a state variable. You need something called a state variable.  This switch statement goes in your control loop.  switch(state_variable){  case PARSING_DATA: break;  case PROTEIN_DATA: break;  case GENE_DATA: break;  } Different parsing code goes in between case and break. Different parsing code goes in between case and break. If you're in the GENE_DATA state you can parse multiple lines and fill a struct or class with gene data that might span multiple lines. If you're in the GENE_DATA state you can parse multiple lines and fill a struct or class with gene data that might span multiple lines.

22 Command-line Arguments C/C++ C/C++  int main(int argc, char *argv[])  Using getopt. This makes things easy, but you're on your own. Type the following on a unix machine.  man 3 getopt Java Java  public static void main (String args[])


Download ppt "Computational Biology, Part B C, C++, Java Review Zia Khan Robert F. Murphy Copyright  2001. All rights reserved."

Similar presentations


Ads by Google