Question

Pretty new to python/programming in general, been working on a script but have run into indentation errors around line for line in csv.reader( open(filename), delimiter="\t"): been trying a few things but could use a little help sorting it out, any ideas?

Could you explain any responses you have, helps with the learning process thank you!

#!/usr/bin/python
import csv
import pprint
pp = pprint.PrettyPrinter(indent=4)
import sys
import getopt
import re

changes = {}

import argparse
parser = argparse.ArgumentParser()

parser.add_argument ("infile", metavar="CSV", nargs="+", type=str, help="data file") 
args = parser.parse_args()

sample_names = []

SIMILARITY_CUTOFF = 95

#
# Function that investigates the similarity between two samples. 
#
#
def similar_samples( sample_name1, sample_name2):

    combined_changes = dict()

    for change, fraction in changes[ sample_name1 ]:
        if ( change not in combined_changes):
            combined_changes[change] = []

        combined_changes[change].append(float(fraction))

    for change, fraction in changes[ sample_name2 ]:
        if ( change not in combined_changes):
            combined_changes[change] = []
        combined_changes[change].append(float(fraction))


    passed_changes = 0
    failed_changes = 0

    for change in combined_changes.keys():

        if ( len(combined_changes[ change ]) == 1):
            failed_changes +=1
            continue

        sum = 0
        count = 0
        for a in combined_changes[ change ]:

            sum += a
            count += 1

            mean = sum/ count


        for a in combined_changes[ change ]:
            if ( mean > a + 2  or mean < a - 2):
                failed_changes += 1
            else:
                passed_changes += 1


#    print "passed changes: %d, failed changes: %d" % ( passed_changes, failed_changes)


    if ( passed_changes * 100 / (passed_changes + failed_changes) > SIMILARITY_CUTOFF):
        print " vs ".join([sample_name1, sample_name2]) + " : Similar samples"
        return 1
    else:
        print " vs ".join([sample_name1, sample_name2]) + " : Different samples"
        return 0



#     print "mean %.2f \n" % ( sum/ count)






for filename in args.infile:
    sample_name = filename
    #sample_name = re.search("^(.*)\_", filename).group(1)
    changes[ sample_name ] = []
sample_names.append( sample_name )

    for line in csv.reader( open(filename), delimiter="\t"):
        for item in line[2:]:

            if not item.strip():
                continue

            item = item.split(":")
            item[1] = item[1].rstrip("%")

            changes[ sample_name].append([line[1]+item[0],item[1]])

for i in range(0, len(sample_names)):
    for j in range(i+1, len(sample_names)):

        similar = similar_samples( sample_names[ i ], sample_names[ j ])


exit()
Was it helpful?

Solution 2

The line before the one you quoted should be one indention more

sample_names.append( sample_name )

This line I mean :)

OTHER TIPS

Indentation error.

Try indenting

sample_names.append( sample_name )

line

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top