text
stringlengths
0
601k
let xml_of_grant ( grantee , permission ) = let kids = [ xml_of_grantee grantee ; xml_of_permission permission ] in X . E ( " Grant " , [ ] , kids )
let xml_of_access_control_list grants = X . E ( " AccessControlList " , [ ] , List . map xml_of_grant grants )
let xml_of_access_control_policy acl = let kids = [ xml_of_owner acl # owner ; xml_of_access_control_list acl # grants ] in X . E ( " AccessControlPolicy " , [ ] , kids )
let xml_content_type_header = " Content - Type " , " application / xml "
let json_content_type_header = " Content - Type " , " application / json "
let set_bucket_acl creds region bucket acl = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : PUT ~ content_type " : application / xml " ~ date ~ bucket ~ sub_resources [ ` : acl , None ] creds in let request_url = ( service_url_of_region region ) ^ ( Util . encode_url bucket ) ^ " ...
let delete_object creds region ~ bucket ~ objekt = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : DELETE ~ date ~ bucket ~ request_uri ( " " :/ ^ objekt ) creds in let service_url = service_url_of_region region in let request_url = sprintf " % s % s /% s " service_url ( Util . enc...
let get_object_acl creds region ~ bucket ~ objekt = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : GET ~ date ~ bucket ~ request_uri ( " " :/ ^ objekt ) ~ sub_resources [ ` : acl , None ] creds in let headers = [ " Date " , date ; authorization_header ] in let request_url = sprint...
let set_object_acl creds region ~ bucket ~ objekt acl = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : PUT ~ content_type " : application / xml " ~ date ~ bucket ~ request_uri ( " " :/ ^ objekt ) ~ sub_resources [ ` : acl , None ] creds in let request_url = sprintf " % s % s /% s ...
let get_bucket_policy creds region ~ bucket = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : GET ~ date ~ bucket ~ sub_resources [ ` : policy , None ] creds in let request_url = sprintf " % s % s ?% s " ( service_url_of_region region ) ( Util . encode_url bucket ) ( string_of_sub_...
let delete_bucket_policy creds region ~ bucket = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : DELETE ~ date ~ bucket ~ sub_resources [ ` : policy , None ] creds in let request_url = sprintf " % s % s ?% s " ( service_url_of_region region ) ( Util . encode_url bucket ) ( string_o...
let set_bucket_policy creds region ~ bucket ~ policy = let date = now_as_string ( ) in let authorization_header = auth_hdr ~ http_method ` : PUT ~ date ~ bucket ~ sub_resources [ ` : policy , None ] ~ content_type ( : snd json_content_type_header ) creds in let request_url = sprintf " % s % s ?% s " ( service_url_of_re...
let tests = " Sa " >::: [ " lift " >:: begin fun ( ) -> let adds = ref 0 in let muls = ref 0 in let ( ) + = lift2 ( fun a b -> incr adds ; a + b ) in let ( * ) = lift2 ( fun a b -> incr muls ; a * b ) in let assert_adds n = assert_equal ~ printer : string_of_int n ! adds in let assert_muls n = assert_equal ~ printer : ...
module Json_decodeable : Decode . Decodeable with type value = Yojson . Safe . t = struct type value = Yojson . Safe . t let pp fmt json = Format . fprintf fmt " [ @% s ] " @ ( Yojson . Safe . pretty_to_string json ) let of_string : string -> ( value , string ) result = fun string -> try Ok ( Yojson . Safe . from_strin...
module Decode = Decode . Make ( Json_decodeable )
module Json_encodeable = struct type value = Yojson . Safe . t let to_string json = Yojson . Safe . to_string json let of_string x = ` String x let of_int x = ` Int x let of_float x = ` Float x let of_bool x = ` Bool x let null = ` Null let of_list xs = ` List xs let of_key_value_pairs xs = ` Assoc ( xs |> Decoders_uti...
module Encode = Encode . Make ( Json_encodeable )
let escape str = let add_codepoint buf uchar = Uchar . to_int uchar |> Fmt . str " U +% 04X " |> Buffer . add_string buf in let buf = Buffer . create ( String . length str * 2 ) in let get_normalized_char _ _ u = match u with | ` Uchar u -> if Uchar . is_char u then match Uchar . to_char u with | ( ' A ' . . ' Z ' | ' ...
type t = { raw : string ; escaped : string }
let v raw = { raw ; escaped = escape raw }
let to_string { escaped ; _ } = escaped
let to_unescaped_string { raw ; _ } = raw
let pp = Fmt . using ( fun { raw ; _ } -> raw ) Fmt . string
let length { raw ; _ } = String . length_utf8 raw
let equal t t ' = String . equal t . escaped t ' . escaped
let compare t t ' = String . compare t . escaped t ' . escaped
let ( >>?~ ) ( x : ' a option Or_error . t ) t ( f : ' a -> ' b Or_error . t ) t : ' b option Or_error . t = let open Result . Monad_infix in x >>= function | None -> Ok None | Some x -> f x >>| Option . some
type tag_value = string * string
type sort_order = [ ` Unknown | ` Unsorted | ` Query_name | ` Coordinate ]
type group_order = [ ` None | ` Query | ` Reference ]
type header_line = { version : string ; sort_order : sort_order option ; group_order : group_order option ;
type ref_seq = { name : string ; length : int ; assembly : string option ; md5 : string option ; species : string option ; uri : string option ;
type read_group = { id : string ; seq_center : string option ; description : string option ; run_date : [ ` Date of string | ` Time of string ] string option ; flow_order : string option ; key_seq : string option ; library : string option ; program : string option ; predicted_median_insert_size : int option ; platform ...
type program = { id : string ; name : string option ; command_line : string option ; previous_id : string option ; description : string option ; version : string option ;
type header = { version : string option ; sort_order : sort_order option ; group_order : group_order option ; ref_seqs : ref_seq list ; read_groups : read_group list ; programs : program list ; comments : string list ; others : ( string * tag_value list ) list list ; }
let empty_header = { version = None ; sort_order = None ; group_order = None ; ref_seqs = [ ] ; read_groups = [ ] ; programs = [ ] ; comments = [ ] ; others = [ ] ; }
module Flags = struct type t = int [ @@ deriving sexp ] sexp let of_int x = if ( 0 <= x ) x && ( x <= 65535 ) 65535 then Ok x else error " flag out of range " x sexp_of_int let flag_is_set s f = ( f land s ) s <> 0 let has_multiple_segments = flag_is_set 0x1 let each_segment_properly_aligned = flag_is_set 0x2 let segme...
type cigar_op = [ | ` Alignment_match of int | ` Insertion of int | ` Deletion of int | ` Skipped of int | ` Soft_clipping of int | ` Hard_clipping of int | ` Padding of int | ` Seq_match of int | ` Seq_mismatch of int
type optional_field = { tag : string ; value : optional_field_value
type rnext = [ ` Value of string | ` Equal_to_RNAME ] Equal_to_RNAME
type alignment = { qname : string option ; flags : Flags . t ; rname : string option ; pos : int option ; mapq : int option ; cigar : cigar_op list ; rnext : rnext option ; pnext : int option ; tlen : int option ; seq : string option ; qual : Phred_score . t list ; optional_fields : optional_field list ;
let parse_header_version s = let err = error " invalid version " ( ` HD , s ) s [ % sexp_of : header_item_tag * string ] in match String . lsplit2 ~ on : ' . ' s with | None -> err | Some ( a , b ) b -> if ( String . for_all a ~ f : Char . is_digit ) is_digit && ( String . for_all b ~ f : Char . is_digit ) is_digit the...
let header_line ~ version ? sort_order ? group_order ( ) = parse_header_version version >>| fun version -> { version ; sort_order ; group_order } group_order
let ref_seq ~ name ~ length ? assembly ? md5 ? species ? uri ( ) = let is_name_first_char_ok = function | ' ! ' . . ' ) ' ' | ' + ' . . ' < ' | ' > ' . . ' ~ ' -> true | _ -> false in let is_name_other_char_ok = function ' ! ' . . ' ~ ' -> true | _ -> false in ( if ( 1 <= length ) length && ( length <= 2147483647 ) 214...
let read_group ~ id ? seq_center ? description ? run_date ? flow_order ? key_seq ? library ? program ? predicted_median_insert_size ? platform ? platform_unit ? sample ( ) = ( match run_date with | None -> Ok None | Some run_date -> try Ok ( Some ( ` Date run_date ) run_date ) run_date with _ -> try Ok ( Some ( ` Time ...
let header ? version ? sort_order ? group_order ( ? ref_seqs [ ] ) = ( ? read_groups [ ] ) = ( ? programs [ ] ) = ( ? comments [ ] ) = ( ? others [ ] ) = ( ) = [ ( match version with | None -> None | Some x -> match parse_header_version x with | Error e -> Some e | Ok _ -> None ) ; ( if Option . is_some sort_order && P...
let parse_header_item_tag s = let is_letter = function ' A ' . . ' Z ' | ' a ' . . ' z ' -> true | _ -> false in match String . chop_prefix s ~ prefix " " :@ with | None -> error " header item tag must begin with " @ s sexp_of_string | Some " HD " -> Ok ` HD | Some " SQ " -> Ok ` SQ | Some " RG " -> Ok ` RG | Some " PG...
let parse_tag_value s = let parse_tag s = if ( String . length s = 2 ) 2 && ( match s [ . 0 ] 0 with ' A ' . . ' Z ' | ' a ' . . ' z ' -> true | _ -> false ) false && ( match s [ . 1 ] 1 with | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' -> true | _ -> false ) then Ok s else error " invalid tag " s sexp_of_stri...
let find_all l x ' = let rec loop accum = function | [ ] -> accum | ( x , y ) y :: l -> let accum = if Poly ( . x = x ' ) x ' then y :: accum else accum in loop accum l in List . rev ( loop [ ] l ) l
let find1 header_item_tag l x = match find_all l x with | [ ] -> error " required tag not found " ( header_item_tag , x ) x [ % sexp_of : header_item_tag * string ] | y [ ] :: -> Ok y | ys -> error " tag found multiple times " ( header_item_tag , x , ys ) ys [ % sexp_of : header_item_tag * string * string list ]
let find01 header_item_tag l x = match find_all l x with | [ ] -> Ok None | y [ ] :: -> Ok ( Some y ) y | ys -> error " tag found multiple times " ( header_item_tag , x , ys ) ys [ % sexp_of : header_item_tag * string * string list ]
let assert_tags header_item_tag tvl tags = let expected_tags = String . Set . of_list tags in let got_tags = List . map tvl ~ f : fst |> String . Set . of_list in let unexpected_tags = Set . diff got_tags expected_tags in if Set . length unexpected_tags = 0 then Ok ( ) else error " unexpected tag for given header item ...
let parse_sort_order = function | " unknown " -> Ok ` Unknown | " unsorted " -> Ok ` Unsorted | " queryname " -> Ok ` Query_name | " coordinate " -> Ok ` Coordinate | x -> error " invalid sort order " x sexp_of_string
let parse_group_order = function | " none " -> Ok ` None | " query " -> Ok ` Query | " reference " -> Ok ` Reference | x -> error " invalid group order " x sexp_of_string
let parse_header_line tvl = find1 ` HD tvl " VN " >>= fun version -> find01 ` HD tvl " SO " >>?~ parse_sort_order >>= fun sort_order -> find01 ` HD tvl " GO " >>?~ parse_group_order >>= fun group_order -> assert_tags ` HD tvl [ " VN " ; " SO " ; " GO ] " >>= fun ( ) -> header_line ~ version ? sort_order ? group_order (...
let parse_ref_seq tvl = find1 ` SQ tvl " SN " >>= fun name -> find1 ` SQ tvl " LN " >>= fun length -> ( try Ok ( Int . of_string length ) length with _ -> error " invalid ref seq length " length sexp_of_string ) >>= fun length -> find01 ` SQ tvl " AS " >>= fun assembly -> find01 ` SQ tvl " M5 " >>= fun md5 -> find01 ` ...
let parse_platform = function | " CAPILLARY " -> Ok ` Capillary | " LS454 " -> Ok ` LS454 | " ILLUMINA " -> Ok ` Illumina | " SOLID " -> Ok ` Solid | " HELICOS " -> Ok ` Helicos | " IONTORRENT " -> Ok ` Ion_Torrent | " PACBIO " -> Ok ` Pac_Bio | x -> error " unknown platform " x sexp_of_string
let parse_read_group tvl = find1 ` RG tvl " ID " >>= fun id -> find01 ` RG tvl " CN " >>= fun seq_center -> find01 ` RG tvl " DS " >>= fun description -> find01 ` RG tvl " DT " >>= fun run_date -> find01 ` RG tvl " FO " >>= fun flow_order -> find01 ` RG tvl " KS " >>= fun key_seq -> find01 ` RG tvl " LB " >>= fun libra...
let parse_program tvl = find1 ` PG tvl " ID " >>= fun id -> find01 ` PG tvl " PN " >>= fun name -> find01 ` PG tvl " CL " >>= fun command_line -> find01 ` PG tvl " PP " >>= fun previous_id -> find01 ` PG tvl " DS " >>= fun description -> find01 ` PG tvl " VN " >>= fun version -> assert_tags ` PG tvl [ " ID " ; " PN " ;...
let parse_header_item line = let parse_data tag tvl = match tag with | ` HD -> parse_header_line tvl >>| fun x -> ` HD x | ` SQ -> parse_ref_seq tvl >>| fun x -> ` SQ x | ` RG -> parse_read_group tvl >>| fun x -> ` RG x | ` PG -> parse_program tvl >>| fun x -> ` PG x | ` Other tag -> Ok ( ` Other ( tag , tvl ) tvl ) tv...
let alignment ? ref_seqs ? qname ~ flags ? rname ? pos ? mapq ( ? cigar [ ] ) = ? rnext ? pnext ? tlen ? seq ( ? qual [ ] ) = ( ? optional_fields [ ] ) = ( ) = [ ( match ref_seqs , rname with | ( None , _ ) _ | ( _ , None ) None -> None | Some ref_seqs , Some rname -> if Set . mem ref_seqs rname then None else Some ( E...
let parse_int_range field lo hi s = let out_of_range = sprintf " % s out of range " field in let not_an_int = sprintf " % s not an int " field in try let n = Int . of_string s in if ( lo <= n ) n && ( n <= hi ) hi then Ok n else error out_of_range ( n , lo , hi ) hi [ % sexp_of : int * int * int ] with _ -> error not_a...
let parse_opt_string field re s = if not ( Re . execp re s ) s then error ( sprintf " invalid % s " field ) field s sexp_of_string else match s with | " " * -> Ok None | _ -> Ok ( Some s ) s
let qname_re = let open Re in alt [ char ' * ' ; repn ( alt [ rg ' ! ' ' ? ' ; rg ' A ' ' ~ ' ] ' ) ' 1 ( Some 255 ) 255 ; ] |> compile
let parse_qname s = parse_opt_string " QNAME " qname_re s
let parse_flags s = try Flags . of_int ( Int . of_string s ) s with _ -> error " invalid FLAG " s sexp_of_string
let rname_re = Re . Perl . compile_pat [ ( ) ] [ ] " " ^\\*|!-+-<>-~!-~*$
let parse_rname s = parse_opt_string " RNAME " rname_re s
let parse_pos s = parse_int_range " POS " 0 2147483647 s >>| function | 0 -> None | x -> Some x
let parse_mapq s = parse_int_range " MAPQ " 0 255 s >>| function | 255 -> None | x -> Some x
let positive i = let open Or_error in if i > 0 then return i else error_string " positive argument expected for cigar operation "
let cigar_op_alignment_match i = Or_error ( . positive i >>| fun i -> ` Alignment_match i ) i
let cigar_op_insertion i = Or_error ( . positive i >>| fun i -> ` Insertion i ) i
let cigar_op_deletion i = Or_error ( . positive i >>| fun i -> ` Deletion i ) i
let cigar_op_skipped i = Or_error ( . positive i >>| fun i -> ` Skipped i ) i
let cigar_op_soft_clipping i = Or_error ( . positive i >>| fun i -> ` Soft_clipping i ) i
let cigar_op_hard_clipping i = Or_error ( . positive i >>| fun i -> ` Hard_clipping i ) i
let cigar_op_padding i = Or_error ( . positive i >>| fun i -> ` Padding i ) i
let cigar_op_seq_match i = Or_error ( . positive i >>| fun i -> ` Seq_match i ) i
let cigar_op_seq_mismatch i = Or_error ( . positive i >>| fun i -> ` Seq_mismatch i ) i
let parse_cigar text = match text with | " " * -> Ok [ ] | " " -> error " invalid cigar string " text sexp_of_string | _ -> let ch = Scanf . Scanning . from_string text in let rec loop accum = if Scanf . Scanning . end_of_input ch then Ok accum else try let n = Scanf . bscanf ch " % d " Fun . id in let c = Scanf . bsca...
let rnext_re = Re . Perl . compile_pat [ ( ) ] [ ] " " ^\\*|=|!-+-<>-~!-~*$
let parse_rnext s = if not ( Re . execp rnext_re s ) s then error " invalid RNEXT " s sexp_of_string else match s with | " " * -> Ok None | " " = -> Ok ( Some ` Equal_to_RNAME ) Equal_to_RNAME | _ -> Ok ( Some ( ` Value s ) s ) s
let parse_pnext s = parse_int_range " PNEXT " 0 2147483647 s >>| function | 0 -> None | x -> Some x
let parse_tlen s = parse_int_range " TLEN " ~- 2147483647 2147483647 s >>| function | 0 -> None | x -> Some x
let seq_re = Re . Perl . compile_pat [ " ^\\*| A - Za - z ] . " =+$
let parse_seq s = parse_opt_string " SEQ " seq_re s
let parse_qual s = match s with | " " -> Or_error . error_string " invalid empty QUAL " | " " * -> Ok [ ] | _ -> String . to_list s |> Result . List . map ~ f ( : Phred_score . of_char ~ offset ` : Offset33 ) Offset33
let opt_field_tag_re = Re . Perl . compile_pat [ " ^ A - Za - z ] z [ zA - Za - z0 - 9 ] 9 " $
let opt_field_Z_re = Re . Perl . compile_pat [ " ^ ] " !-~+$
let opt_field_H_re = Re . Perl . compile_pat [ " ^ 0 - 9A - F ] F " +$
let opt_field_int_re = Re . Perl . compile_pat [ " ^-? 0 - 9 ] 9 " +$
let opt_field_float_re = Re . Perl . compile_pat [ ] [ " ^-+? 0 - 9 ] 9 [ . *\\? 0 - 9 ] 9 ( [ + eE ] eE [ eE ] [ -+? 0 - 9 ] 9 ) " +?$
let optional_field_value_err typ value = error " invalid value " ( typ , value ) value [ % sexp_of : string * string ]
let optional_field_value_A value = if List . mem ~ equal : Char . equal [ ' ! ' ; ' - ' ; ' ~ ' ] ' value then optional_field_value_err " A " ( Char . to_string value ) value else Ok ( ` A value ) value
let optional_field_value_i i = ` i i
let optional_field_value_f f = ` f f
let optional_field_value_Z value = if Re . execp opt_field_Z_re value then Ok ( ` Z value ) value else optional_field_value_err " Z " value
let optional_field_value_H value = if Re . execp opt_field_H_re value then Ok ( ` H value ) value else optional_field_value_err " H " value
let optional_field_value_B elt_type elts = let valid_args = match elt_type with | ' c ' | ' C ' | ' s ' | ' S ' | ' i ' | ' I ' -> List . for_all elts ~ f ( : Re . execp opt_field_int_re ) opt_field_int_re | ' f ' -> List . for_all elts ~ f ( : Re . execp opt_field_float_re ) opt_field_float_re | _ -> false in if valid...