Building a dictionary out of components in a file
Question: I have a file, where in each line, it contains a readname; a '+' or '-'; a
position marked by a number.

I went ahead to first open the file and have in Python script:

    
    
        #!/usr/bin/env python
        import sys
    
        file=open('filepath')
    
        dictionary={}
    
        for line in file:
    
            reads=line.split()
    
            read_name=reads[0]
    
            methylation_state=reads[1] #this is a plus or minus
    
            position=int(reads[2])
    

I am having a hard time building a dictionary, where I would have
{keys:values} as {methylation_state:position}.

If someone can please help me, I would greatly appreciate it. I hope this was
clear enough.

**SAMPLES**

input1.txt

    
    
    SRR1035452.21010_CRIRUN_726:7:1101:4566:6721_length=36 + 59399861
    SRR1035452.21010_CRIRUN_726:7:1101:4566:6721_length=36 + 59399728
    SRR1035452.21010_CRIRUN_726:7:1101:4566:6721_length=36 + 59399735
    SRR1035452.21010_CRIRUN_726:7:1101:4566:6721_length=36 + 59399752
    SRR1035452.21044_CRIRUN_726:7:1101:5464:6620_length=36 + 31107092
    

input2.txt

    
    
    SRR1035454.47_CRIRUN_726:7:1101:2618:2094_length=36 + 18922145
    SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460469
    SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460488
    SRR1035454.174_CRIRUN_726:7:1101:6245:2159_length=36 + 51460631
    


Answer: It sounds like you just need simple sets of positions. Once you know all of
the positions in each file you can perform many types of operations.

    
    
    def positions(filename):
        # split on + and the second element is what we want
        return set(line.split('+')[1].strip() for line in open(filename)
            if '+' in line)
    
    # get sets from both files
    f1 = positions('f1.txt')
    f2 = positions('f2.txt')
    # with sets, subtraction shows you what is in one not the other
    print("in 1 not 2", f1 - f2)
    print("in 2 not 1", f2 - f1)
    

`positions` was implemented with python's compact "list comprehensions". I
have no idea where that name came from. You could break it into parts to see
the individual steps with the following, but the first implementation is clear
once you get used to python.

    
    
    def positions(filename):
         # open the file for reading
        with open(filename) as fp:
            # set will hold positions
            pos = set()
            # read the file line by line
            for line in fp:
                # we only care about lines with pluses
                if '+' in line:
                    # split into two parts
                    parts = line.split()
                    # position is the second part but we need to get rid of 
                    # extra spaces and newline
                    position = parts[1].strip()
                    # add to set. if position is already in set, you don't get
                    # a second one, this one is dropped
                    pos.add(position)
        return pos
    

