File: dataface.py

package info (click to toggle)
bowtie2 2.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,492 kB
  • sloc: cpp: 63,838; perl: 7,232; sh: 1,131; python: 987; makefile: 541; ansic: 122
file content (81 lines) | stat: -rwxr-xr-x 1,767 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
76
77
78
79
80
81
#!/usr/bin/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