简体   繁体   中英

index error, while executing python

I am a beginner in python, currently executing a piece of code but it throws the following error.I tried my best to solve but could't do it. Please help me. The code is as follows.

    continue
    num_snps_skipped += 1
    samp_id = sline[id_index]
    ref_allele = sline[ref_allele_index]
    tum_allele = sline[tum_allele_index]
    snp = ref_allele + tum_allele
    if not snp in transitions:
        snp = nucleotide_complement[ref_allele] + nucleotide_complement[tum_allele]
    ref_trinuc = sline[ref_tri_index]
    if ref_trinuc == "NA":
        print "Warning: Reference allele not available on "+\
                "line %d; skipping line"%line_number
        continue
    if not ref_trinuc[1] == snp[0]:
        print "Warning: Reference allele does not match reference "+\
                "trinucleotide; skipping line %d"%line_number**
        continue
    snp_with_ctx = ref_trinuc[0] + snp + ref_trinuc[2]
    if not samp_id in signatures:
        signatures[samp_id] = [0 for i in substitution_order]
    if snp_with_ctx not in substitution_order:
        print "Warning: substitution on line " + \
                "%d is %s, not "%(line_number,snp_with_ctx) + \
                "found among possible substitutions. Skipping line."

I am getting the error as follows

    Traceback (most recent call last):
  File "main.py", line 59, in <module>
    signatures = signature.make(args.in_file, substitution_order=stratton['substitution_order'], out_path = args.spectrum_output)
  File "/home/ateeqanees/Mutation/centos/mutation-signatures-master/signature.py", line 73, in make
    if not ref_trinuc[1] == snp[0]:
IndexError: string index out of range

Please help me. It would be of great help.

Thanks a lot !!! Dav

More than likely your ref_trinuc string has one or zero characters. That's why if not ref_trinuc[1] == snp[0]: is giving an index error, because it's trying to grab the second character.

Before that line try printing ref_trinuc to see what it holds.

If it is normal that ref_trinuc sometimes is shorter than 2 characters, try this if statement:

if (len(ref_trinuc) > 1) and (not ref_trinuc[1] == snp[0]):

Because if the length is 1 or 0, in this case the second half will not be attempted and no index error thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM