Mercurial > repos > bebatut > fasta_add_barcode
annotate fasta_add_barcode.py @ 0:04699558a38a draft default tip
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
author | bebatut |
---|---|
date | Fri, 15 Apr 2016 06:04:56 -0400 |
parents | |
children |
rev | line source |
---|---|
0
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
1 #!/usr/bin/env python |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
3 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
4 import sys |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
5 import os |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
6 import argparse |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
7 import copy |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
8 import operator |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
9 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
10 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
11 def write_seq_fasta_format(seq, output_file): |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
12 split_seq = [seq[i:i+60] for i in xrange(0,len(seq),60)] |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
13 for split in split_seq: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
14 output_file.write(split + '\n') |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
15 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
16 def fasta_add_barcode(args): |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
17 mapping = {} |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
18 with open(args.input_mapping_file,'r') as input_mapping_file: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
19 for line in input_mapping_file: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
20 split_line = line[:-1].split('\t') |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
21 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
22 if len(split_line) != 2: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
23 string = 'Incorrect number of column in mapping file.' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
24 string += '\nTwo tabular separated columns are expected' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
25 raise ValueError(string) |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
26 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
27 mapping[split_line[0]] = split_line[1] |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
28 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
29 seq_id = '' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
30 seq = '' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
31 with open(args.input_sequence_file,'r') as input_sequence_file: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
32 with open(args.output_sequence_file, 'w') as output_sequence_file: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
33 for line in input_sequence_file: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
34 if line.startswith('>'): |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
35 if seq != '': |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
36 if not mapping.has_key(seq_id): |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
37 string = 'A sequence identifier (' + seq_id + ') is' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
38 string += ' not found in mapping file' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
39 raise ValueError(string) |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
40 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
41 output_sequence_file.write('>' + seq_id + '\n') |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
42 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
43 barcode = mapping[seq_id] |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
44 seq = barcode + seq |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
45 write_seq_fasta_format(seq, output_sequence_file) |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
46 seq_id = line[1:-1].split( )[0] |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
47 seq = '' |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
48 else: |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
49 seq += line[:-1] |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
50 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
51 ######## |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
52 # Main # |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
53 ######## |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
54 if __name__ == "__main__": |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
55 parser = argparse.ArgumentParser() |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
56 parser.add_argument('--input_sequence_file', required=True) |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
57 parser.add_argument('--input_mapping_file', required=True) |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
58 parser.add_argument('--output_sequence_file', required=True) |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
59 args = parser.parse_args() |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
60 |
04699558a38a
planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/tools/fasta_add_barcode commit e857f7126443e115f11954085423f8999bc870aa-dirty
bebatut
parents:
diff
changeset
|
61 fasta_add_barcode(args) |