What I would like the program to do is to take sequences related to a certain barcode and perform the defined function (average length and standard deviation of sequences, minus the barcode and non-relevant txt, identified by the same barcode). I have written something similar and based it off the similar program but I keep getting an indexerror. The idea is that all the sequences with the first barcode will be processed as barcodeCounter = 0 and the second one as barcodeCounter = 1, etc. Hopefully that is enough info, sorry if it is messy.
Input:
import sys
import math
def avsterr(x):
ave = sum(x)/len(x)
ssq = 0.0
for y in x:
ssq += (y-ave)*(y-ave)
var = ssq / (len(x)-1)
sdev = math.sqrt(var)
stderr = sdev / math.sqrt(len(x))
return (ave,stderr)
barcode = sys.argv[1]
sequence = sys.argv[2]
lengths = []
toprocess = []
b = open(barcode,"r")
barcodeCounter = 0
for barcode in b:
barcodeCounter = barcodeCounter + 1
barcode = barcode.strip()
print "barcode: %s" % barcode
handle = open(sequence, "r")
for line in handle:
print line
seq = line.split(' ',1)[-1].strip()
print "seq: %s" % seq
potential_barcode = seq[0:len(barcode)]
print "something"
if potential_barcode == barcode:
print "Checking sequences"
outseq = seq.replace(potential_barcode, "", 1)
outseq_length = [len(outseq)]
# toprocess.append("")
# toprocess[barcodeCounter] += outseq.strip
toprocess[barcodeCounter].extend(outseq.strip) #IndexError/line40
# toprocess[barcodeCounter] = toprocess[barcodeCounter] + outseq.strip
print "outseq: %s" % outseq
print "Barcodes to be processed: %s" % toprocess[barcodeCounter]
print "BC: %i" % barcodeCounter
handle.close()
b.close()
one = len(toprocess[0])
#two = lengths[2]
#three = lengths[3]
print one
#(av,st) = avsterr(lengths)
#print "%f +/- %f" % (av,st)
Output:
barcode: ATTAG
S01 ATTAGAAAAAAA
seq: ATTAGAAAAAAA
something
Checking sequences
Traceback (most recent call last):
File "./FinalProject.py", line 40, in <module>
toprocess[barcodeCounter].extend(outseq.strip)
IndexError: list index out of range
This is the code I'm basing it on.
sequenceCounter = -1
for line in handle:
if line[0] == ">":
sequenceCounter = sequenceCounter + 1
# print "seqid %s\n" % line
seqidList.append(line)
seqList.append("")
if line[0] != ">":
seqList[sequenceCounter] = seqList[sequenceCounter] + line.strip()
EDIT: Added the enumerate function and commented out barcodeCounter stuff.
barcode = sys.argv[1]
sequence = sys.argv[2]
lengths = []
toprocess = []
b = open(barcode,"r")
#barcodeCounter = -1
for barcodeCounter, barcode in enumerate(b):
# barcodeCounter = barcodeCounter + 1
barcode = barcode.strip()
print "barcode: %s" % barcode
handle = open(sequence, "r")
for line in handle:
print line
seq = line.split(' ',1)[-1].strip()
print "seq: %s" % seq
potential_barcode = seq[0:len(barcode)]
print "something"
if potential_barcode == barcode:
print "Checking sequences"
outseq = seq.replace(potential_barcode, "", 1)
outseq_length = [len(outseq)]
toprocess.append("")
# toprocess[barcodeCounter] += outseq.strip
toprocess[barcodeCounter].append(outseq.strip) #AttributeError line 40
# toprocess[barcodeCounter] = toprocess[barcodeCounter] + outseq.strip
print "outseq: %s" % outseq
print "Barcodes to be processed: %s" % toprocess[barcodeCounter]
print "BC: %i" % barcodeCounter
handle.close()
b.close()
New error:
barcode: ATTAG
S01 ATTAGAAAAAAA
seq: ATTAGAAAAAAA
something
Checking sequences
Traceback (most recent call last):
File "./FinalProject.py", line 40, in <module>
toprocess[barcodeCounter].append(outseq.strip)
AttributeError: 'str' object has no attribute 'append'
Code without the issue:
barcode = sys.argv[1]
sequence = sys.argv[2]
lengths = []
toprocess = []
b = open(barcode,"r")
#barcodeCounter = -1
for barcodeCounter, barcode in enumerate(b):
# barcodeCounter = barcodeCounter + 1
barcode = barcode.strip()
print "barcode: \n%s\n" % barcode
handle = open(sequence, "r")
for line in handle:
print line
seq = line.split(' ',1)[-1].strip()
print "seq: %s" % seq
potential_barcode = seq[0:len(barcode)]
# print "something"
if potential_barcode == barcode:
print "Checking sequences"
outseq = seq.replace(potential_barcode, "", 1)
outseq_length = [len(outseq)]
toprocess.append("")
toprocess[barcodeCounter] = toprocess[barcodeCounter] + outseq
@abarnert You were helpful, thank you. I'm not the brightest when it comes to programming sometimes(most the time). I had to also change the way I added the new sequences because they are str
not list
.