-2

This program takes three files and compares the key among three files, and only print those entries which are specifically present in "AGO1" and not in others. I can print the result on the console. Please let me know how can I write results into a text file "Only_AGO1.txt" ONLY through the argument of function "OnlyArgSeq".

import sys
file1 = sys.argv[1]
file2 = sys.argv[2]
file4 = sys.argv[3]

AGO={}
AGO1={}
AGO2={}
AGO4={}

# Function "ArgSeq" to read each line of Argonoute "file" (Sequence, copy number)
# and put into a dictionary "AGO"
def ArgSeq(AGO,file):
    with open(file) as f:
        for line in f:
            (key, val) = line.split()
            AGO[key] = val

ArgSeq(AGO1,file1) # AGO1 dictionary
ArgSeq(AGO2,file2) # AGO2 dictionary
ArgSeq(AGO4,file4) # AGO4 dictionary


# Function "OnlyArgSeq" to find sRNA loading only to specific ARG
# eg sorting into ARG1, but not ARG2,ARG4
def OnlyArgSeq(AGO, *kwargs):
    only_AGO = {k:v for k,v in AGO.items()
                if not any(k in dicts for dicts in (kwargs))}
    for key in only_AGO.keys():
        print  key, only_AGO[key]

OnlyArgSeq(AGO1, AGO2, AGO4) # sRNA sorting specifically to AGO1, Only_AGO1.txt 

AGO1.txt

AAAAAAAATTGTTGCCGTTGG   1
AAAAAAACTTTGCTTATTTGTTCA    1
AAAAAAATGCTTATTTCAAATCGG    1
AAAAAAATGGGTCGGTTGTTTCA 1
AAAAAAATTGTTGCCGTTGGG   1

AGO2.txt

TTTTTTTTTTCGTCAGTTGGGTTC    1
TTTTTTTTTTGTCAAATTCTG   1
TTTTTTTTTTTCTTTGTCATCCGA    1
TTTTTTTTTTTGTCATCCAAA   1
TTTTTTTTTTTTATGATGTACA  1
AAAAAAATTGTTGCCGTTGGG   2

AGO4.txt

TTTTTTTTGAATATTTTGGTTGG 1
TTTTTTTTGGTAAGCTGTTAA   1
TTTTTTTTTACTGTAGTTTCTT  1
TTTTTTTTTCATGATTTCTTCCTT    2
TTTTTTTTTTAAATATTCTTTTGCCT  3
AAAAAAATTGTTGCCGTTGGG   2
firoz
  • 91
  • 7
  • So are you wanting to know how to write to a text file? – jacoblaw Aug 24 '17 at 17:38
  • 2
    Possible duplicate of [Redirect stdout to a file in Python?](https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python) – Erich Aug 24 '17 at 17:38
  • are you asking to redirect stdout? import sys and set stdout to a file. Afterward you can do this: https://stackoverflow.com/questions/14245227/python-reset-stdout-to-normal-after-previously-redirecting-it-to-a-file – Erich Aug 24 '17 at 17:39
  • My question is different. I wanted to put my file name as argument eg: OnlyArgSeq("Only_AGO1.txt"AGO1, AGO2, AGO4). And the output will write into "Only_AGO1.txt" – firoz Aug 24 '17 at 17:43
  • While not relevant to the question I will add that it is generally accepted to use *args and **kwargs, not *kwargs. The first is meant for a variable number of arguments, the second for a variable number of keyword arguments. It's an important distinction to know and it can make your code difficult to interpret. See: https://stackoverflow.com/questions/3394835/args-and-kwargs – Erich Aug 24 '17 at 18:27

2 Answers2

0

I wanted to put my file name as argument eg: OnlyArgSeq("Only_AGO1.txt"AGO1, AGO2, AGO4). And the output will write into "Only_AGO1.txt"

def myFunc(filename, [other args....]):
    #do some things

    with open(filename, 'w') as fOBJ:
        fOBJ.write(myVariable)

call as:

myFunc("ONLY_AGO1.txt", AGO1, AGO2, AGO3) 

for your specific use case.

Erich
  • 1,902
  • 1
  • 17
  • 23
  • Thank you @Erich, I change according to your suggestion but showing SyntaxError: invalid syntax at "def OnlyArgSeq('filename',[AGO, *kwargs]):" I am using python 2.7 – firoz Aug 24 '17 at 18:05
  • Do not put the other arguments in brackets. Those were meant to be a pseudo-syntax for the purpose of explaining my answer. Instead you should write it how you would for the usecase, e.g. def func(filename, AGO, *args) in your case. and then call it as I have included in my answer. – Erich Aug 24 '17 at 18:25
0

Alter your function to the following -

def OnlyArgSeq(filename, AGO, *kwargs):
    only_AGO = {k:v for k,v in AGO.items() if not any(k in dicts for dicts in (kwargs))}

    with open(filename) as fp:
        for key in only_AGO.keys():
            #print  key, only_AGO[key]
            fp.write("%s %s" % (key, only_AGO[key]))