用 python生成json格式的配置文件

python 基础知识

writelines 和 write 区别

  1. write()需要传入一个字符串做为参数,否则会报错
  2. writelines()既可以传入字符串又可以传入一个字符序列,并将该字符序列写入文件
  3. 注意必须传入的是字符序列,不能是数字序列(卡在这里搞了半天)

lambda函数

lambda作为一个表达式,定义了一个匿名函数

1
2
3
4
5
6
7
8
9
10
>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
139

列表生成式

列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。

for循环后面还可以加上if判断,这样我们就可以筛选出仅偶数的平方:

1
2
>>> [x * x for x in range(1, 11) if x % 2 == 0]
[4, 16, 36, 64, 100]

还可以使用两层循环,可以生成全排列:

1
2
>>> [m + n for m in 'ABC' for n in 'XYZ']
['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']

任务

用文件名(存在列表当值),生成json配置文件

1
2
3
4
5
6
7
{
"label" : "sample",
"key" : "sample",
"storeClass" : "JBrowse/Store/SeqFeature/VCFTabix",
"urlTemplate" : "sample.vcf.gz",
"type" : "JBrowse/View/Track/HTMLVariants"
}

json 模块

示例:

1
2
3
4
5
6
>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
"4": 5,
"6": 7
}

原创脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python3
'''
Make a samples.json file with sample names and file names.
'''

import json
from glob import glob

# match filenames
fastqs = glob('/usr/share/nginx/html/vcf/*.vcf.gz')
files = {}

# extract a sample name from each filename.
samples = [fastq.split('/')[-1].split('_')[0] for fastq in fastqs]
samples1 = sorted(samples)

for sample in samples1:
files['label'] = sample
files['key'] = sample
files['storeClass'] = 'JBrowse/Store/SeqFeature/VCFTabix'
files['urlTemplate'] = 'vcf/' + sample + '_filtered.vcf.gz'
files['category'] = 'VCF'
files['type'] = 'JBrowse/View/Track/HTMLVariants'
js = json.dumps(files, indent=4, sort_keys=True)
js1 = js + ',' + '\n'
open('mysamples1.json', 'a').writelines(js1)

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"category": "VCF",
"key": "HB-10-1",
"label": "HB-10-1",
"storeClass": "JBrowse/Store/SeqFeature/VCFTabix",
"type": "JBrowse/View/Track/HTMLVariants",
"urlTemplate": "vcf/HB-10-1_filtered.vcf.gz"
},
{
"category": "VCF",
"key": "HB-10-2",
"label": "HB-10-2",
"storeClass": "JBrowse/Store/SeqFeature/VCFTabix",
"type": "JBrowse/View/Track/HTMLVariants",
"urlTemplate": "vcf/HB-10-2_filtered.vcf.gz"
},

JBrowse 结果

其他

安装制定版本为3.6,为什么python的版本是3.5.5

1
2
3
(py3.6) [qi@localhost ~]$ python --version
Python 3.5.5
(py3.6) [qi@localhost ~]$
1
conda install -c anaconda ipython  # su

目录高亮是怎么回事?

说明这个目录权限太高了,所有人都能够访问、修改、删除、运行。
正常情况,应该是Owner可以读写,all可读,root可以修改可执行权限才对。
正常情况,对于目录,应该权限设定为744,对于文件默认权限应该设定为644。
用户私有文件,权限应该设定为600。

更改文件拥有者

1
2
ls -ld html/
chown -R qi:qi html/

py3.6的环境,我安装了samtools,也用不了。自然也就用不了,bgzip和tabix。

1
2
3
(py3.6) [qi@localhost vcf]$ conda list | grep samtools
perl-bio-samtools 1.43 pl526h1341992_1 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
samtools 1.9 h57cc563_6 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda