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()
Was it helpful?

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:

  1. main:
    1. new_fastq = fastq(str) does not pass the seq we just input, it passes the built-in string type;
  2. __init__:
    1. 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;
    2. But whatever you call it, be consistent between the function definition and what is inside it - where do you think inStr is coming from?
  3. lists:
    1. Why is this separate to and not even called by __init__?
    2. Why don't you pass any arguments?
    3. What is the argument parameters even for?
  4. zip:
    1. Rather than define a method to print the object, it is more Pythonic to define fastq.__str__ that returns a string representation. Then you can print(str(new_fastq)). That being said;
    2. 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;
    3. Again, you put unnecessary arguments in the definition, then don't bother to pass them anyway;
    4. What is self.transform supposed to be? It is never mentioned anywhere else. Do you mean self.str (which, again, should be called something else, for reasons of masking a built-in and not actually being a string)?
    5. myZip is one of the arguments you never passed, and I think you actually want self.Zip; but
    6. Why would you create x = tuple(y) then on the next line print(tuple(y))? print(x)!

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
scroll top