简体   繁体   中英

Access each index result from enumerate() - Python

I have data, that looks like this:

Name Nm1    *    *
Ind1     AACTCAGCTCACG
Ind2     GTCATCGCTACGA 
Ind3     CTTCAAACTGACT

I need to grab the letter from each position marked by an asterix in the "Name"-line and print this, along with the index of the asterix

So the result would be

Ind1, 12, T
Ind2, 12, A
Ind3, 12, C
Ind1, 17, T
Ind2, 17, T
Ind3, 17, T

I'm trying to use enumerate() to retrieve the positions of the asterix's, and then my thought was, that I could use these indexes to grab the letters.

import sys
import csv

input = open(sys.argv[1], 'r')
Output = open(sys.argv[1]+"_processed", 'w')

indlist = (["Individual_1,", "Individual_2,", "Individual_3,"])

with (input) as searchfile:
    for line in searchfile:
        if '*' in line:
            LocusID = line[2:13]
            LocusIDstr = LocusID.strip()
            hit = line
            for i, x in enumerate(hit):
                     if x=='*':
                      position = i
                      print position

    for item in indlist:
        Output.write("%s%s%s\n" % (item, LocusIDstr, position))

Output.close()

If the enumerate() outputs eg

12
17

How do I access each index seperately?

Also, when I print the position, I get the numbers I want. When I write to the file, however, only the last position is written. Why is this?

----------------EDIT-----------------

After advice below, I have edited split up my code to make it a bit more simple (for me) to understand.

import sys
import csv

input = open(sys.argv[1], 'r')
Output = open(sys.argv[1]+"_FGT_Data", 'w')

indlist = (["Individual_1,", "Individual_2,", "Individual_3,"])

with (input) as searchfile:
    for line in searchfile:
        if '*' in line:
            LocusID = line[2:13]
            LocusIDstr = LocusID.strip()
            print LocusIDstr
            hit = line
            for i, x in enumerate(hit):
                    if x=='*':
                      position = i
                      #print position

input = open(sys.argv[1], 'r')

with (input) as searchfile:
    for line in searchfile:
        if line [0] == ">":
            print line[position], position

with (Output) as writefile:
    for item in indlist:
        writefile.write("%s%s%s\n" % (item, LocusIDstr, position))

Output.close()

I still do not have a solution for how to acces each of the indexes, though.

Edit changed to work with the file you gave me in your comment. if you have made this file yourself, consider working with columns next time.

import sys

read_file = sys.argv[1]
write_file = "%s_processed.%s"%(sys.argv[1].split('.')[0],sys.argv[1].split('.')[1])
indexes = []
lines_to_write = []

with open(read_file,'r') as getindex:
    first_line = getindex.readline()
    for i, x in enumerate(first_line):
        if x == '*':
            indexes.append(i-11)

with open(read_file,'r') as getsnps:
    for line in getsnps:
        if line.startswith(">"):
            sequence = line.split("    ")[1]
            for z in indexes:
                string_to_append = "%s\t%s\t%s"%(line.split("    ")[0],z+1,sequence[z])
                lines_to_write.append(string_to_append)

with open(write_file,"w") as write_file:
    write_file.write("\n".join(lines_to_write))

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