Question
So I'm trying to parse a FastQ sequence, but I'm a beginner to Python, and I'm a little confused as to why my code isn't working. This is what the program is supposed to carry out:
if I enter the FASTQ seqname line...
@EAS139:136:FC706VJ:2:2104:15343:197393
...then the program should output:
Instrument = EAS139
Run ID = 136
Flow Cell ID = FC706VJ
Flow Cell Lane = 2
Tile Number = 2104
X-coord = 15343
Y-coord = 197393
Here's my unfinished code thus far:
class fastq:
def __init__(self,str):
self.str = inStr.replace ('@',' ').split (':')
def lists (self,parameters):
self.parameters = ("Instrument","Run ID","Flow Cell ID","Flow Cell Lane","Tile Number","X-coordinates","y-coordinates")
def zip (self,myZip,zippedTuple):
self.Zip = zip(self.parameters,self.transform)
self.zippedTuple = tuple(myZip)
print (tuple(myZip))
def main():
seq = input('Enter FastQ sequence:')
new_fastq = fastq(str)
new_fastq.lists()
new_fastq.zip()
main()
Solution
The reason that your code isn't working is that it's more-or-less entirely wrong. To address your errors in the order we reach them when trying to run the program:
main
:new_fastq = fastq(str)
does not pass theseq
we just input, it passes the built-in string type;
__init__
:- Calling the argument to
fastq.__init__
str
is a bad idea as it masks the very built-in we just tried to pass to it; - But whatever you call it, be consistent between the function definition and what is inside it - where do you think
inStr
is coming from?
- Calling the argument to
lists
:- Why is this separate to and not even called by
__init__
? - Why don't you pass any arguments?
- What is the argument
parameters
even for?
- Why is this separate to and not even called by
zip
:- Rather than define a method to print the object, it is more Pythonic to define
fastq.__str__
that returns a string representation. Then you canprint(str(new_fastq))
. That being said; - Again, you mask a built-in. On this occasion, it's more of a problem because you actually try to use the built-in inside the method that masks it. Call it something else;
- Again, you put unnecessary arguments in the definition, then don't bother to pass them anyway;
- What is
self.transform
supposed to be? It is never mentioned anywhere else. Do you meanself.str
(which, again, should be called something else, for reasons of masking a built-in and not actually being a string)? myZip
is one of the arguments you never passed, and I think you actually wantself.Zip
; but- Why would you create
x = tuple(y)
then on the next lineprint(tuple(y))
?print(x)
!
- Rather than define a method to print the object, it is more Pythonic to define
Addressing those points, plus some bonus PEP-008 tidying:
class FastQ:
def __init__(self, seq):
self.elements = seq.replace ('@',' ').split (':')
self.parameters = ("Instrument", "Run ID", "Flow Cell ID",
"Flow Cell Lane", "Tile Number",
"X-coordinates", "y-coordinates")
def __str__(self):
"""A rough idea to get you started."""
return "\n".join(map(str, zip(self.parameters, self.elements)))
def main():
seq = input('Enter FastQ sequence: ')
new_fastq = FastQ(seq)
print(str(new_fastq))
main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow