- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
[MacOS, Python 2.7]
我正在尝试解析 .txt 文件并提取我想要创建制表符分隔表的字符串。我将不得不为许多文件执行此操作,但我在选择某些字符串时遇到了问题。
下面是一个输入文件示例:
# Assembly name: ASM1844v1
# Organism name: Acinetobacter baumannii ACICU (g-proteobacteria)
# Infraspecific name: strain=ACICU
# Taxid: 405416
# BioSample: SAMN02603140
# BioProject: PRJNA17827
# Submitter: CNR - National Research Council
# Date: 2008-4-15
# Assembly type: n/a
# Release type: major
# Assembly level: Complete Genome
# Genome representation: full
# GenBank assembly accession: GCA_000018445.1
# RefSeq assembly accession: GCF_000018445.1
# RefSeq assembly and GenBank assemblies identical: yes
#
## Assembly-Units:
## GenBank Unit Accession RefSeq Unit Accession Assembly-Unit name
## GCA_000018455.1 GCF_000018455.1 Primary Assembly
#
# Ordered by chromosome/plasmid; the chromosomes/plasmids are followed by
# unlocalized scaffolds.
# Unplaced scaffolds are listed at the end.
# RefSeq is equal or derived from GenBank object.
#
# Sequence-Name Sequence-Role Assigned-Molecule Assigned-Molecule-Location/Type GenBank-Accn Relationship RefSeq-Accn Assembly-Unit Sequence-Length UCSC-style-name
ANONYMOUS assembled-molecule na Chromosome
CP000863.1 = NC_010611.1 Primary Assembly 3904116 na
pACICU1 assembled-molecule pACICU1 Plasmid CP000864.1 = NC_010605.1 Primary Assembly 28279 na
pACICU2 assembled-molecule pACICU2 Plasmid CP000865.1 = NC_010606.1 Primary Assembly 64366 na
到目前为止,我的代码如下所示,其中 headstring 表示列标题:
# Open the input file for reading
InFile = open(InFileName, 'r')
#f = open(InFileName, 'r')
# Write the header
Headstring= "GenBank_Assembly_ID RefSeq_Assembly_ID Assembly_level Chromosome Plasmid Refseq_chromosome Refseq_plasmid1 Refseq_plasmid2 Refseq_plasmid3 Refseq_plasmid4 Refseq_plasmid5"
# Set up chromosome and plasmid count
ccount = 0
pcount = 0
# Look for corresponding data from each file
with open(InFileName, 'r') as searchfile:
for line in searchfile:
if re.search( r': (GCA_[\d\.]+)', line, re.M|re.I):
GCA = re.search( r': (GCA_[\d\.]+)', line, re.M|re.I)
print GCA.group(1)
GCA = GCA.group(1)
if re.search( r': (GCF_[\d\.]+)', line, re.M|re.I):
GCF = re.search( r': (GCF_[\d\.]+)', line, re.M|re.I)
print GCF.group(1)
GCF = GCF.group(1)
if re.search ( r'level: (.+$)', line, re.M|re.I):
assembly = re.search( r'level: (.+$)', line, re.M|re.I)
print assembly.group(1)
assembly = assembly.group(1)
if "Chromosome" in line:
ccount += 1
print ccount
if "Plasmid" in line:
pcount += 1
print pcount
OutputString = "%s\t%s\t%s\t%s\t%s\t" % (GCA, GCF, assembly, ccount, pcount)
OutFile=open(OutFileName, 'w')
OutFile.write(Headstring+'\n'+OutputString)
InFile.close()
OutFile.close()
我遇到的主要问题是我想提取字符串 NC_010611.1
、NC_010605.1
和 NC_010606.1
, 并在它们之间有制表符空格在同一行上,因此它们分别位于标题 Refseq_chromosome、Refseq_plasmid1
和 Refseq_plasmid2
下。但我只希望脚本在 assembly = "Chromosome"
或 "Complete Genome"
时搜索这些。我不确定如何仅在该条件为 true
时搜索字符串。
我知道获取这些字符串的正则表达式可以是 =\t(\w+..)
,但我只知道这些。
我是 Python 的新手,所以解释会很棒。
最佳答案
看看这个例子:
import re
InFileName = 'YOUR_INPUT_FILE_NAME'
OutFileName = 'YOUR_OUTPUT_FILE_NAME'
# Write the header
Headstring= "GenBank_Assembly_ID\tRefSeq_Assembly_ID\tAssembly_level\tChromosome\tPlasmid\tRefseq_chromosome\tRefseq_plasmid1\tRefseq_plasmid2\tRefseq_plasmid3\tRefseq_plasmid4\tRefseq_plasmid5"
# Look for corresponding data from each file
with open(InFileName, 'r') as InFile, open(OutFileName, 'w') as OutFile:
chromosomes = []
plasmids = []
for line in InFile:
if line.lstrip()[0] == '#':
# Process header part of the file differently from the data part
if re.search( r': (GCA_[\d\.]+)', line, re.M|re.I):
GCA = re.search( r': (GCA_[\d\.]+)', line, re.M|re.I)
print GCA.group(1)
GCA = GCA.group(1)
if re.search( r': (GCF_[\d\.]+)', line, re.M|re.I):
GCF = re.search( r': (GCF_[\d\.]+)', line, re.M|re.I)
print GCF.group(1)
GCF = GCF.group(1)
if re.search ( r'level: (.+$)', line, re.M|re.I):
assembly = re.search( r'level: (.+$)', line, re.M|re.I)
print assembly.group(1)
assembly = assembly.group(1)
elif assembly in ['Chromosome', 'Complete Genome']:
# Process each data line separately
split_line = line.split()
Type = split_line[3]
RefSeq_Accn = split_line[6]
if Type == "Chromosome":
chromosomes.append(RefSeq_Accn)
if Type == "Plasmid":
plasmids.append(RefSeq_Accn)
# Merge names of up to N chromosomes
N = 1
cstr = ''
for i in range(N):
if i < len(chromosomes):
nextChromosome = chromosomes[i]
else:
nextChromosome = ''
cstr += '\t' + nextChromosome
# Merge names of up to M plasmids
M = 5
pstr = ''
for i in range(M):
if i < len(plasmids):
nextPlasmid = plasmids[i]
else:
nextPlasmid = ''
pstr += '\t' + nextPlasmid
OutputString = "%s\t%s\t%s\t%s\t%s" % (GCA, GCF, assembly, len(chromosomes), len(plasmids))
OutputString += cstr
OutputString += pstr
OutFile.write(Headstring+'\n'+OutputString)
输入:
# Assembly name: ASM1844v1
# Organism name: Acinetobacter baumannii ACICU (g-proteobacteria)
# Infraspecific name: strain=ACICU
# Taxid: 405416
# BioSample: SAMN02603140
# BioProject: PRJNA17827
# Submitter: CNR - National Research Council
# Date: 2008-4-15
# Assembly type: n/a
# Release type: major
# Assembly level: Complete Genome
# Genome representation: full
# GenBank assembly accession: GCA_000018445.1
# RefSeq assembly accession: GCF_000018445.1
# RefSeq assembly and GenBank assemblies identical: yes
#
## Assembly-Units:
## GenBank Unit Accession RefSeq Unit Accession Assembly-Unit name
## GCA_000018455.1 GCF_000018455.1 Primary Assembly
#
# Ordered by chromosome/plasmid; the chromosomes/plasmids are followed by
# unlocalized scaffolds.
# Unplaced scaffolds are listed at the end.
# RefSeq is equal or derived from GenBank object.
#
# Sequence-Name Sequence-Role Assigned-Molecule Assigned-Molecule-Location/Type GenBank-Accn Relationship RefSeq-Accn Assembly-Unit Sequence-Length UCSC-style-name
ANONYMOUS assembled-molecule na Chromosome CP000863.1 = NC_010611.1 Primary Assembly 3904116 na
pACICU1 assembled-molecule pACICU1 Plasmid CP000864.1 = NC_010605.1 Primary Assembly 28279 na
pACICU2 assembled-molecule pACICU2 Plasmid CP000865.1 = NC_010606.1 Primary Assembly 64366 na
输出:
GenBank_Assembly_ID RefSeq_Assembly_ID Assembly_level Chromosome Plasmid Refseq_chromosome Refseq_plasmid1 Refseq_plasmid2 Refseq_plasmid3 Refseq_plasmid4 Refseq_plasmid5
GCA_000018445.1 GCF_000018445.1 Complete Genome 1 2 NC_010611.1 NC_010605.1 NC_010606.1
与您的脚本的主要区别:
if line.lstrip()[0] == '#'
以不同于“表格行”的方式处理“标题”行(以散列字符开头的行)在底部(实际包含每个序列数据的行)。if assembly in ['Chromosome', 'Complete Genome']
- 这是您在问题中指定的条件split_line = line.split()
。之后,我通过 Type = split_line[3]
获取类型(这是表数据中的第四列)并且 RefSeq_Accn = split_line[6]
给我第七列在表格中。关于python - 通过 .txt 文件解析以创建制表符分隔的输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50330355/
我知道我要求太多,但也许你也可以帮助解决这个问题。 a.txt 包含单词,b.txt 包含字符串。 我想知道 b.txt 中有多少个字符串以 a.txt 中的单词结尾 例子:一个.txt apple
这个问题在这里已经有了答案: erge text files ordered by numerical filenames in Bash (3 个答案) 关闭 4 年前。 我有一个文件夹,其中包含
我在一个目录中有几个平面文件 (.txt)。所有这些文件的格式都是 *.txt.txt,所以我想将其重命名为 *.txt?有什么简单的方法可以一起重命名? 当我尝试 ren *.txt.txt *.t
这个问题在这里已经有了答案: How can I use a file in a command and redirect output to the same file without trunc
您是否有任何理由应该或不应该允许访问 javascript 或 css 文件?特别是常见的文件,如 jquery。 最佳答案 人们普遍认为,搜索引擎每天为给定站点分配一定数量的带宽或 URL。因此,一
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 已关闭
这是相同的代码。我面临的问题是,我无法在任何文件上写入任何内容。请帮忙解决这个问题 #include #include int main() { FILE *fe; FILE *fo;
我想要特定于域的 robots.txt,到目前为止这有效: RewriteRule ^robots\.txt$ robots/%{HTTP_HOST}.txt [L] 但我希望有一个后备方案,因此如果
我正在调试一些构建成功运行的 SQL 命令的代码。 然而,在查询结束时,查询结果似乎被写入了一个文本文件。 完整的查询如下 echo SELECT DATE,DATETABLE,DATE,APPDAT
这个问题已经有答案了: difference between grep Vs cat and grep (5 个回答) 已关闭 8 年前。 我看到一个例子,其中有人这样做: cat source.tx
我想将表中的数据从以 csv 格式存储的文本文件插入到 sql server 表中。为此,我正在使用批量插入语句。现在我需要在“From”子句中指定文件名。我不想在那里使用网络位置或本地位置。我想将我
假设我有一个测试文件夹 (test.domain.com) 并且我不希望搜索引擎在其中抓取,我是否需要在测试文件夹中有一个 robots.txt 或者我可以只放置一个 robots.txt在根目录中,
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
这个问题在这里已经有了答案: order of directives in robots.txt, do they overwrite each other or complement each ot
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
在过去的几年中,当我引用“名字”字段的文本框控件时,我一直使用 FirstNameTxt 命名约定。但是,我注意到大多数其他开发人员倾向于使用命名约定 txtFirstName 哪个是最好的约定?为什
我只想允许目录 /minsc 中的一个文件,但我想禁止该目录的其余部分。 现在 robots.txt 中是这样的: User-agent: * Crawl-delay: 10 # Directorie
我正在编写一个将 youtube.com 映射到另一个域的代理服务器(因此用户可以轻松地从德国等国家/地区访问 youtube,而无需审查搜索结果和视频)。 不幸的是,我的 robots.txt 中存
我没有编程技能,但有一项非常具体的任务:我必须将一个庞大的文本文件拆分成多个,并在特定的文本标记 (@) 处拆分它们。我决定尝试使用 Powershell 脚本来完成此任务。 到目前为止,这就是我想出
我是一名优秀的程序员,十分优秀!