File: test_generator.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 (121 lines) | stat: -rwxr-xr-x 3,985 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/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 sys
import os
import random
from xml.dom.minidom import parse, getDOMImplementation

def getInputData(finName):
    fin = open(finName)
    data = []
    line = fin.readline()
    while line:
        data.append(line.strip())
        line = fin.readline()
    fin.close()
    return data

def getAllChunkNames(data):
    names = []
    for string in data:
        for ltr in string:
            if ltr != "+" and ltr != "-":
                names.append(ltr)
    return set(names)

def generateRandomSequence(seqLen):
    seq = ""
    nucl = ["A", "C", "G", "T"]
    for x in range(seqLen):
        i = random.randint(0, 3)
        seq += nucl[i]
    return seq

def getChunkSequences(longChunkLen, shortChunkLen, chunkNames):
    seqs = {}
    for name in chunkNames:
        assert len(name) == 1
        lenToGen = longChunkLen
        if name.islower():
            lenToGen = shortChunkLen
        seqs[name] = generateRandomSequence(lenToGen)
    return seqs

def getReverseComplement(seq):
    revNucl = {"A" : "T", "T" : "A", "C" : "G", "G" : "C"}
    revSeq = ""
    for x in reversed(seq):
        revSeq += revNucl[x]
    return revSeq

def getSequenceByChunkString(chunkString, chunkSeqs):
    seq = ""
    for i in range(0, len(chunkString)-1, 2):
        ltr = chunkString[i]
        chunkName = chunkString[i+1]
        if ltr == "+":
            seq += chunkSeqs[chunkName]
        elif ltr == "-":
            seq += getReverseComplement(chunkSeqs[chunkName])
    return seq

def getAllContigData(contigNodes):
    data = []
    for contigNode in contigNodes:
        for node in contigNode.childNodes:
            if node.nodeType == node.TEXT_NODE:
                data.append(node.data)
    return data

if __name__ == "__main__":
    random.seed(239)
    if len(sys.argv) < 5:
        print("Usage: " + sys.argv[0] + " <name of input file> <output format xml/fasta> <name of output file> <length of chunk> (<length of chunk for lower case>)")
        sys.exit()
    #random.seed()
    chunkLen = int(sys.argv[4])
    shortChunkLen = chunkLen
    if len(sys.argv) > 5:
        shortChunkLen = int(sys.argv[5])
    dom = parse(sys.argv[1])
    contigNodes = dom.getElementsByTagName("contig")
    chunkNames = getAllChunkNames(getAllContigData(contigNodes))
    chunkSeqs = getChunkSequences(chunkLen, shortChunkLen, chunkNames)
    for node in contigNodes:
        for child in node.childNodes:
            child.data = getSequenceByChunkString(child.data, chunkSeqs)

    assert sys.argv[2] == "xml" or sys.argv[2] == "fasta";
    if sys.argv[2] == "xml":
        fout = open(sys.argv[3], "w")
        dom.writexml(fout+".xml")
        fout.close()   
    else:
        if not os.path.exists(sys.argv[3]):
            os.makedirs(sys.argv[3])
        exampleNodes = dom.getElementsByTagName("example")
        for example in exampleNodes:
            folder = sys.argv[3] + "/example_" + example.attributes["n"].value + "/"
            if not os.path.exists(folder):
                os.makedirs(folder)
            genome_cnt = 1
            for genome in example.getElementsByTagName("genome"):
                filename = folder + "genome_" + str(genome_cnt) + ".fasta"
                fout = open(filename, "w")
                contig_cnt = 1
                for contig in genome.getElementsByTagName("contig"):
                    fout.write(">contig_" + str(contig_cnt) + "\n")
                    for node in contig.childNodes:
                        fout.write(node.data + "\n")
                    contig_cnt = contig_cnt + 1
                fout.close()
                genome_cnt = genome_cnt + 1;