/*this program counts the number of transitions and tranversion occur between two
*genes when two DNA strings from a source file are compared using threading.*/

import java.io.*;
import java.util.*;
import java.lang.*;

public class Gacer021109{
	public static Vector<String> DNA = new Vector<String>();
	public static int cnt=0;
	
	public static void main(String[] args) throws IOException{
	   BufferedReader stndin = new BufferedReader(new InputStreamReader(System.in));        
       
       System.out.print("Enter source file: ");
       String source = stndin.readLine();        
       
       File inFile = new File(source);
       FileReader in = new FileReader(inFile);
       BufferedReader br = new BufferedReader(in);
       Thread1 t1 = new Thread1();
       
       while(true) {
	   	  String s = null;
			 try {	
			 	  s = br.readLine();	
			 }
			 catch (IOException e) {
				 System.out.println("Error reading file");
				 System.exit(1);
			 }
  		 	 if(s == null) break;
             DNA.addElement(s.trim());
       	     cnt++;
		}
		
        in.close();
        t1.start();
	}
	
static class Thread1 extends Thread {
    public static int[] transition = new int[(cnt*(cnt-1)/2)];
    public static int[] transversion = new int[(cnt*(cnt-1)/2)];
    
    public void run() {
    	int n=0,m=0;
    	for(int i=0; i<(cnt*(cnt-1)/2); i++){
    		transition[i] = 0;
    		transversion[i] = 0;
    	
    	}
    	for(int i=0; i<cnt; i++){
    		if(i+1 < cnt){
    			for(int j=(i+1); j<cnt; j++){
    				String str1 = DNA.elementAt(i);
    				String str2 = DNA.elementAt(j);
    			
    				for(int k=0; k<str1.length(); k++){
    					char let1 = str1.charAt(k);
    					char let2 = str2.charAt(k);
    				
    					if(let1 == let2);
           		 		else if(let1=='A'&& let2=='G' || let1=='C'&& let2=='T' || let1=='G'&& let2=='A' || let1=='T'&& let2=='C') 
           		 			 transition[n]++;
            			else transversion[n]++;
    				}
    				n++;
    			}
    		}
    	}
    	for(int i=0; i<cnt; i++){
    		if(i+1 < cnt){
    			for(int j=(i+1); j<cnt; j++){
    				System.out.println("Gene["+(i+1)+"] vs Gene["+(j+1)+"]");
    				System.out.println("Transition: " + transition[m]);
  	 				System.out.println("Transversion: " + transversion[m]);
  	 				m++;
    			}
    		}
    	}
    }
}	
}

ERROR: Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 0
at Gacer021109$Thread1.run(Gacer021109.java:45)

Sample DNA Genes inside the file:
ACGATGCTACTGCTAGCT
GCTAATCGTAGCGATGTC
ACTGATACAGATTAACCG

Output should be:
Gene[1] vs Gene[2]
Transition: 3
transversion: 11

Gene[1] vs Gene[3]
Transition: 3
transversion: 10

Gene[2] vs Gene[3]
Transition: 6
transversion: 7

Recommended Answers

All 6 Replies

I'm not going to count each line to find line 45, but it's most likely your first access of the "transition" array in Thread1. "(cnt*(cnt-1)/2)" is most likely coming up zero and leaving you with zero-length arrays.

How come "(cnt*(cnt-1)/2)" is most likely coming up zero???
"cnt" is the number of strings stored in a file.
When cnt=4, (cnt*(cnt-1)/2)=6.
Thus "(cnt*(cnt-1)/2)" will only be zero if cnt < 2.

And are you certain that "cnt" is greater than zero when those arrays are getting declared?

hmmm.. i think the value of "cnt" is not read in Thread1..
How do i pass the value of cnt in Thread1???

Well, it is "read" in Thread1, because it's the same static "cnt" that you declared in your top level class - but you really should pass it as a parameter. I'd recommend writing a constructor for Thread1 (which you really should rename - Thread1 doesn't really convey anything about what that class is for) which takes that count as a parameter and initializes the arrays to the proper sizes. Make those arrays plain private members. They don't need to be public static.

I'd also recommend adding a println check on the value of "cnt" before starting that thread to verify that it is the value you think that it should be.

it is working properly now..thanks for the help!.. (^-^)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.