views:

282

answers:

4

I have the following object :

class Repeat{
    private long startIndex;
    private long endIndex;
    private int length;
    private float repetitions;
    private float period;
    private int errors;
    private float percentOverlap;

    public void setPercentOverlap(float percentOverlap) {
       this.percentOverlap = percentOverlap;
    }

    public float getPercentOverlap() {
       return percentOverlap;
    }
    .
    . other sets gets etc.
    .
}

When i set the percentOverlap and add the Repeat to

ArrayList<Repeat> overlaps = new ArrayList<Repeat>(); 
overlaps.add(repeat);

Then when i dump this collection to a csv file. I m getting 0.0 for some of the values but not all of them. ie. 6.25 becomes 0.0. I even see this on command line.

Here is the console output:
-> before i add
->Start Index: 570433 End Index: 570465 Overlap :100.0
->Start Index: 570433 End Index: 570465 Overlap :6.25
->Start Index: 570433 End Index: 570465 Overlap :0.0
->Start Index: 570470 End Index: 570510 Overlap :85.0
->Start Index: 570470 End Index: 570510 Overlap :100.0

When i iterate the collection, this is what comes out.
Start Index: 570433 End Index: 570465 Overlap :0.0
Start Index: 570433 End Index: 570465 Overlap :0.0
Start Index: 570433 End Index: 570465 Overlap :0.0
Start Index: 570470 End Index: 570510 Overlap :100.0
Start Index: 570470 End Index: 570510 Overlap :100.0

I took out file writing, just printing to console.

Why is this happening?

Any ideas?

Thanks.

+1  A: 

From the output, a rough guess is that you add the same object to the ArrayList several times, when you should have added a new instance. The code shown looks ok, so the error is elsewhere.

nos
Thanks for the answer, but there are multiple repeats that has the same beginning and end. So this is not really what i m looking for.I m printing the repeat before adding to the list and after adding to the list. percentOverlaps are different.
Yes, but what nos said could easily apply - since you did not give the code. Perhaps you create a single instance of Repeat and then set its properties and print that in a loop - each pass through the loop will show the values you set. However, you add this instance to the list multiple times - the list only holds a reference to the object, not the values of the object. Thus, when you change a previously added object all the instances in the list appear to change (because they really are the same one). It appears you create a new instance for every unique end-index.
Kevin Brock
+1  A: 

Looks like Start Index and End Index of below entries are same.

->Start Index: 570433 End Index: 570465 Overlap :100.0
->Start Index: 570433 End Index: 570465 Overlap :6.25
->Start Index: 570433 End Index: 570465 Overlap :0.0

->Start Index: 570470 End Index: 570510 Overlap :85.0
->Start Index: 570470 End Index: 570510 Overlap :100.0 

.

Then in output, Overlap value (0.0, 100.0) is repeated for same Start Index and End Index. Which implies same object is reused for given Start Index and End Index. Hence values of all previous Repeat objects is repeated. With new Start Index and End Index, new Repeat object is created.

Please check the code logic where Repeat object instance are created.

Gladwin Burboz
A: 
for(Repeat repeat1 : collection1){

                // length of repeat from first collection input
                long length1 = repeat1.getEndIndex() - repeat1.getStartIndex();

                float percentOverlap;
                long overlapamount = 0;

                // For each repeat in collection2
                HERE:for(Repeat repeat2 : collection2){


                    // length of repeat from second collection input
                    long length2 = repeat2.getEndIndex() - repeat2.getStartIndex();

                    // if this condition fails, no reason to iterate any further.
                    // This is the feature that c# lacks. It saves quite a processing time. 
                    if (repeat1.getStartIndex() > repeat2.getEndIndex() || repeat1.getEndIndex() < repeat2.getStartIndex())
                    {
                         continue HERE;
                    }

                    if (repeat1.getStartIndex() <= repeat2.getStartIndex() && repeat2.getStartIndex() <= repeat1.getEndIndex())
                    {
                        // if repeat1 start index is less than or equal to repeat 2 start index
                        // and if repeat2 start index is less than or equal to repeat1 end index.
                        // 1,3 - 2,4
                        overlapamount = repeat1.getEndIndex() - repeat2.getStartIndex();
                    }
                    else if (repeat2.getStartIndex() <= repeat1.getStartIndex() && repeat1.getEndIndex() <= repeat2.getEndIndex())
                    {
                        // if repeat 2 start index is less than or equal to repeat 1 start index
                        // and if repeat 1 end index is less than or equal to repeat 2 end index
                        // 2,3 - 1,4
                        overlapamount = repeat1.getEndIndex() - repeat1.getStartIndex();
                    }
                    else if (repeat2.getStartIndex() <= repeat1.getStartIndex() && repeat2.getEndIndex() <= repeat1.getEndIndex())
                    {
                        // if repeat 2 start index is less than or equal to repeat1 start index
                        // and repeat 2 end index is less than or equal to repeat 1 end index
                        //
                        // 2,4 - 1,3
                        overlapamount = repeat2.getEndIndex() - repeat1.getStartIndex();
                    }
                    else if (repeat1.getStartIndex() <= repeat2.getStartIndex() && repeat2.getEndIndex() <= repeat1.getEndIndex())
                    {
                        // repeat 1 start index is less than or equal to repeat 2 start index
                        // and repeat 2 end index is less than or equal to repeat 1 end index
                        // 
                        // 1,4 - 2,3
                        overlapamount = repeat2.getEndIndex() - repeat2.getStartIndex();
                    } 

                    // Finds the overlapping percentage
                    percentOverlap = (float) (Math.max((float)((float)overlapamount /(float)length1), (float)((float)overlapamount /(float)length2)) * 100);
                    System.out.println(percentOverlap);
                    repeat1.setPercentOverlap(percentOverlap);

                    // Populates collection by percentage
                    overlaps.add(repeat1);
                    System.out.println("->"+repeat1.toString());
                }

            }

         for(Repeat x : overlaps){
                System.out.println(x.toString());
         }

    }

Here is the code. That s how the data is. This is related to comparing DNA scanning algorithms between two approach and finding overlapping etc.

If you look at the foreach loop at the end, this is where i m getting all the invalid values. If you look at the print statement inside the inner loop, the values are legit.
As mentioned above, you're messing up with instances. You are manipulating the same instance several time. It's not clear from the little code shown what the fix is, but think about it. You're picking one object from collection1, you're manipulating that one object several times in the inner foreach loop. You should create a new instance and add that to 'overlaps' Not add the same repeat1 object several times..
nos
+2  A: 

Everyone is telling you the right answer: you are adding the same object to the collection more than once.

Your outer loop makes a new Repeat repeat1. Your inner loop sets values in this object and adds it to the collection once on each iteration.

Even though you set different values in repeat1 for each inner iteration, it is still the same object.

This is why you are getting the results you show. Your collection looks something like this:

1: First repeat1 2: First repeat1 3: First repeat1 4: Second repeat1 5: Second repeat1

etc.

Skip Head