create_db.py 2.79 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""
Un fichier pour faire des tests
"""

Pauline Pommeret's avatar
Pauline Pommeret committed
7
import os
8 9
import argparse

Pauline Pommeret's avatar
Pauline Pommeret committed
10
import sequence
Pauline Pommeret's avatar
Pauline Pommeret committed
11 12 13
from lib.XylokExceptions import NoFastaProvided
import lib.trx as trx_lib
import lib.database as database
Pauline Pommeret's avatar
Pauline Pommeret committed
14

15
if __name__ == "__main__":
Pauline Pommeret's avatar
Pauline Pommeret committed
16
    DBCURSOR = database.PGCursor()
Pauline Pommeret's avatar
Pauline Pommeret committed
17

18
    # Use argparse to parse arguments. We first create a parser.
Pauline Pommeret's avatar
Pauline Pommeret committed
19
    PARSER = argparse.ArgumentParser(description="ToDo")
20 21

    # Adding arguments to the parser
22 23 24 25 26 27 28
    PARSER.add_argument("-a", "--alphabet", type=str, default="dna", help="[str] sequences alphabet (dna, rna, prot), currently only dna is implemented (default: 'dna')", action="store")
    PARSER.add_argument("-A", "--alpha", type=float, default=0.05, help="[float] alpha parameter of the Student table that is to be used in the statistical analysis (default: 0.05)", action="store")
    PARSER.add_argument("-c", "--centering", type=int, default=72, help="[int] number of bp that are to be considered as the center of the sequence (default: 72)", action="store")
    PARSER.add_argument("-g", "--graph", type=int, default=None, help="[int] number of graphs that are to be plotted, currently not implemented (default: None)", action="store")
    PARSER.add_argument("-s", "--sliding", type=int, default=72, help="[int] number of bp that are to be included in the smoothing window (default: 72)", action="store")
    PARSER.add_argument("-t", "--trx-scale-file", type=str, default=trx_lib.SCALE_FILE, help="[str] path to trx scale file (default: trx_lib.SCALE_FILE)", action="store")
    PARSER.add_argument("datadir", type=str, help="[str] path to the data directory", action="store")
29 30

    # Then, parse it
Pauline Pommeret's avatar
Pauline Pommeret committed
31 32
    ARGS = PARSER.parse_args()
    if not ARGS.datadir:
Pauline Pommeret's avatar
Pauline Pommeret committed
33
        raise EnvironmentError("You have to give a data directory.")
Pauline Pommeret's avatar
Pauline Pommeret committed
34

Pauline Pommeret's avatar
Pauline Pommeret committed
35 36
    for directory in os.listdir(ARGS.datadir):
        cur_dir = os.path.join(ARGS.datadir, directory)
Pauline Pommeret's avatar
Pauline Pommeret committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        md_parameters = {}
        label = ""
        fasta_file = None
        for filepath in os.listdir(cur_dir):
            if filepath == "sequence.fasta":
                fasta_file = os.path.join(cur_dir, filepath)
            if filepath == "label":
                label = open(os.path.join(cur_dir, filepath)).readlines()[0].strip("\n")
            if ".dat" in filepath:
                print filepath.replace(".dat", "")
                md_parameters[filepath.replace(".dat", '')] = os.path.join(cur_dir, filepath)
        if fasta_file is None:
            raise NoFastaProvided("There is no fasta file in %r" % (cur_dir,))
        print label
        print fasta_file
        print md_parameters
Pauline Pommeret's avatar
Pauline Pommeret committed
53 54
        seq = sequence.Sequence(fasta_file, md_parameters, label, alphabet=ARGS.alphabet, trx_scale_path=ARGS.trx_scale_file, sliding=ARGS.sliding, centering=ARGS.centering, alpha=ARGS.alpha, graph=ARGS.graph)
        DBCURSOR.store_sequence(seq)