File: ideal_by_fasta.py

package info (click to toggle)
spades 3.13.1+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 22,172 kB
  • sloc: cpp: 136,213; ansic: 48,218; python: 16,809; perl: 4,252; sh: 2,115; java: 890; makefile: 507; pascal: 348; xml: 303
file content (52 lines) | stat: -rwxr-xr-x 1,509 bytes parent folder | download | duplicates (2)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python3

############################################################################
# Copyright (c) 2015 Saint Petersburg State University
# Copyright (c) 2011-2014 Saint Petersburg Academic University
# All Rights Reserved
# See file LICENSE for details.
############################################################################

import os
import stat
import sys
import shutil

def read_fasta(filename):
    """
        Returns list of FASTA entries (in tuples: name, seq)
    """
    res_name = []
    res_seq = []
    first = True
    seq = ''

    for line in open(filename):
        if line[0] == '>':
            res_name.append(line.strip())
            if not first:
                res_seq.append(seq)
            else:
                first = False
            seq = ''
        else:
            seq += line.strip()
    res_seq.append(seq)
    return zip(res_name, res_seq)


def simulate_by_fasta(fasta, read_length, filename):
    outfile = open(filename, 'w')
    ideal = ''.join(['I' for i in range(0, read_length)])
    n = 0
    for name, seq in fasta:
        for i in xrange(0, len(seq) - read_length - 1):
            outfile.write('@' + name[1:] + '_' + str(n) + '_' + str(i) + '\n')
            outfile.write(seq[i : i + read_length] + '\n')
            outfile.write('+' + name[1:] + '_' + str(n) + '_' + str(i) + '\n')
            outfile.write(ideal + '\n')
        n += 1
    outfile.close()


simulate_by_fasta(read_fasta(sys.argv[1]), int(sys.argv[2]), sys.argv[3])