Thanks for the comments!
I edited my script already based on the comments and now the script is working fine.
Hi,
I want to take input file after file. But for some reasons, it just take input of only one file not the other files. (sys.argv is not working)
A small example how I am using this code:
import sys
from sys import argv
with open(sys.argv[1], 'r') as f:
contents = f.read()
print (contents)
Even this code is not working.
The code is as follows:
#!/usr/bin/env
import collections
data = collections.defaultdict(list)
import sys
from sys import argv
file=open("../names.dmp","r")
#original=open("test.txt","r")
write=open("write.txt","w")
missing=open("missing.txt","w")
for line in file:
columns = line.strip().split("|")
data[columns[0].strip()].append(columns[1].replace("\t", ""))
tax=0
name=""
none=0
count=1
with open(sys.argv[1], 'r') as original:
for line in original:
line=original.readline()
if line.startswith(">"):
try:
chunk=line.rsplit('[', 1)[1]
index = chunk[:-2]
if len(index) <= 50:
for k,v in data.items():
if index in v:
tax=k
break
else:
continue
if tax != 0:
lne=original.readline()
for pine in original:
if pine.startswith("\n"):
break
else:
lne=lne+pine
finalline=line[:1] + tax + "|"+"gi_"+line[1:]+lne+"\n"
write.write(finalline)
tax=0
else:
lne=original.readline()
for pine in original:
if pine.startswith("\n"):
break
else:
lne=lne+pine
missing.write(line[:1] + str(none) + "|"+"gi_"+line[1:]+lne+"\n")
tax=0
except:
lne=original.readline()
for pine in original:
if pine.startswith("\n"):
break
else:
lne=lne+pine
missing.write(line[:1] + str(none) + "|"+"gi_"+line[1:]+lne+"\n")
tax=0
the code works fine when given input as a single file but it does not work if I use sys.argv.
Thanks!
I'm not entirely sure what you are trying to do, and for sure your code could use some improvements.
import sys
from sys import argv
It's pointless to additionally import argv separately. You can just import sys and use sys.argv
file=open("../names.dmp","r")
file is not a good variable name in python, since it's a reserved keyword.
I see you are manually parsing a fasta file, which is generally not advisable. I would suggest to use BioPython SeqIO for this job. Will make your code easier to read and less room for errors.
the code works fine when given input as a single file but it does not work if I use sys.argv.
It's (to me) unclear how you use sys.argv and what exactly doesn't work.
In addition to @wouter's remarks:
1) The shebang #!/usr/bin/env should probably be #!/usr/bin/env python.
2) write=open("write.txt","w") is a poor choice, because write is a function to write to output. Same remark as for the usage of file. A safe way to use filehandles is to make the caps lock (e.g. OUTPUT=open("filename", "w")).
3) line.strip().split("|") is totally fine but if your task is to remove the whitespaces/newlines at the end of the line, then use rstrip() instead. See https://docs.python.org/2/library/stdtypes.html#str.rstrip
4) line=original.readline() is redundant: when you do for line in original you are already doing readline() on the implicit iterator.
5) write.write(finalline) really?
6) except should be used coupled with the type of exception, like for example except KeyError, otherwise you handle each exception the same way and perhaps you don't want that.
Thanks for the comments!
I edited my script already based on the comments and now the script is working fine.
Log in to answer this question.