Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi there,
I have a program that is filling List<float> c_dat and t_dat with 2, 4, or 6 numbers at a time. Then the next step does some math to figure out variance and keep track of things by appending them. The problem is that the qC[0] item is quite often 0 and this is causing me a lot of issues later on in the code trying to do more calculations. I'm no python expert - can I get some guidance on the following code snippet?

if C_count >= 0 and T_count >= 0:
	for j in range(len(C_dat)/2):
		#print dip_dat[2*j],dip_dat[2*j+1]
		m = C_dat[2*j]+C_dat[2*j+1]
		qC[0]+= float(C_dat[2*j])	# ref allele freq						
		qC[1]+= float(m)
	for j in range(len(T_dat)/2):
		m = T_dat[2*j]+T_dat[2*j+1]
		qT[0]+= float(T_dat[2*j])	# ref allele freq						
		qT[1]+= float(m)

	#print qT[1],qC[1]						
	if (qT[1] >= Min_reads and qC[1] >= Min_reads):
# Here I am outputting a standardized data format
# columns: scaffold, position, ref allele count 1, total count 1,ref allele count 2, total count 2
		outcomes[2]+=1#print "here"

		var_C = 1.0/qC[1]
		var_T = 1.0/qT[1]

What I have tried:

I've tried making double and float variables that just get appended to such as qC and qC_m and I've tried arraylists and lists but the results are the same.

Here's my current version but it's not working out...
if (C_count >= 0 && T_count >= 0)

        double[] qC = { 0.0, 0.0 };
        double[] qT = { 0.0, 0.0 };

        var range1 = Enumerable.Range(0, (C_dat.Count / 2));
        foreach (int i in range1)
            double m = Convert.ToDouble(C_dat[2 * i]) + Convert.ToDouble(C_dat[2 * i + 1]);
            //qC[0] is fairly consistently 0
            qC[0] = qC[0] + Convert.ToDouble(C_dat[2 * i]);
            if (C_dat.Count == 2 && qC[0] == 0)
                Console.WriteLine(" C_dat count = " + C_dat.Count + " referencing C_dat[2*i] or C_dat " + 2 * i + " item = " + C_dat[2 * i]);
            qC[1] = qC[1] + m;

        var range2 = Enumerable.Range(0,(T_dat.Count/2));
        foreach (int i in range2)
            double m = Convert.ToDouble(T_dat[2 * i]) + Convert.ToDouble(T_dat[2 * i + 1]);
            qT[0] = qT[0] + Convert.ToDouble(T_dat[2 * i]);
            qT[1] = qT[1] + m;

        if (qT[1] >= min_reads && qC[1] >= min_reads)
            //skipping output for yut file
            Locations.Add(cols[0] + "_" + cols[1]);
            double qC_hat = qC[0] / qC[1];
            double qT_hat = qT[0] / qT[1];
            double var_C = 1.0 / qC[1];
            double var_T = 1.0 / qT[1];
Updated 18-Mar-17 8:59am

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Share this answer
dfarr1 18-Mar-17 1:33am    
This snippet came out of a 3-level nested loop in a program that analyzes variances in individual nucleotides across an entire genome. Debugging is great but I cannot interpret the variables line by line. I've narrowed the issue to what I believe is an error transcribing and filling an array, but since Python is looser than I'm comfortable with, I've asked for someone to check and see if my interpretation is functionally equivalent or if there's a better way to translate this snippet.
Patrice T 18-Mar-17 1:54am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
The code is functionally equivalent. You say that your problem is that qc[0] is often 0 and its causing problems. With your code your taking some C_dat array and converting every EVEN index of that array (0,2,4,etc) into a double and adding it to qc[0]. So the only reason qc[0] should equal 0 is if every even index of C_dat is 0. You should look into your code and see why that is happening.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900