How to read a txt. file in processing (Specifically picking out individual letters)

edited October 2013 in How To...

Hello ladies and gents,

I am trying to read a text file and so far I have got this....

String[] dna;
int index = 0;

void setup() {
  size(500, 500);
  background(0);
  stroke(255);
  frameRate(12);
  dna = loadStrings("dna.txt");
}

But the actual text file contains 4 random letters scrambled up, such as "cccgtacgag"

I am trying to figure out read out specific letter and translate them into a certain number.

Does anyone have any clue on how to do this? Any help would be appreciated

Cheers,

Answers

  • You could split the strings in characters, and then substitute each character with a certain number - if I understood correctly (I guess you want a specific number and not the ASCII value?). So you could pass it into the following function:

    String content; //This string represents your dna variable from the example
    
    //Pass it to a function that substitutes your characters
    
    content = editFileContent( content.toLowerCase() );
    
    String editFileContent(String content) {
      char[] chars = content.toCharArray();
      for (int i = 0; i < chars.length; i++) {
        if ( chars[i] == 'a')
          chars[i] = '0';
      }
      content = new String(chars);
      return content;
    }
    
    //Content then has the characters replaced and you can continue for processing
    
  • Hey thanks so much for the reply. I really new at this so I'm not exactly sure when to put all of this in my code because it keeps saying cannot find 'content'

    Also just before your reply, I found a function called byte, could that also be used?

  • Characters in Java are not bytes, they are of type char.

  • The "content" is a variable name that actually should be replaced with your dna variable. This function I posted takes a large string i.e. your text file, and creates a char array (separate characters from strings). Then it performs the check to replace whatever you want. The result should return to your string variable dna (or content in my example). If you print the string after the function you will see what I mean. If you are new at this, have a look at this tut: http://processing.org/tutorials/text/

  • Ok i gotcha, yea I've read and re-read that page and slowly getting it. I'm having a hard time knowing in where to put the code you sent me. (If I should put it above void setup, or in it)

  • edited October 2013

    .

  • edited October 2013

    Is this possible?

     String[] lines;
    
    
        String data;
        int i=0; 
        char letter;
        int a;
        int c;
        int g;
        int t;
        void setup(){
          size(500,500);
    
           background(0);
    
          lines = loadStrings("dna.txt");
          data = lines[0]; 
          frameRate(30);
        }
    
        void draw(){
          if ( frameCount % 3 == 0 ) {
             letter = data.charAt(i); 
             println(letter);
             if (i < data.length()) i++; 
           }
    
           if(letter == 'A'){
           a = 2;
           if (i < data.length()) i++;
        }
    
         if(letter == 'C'){
            c = 3;
            if (i < data.length()) i++;
        }
    
         if(letter == 'G'){
            g = 4;
            if (i < data.length()) i++;
        }
          if(letter == 'T'){
            t = 5;
            if (i < data.length()) i++;
          }
        }
    
  • edited October 2013

    You should take a reading at Processing's reference about the Array structure:
    http://processing.org/reference/Array.html

    For there's no method called length() for it. Rather, we gotta use its field length! 8-X
    Didn't pay attention data was a String already! b-(

     lines = loadStrings("dna.txt");
     data = lines[0]; 
    

    Hmm... Just to make sure -> Does your "dna.txt" file consist of solely of 1 line?
    Be aware that if it happens to exist more lines than justlines[0]within that file, you're ignoring and discarding them all! (~~)

    Another observation is about those 4 variables -> a, c, g, t.
    I can't figure out what they're for besides being assigned values 2, 3, 4, 5 respectively!? :-<

  • I did some changing up and I have instead tried to make it so each letter makes a different sound, although I cannot get it to keep looping. It will make the first sound then will stop and not play the other letters, any ideas?

    import ddf.minim.*;
    
    
    Minim minim;
    
    AudioPlayer bass2;
    AudioPlayer bass3;
    AudioPlayer bass4;
    AudioPlayer bass5;
    
    String[] lines;
    
    
    String data;
    int i=0; 
    char letter;
    int a;
    int c;
    int g;
    int t;
    void setup(){
      size(500,500);
    
    
       background(0);
    
      lines = loadStrings("dna.txt");
      data = lines[0]; 
      frameRate(10);
    
      minim = new Minim(this);
    bass2 = minim.loadFile("beep-2.mp3");
    bass3 = minim.loadFile("beep-3.mp3");
    bass4 = minim.loadFile("beep-4.mp3");
    bass5 = minim.loadFile("beep-5.mp3");
    }
    
    void draw(){
    
    
      if ( frameCount % 3 == 0 ) {
         letter = data.charAt(i); 
         println(letter);
         if (i < data.length()) i++; 
       }
    
       if(letter == 'A'){
       bass2.play() ;
       stroke(255,0,0);
       noFill();
       ellipse(100,100,100,100);
       if (i < data.length()) i++;
    
    }
    
     if(letter == 'C'){
      bass3.play() ;
         stroke(0,0,255);
       noFill();
       ellipse(400,100,100,100);
        if (i < data.length()) i++;
    }
    
     if(letter == 'G'){
      bass4.play() ;
        stroke(0,255,0);
       noFill();
       ellipse(400,400,100,100);
        if (i < data.length()) i++;
    }
      if(letter == 'T'){
       bass5.play() ;
        stroke(255);
       noFill();
       ellipse(100,400,100,100);
        if (i < data.length()) i++;
      }
    }
    
  • Concerning the code I posted you will have to put the String editFileContent function by its own - outside setup or draw. So when you call the following line from setup, it will run the function and return the result for post-processing.

    content = editFileContent( content.toLowerCase() );

    In the code you posted, I find odd that you create an index that constantly adds to itself, you will get an error soon. Why not try to use a while or for loop to define your index count? Also, bear in mind that when you read your text file contents it only happens once in the setup function.

    To have a loop of sounds based on your letters that are inside your text, why not try to use a delay between them? Use the println function to see what is coming out from your if statements and what letter is being validated as true. Also do the following - if you are not sure if the letter is capitalized, or use the content.toLowerCase() as in my example (where content is the String)

    if(letter == 'G' || letter == 'g' ){

Sign In or Register to comment.