폴더에 여러 파일 (1.20.1_Indel_allEff.vcf, 1.20.2_Indel_allEff.vcf .... 1.200.1_Indel_allEff.vcf)의 항목을 순서대로 결합하려고합니다. 이런 식으로 보이는 행렬을 얻으려면.여러 파일의 항목을 Python을 사용하여 하나의 행렬로 결합
Fm Chromosome Position Ref Alt Gene X1.20.1 X1.20.2 X1.20.3
Fm chrI 100007 AT A CAR2 0 0 0
Fm chrX 3000676 G T HYM1 0 0 0.5
, X1.20.1
, X1.20.2
, X1.20.3
..... X1.200.3
폴더에 포함 된 개별 파일 이름과 주파수 값입니다.
snps = defaultdict(lambda: defaultdict(str))
myfiles=listdir(str(sys.argv[1]))
for f1 in myfiles:
f = open(f1)
tpp = f1.split("_")[0].split(".")
tp=tpp[0]+'.'+tpp[1]+'.'+tpp[2]
for l in f:
ls = l.split()
if l.find("#") == -1 and len(ls) > 6:
chrom = ls[0]
pos = ls[1]
ref = ls[2]
alt = ls[3]
freq = ls[4]
typ = ls[5]
gene = ls[6]
if len(alt) == 1:
snps[pos+"_"+ref+"-"+alt+"_"+chrom+"_"+gene+"_"+typ][tp] = freq
elif len(alt) > 1:
for k in range (0,len(alt.split(","))):
snps[pos+"_"+ref+alt.split(",")[k]+"_"+chrom+"_"+gene+"_"+typ][tp] = freq.split(",")[k]
f.close()
traj = 1
tp_list = ['1.20.1','1.20.2','1.20.3','1.30.1','1.30.2','1.30.3','1.40.1','1.40.2','1.40.3','1.50.1','1.50.2','1.50.3','1.60.1','1.60.2','1.60.3','1.90.1','1.90.2','1.90.3','1.100.1','1.100.2','1.100.3','1.130.1','1.130.2','1.130.3','1.200.1','1.200.2','1.200.3']
print "Fermentor\tTrajectory\tChromosome\tPosition\tMutation\tGene\tEffect\t1.20.1\t1.20.2\t1.20.3\t1.30.1\t1.30.2\t1.30.3\t1.40.1\t1.40.3\t1.50.1\t1.50.2\t1.50.3\t1.60.1\t1.60.2\t1.60.3\t1.90.1\t1.90.2\t1.90.3\t1.100.1\t1.100.2\t1.100.3\t1.130.1\t1.130.2\t1.130.3\t1.200.1\t1.200.2\t1.200.3"
for pos in sorted(snps.keys()):
pos1 = pos.split("_")[0]
mut = pos.split("_")[1]
chrom = pos.split("_")[2]
gene = pos.split("_")[3]
typ = pos.split("_")[4]
tp_string = ""
for tp in tp_list:
if len(snps[pos][tp])>0:
tp_string += "\t"+str(snps[pos][tp])
else:
tp_string += "\t"+str("0/0")
print "F1"+"\t"+str(traj)+"\t"+chrom+"\t"+pos1+"\t"+mut+"\t"+gene+"\t"+typ+"\t"+tp_string
traj += 1
그러나,이 모든 비록 나는 코드가 폴더에있는 파일 중 일부를 인식하지 못하는 오류를 얻고있다 같은 형식.
내 명령과 오류가 나는 얻을 :
python F1_comparison.py Fer1 > output.csv
Traceback (most recent call last):
File "Fer1_comparison.py", line 18, in <module>
f = open(f1)
IOError: [Errno 2] No such file or directory: '1.30.2_INDEL_allEff.vcf'
은 누군가가 나에게이 문제를하시기 바랍니다 알아내는 데 도움이 수 있습니까? 그것은 큰 도움이 될 것입니다. 당신은 조금 더 더 간결하고, 슬라이스를 사용하여 str.format을 풀고 더 효율적이 아니라 반복적으로 분할 할 수있는 코드를 작성할 수 있습니다
from os import path, listdir
pth = sys.argv[1] # get full path
myfiles = listdir(pth) # get list of all files in that path/directory
for f1 in myfiles:
with open(path.join(pth,f1)) as f: # join -> pth/f1. with also closes your file
tpp = f1.split("_",1)[0].split(".")
tp = ".".join(tpp[0:3]) # same as tp=tpp[0]+'.'+tpp[1]+'.'+tpp[2]
for line in f:
# continue your code ...
: 감사
안녕하세요, 내 명령을 교차 확인했습니다. 동일한 디렉토리에서 코드를 실행했습니다. 하지만 여전히 오류가 발생합니다. – Hash
@PM 2Ring : 들여 쓰기를 수정했습니다. 여기에 코드를 복사하는 동안 들여 쓰기로 오류가 발생했습니다. 대안을 제안 해 주시겠습니까? – Hash
감사합니다. PM 2Ring. 수정 코드를 업데이트했습니다. – Hash