aboutsummaryrefslogtreecommitdiff
path: root/wqflask/maintenance/get_group_samplelists.py
blob: 04e9488679f26c3cdbaac509d67b52599ca99667 (plain)
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
from __future__ import absolute_import, print_function, division

import os
import glob
import gzip

from base import webqtlConfig

def process_genofiles(geno_dir=webqtlConfig.GENODIR):
    print("Yabba")
    #sys.exit("Dabba")
    os.chdir(geno_dir)
    for geno_file in glob.glob("*"):
        if geno_file.lower().endswith(('.geno', '.geno.gz')):
            #group_name = genofilename.split('.')[0]
            sample_list = get_samplelist(geno_file)


def get_samplelist(file_type, geno_file):
    if file_type == "geno":
        return get_samplelist_from_geno(geno_file)
    elif file_type == "plink":
        return get_samplelist_from_plink(geno_file)

def get_samplelist_from_geno(genofilename):
    if os.path.isfile(genofilename + '.gz'):
        genofilename += '.gz'
        genofile = gzip.open(genofilename)
    else:
        genofile = open(genofilename)

    for line in genofile:
        line = line.strip()
        if not line:
            continue
        if line.startswith(("#", "@")):
            continue
        break

    headers = line.split()

    if headers[3] == "Mb":
        samplelist = headers[4:]
    else:
        samplelist = headers[3:]
    return samplelist

def get_samplelist_from_plink(genofilename):
    genofile = open(genofilename)

    samplelist = []
    for line in genofile:
        line = line.split(" ")
        samplelist.append(line[1])

    return samplelist