python生信小練習(xí)(三)

生信菜鳥(niǎo)團(tuán)的編程練習(xí):

對(duì)FASTQ的操作

  • 5,3段截掉幾個(gè)堿基
  • 序列長(zhǎng)度分布統(tǒng)計(jì)
  • FASTQ 轉(zhuǎn)換成 FASTA
  • 統(tǒng)計(jì)堿基個(gè)數(shù)及GC%
    對(duì)FASTA的操作
  • 取互補(bǔ)序列
  • 取反向序列
  • DNA to RNA
  • 大小寫(xiě)字母形式輸出
  • 每行指定長(zhǎng)度輸出序列
  • 按照序列長(zhǎng)度/名字排序
  • 提取指定ID的序列
  • 隨機(jī)抽取序列
def trim(file, terminal5, terminal3):
    fastq = {}
    count = 1
    for line in open(file):
        if count % 4 == 1:           #取第一行作為reads name
            readID = line.strip()
            fastq[readID] = []
        elif count % 4 == 2:         #取第二行作為序列
            seq = line.strip()
            fastq[readID] = seq[terminal5 : -terminal3]           #序列切片操作,截取兩端,保留中間序列,并存儲(chǔ)為字典
        count += 1
    with open(r'E:\Bioinformatics\Python\practice\PyCharm\practice of biotrainee\trim.txt', 'w') as f:
        for key, value in fastq.items():
            print('{}\n{}'.format(key, value), file = f)

f1 = r'E:\Bioinformatics\Python\practice\chentong\notebook-master\data\test1.fq'

trim(f1, 5, 8)

def readLength(file):
    fastq = {}
    count = 1
    for line in open(file):
        if count % 4 == 1:  # 取第一行作為reads name
            readID = line.strip()
            fastq[readID] = []
        elif count % 4 == 2:  # 取第二行作為序列
            seq = line.strip()
            fastq[readID] = len(seq)  # 序列長(zhǎng)度統(tǒng)計(jì),并存儲(chǔ)為字典
        count += 1
    for key, value in fastq.items():
        print(value)


def fq2fa(file):
    fastq = {}
    count = 1
    for line in open(file):
        if count % 4 == 1:  # 取第一行作為reads name
            readID = line.split(' ')[1:]    #去除@,取第一個(gè)空格前字符為ID
            fastq[readID] = []
        elif count % 4 == 2:  # 取第二行作為序列
            seq = line.strip()
            fastq[readID] = seq
        count += 1
    with open(r'E:\Bioinformatics\Python\practice\PyCharm\practice of biotrainee\fq2fa.txt', 'w') as f:
        for key, value in fastq.items():
            print('>{}\n{}'.format(key, value), file = f)

def countGC(file):
    count = 1
    seq = []
    for line in open(file):
        if count % 4 == 2:  # 取第二行作為序列
            seq.append(line.strip())
        count += 1
    seq1 = ''.join(seq)
    gc = 0
    for i in seq1:
        if i == 'G' or i == 'C':
            gc += 1
    print('The number of length is {}'.format(len(seq1)))
    print('GC% is {}%'.format(gc/len(seq1)*100))

def complementary(file):
    fasta = {}
    for line in open(file):
        if line.startswith('>'):
            key = line.strip()
            fasta[key] = []
        else:
            complem = line.strip().replace('A', 't').replace('T', 'a').replace('G', 'c').replace('C', 'g').upper()
            fasta[key].append(complem)
    for key, value in fasta.items():
        print(key)
        value2 = ''.join(value)
        for i in range(0, len(value2), 60):
            print(value2[i: i + 60])

def reverse(file):
    fasta = {}
    for line in open(file):
        if line.startswith('>'):
            key = line.strip()
            fasta[key] = []
        else:
            fasta[key].append(line.strip())
    for key, value in fasta.items():
        print(key)
        rev = ''.join(value)[:: -1]
        for i in range(0, len(rev), 60):
            print(rev[i: i + 60])

def dna2rna(file):
    fasta = {}
    for line in open(file):
        if line.startswith('>'):
            key = line.strip()
            fasta[key] = []
        else:
            seq = list(line.strip())
            for i in range(len(seq)):
                if seq[i] == 'T':
                    seq[i] = 'U'
                elif seq[i] == 't':
                    seq[i] = 'u'
            fasta[key].append(''.join(seq))
    for key, value in fasta.items():
        print(key)
        value2 = ''.join(value)
        for i in range(0, len(value2), 60):
            print(value2[i: i + 60])

def upperandlower(file):
    upper = {}
    lower = {}
    for line in open(file):
        if line.startswith('>'):
            key = line.strip()
            upper[key] = []
            lower[key] = []
        else:
            upper[key].append(line.strip().upper())
            lower[key].append(line.strip().lower())
    for key, value in upper.items():
        print(key)
        value2 = ''.join(value)
        for i in range(0, len(value2), 60):
            print(value2[i: i + 60])
    for key, value in lower.items():
        print(key)
        value2 = ''.join(value)
        for i in range(0, len(value2), 60):
            print(value2[i: i + 60])

def sortLength():
    fasta = {}
    for line in open(file):
        if line.startswith('>'):
            key = line.strip()
            fasta[key] = []
        else:
            fasta[key].append(line.strip())
    for key, value in fasta.items():
        seq = ''.join(value)
        fasta[key] = seq
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容