0

I am successfully checking, if a string matches any value in a dictionary. The string is a substring of the value (if matching). Upon matching, I append the key of the value, where the substring was found, to a list.

Is there any way in python to introduce something like an if condition, to do the following:

"only append to the list, if the substring matches at position 60+ (of 100) in the value"

My code so far:

#!/usr/bin/python3
# imports
import re,sys
import itertools
# handling stdin
faFile=sys.argv[1]
pat=sys.argv[2]
pat2=sys.argv[3]
# opening .fa-File and building dictionary
with open (faFile) as f:
    # reading file-content line by line
    content=f.readlines()
    # stripping linebreaks and >
    content=[x.strip()for x in content]
    content=[x.strip('>') for x in content]
# building dictionary from content
# IDs are keys, sequences are values
d=dict(itertools.zip_longest(*[iter(content)]*2, fillvalue=""))

# motif- search function
def search (myDict, search1, search2):
    # initialize empty list to store found keys
    search.a=[]
    # iterating through dictionary
    for key, value in myDict.items():
        if search1 in value:
            if search2 in value:
                search.a.append(key)

search(d,pat,pat2)
print(search.a)

Command-line call:

./search.py file.fa pat pat2

I thought of something like introducing a counter, but couldn't figure it out yet

Shushiro
  • 577
  • 1
  • 9
  • 32
  • 2
    Something like `if search1 in value[60:]`? – yinnonsanders Nov 01 '17 at 11:19
  • oh, I did not find this yet! So, I can address positions IN the value? and this would mean from 60 to end? – Shushiro Nov 01 '17 at 11:20
  • Yes, look here for more information: https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation – yinnonsanders Nov 01 '17 at 11:22
  • Side note from this, but when I read `search.a` I can't hold myself asking why you did not use a return value? :) You are bounding a result to a function object, which is expected to be a context less object (well... not totally true, but here mostly). Avoid doing that, and if you are scared about scoping of your variables, this might be useful: http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html – FunkySayu Nov 01 '17 at 11:23
  • @yinnonsanders thank you, if you want to post an answer I can upvote, do so :) – Shushiro Nov 01 '17 at 11:24
  • @yinnosanders concnering the search.a: It was the first "thing" I encountered, how to get my "a" available out of the function. I also read someting about "global", but didnt bother, since this worked out – Shushiro Nov 01 '17 at 11:25
  • (this is quite off-topic, sorry about that) to give you an idea, if you call `search` function only once with a large amount of data, `search.a` will be forever in memory. If you use `a = []` `return a`, you give to the caller the responsibility to remove the returned value once it is considered not useful. This is mostly FYI :) – FunkySayu Nov 01 '17 at 11:28

0 Answers0