[英]Python reading a complicated .txt file
I have a .txt with data like this: 我有一个.txt,其数据如下:
Header:ensembl gene ID|Ensembl Transcript ID|CDS start|CDS end|5'UTR start|5'UTR end|3'UTR start|3'UTR end|Transcripts start|Transcripts end
>ENSMUSG00000002477|ENSMUST00000002551|*some junk information*...etc.|
TCGCGCGTCCGCAGGCCTCCGCGCGCTTTTCCG....etc.
>ENSMUSG00000002835|ENSMUST00000002914|...etc.|
GCAGAAGTGACACCGGTGGGAGGCG...etc.
I have codes written to get me to a point I have the names ENSMUSG0000000xxxx 我写了一些代码以使我达到一个名字ENSMUSG0000000xxxx
I want to pick out the names I have from the .txt with the next line eg"TACGTACG" read in a triple form eg"TAC" "GTA" 我想从.txt的下一行中选择我拥有的名称,例如以三重形式读取的“ TACGTACG”,例如“ TAC”,“ GTA”
And then I want to do the same thing but instead of reading from the 1st letter I want to start at the 2nd, using the above example it will read "ACG" and "TAG" 然后我想做同样的事情,但是不是从第一个字母开始阅读,而是从上面的示例中读取“ ACG”和“ TAG”,而不是从第二个字母开始
and the same thing again but skip the first 2 letters 再说一遍,只是跳过前2个字母
I really don't know how would I do it especially the reading 3 letters part. 我真的不知道该怎么办,尤其是阅读3个字母的部分。 Can someone give me a hand please?
有人可以帮我吗?
These are the codes I have so far: 这些是我到目前为止的代码:
import csv
import os.path
#open files + readlines
with open("C:/Users/Ivan Wong/Desktop/Placement/Lists of targets/Mouse/UCSC to Ensembl.csv", "r") as f:
reader = csv.reader(f, delimiter = ',')
#find files with the name in 1st row
for row in reader:
graph_filename = os.path.join("C:/Users/Ivan Wong/Desktop/Placement/Interesting reading/3'ORF",row[0]+"_nt_counts.txt.png")
if os.path.exists(graph_filename):
y = row[0]+'_nt_counts.txt'
r = open('C:/Users/Ivan Wong/Desktop/Placement/fp_mesc_nochx/'+y, 'r')
k = r.readlines()
r.close
del k[:1]
k = map(lambda s: s.strip(), k)
interger = map(int, k)
import itertools
#adding the numbers for every 3 rows
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.izip_longest(*args, fillvalue=fillvalue)
result = map(sum, grouper(3, interger, 0))
e = row[1]
cDNA = open('C:/Users/Ivan Wong/Desktop/Placement/Downloaded seq/Mouse/cDNA.txt', 'r')
q = cDNA.readlines()
cDNA.close
#To delete the 1st line that I do not want at all
del q[:1]
Now I just have an idea, and I want to break them down by steps 现在我有了一个主意,我想按步骤细分它们
1st: i want to find out the names (I named it e) in the list from my .txt (named q) 第一:我想从我的.txt(名为q)中找到列表中的名称(我将其命名为e)
2nd: I want to make it read the next line until it reaches another name (e) 2nd:我想让它读取下一行,直到达到另一个名字(e)
3rd: break those lines I read into a single string like this "A", "T", "C", "G", "A", "A" etc. 第三:将我读取的这些行分成单个字符串,例如“ A”,“ T”,“ C”,“ G”,“ A”,“ A”等。
4th: do the read 3 letters thing so - "ATC", "GAA" 第四:做读3个字母的事情-“ ATC”,“ GAA”
5th: write them into a file, then go back to 4th step but this time make it start with the 2nd letter 第五步:将它们写入文件,然后返回到第四步,但是这次使它以第二个字母开头
6th: basically 5th step but start on the 3rd letter this time 第6个:基本上是第5步,但是这次是从第3个字母开始
Although I have this idea, I do not have the programming knowledge to do this, can someone please help me 虽然我有这个主意,但我没有编程知识,可以请有人帮我
Since this is not homework here's a way to get started. 由于这不是家庭作业,因此这是一种入门方法。 Assuming the lines that you are interested are those that don't start with
'>'
the slicing operation will help here. 假设您感兴趣的行不是以
'>'
开头的行,那么切片操作将在这里有所帮助。
with open('data.txt') as inf:
for line in inf:
if not line.startswith('>'):
strings3 = [line[i:i+3]for i in range(len(line))]
will collect the 3 letter sequences you are interested in on each line: 将在每一行上收集您感兴趣的3个字母序列:
Input line: 输入线:
GCAGAAGTGACACCGGTGGGAGGCG
Output 输出量
['GCA', 'CAG', 'AGA', 'GAA', 'AAG', 'AGT', 'GTG', 'TGA', 'GAC', 'ACA', 'CAC', 'ACC', 'CCG', 'CGG', 'GGT', 'GTG', 'TGG', 'GGG', 'GGA', 'GAG', 'AGG', 'GGC', 'GCG', 'CG\n', 'G\n', '\n']
Note that if the number of characters on the line aren't evenly divisible by 3 you'l get some shorter strings and also some newlines. 请注意,如果行上的字符数不能被3整除,您会得到一些较短的字符串和一些换行符。
You also might be able to re-use the grouper
function from the other question you just posed recently. 您也许还可以重用最近刚提出的另一个问题中的
grouper
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.