File: dataface.py

package info (click to toggle)
bowtie 1.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,816 kB
  • sloc: cpp: 37,094; perl: 5,806; ansic: 1,474; sh: 1,197; python: 462; makefile: 419
file content (75 lines) | stat: -rwxr-xr-x 1,669 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import os
import logging


class DataFace(object):
    """ Some data IFace bowtie can work with.
    """

    def size(self):
        raise NotImplementedError("size() needs to be implemented!")



class SamFile(DataFace):

    def __init__(self,sam_desc):
        self.file_desc = sam_desc


    def size(self):
        if hasattr(self,'no_frags'):
            return self.no_frags

        count = 0
        try:
            fh = open(self.file_desc,"r")
            for line in fh:
                if line[0] != "@":
                    count += 1
        except:
            logging.error("Exception reading sam file!")
            fh.close()
            raise
        else:
            fh.close()

        self.no_frags = count
        return count



class FastaQFile(DataFace):

    def __init__(self,fastq_desc):
        self.file_desc = fastq_desc


    def size(self):
        if hasattr(self,'no_seqs'):
            return self.no_seqs

        count = 0
        try:
            fh = open(self.file_desc,"r")
            lno = 0
            for line in fh:
                if lno == 0:
                    if line[0] != '@':
                        raise TypeError("This does not look like a fastq file!")
                    count += 1
                if lno == 2:
                    if line[0] != '+':
                        raise TypeError("This does not look like a fastq file!")
                lno = (lno + 1) % 4
        except:
            logging.error("Exception reading fastq file!")
            fh.close()
            raise
        else:
            fh.close()

        self.no_seqs = count
        return self.no_seqs